-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
254 lines (214 loc) · 7.07 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
250
251
252
253
254
package protocol
import (
log "github.com/sirupsen/logrus"
"github.com/bytom-gm/errors"
"github.com/bytom-gm/protocol/bc"
"github.com/bytom-gm/protocol/bc/types"
"github.com/bytom-gm/protocol/state"
"github.com/bytom-gm/protocol/validation"
)
var (
// ErrBadBlock is returned when a block is invalid.
ErrBadBlock = errors.New("invalid block")
// ErrBadStateRoot is returned when the computed assets merkle root
// disagrees with the one declared in a block header.
ErrBadStateRoot = errors.New("invalid state merkle root")
)
// BlockExist check is a block in chain or orphan
func (c *Chain) BlockExist(hash *bc.Hash) bool {
return c.index.BlockExist(hash) || c.orphanManage.BlockExist(hash)
}
// GetBlockByHash return a block by given hash
func (c *Chain) GetBlockByHash(hash *bc.Hash) (*types.Block, error) {
return c.store.GetBlock(hash)
}
// GetBlockByHeight return a block header by given height
func (c *Chain) GetBlockByHeight(height uint64) (*types.Block, error) {
node := c.index.NodeByHeight(height)
if node == nil {
return nil, errors.New("can't find block in given height")
}
return c.store.GetBlock(&node.Hash)
}
// GetHeaderByHash return a block header by given hash
func (c *Chain) GetHeaderByHash(hash *bc.Hash) (*types.BlockHeader, error) {
node := c.index.GetNode(hash)
if node == nil {
return nil, errors.New("can't find block header in given hash")
}
return node.BlockHeader(), nil
}
// GetHeaderByHeight return a block header by given height
func (c *Chain) GetHeaderByHeight(height uint64) (*types.BlockHeader, error) {
node := c.index.NodeByHeight(height)
if node == nil {
return nil, errors.New("can't find block header in given height")
}
return node.BlockHeader(), nil
}
func (c *Chain) calcReorganizeNodes(node *state.BlockNode) ([]*state.BlockNode, []*state.BlockNode) {
var attachNodes []*state.BlockNode
var detachNodes []*state.BlockNode
attachNode := node
for c.index.NodeByHeight(attachNode.Height) != attachNode {
attachNodes = append([]*state.BlockNode{attachNode}, attachNodes...)
attachNode = attachNode.Parent
}
detachNode := c.bestNode
for detachNode != attachNode {
detachNodes = append(detachNodes, detachNode)
detachNode = detachNode.Parent
}
return attachNodes, detachNodes
}
func (c *Chain) connectBlock(block *types.Block) (err error) {
bcBlock := types.MapBlock(block)
if bcBlock.TransactionStatus, err = c.store.GetTransactionStatus(&bcBlock.ID); err != nil {
return err
}
utxoView := state.NewUtxoViewpoint()
if err := c.store.GetTransactionsUtxo(utxoView, bcBlock.Transactions); err != nil {
return err
}
if err := utxoView.ApplyBlock(bcBlock, bcBlock.TransactionStatus); err != nil {
return err
}
node := c.index.GetNode(&bcBlock.ID)
if err := c.setState(node, utxoView); err != nil {
return err
}
for _, tx := range block.Transactions {
c.txPool.RemoveTransaction(&tx.Tx.ID)
}
return nil
}
func (c *Chain) reorganizeChain(node *state.BlockNode) error {
attachNodes, detachNodes := c.calcReorganizeNodes(node)
utxoView := state.NewUtxoViewpoint()
for _, detachNode := range detachNodes {
b, err := c.store.GetBlock(&detachNode.Hash)
if err != nil {
return err
}
detachBlock := types.MapBlock(b)
if err := c.store.GetTransactionsUtxo(utxoView, detachBlock.Transactions); err != nil {
return err
}
txStatus, err := c.GetTransactionStatus(&detachBlock.ID)
if err != nil {
return err
}
if err := utxoView.DetachBlock(detachBlock, txStatus); err != nil {
return err
}
log.WithFields(log.Fields{"height": node.Height, "hash": node.Hash.String()}).Debug("detach from mainchain")
}
for _, attachNode := range attachNodes {
b, err := c.store.GetBlock(&attachNode.Hash)
if err != nil {
return err
}
attachBlock := types.MapBlock(b)
if err := c.store.GetTransactionsUtxo(utxoView, attachBlock.Transactions); err != nil {
return err
}
txStatus, err := c.GetTransactionStatus(&attachBlock.ID)
if err != nil {
return err
}
if err := utxoView.ApplyBlock(attachBlock, txStatus); err != nil {
return err
}
log.WithFields(log.Fields{"height": node.Height, "hash": node.Hash.String()}).Debug("attach from mainchain")
}
return c.setState(node, utxoView)
}
// SaveBlock will validate and save block into storage
func (c *Chain) saveBlock(block *types.Block) error {
bcBlock := types.MapBlock(block)
parent := c.index.GetNode(&block.PreviousBlockHash)
if err := validation.ValidateBlock(bcBlock, parent); err != nil {
return errors.Sub(ErrBadBlock, err)
}
if err := c.store.SaveBlock(block, bcBlock.TransactionStatus); err != nil {
return err
}
c.orphanManage.Delete(&bcBlock.ID)
node, err := state.NewBlockNode(&block.BlockHeader, parent)
if err != nil {
return err
}
c.index.AddNode(node)
return nil
}
func (c *Chain) saveSubBlock(block *types.Block) *types.Block {
blockHash := block.Hash()
prevOrphans, ok := c.orphanManage.GetPrevOrphans(&blockHash)
if !ok {
return block
}
bestBlock := block
for _, prevOrphan := range prevOrphans {
orphanBlock, ok := c.orphanManage.Get(prevOrphan)
if !ok {
log.WithFields(log.Fields{"hash": prevOrphan.String()}).Warning("saveSubBlock fail to get block from orphanManage")
continue
}
if err := c.saveBlock(orphanBlock); err != nil {
log.WithFields(log.Fields{"hash": prevOrphan.String(), "height": orphanBlock.Height}).Warning("saveSubBlock fail to save block")
continue
}
if subBestBlock := c.saveSubBlock(orphanBlock); subBestBlock.Height > bestBlock.Height {
bestBlock = subBestBlock
}
}
return bestBlock
}
type processBlockResponse struct {
isOrphan bool
err error
}
type processBlockMsg struct {
block *types.Block
reply chan processBlockResponse
}
// ProcessBlock is the entry for chain update
func (c *Chain) ProcessBlock(block *types.Block) (bool, error) {
reply := make(chan processBlockResponse, 1)
c.processBlockCh <- &processBlockMsg{block: block, reply: reply}
response := <-reply
return response.isOrphan, response.err
}
func (c *Chain) blockProcesser() {
for msg := range c.processBlockCh {
isOrphan, err := c.processBlock(msg.block)
msg.reply <- processBlockResponse{isOrphan: isOrphan, err: err}
}
}
// ProcessBlock is the entry for handle block insert
func (c *Chain) processBlock(block *types.Block) (bool, error) {
blockHash := block.Hash()
if c.BlockExist(&blockHash) {
log.WithFields(log.Fields{"hash": blockHash.String(), "height": block.Height}).Info("block has been processed")
return c.orphanManage.BlockExist(&blockHash), nil
}
if parent := c.index.GetNode(&block.PreviousBlockHash); parent == nil {
c.orphanManage.Add(block)
return true, nil
}
if err := c.saveBlock(block); err != nil {
return false, err
}
bestBlock := c.saveSubBlock(block)
bestBlockHash := bestBlock.Hash()
bestNode := c.index.GetNode(&bestBlockHash)
if bestNode.Parent == c.bestNode {
log.Debug("append block to the end of mainchain")
return false, c.connectBlock(bestBlock)
}
if bestNode.Height > c.bestNode.Height && bestNode.WorkSum.Cmp(c.bestNode.WorkSum) >= 0 {
log.Debug("start to reorganize chain")
return false, c.reorganizeChain(bestNode)
}
return false, nil
}