forked from tsileo/blkparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
47 lines (40 loc) · 1.35 KB
/
block.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
package blkparser
import (
"bytes"
"encoding/binary"
)
type Block struct {
Raw []byte `json:"-"`
Hash string `json:"hash"`
Height uint `json:"height"`
Txs []*Tx `json:"tx,omitempty"`
Version uint32 `json:"ver"`
MerkleRoot string `json:"mrkl_root"`
BlockTime uint32 `json:"time"`
Bits uint32 `json:"bits"`
Nonce uint32 `json:"nonce"`
Size uint32 `json:"size"`
TxCnt uint32 `json:"n_tx"`
TotalBTC uint64 `json:"total_out"`
BlockReward float64 `json:"-"`
Parent string `json:"prev_block"`
Next string `json:"next_block"`
}
func NewBlock(rawblock []byte) (block *Block, err error) {
block = new(Block)
block.Raw = rawblock
block.Hash = GetShaString(rawblock[:80])
block.Version = binary.LittleEndian.Uint32(rawblock[0:4])
if !bytes.Equal(rawblock[4:36], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) {
block.Parent = HashString(rawblock[4:36])
}
block.MerkleRoot = HashString(rawblock[36:68])
block.BlockTime = binary.LittleEndian.Uint32(rawblock[68:72])
block.Bits = binary.LittleEndian.Uint32(rawblock[72:76])
block.Nonce = binary.LittleEndian.Uint32(rawblock[76:80])
block.Size = uint32(len(rawblock))
txs, _ := ParseTxs(rawblock[80:])
block.Txs = txs
block.TxCnt = uint32(len(txs))
return
}