-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.go
144 lines (125 loc) · 2.81 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package doge
const (
VersionAuxPoW = 256
CoinbaseVOut = 0xffffffff
)
var CoinbaseTxID = [32]byte{}
type Block struct {
Header BlockHeader
AuxPoW *MerkleTx // if IsAuxPoW()
Tx []BlockTx
}
type BlockHeader struct {
Version uint32
PrevBlock []byte // 32 bytes
MerkleRoot []byte // 32 bytes
Timestamp uint32
Bits uint32
Nonce uint32
}
func (b *BlockHeader) IsAuxPoW() bool {
return (b.Version & VersionAuxPoW) != 0
}
type MerkleTx struct {
CoinbaseTx BlockTx
ParentHash []byte // 32 bytes
CoinbaseBranch MerkleBranch
BlockchainBranch MerkleBranch
ParentBlock BlockHeader
}
type MerkleBranch struct {
Hash [][]byte // 32 bytes each
SideMask uint32
}
type BlockTx struct {
Version uint32
VIn []BlockTxIn
VOut []BlockTxOut
LockTime uint32
TxID string // hex, computed from tx data
}
type BlockTxIn struct {
TxID []byte // 32 bytes
VOut uint32
Script []byte // varied length
Sequence uint32
}
type BlockTxOut struct {
Value int64
Script []byte // varied length
}
func DecodeBlock(blockBytes []byte) Block {
s := &Stream{b: blockBytes}
return readBlock(s)
}
func readBlock(s *Stream) (b Block) {
b.Header = readHeader(s)
if b.Header.IsAuxPoW() {
b.AuxPoW = readMerkleTx(s)
}
numTx := s.var_uint()
for i := uint64(0); i < numTx; i++ {
b.Tx = append(b.Tx, readTx(s))
}
return
}
func readHeader(s *Stream) (b BlockHeader) {
b.Version = s.uint32le()
b.PrevBlock = s.bytes(32)
b.MerkleRoot = s.bytes(32)
b.Timestamp = s.uint32le()
b.Bits = s.uint32le()
b.Nonce = s.uint32le()
return
}
func readMerkleTx(s *Stream) *MerkleTx {
var m MerkleTx
m.CoinbaseTx = readTx(s)
m.ParentHash = s.bytes(32)
m.CoinbaseBranch = readMerkleBranch(s)
m.BlockchainBranch = readMerkleBranch(s)
m.ParentBlock = readHeader(s)
return &m
}
func readMerkleBranch(s *Stream) (b MerkleBranch) {
numHash := s.var_uint()
for i := uint64(0); i < numHash; i++ {
b.Hash = append(b.Hash, s.bytes(32))
}
b.SideMask = s.uint32le()
return
}
func DecodeTx(txBytes []byte) BlockTx {
s := &Stream{b: txBytes}
return readTx(s)
}
func readTx(s *Stream) (tx BlockTx) {
start := s.p
tx.Version = s.uint32le()
tx_in := s.var_uint()
for i := uint64(0); i < tx_in; i++ {
tx.VIn = append(tx.VIn, readTxIn(s))
}
tx_out := s.var_uint()
for i := uint64(0); i < tx_out; i++ {
tx.VOut = append(tx.VOut, readTxOut(s))
}
tx.LockTime = s.uint32le()
// Compute TX hash from transaction bytes.
tx.TxID = TxHashHex(s.b[start:s.p])
return
}
func readTxIn(s *Stream) (in BlockTxIn) {
in.TxID = s.bytes(32)
in.VOut = s.uint32le()
script_len := s.var_uint()
in.Script = s.bytes(script_len)
in.Sequence = s.uint32le()
return
}
func readTxOut(s *Stream) (out BlockTxOut) {
out.Value = int64(s.uint64le())
script_len := s.var_uint()
out.Script = s.bytes(script_len)
return
}