-
Notifications
You must be signed in to change notification settings - Fork 113
/
block.go
249 lines (223 loc) · 5.87 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
239
240
241
242
243
244
245
246
247
248
249
package block
import (
"errors"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/iost-official/go-iost/account"
"github.com/iost-official/go-iost/common"
blockpb "github.com/iost-official/go-iost/core/block/pb"
"github.com/iost-official/go-iost/core/merkletree"
"github.com/iost-official/go-iost/core/tx"
"github.com/iost-official/go-iost/core/version"
"github.com/iost-official/go-iost/crypto"
)
// BlockHead versions
const (
V0 int64 = iota
V1
)
// BlockHead is the struct of block head.
type BlockHead struct { // nolint
Version int64
ParentHash []byte
TxMerkleHash []byte
TxReceiptMerkleHash []byte
Info []byte
Number int64
Witness string
Time int64
GasUsage int64
}
// ToPb convert BlockHead to proto buf data structure.
func (b *BlockHead) ToPb() *blockpb.BlockHead {
return &blockpb.BlockHead{
Version: b.Version,
ParentHash: b.ParentHash,
TxMerkleHash: b.TxMerkleHash,
TxReceiptMerkleHash: b.TxReceiptMerkleHash,
Info: b.Info,
Number: b.Number,
Witness: b.Witness,
Time: b.Time,
}
}
// ToBytes converts BlockHead to a specific byte slice.
func (b *BlockHead) ToBytes() []byte {
se := common.NewSimpleEncoder()
se.WriteInt64(b.Version)
se.WriteBytes(b.ParentHash)
se.WriteBytes(b.TxMerkleHash)
se.WriteBytes(b.TxReceiptMerkleHash)
se.WriteBytes(b.Info)
se.WriteInt64(b.Number)
se.WriteString(b.Witness)
se.WriteInt64(b.Time)
return se.Bytes()
}
// FromPb convert BlockHead from proto buf data structure.
func (b *BlockHead) FromPb(bh *blockpb.BlockHead) *BlockHead {
b.Version = bh.Version
b.ParentHash = bh.ParentHash
b.TxMerkleHash = bh.TxMerkleHash
b.TxReceiptMerkleHash = bh.TxReceiptMerkleHash
b.Info = bh.Info
b.Number = bh.Number
b.Witness = bh.Witness
b.Time = bh.Time
return b
}
// Encode is marshal
func (b *BlockHead) Encode() ([]byte, error) {
bhByte, err := proto.Marshal(b.ToPb())
if err != nil {
return nil, errors.New("fail to encode blockhead")
}
return bhByte, nil
}
// Decode is unmarshal
func (b *BlockHead) Decode(bhByte []byte) error {
bh := &blockpb.BlockHead{}
err := proto.Unmarshal(bhByte, bh)
if err != nil {
return errors.New("fail to decode blockhead")
}
b.FromPb(bh)
return nil
}
func (b *BlockHead) hash() []byte {
return common.Sha3(b.ToBytes())
}
// Rules create new rules for this block
func (b *BlockHead) Rules() *version.Rules {
return version.NewRules(b.Number)
}
// Block is the implementation of block
type Block struct {
hash []byte
Head *BlockHead
Sign *crypto.Signature
Txs []*tx.Tx
Receipts []*tx.TxReceipt
TxHashes [][]byte
ReceiptHashes [][]byte
}
// CalculateGasUsage calculates the block's gas usage.
func (b *Block) CalculateGasUsage() int64 {
if b.Head.GasUsage == 0 {
for _, txr := range b.Receipts {
b.Head.GasUsage += txr.GasUsage
}
}
return b.Head.GasUsage
}
// CalculateTxMerkleHash calculate the merkle hash of the transaction.
func (b *Block) CalculateTxMerkleHash() []byte {
m := merkletree.MerkleTree{}
hashes := make([][]byte, 0, len(b.Txs))
for _, tx := range b.Txs {
hashes = append(hashes, tx.Hash())
}
m.Build(hashes)
return m.RootHash()
}
// CalculateTxReceiptMerkleHash calculate the merkle hash of the transaction receipt.
func (b *Block) CalculateTxReceiptMerkleHash() []byte {
m := merkletree.TXRMerkleTree{}
m.Build(b.Receipts)
return m.RootHash()
}
// Encode is marshal
func (b *Block) Encode() ([]byte, error) {
br := &blockpb.Block{
Head: b.Head.ToPb(),
BlockType: blockpb.BlockType_NORMAL,
}
for _, t := range b.Txs {
br.Txs = append(br.Txs, t.ToPb())
}
for _, r := range b.Receipts {
br.Receipts = append(br.Receipts, r.ToPb())
}
if b.Sign != nil {
br.Sign = b.Sign.ToPb()
}
brByte, err := proto.Marshal(br)
if err != nil {
return nil, errors.New("fail to encode blockraw")
}
return brByte, nil
}
// Decode is unmarshal
func (b *Block) Decode(blockByte []byte) error {
br := &blockpb.Block{}
err := proto.Unmarshal(blockByte, br)
if err != nil {
return errors.New("fail to decode blockraw")
}
h := &BlockHead{}
h.FromPb(br.Head)
b.Head = h
b.TxHashes = nil
sig := &crypto.Signature{}
b.Sign = sig.FromPb(br.Sign)
switch br.BlockType {
case blockpb.BlockType_NORMAL:
for _, t := range br.Txs {
tt := &tx.Tx{}
b.Txs = append(b.Txs, tt.FromPb(t))
}
for _, r := range br.Receipts {
rcpt := &tx.TxReceipt{}
b.Receipts = append(b.Receipts, rcpt.FromPb(r))
}
case blockpb.BlockType_ONLYHASH:
b.TxHashes = br.TxHashes
b.ReceiptHashes = br.ReceiptHashes
}
b.CalculateHeadHash()
return nil
}
// CalculateHeadHash calculate the hash of the head
func (b *Block) CalculateHeadHash() {
b.hash = b.Head.hash()
}
// HeadHash return block hash
func (b *Block) HeadHash() []byte {
return b.hash
}
// LenTx return len of transaction
func (b *Block) LenTx() int {
return len(b.Txs)
}
// EncodeM is marshal
func (b *Block) EncodeM() ([]byte, error) {
br := &blockpb.Block{
Head: b.Head.ToPb(),
BlockType: blockpb.BlockType_ONLYHASH,
}
br.Sign = b.Sign.ToPb()
for _, t := range b.Txs {
br.TxHashes = append(br.TxHashes, t.Hash())
}
for _, r := range b.Receipts {
br.ReceiptHashes = append(br.ReceiptHashes, r.Hash())
}
brByte, err := proto.Marshal(br)
if err != nil {
return nil, errors.New("fail to encode blockraw")
}
return brByte, nil
}
// VerifySelf verify block's signature and some base fields.
func (b *Block) VerifySelf() error {
signature := b.Sign
signature.SetPubkey(account.DecodePubkey(b.Head.Witness))
hash := b.HeadHash()
if !signature.Verify(hash) {
return fmt.Errorf("The signature of block %v is wrong", common.Base58Encode(hash))
}
if len(b.Txs) != len(b.Receipts) {
return fmt.Errorf("Tx len %v unmatch receipt len %v", len(b.Txs), len(b.Receipts))
}
return nil
}