-
Notifications
You must be signed in to change notification settings - Fork 3
/
blockheader.go
49 lines (41 loc) · 1.06 KB
/
blockheader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package simpleBlockchain
import "encoding/json"
type BlockHeader struct {
Version uint32 `json:"version"`
PrevBlock Hashes `json:"prevblock"`
MerkleRoot Hashes `json:"merkleroot"`
TimeStamp uint32 `json:"timestamp"`
Bits uint32 `json:"bits"`
Nonce uint32 `json:"nonce"`
Height int `json:"height"`
}
func (bh *BlockHeader) Serialize() ([]byte, error) {
bbh, err := json.Marshal(bh)
if err != nil {
return nil, err
}
return bbh, nil
}
func copyBlockHeader(blockheader *BlockHeader) BlockHeader{
newPrevBlock := make([]byte, len(blockheader.PrevBlock))
newMerkleRoot := make([]byte, len(blockheader.MerkleRoot) )
copy(newPrevBlock, blockheader.PrevBlock )
copy(newMerkleRoot, blockheader.MerkleRoot)
return BlockHeader{
blockheader.Version,
newPrevBlock,
newMerkleRoot,
blockheader.TimeStamp,
blockheader.Bits,
blockheader.Nonce,
blockheader.Height,
}
}
func DeserializeBlockHeader(data []byte) (*BlockHeader, error) {
var bh BlockHeader
err := json.Unmarshal(data, &bh)
if err != nil {
return nil, err
}
return &bh, nil
}