forked from l0rdicon/blkparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.go
More file actions
78 lines (67 loc) · 2.17 KB
/
block.go
File metadata and controls
78 lines (67 loc) · 2.17 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package blkparser
import (
"bytes"
"encoding/binary"
"encoding/hex"
)
// BlockPos models the position of a block's data in blockchain files.
type BlockPos struct {
FileId uint32 `json:"fileId"`
Pos int64 `json:"pos"`
}
// Block models the data in a blockchain block.
type Block struct {
Raw []byte `json:"-"`
Pos BlockPos `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"`
BlockSig string `json:"signature"`
TxCnt uint32 `json:"n_tx"`
TotalBTC uint64 `json:"total_out"`
BlockReward float64 `json:"-"`
Parent string `json:"prev_block"`
Next string `json:"next_block"`
}
func (block *Block) IsProofOfStake() bool {
if len(block.Txs) > 1 && block.Txs[1].IsCoinStake() {
return true
}
return false
}
// NewBlock deserializes a byte slice into a Block.
func NewBlock(rawblock []byte) (block *Block, err error) {
block = new(Block)
block.Raw = rawblock
block.Version = binary.LittleEndian.Uint32(rawblock[0:4])
if block.Version > 6 {
block.Hash = GetShaString(rawblock[:80])
} else {
block.Hash, _ = GetScryptString(rawblock[:80])
}
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, offset, _ := ParseTxs(rawblock[80:])
block.Txs = txs
block.TxCnt = uint32(len(txs))
if block.IsProofOfStake() {
offset += 80
blocksig, blocksigsize := DecodeVariableLengthInteger(rawblock[offset:])
offset += blocksigsize
blocksigHex := hex.EncodeToString(rawblock[offset : offset+blocksig])
block.BlockSig = blocksigHex
}
return
}