-
Notifications
You must be signed in to change notification settings - Fork 79
/
block.go
238 lines (208 loc) · 6.17 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package block
import (
"encoding/json"
"errors"
"math"
"github.com/Workiva/go-datastructures/queue"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
)
const (
// MaxTransactionsPerBlock is the maximum number of transactions per block.
MaxTransactionsPerBlock = math.MaxUint16
)
// ErrMaxContentsPerBlock is returned when the maximum number of contents per block is reached.
var ErrMaxContentsPerBlock = errors.New("the number of contents exceeds the maximum number of contents per block")
var expectedHeaderSizeWithEmptyWitness int
func init() {
expectedHeaderSizeWithEmptyWitness = io.GetVarSize(new(Header))
}
// Block represents one block in the chain.
type Block struct {
// The base of the block.
Header
// Transaction list.
Transactions []*transaction.Transaction
// True if this block is created from trimmed data.
Trimmed bool
}
// auxBlockOut is used for JSON i/o.
type auxBlockOut struct {
Transactions []*transaction.Transaction `json:"tx"`
}
// auxBlockIn is used for JSON i/o.
type auxBlockIn struct {
Transactions []json.RawMessage `json:"tx"`
}
// ComputeMerkleRoot computes Merkle tree root hash based on actual block's data.
func (b *Block) ComputeMerkleRoot() util.Uint256 {
hashes := make([]util.Uint256, len(b.Transactions))
for i, tx := range b.Transactions {
hashes[i] = tx.Hash()
}
return hash.CalcMerkleRoot(hashes)
}
// RebuildMerkleRoot rebuilds the merkleroot of the block.
func (b *Block) RebuildMerkleRoot() {
b.MerkleRoot = b.ComputeMerkleRoot()
}
// NewBlockFromTrimmedBytes returns a new block from trimmed data.
// This is commonly used to create a block from stored data.
// Blocks created from trimmed data will have their Trimmed field
// set to true.
func NewBlockFromTrimmedBytes(stateRootEnabled bool, b []byte) (*Block, error) {
block := &Block{
Header: Header{
StateRootEnabled: stateRootEnabled,
},
Trimmed: true,
}
br := io.NewBinReaderFromBuf(b)
block.Header.DecodeBinary(br)
lenHashes := br.ReadVarUint()
if lenHashes > MaxTransactionsPerBlock {
return nil, ErrMaxContentsPerBlock
}
if lenHashes > 0 {
block.Transactions = make([]*transaction.Transaction, lenHashes)
for i := 0; i < int(lenHashes); i++ {
var hash util.Uint256
hash.DecodeBinary(br)
block.Transactions[i] = transaction.NewTrimmedTX(hash)
}
}
return block, br.Err
}
// New creates a new blank block with proper state root setting.
func New(stateRootEnabled bool) *Block {
return &Block{
Header: Header{
StateRootEnabled: stateRootEnabled,
},
}
}
// Trim returns a subset of the block data to save up space
// in storage.
// Notice that only the hashes of the transactions are stored.
func (b *Block) Trim() ([]byte, error) {
buf := io.NewBufBinWriter()
numTx := len(b.Transactions)
buf.Grow(b.GetExpectedBlockSizeWithoutTransactions(numTx) + util.Uint256Size*numTx)
b.Header.EncodeBinary(buf.BinWriter)
buf.WriteVarUint(uint64(numTx))
for _, tx := range b.Transactions {
h := tx.Hash()
h.EncodeBinary(buf.BinWriter)
}
if buf.Err != nil {
return nil, buf.Err
}
return buf.Bytes(), nil
}
// DecodeBinary decodes the block from the given BinReader, implementing
// Serializable interface.
func (b *Block) DecodeBinary(br *io.BinReader) {
b.Header.DecodeBinary(br)
contentsCount := br.ReadVarUint()
if contentsCount > MaxTransactionsPerBlock {
br.Err = ErrMaxContentsPerBlock
return
}
txes := make([]*transaction.Transaction, contentsCount)
for i := 0; i < int(contentsCount); i++ {
tx := &transaction.Transaction{}
tx.DecodeBinary(br)
txes[i] = tx
}
b.Transactions = txes
if br.Err != nil {
return
}
}
// EncodeBinary encodes the block to the given BinWriter, implementing
// Serializable interface.
func (b *Block) EncodeBinary(bw *io.BinWriter) {
b.Header.EncodeBinary(bw)
bw.WriteVarUint(uint64(len(b.Transactions)))
for i := 0; i < len(b.Transactions); i++ {
b.Transactions[i].EncodeBinary(bw)
}
}
// Compare implements the queue Item interface.
func (b *Block) Compare(item queue.Item) int {
other := item.(*Block)
switch {
case b.Index > other.Index:
return 1
case b.Index == other.Index:
return 0
default:
return -1
}
}
// MarshalJSON implements json.Marshaler interface.
func (b Block) MarshalJSON() ([]byte, error) {
auxb, err := json.Marshal(auxBlockOut{
Transactions: b.Transactions,
})
if err != nil {
return nil, err
}
baseBytes, err := json.Marshal(b.Header)
if err != nil {
return nil, err
}
// Stitch them together.
if baseBytes[len(baseBytes)-1] != '}' || auxb[0] != '{' {
return nil, errors.New("can't merge internal jsons")
}
baseBytes[len(baseBytes)-1] = ','
baseBytes = append(baseBytes, auxb[1:]...)
return baseBytes, nil
}
// UnmarshalJSON implements json.Unmarshaler interface.
func (b *Block) UnmarshalJSON(data []byte) error {
// As Base and auxb are at the same level in json,
// do unmarshalling separately for both structs.
auxb := new(auxBlockIn)
err := json.Unmarshal(data, auxb)
if err != nil {
return err
}
err = json.Unmarshal(data, &b.Header)
if err != nil {
return err
}
if len(auxb.Transactions) != 0 {
b.Transactions = make([]*transaction.Transaction, 0, len(auxb.Transactions))
for _, txBytes := range auxb.Transactions {
tx := &transaction.Transaction{}
err = tx.UnmarshalJSON(txBytes)
if err != nil {
return err
}
b.Transactions = append(b.Transactions, tx)
}
}
return nil
}
// GetExpectedBlockSize returns expected block size which should be equal to io.GetVarSize(b).
func (b *Block) GetExpectedBlockSize() int {
var transactionsSize int
for _, tx := range b.Transactions {
transactionsSize += tx.Size()
}
return b.GetExpectedBlockSizeWithoutTransactions(len(b.Transactions)) + transactionsSize
}
// GetExpectedBlockSizeWithoutTransactions returns expected block size without transactions size.
func (b *Block) GetExpectedBlockSizeWithoutTransactions(txCount int) int {
size := expectedHeaderSizeWithEmptyWitness - 1 - 1 + // 1 is for the zero-length (new(Header)).Script.Invocation/Verification
io.GetVarSize(&b.Script) +
io.GetVarSize(txCount)
if b.StateRootEnabled {
size += util.Uint256Size
}
return size
}