/
block.go
165 lines (140 loc) · 4.34 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
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package block
import (
"encoding/hex"
"time"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/endorsement"
"github.com/iotexproject/iotex-core/pkg/log"
)
// Errors
var (
ErrTxRootMismatch = errors.New("transaction merkle root does not match")
ErrDeltaStateMismatch = errors.New("delta state digest doesn't match")
ErrReceiptRootMismatch = errors.New("receipt root hash does not match")
)
// Block defines the struct of block
type Block struct {
Header
Body
Footer
// TODO: move receipts out of block struct
Receipts []*action.Receipt
}
// ConvertToBlockHeaderPb converts BlockHeader to BlockHeader
func (b *Block) ConvertToBlockHeaderPb() *iotextypes.BlockHeader {
return b.Header.BlockHeaderProto()
}
// ConvertToBlockPb converts Block to Block
func (b *Block) ConvertToBlockPb() *iotextypes.Block {
footer, err := b.ConvertToBlockFooterPb()
if err != nil {
log.L().Panic("failed to convert block footer to protobuf message")
}
return &iotextypes.Block{
Header: b.ConvertToBlockHeaderPb(),
Body: b.Body.Proto(),
Footer: footer,
}
}
// Serialize returns the serialized byte stream of the block
func (b *Block) Serialize() ([]byte, error) {
return proto.Marshal(b.ConvertToBlockPb())
}
// ConvertFromBlockPb converts Block to Block
func (b *Block) ConvertFromBlockPb(pbBlock *iotextypes.Block) error {
b.Header = Header{}
if err := b.Header.LoadFromBlockHeaderProto(pbBlock.GetHeader()); err != nil {
return err
}
b.Body = Body{}
if err := b.Body.LoadProto(pbBlock.GetBody()); err != nil {
return err
}
return b.ConvertFromBlockFooterPb(pbBlock.GetFooter())
}
// Deserialize parses the byte stream into a Block
func (b *Block) Deserialize(buf []byte) error {
pbBlock := iotextypes.Block{}
if err := proto.Unmarshal(buf, &pbBlock); err != nil {
return err
}
if err := b.ConvertFromBlockPb(&pbBlock); err != nil {
return err
}
b.Receipts = nil
// verify merkle root can match after deserialize
if err := b.VerifyTxRoot(b.CalculateTxRoot()); err != nil {
return err
}
return nil
}
// VerifyTxRoot verifies the transaction root hash
func (b *Block) VerifyTxRoot(root hash.Hash256) error {
if b.Header.txRoot != root {
return ErrTxRootMismatch
}
return nil
}
// VerifyDeltaStateDigest verifies the delta state digest in header
func (b *Block) VerifyDeltaStateDigest(digest hash.Hash256) error {
if b.Header.deltaStateDigest != digest {
return ErrDeltaStateMismatch
}
return nil
}
// VerifyReceiptRoot verifies the receipt root in header
func (b *Block) VerifyReceiptRoot(root hash.Hash256) error {
if b.Header.receiptRoot != root {
return ErrReceiptRootMismatch
}
return nil
}
// RunnableActions abstructs RunnableActions from a Block.
func (b *Block) RunnableActions() RunnableActions {
return RunnableActions{actions: b.Actions, txHash: b.txRoot}
}
// Finalize creates a footer for the block
func (b *Block) Finalize(endorsements []*endorsement.Endorsement, ts time.Time) error {
if len(b.endorsements) != 0 {
return errors.New("the block has been finalized")
}
b.endorsements = endorsements
b.commitTime = ts
return nil
}
// TransactionLog returns transaction logs in the block
func (b *Block) TransactionLog() *BlkTransactionLog {
if len(b.Receipts) == 0 {
return nil
}
blkLog := BlkTransactionLog{
actionLogs: []*TransactionLog{},
}
for _, r := range b.Receipts {
if log := ReceiptTransactionLog(r); log != nil {
blkLog.actionLogs = append(blkLog.actionLogs, log)
}
}
if len(blkLog.actionLogs) == 0 {
return nil
}
return &blkLog
}
// ActionHashs returns action hashs in the block
func (b *Block) ActionHashs() []string {
actHash := make([]string, len(b.Actions))
for i := range b.Actions {
h := b.Actions[i].Hash()
actHash[i] = hex.EncodeToString(h[:])
}
return actHash
}