github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/consensus/cbft/cbft.go (about) 1 // Package bft implements the BFT consensus engine. 2 package cbft 3 4 import ( 5 "bytes" 6 "container/list" 7 "crypto/ecdsa" 8 "encoding/hex" 9 "encoding/json" 10 "errors" 11 "github.com/PlatONnetwork/PlatON-Go/common" 12 "github.com/PlatONnetwork/PlatON-Go/common/hexutil" 13 math2 "github.com/PlatONnetwork/PlatON-Go/common/math" 14 "github.com/PlatONnetwork/PlatON-Go/consensus" 15 "github.com/PlatONnetwork/PlatON-Go/core" 16 "github.com/PlatONnetwork/PlatON-Go/core/cbfttypes" 17 "github.com/PlatONnetwork/PlatON-Go/core/state" 18 "github.com/PlatONnetwork/PlatON-Go/core/types" 19 "github.com/PlatONnetwork/PlatON-Go/core/vm" 20 "github.com/PlatONnetwork/PlatON-Go/crypto" 21 "github.com/PlatONnetwork/PlatON-Go/log" 22 "github.com/PlatONnetwork/PlatON-Go/p2p/discover" 23 "github.com/PlatONnetwork/PlatON-Go/params" 24 "github.com/PlatONnetwork/PlatON-Go/rpc" 25 "math" 26 "math/big" 27 "sync" 28 "sync/atomic" 29 "time" 30 ) 31 32 const ( 33 former int32 = iota 34 current 35 next 36 all 37 ) 38 39 var ( 40 errSign = errors.New("sign error") 41 errUnauthorizedSigner = errors.New("unauthorized signer") 42 errIllegalBlock = errors.New("illegal block") 43 lateBlock = errors.New("block is late") 44 errDuplicatedBlock = errors.New("duplicated block") 45 errBlockNumber = errors.New("error block number") 46 errUnknownBlock = errors.New("unknown block") 47 errFutileBlock = errors.New("futile block") 48 errGenesisBlock = errors.New("cannot handle genesis block") 49 errHighestLogicalBlock = errors.New("cannot find a logical block") 50 errListConfirmedBlocks = errors.New("list confirmed blocks error") 51 errMissingSignature = errors.New("extra-data 65 byte signature suffix missing") 52 extraSeal = 65 53 windowSize = 10 54 55 //periodMargin is a percentum for period margin 56 periodMargin = uint64(20) 57 58 //maxPingLatency is the time in milliseconds between Ping and Pong 59 maxPingLatency = int64(5000) 60 61 //maxAvgLatency is the time in milliseconds between two peers 62 maxAvgLatency = int64(2000) 63 ) 64 65 type Cbft struct { 66 config *params.CbftConfig 67 ppos *ppos 68 rotating *rotating 69 blockSignOutCh chan *cbfttypes.BlockSignature //a channel to send block signature 70 cbftResultOutCh chan *cbfttypes.CbftResult //a channel to send consensus result 71 highestLogicalBlockCh chan *types.Block 72 closeOnce sync.Once 73 exitCh chan struct{} 74 txPool *core.TxPool 75 blockExtMap sync.Map //map[common.Hash]*BlockExt //store all received blocks and signs 76 dataReceiveCh chan interface{} //a channel to receive data from miner 77 blockChain *core.BlockChain //the block chain 78 highestLogical atomic.Value //highest block in logical path, local packages new block will base on it 79 highestConfirmed atomic.Value //highest confirmed block in logical path 80 rootIrreversible atomic.Value //the latest block has stored in chain 81 //signedSet map[uint64]struct{} //all block numbers signed by local node 82 signedSet sync.Map 83 blockChainCache *core.BlockChainCache 84 netLatencyMap map[discover.NodeID]*list.List 85 netLatencyLock sync.RWMutex 86 } 87 88 func (cbft *Cbft) getRootIrreversible() *BlockExt { 89 v := cbft.rootIrreversible.Load() 90 if v == nil { 91 return nil 92 } else { 93 return v.(*BlockExt) 94 } 95 } 96 97 func (cbft *Cbft) getHighestConfirmed() *BlockExt { 98 v := cbft.highestConfirmed.Load() 99 if v == nil { 100 return nil 101 } else { 102 return v.(*BlockExt) 103 } 104 } 105 func (cbft *Cbft) getHighestLogical() *BlockExt { 106 v := cbft.highestLogical.Load() 107 if v == nil { 108 return nil 109 } else { 110 return v.(*BlockExt) 111 } 112 } 113 114 var cbft *Cbft 115 116 // New creates a concurrent BFT consensus engine 117 func New(config *params.CbftConfig, blockSignatureCh chan *cbfttypes.BlockSignature, cbftResultCh chan *cbfttypes.CbftResult, highestLogicalBlockCh chan *types.Block) *Cbft { 118 119 _ppos := newPpos(config) 120 121 cbft = &Cbft{ 122 config: config, 123 ppos: _ppos, 124 rotating: newRotating(_ppos, config.Duration), 125 blockSignOutCh: blockSignatureCh, 126 cbftResultOutCh: cbftResultCh, 127 highestLogicalBlockCh: highestLogicalBlockCh, 128 exitCh: make(chan struct{}), 129 //blockExtMap: make(map[common.Hash]*BlockExt), 130 //signedSet: make(map[uint64]struct{}), 131 dataReceiveCh: make(chan interface{}, 256), 132 netLatencyMap: make(map[discover.NodeID]*list.List), 133 } 134 135 _ppos.ticketContext.SetChainInfo(cbft) 136 137 flowControl = NewFlowControl() 138 139 go cbft.dataReceiverLoop() 140 141 return cbft 142 } 143 144 func NewFaker() *Cbft { 145 blockSignatureCh := make(chan *cbfttypes.BlockSignature, 20) 146 cbftResultCh := make(chan *cbfttypes.CbftResult) 147 highestLogicalBlockCh := make(chan *types.Block, 20) 148 return New(params.TestnetChainConfig.Cbft, blockSignatureCh, cbftResultCh, highestLogicalBlockCh) 149 } 150 151 // BlockExt is an extension from Block 152 type BlockExt struct { 153 block *types.Block `json:"-"` 154 inTree bool `json:"-"` 155 inTurn bool `json:"-"` 156 isExecuted bool `json:"-"` 157 isSigned bool `json:"-"` 158 isConfirmed bool `json:"-"` 159 Number uint64 `json:"number"` 160 Hash string `json:"hash"` 161 rcvTime int64 162 signs []*common.BlockConfirmSign `json:"-"`//all signs for block 163 parent *BlockExt 164 Children []*BlockExt `json:"children"` 165 166 } 167 168 169 func (ext *BlockExt) toJson() string { 170 jsons, errs := json.Marshal(ext) 171 if errs != nil { 172 log.Warn("can marshal BlockExt struct to json string", "number", ext.Number, "hash", ext.Hash) 173 return "" 174 } 175 return string(jsons) 176 } 177 178 179 // New creates a BlockExt object 180 func NewBlockExt(block *types.Block, blockNum uint64) *BlockExt { 181 return &BlockExt{ 182 block: block, 183 Number: blockNum, 184 signs: make([]*common.BlockConfirmSign, 0), 185 Hash: block.Hash().TerminalString(), 186 } 187 } 188 189 func NewEmptyExt(blockNum uint64) *BlockExt { 190 return &BlockExt{ 191 Number: blockNum, 192 signs: make([]*common.BlockConfirmSign, 0), 193 } 194 } 195 196 var flowControl *FlowControl 197 198 // FlowControl is a rectifier for sequential blocks 199 type FlowControl struct { 200 nodeID discover.NodeID 201 lastTime int64 202 maxInterval int64 203 minInterval int64 204 } 205 206 func NewFlowControl() *FlowControl { 207 return &FlowControl{ 208 nodeID: discover.NodeID{}, 209 maxInterval: int64(cbft.config.Period*1000 + cbft.config.Period*1000*periodMargin/100), 210 minInterval: int64(cbft.config.Period*1000 - cbft.config.Period*1000*periodMargin/100), 211 } 212 } 213 214 // control checks if the block is received at a proper rate 215 func (flowControl *FlowControl) control(nodeID discover.NodeID, rcvTime int64) bool { 216 passed := false 217 if flowControl.nodeID == nodeID { 218 differ := rcvTime - flowControl.lastTime 219 if differ >= flowControl.minInterval && differ <= flowControl.maxInterval { 220 passed = true 221 } else { 222 passed = false 223 } 224 } else { 225 passed = true 226 } 227 flowControl.nodeID = nodeID 228 flowControl.lastTime = rcvTime 229 230 return passed 231 } 232 233 // findBlockExt finds BlockExt in cbft.blockExtMap 234 func (cbft *Cbft) findBlockExt(hash common.Hash) *BlockExt { 235 if v, ok := cbft.blockExtMap.Load(hash); ok { 236 return v.(*BlockExt) 237 } 238 return nil 239 } 240 241 //collectSign collects all signs for a block 242 func (cbft *Cbft) collectSign(ext *BlockExt, sign *common.BlockConfirmSign) { 243 if sign != nil { 244 ext.signs = append(ext.signs, sign) 245 blockNumber := big.NewInt((int64(ext.Number))) 246 parentNumber := new(big.Int).Sub(blockNumber, common.Big1) 247 //if ext.isLinked && ext.block != nil { 248 if ext.inTree { // ext.block != nil is unnecessary 249 if len(ext.signs) >= cbft.getThreshold(parentNumber, ext.block.ParentHash(), blockNumber) { 250 ext.isConfirmed = true 251 } 252 } 253 } 254 } 255 256 // isParent checks if a block is another's parent 257 func (parent *BlockExt) isParent(child *types.Block) bool { 258 if parent.block != nil && parent.block.NumberU64()+1 == child.NumberU64() && parent.block.Hash() == child.ParentHash() { 259 return true 260 } 261 return false 262 } 263 264 // findParent finds ext's parent with non-nil block 265 func (cbft *Cbft) findParent(ext *BlockExt) *BlockExt { 266 if ext.block == nil { 267 return nil 268 } 269 parent := cbft.findBlockExt(ext.block.ParentHash()) 270 if parent != nil { 271 if parent.block == nil { 272 log.Warn("parent block has not received") 273 } else if parent.block.NumberU64()+1 == ext.block.NumberU64() { 274 return parent 275 } else { 276 log.Warn("data error, parent block hash is not mapping to number") 277 } 278 } 279 return nil 280 } 281 282 // collectTxs collects exts's transactions 283 func (cbft *Cbft) collectTxs(exts []*BlockExt) types.Transactions { 284 txs := make([]*types.Transaction, 0) 285 for _, ext := range exts { 286 copy(txs, ext.block.Transactions()) 287 } 288 return types.Transactions(txs) 289 } 290 291 // findChildren finds current blockExt's all children with non-nil block 292 func (cbft *Cbft) findChildren(parent *BlockExt) []*BlockExt { 293 if parent.block == nil { 294 return nil 295 } 296 children := make([]*BlockExt, 0) 297 298 f := func(k, v interface{}) bool { 299 // The input and input types of this function are fixed and cannot be modified. 300 // You can write your own code in the body of the function, call k, v in the map 301 child := v.(*BlockExt) 302 if child.block != nil && child.block.ParentHash() == parent.block.Hash() { 303 if child.block.NumberU64()-1 == parent.block.NumberU64() { 304 children = append(children, child) 305 } else { 306 log.Warn("data error, child block hash is not mapping to number") 307 } 308 } 309 return true 310 } 311 cbft.blockExtMap.Range(f) 312 313 if len(children) == 0 { 314 return nil 315 } else { 316 return children 317 } 318 } 319 320 // saveBlockExt saves block in memory 321 func (cbft *Cbft) saveBlockExt(hash common.Hash, ext *BlockExt) { 322 cbft.blockExtMap.Store(hash, ext) 323 324 length := 0 325 cbft.blockExtMap.Range(func(_, _ interface{}) bool { 326 length++ 327 return true 328 }) 329 log.Debug("save block in memory", "hash", hash, "number", ext.Number, "totalBlocks", length) 330 } 331 332 // isAncestor checks if a block is another's ancestor 333 func (lower *BlockExt) isAncestor(higher *BlockExt) bool { 334 335 if higher == nil || higher.block == nil || lower == nil || lower.block == nil { 336 return false 337 } 338 generations := higher.block.NumberU64() - lower.block.NumberU64() 339 if generations <= 0 { 340 return false 341 } 342 343 for i := uint64(0); i < generations; i++ { 344 parent := higher.parent 345 if parent != nil { 346 higher = parent 347 } else { 348 return false 349 } 350 } 351 352 if lower.block.Hash() == higher.block.Hash() && lower.block.NumberU64() == higher.block.NumberU64() { 353 return true 354 } 355 return false 356 } 357 358 // findHighest finds the highest block from current start; If there are multiple highest blockExts, returns the one that singed by self; if none of blocks signed by self, returns the one that has most signs 359 func (cbft *Cbft) findHighest(current *BlockExt) *BlockExt { 360 highest := current 361 for _, child := range current.Children { 362 current := cbft.findHighest(child) 363 if current.block.NumberU64() > highest.block.NumberU64() || (current.block.NumberU64() == highest.block.NumberU64() && (current.isSigned || len(current.signs) > len(highest.signs))) { 364 highest = current 365 } 366 } 367 return highest 368 } 369 370 // findHighestLogical finds a logical path and return the highest block. 371 // the precondition is cur is a logical block, so, findHighestLogical will return cur if the path only has one block. 372 func (cbft *Cbft) findHighestLogical(cur *BlockExt) *BlockExt { 373 lastClosestConfirmed := cbft.findLastClosestConfirmedIncludingSelf(cur) 374 if lastClosestConfirmed == nil { 375 return cbft.findHighest(cur) 376 } else { 377 return cbft.findHighest(lastClosestConfirmed) 378 } 379 } 380 381 // findLastClosestConfirmedIncludingSelf return the last found block by call findClosestConfirmedExcludingSelf in a circular manner 382 func (cbft *Cbft) findLastClosestConfirmedIncludingSelf(cur *BlockExt) *BlockExt { 383 log.Debug("findLastClosestConfirmedIncludingSelf", "cur.number", cur.Number, "cur.hash", cur.Hash, "cur.isConfirmed", cur.isConfirmed) 384 var lastClosestConfirmed *BlockExt 385 for { 386 lastClosestConfirmed = cbft.findClosestConfirmedExcludingSelf(cur) 387 if lastClosestConfirmed == nil || lastClosestConfirmed.block.Hash() == cur.block.Hash() { 388 break 389 } else { 390 //fmt.Printf("lastClosestConfirmed, number=%d, rcvTime=%d\r\n", lastClosestConfirmed.Number, lastClosestConfirmed.rcvTime) 391 cur = lastClosestConfirmed 392 } 393 } 394 if lastClosestConfirmed != nil { 395 return lastClosestConfirmed 396 } else if cur.isConfirmed { 397 return cur 398 } else { 399 log.Debug("findLastClosestConfirmedIncludingSelf return nil", "cur.number", cur.Number, "cur.hash", cur.Hash, "cur.isConfirmed", cur.isConfirmed) 400 return nil 401 } 402 } 403 404 // findClosestConfirmedIncludingSelf returns the closest confirmed block in current's descendant (including current itself). 405 // return nil if there's no confirmed in current's descendant. 406 func (cbft *Cbft) findClosestConfirmedIncludingSelf(current *BlockExt) *BlockExt { 407 closest := current 408 if current.inTree && current.isExecuted && !current.isConfirmed { 409 closest = nil 410 } 411 for _, child := range current.Children { 412 temp := cbft.findClosestConfirmedIncludingSelf(child) 413 if closest == nil || (temp != nil && temp.inTree && temp.isExecuted && temp.isConfirmed && temp.Number < closest.Number) { 414 closest = temp 415 } 416 } 417 return closest 418 } 419 420 // findClosestConfirmedExcludingSelf returns the closest confirmed block in current's descendant (excluding current itself). 421 // return nil if there's no confirmed in current's descendant. 422 func (cbft *Cbft) findClosestConfirmedExcludingSelf(current *BlockExt) *BlockExt { 423 var closest *BlockExt 424 for _, child := range current.Children { 425 if child != nil && child.inTree && child.isExecuted && child.isConfirmed { 426 return child 427 } else { 428 temp := cbft.findClosestConfirmedIncludingSelf(child) 429 if closest == nil || (temp != nil && temp.Number < closest.Number) { 430 closest = temp 431 } 432 } 433 } 434 return closest 435 } 436 437 // signLogicalAndDescendant signs logical block go along the logical path from current block, and will not sign the block if there's another same number block has been signed. 438 func (cbft *Cbft) signLogicalAndDescendant(current *BlockExt) { 439 log.Debug("sign logical block and its descendant", "hash", current.block.Hash(), "number", current.block.NumberU64()) 440 highestLogical := cbft.findHighestLogical(current) 441 442 logicalBlocks := cbft.backTrackBlocks(highestLogical, current, true) 443 444 //var highestConfirmed *BlockExt 445 for _, logical := range logicalBlocks { 446 if logical.inTurn && !logical.isSigned { 447 //if _, signed := cbft.signedSet[logical.block.NumberU64()]; !signed { 448 if _, signed := cbft.signedSet.Load(logical.block.NumberU64()); !signed { 449 cbft.sign(logical) 450 log.Debug("reset TxPool after block signed", "hash", logical.block.Hash(), "number", logical.Number) 451 cbft.txPool.Reset(logical.block) 452 } 453 } 454 } 455 } 456 457 func (cbft *Cbft) signLogicalAndDescendantMock(current *BlockExt) { 458 highestLogical := cbft.findHighestLogical(current) 459 logicalBlocks := cbft.backTrackBlocks(highestLogical, current, true) 460 461 for _, logical := range logicalBlocks { 462 if logical.inTurn && !logical.isSigned { 463 if _, signed := cbft.signedSet.Load(logical.block.NumberU64()); !signed { 464 cbft.signMock(logical) 465 } 466 } 467 } 468 } 469 470 // executeBlockAndDescendant executes the block's transactions and its descendant 471 func (cbft *Cbft) executeBlockAndDescendant(current *BlockExt, parent *BlockExt) error { 472 if !current.isExecuted { 473 if err := cbft.execute(current, parent); err != nil { 474 current.inTree = false 475 current.isExecuted = false 476 //remove bad block from tree and map 477 cbft.removeBadBlock(current) 478 //log.Error("execute block error", "hash", current.block.Hash(), "number", current.block.NumberU64()) 479 return err 480 } else { 481 current.inTree = true 482 current.isExecuted = true 483 } 484 } 485 486 for _, child := range current.Children { 487 if err := cbft.executeBlockAndDescendant(child, current); err != nil { 488 //remove bad block from tree and map 489 cbft.removeBadBlock(child) 490 return err 491 } 492 } 493 return nil 494 } 495 496 497 func (cbft *Cbft) executeBlockAndDescendantMock(current *BlockExt, parent *BlockExt) error { 498 if !current.isExecuted { 499 current.inTree = true 500 current.isExecuted = true 501 } 502 503 for _, child := range current.Children { 504 if err := cbft.executeBlockAndDescendantMock(child, current); err != nil { 505 //remove bad block from tree and map 506 cbft.removeBadBlock(child) 507 return err 508 } 509 } 510 return nil 511 } 512 513 // sign signs a block 514 func (cbft *Cbft) sign(ext *BlockExt) { 515 sealHash := ext.block.Header().SealHash() 516 if signature, err := cbft.signFn(sealHash.Bytes()); err == nil { 517 log.Debug("Sign block ", "hash", ext.block.Hash(), "number", ext.block.NumberU64(), "sealHash", sealHash, "signature", hexutil.Encode(signature[:8])) 518 519 sign := common.NewBlockConfirmSign(signature) 520 ext.isSigned = true 521 522 cbft.collectSign(ext, sign) 523 524 //save this block number 525 //cbft.signedSet[ext.block.NumberU64()] = struct{}{} 526 cbft.signedSet.Store(ext.block.NumberU64(), struct{}{}) 527 528 blockHash := ext.block.Hash() 529 530 //send the BlockSignature to channel 531 blockSign := &cbfttypes.BlockSignature{ 532 SignHash: sealHash, 533 Hash: blockHash, 534 Number: ext.block.Number(), 535 Signature: sign, 536 ParentHash: ext.block.ParentHash(), 537 } 538 cbft.blockSignOutCh <- blockSign 539 } else { 540 panic("sign block fatal error") 541 } 542 } 543 544 func (cbft *Cbft) signMock(ext *BlockExt) { 545 sealHash := ext.block.Header().SealHash() 546 if signature, err := cbft.signFn(sealHash.Bytes()); err == nil { 547 sign := common.NewBlockConfirmSign(signature) 548 ext.isSigned = true 549 550 cbft.collectSign(ext, sign) 551 552 //save this block number 553 //cbft.signedSet[ext.block.NumberU64()] = struct{}{} 554 cbft.signedSet.Store(ext.block.NumberU64(), struct{}{}) 555 556 } else { 557 panic("sign block fatal error") 558 } 559 } 560 561 // execute executes the block's transactions based on its parent 562 // if success then save the receipts and state to consensusCache 563 func (cbft *Cbft) execute(ext *BlockExt, parent *BlockExt) error { 564 log.Debug("execute block", "hash", ext.block.Hash(), "number", ext.block.NumberU64(), "ParentHash", parent.block.Hash()) 565 state, err := cbft.blockChainCache.MakeStateDB(parent.block) 566 if err != nil { 567 log.Error("execute block error, cannot make state based on parent", "hash", ext.block.Hash(), "Number", ext.block.NumberU64(), "ParentHash", parent.block.Hash(), "err", err) 568 return errors.New("execute block error") 569 } 570 571 //to execute 572 blockInterval := new(big.Int).Sub(ext.block.Number(), cbft.blockChain.CurrentBlock().Number()) 573 receipts, err := cbft.blockChain.ProcessDirectly(ext.block, state, parent.block, blockInterval) 574 575 if err == nil { 576 //save the receipts and state to consensusCache 577 stateIsNil := state == nil 578 log.Debug("execute block success", "hash", ext.block.Hash(), "number", ext.block.NumberU64(), "ParentHash", parent.block.Hash(), "lenReceipts", len(receipts), "stateIsNil", stateIsNil, "root", ext.block.Root()) 579 sealHash := ext.block.Header().SealHash() 580 cbft.blockChainCache.WriteReceipts(sealHash, receipts, ext.block.NumberU64()) 581 cbft.blockChainCache.WriteStateDB(sealHash, state, ext.block.NumberU64()) 582 //cbft.blockChainCache.MarkBlockHash(ext.block.Hash()) 583 } else { 584 log.Error("execute block error", "hash", ext.block.Hash(), "number", ext.block.NumberU64(), "ParentHash", parent.block.Hash(), "err", err) 585 return errors.New("execute block error") 586 } 587 return nil 588 } 589 590 // backTrackBlocks return blocks from start to end, these blocks are in a same tree branch. 591 // The result is sorted by block number from lower to higher. 592 func (cbft *Cbft) backTrackBlocks(start *BlockExt, end *BlockExt, includeEnd bool) []*BlockExt { 593 log.Trace("back track blocks", "startHash", start.block.Hash(), "startParentHash", end.block.ParentHash(), "endHash", start.block.Hash()) 594 595 result := make([]*BlockExt, 0) 596 597 if start.block.Hash() == end.block.Hash() && includeEnd { 598 result = append(result, start) 599 } else if start.block.NumberU64() > end.block.NumberU64() { 600 found := false 601 result = append(result, start) 602 603 for { 604 parent := start.parent 605 if parent == nil { 606 break 607 } else if parent.block.Hash() == end.block.Hash() && parent.block.NumberU64() == end.block.NumberU64() { 608 //log.Debug("ending of back track block ") 609 if includeEnd { 610 result = append(result, parent) 611 } 612 found = true 613 break 614 } else { 615 //log.Debug("found new block", "hash", parent.block.Hash(), "ParentHash", parent.block.ParentHash(), "number", parent.block.NumberU64()) 616 result = append(result, parent) 617 start = parent 618 } 619 } 620 621 if found { 622 //sorted by block number from lower to higher 623 if len(result) > 1 { 624 reverse(result) 625 } 626 } else { 627 result = nil 628 } 629 } 630 return result 631 } 632 633 func reverse(s []*BlockExt) { 634 for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { 635 s[i], s[j] = s[j], s[i] 636 } 637 } 638 639 // SetPrivateKey sets local's private key by the backend.go 640 func (cbft *Cbft) SetPrivateKey(privateKey *ecdsa.PrivateKey) { 641 cbft.config.PrivateKey = privateKey 642 cbft.config.NodeID = discover.PubkeyID(&privateKey.PublicKey) 643 } 644 645 func SetBlockChainCache(blockChainCache *core.BlockChainCache) { 646 cbft.blockChainCache = blockChainCache 647 } 648 649 // setHighestLogical sets highest logical block and send it to the highestLogicalBlockCh 650 func (cbft *Cbft) setHighestLogical(highestLogical *BlockExt) { 651 cbft.highestLogical.Store(highestLogical) 652 cbft.highestLogicalBlockCh <- highestLogical.block 653 } 654 655 // SetBackend sets blockChain and txPool into cbft 656 func SetBackend(blockChain *core.BlockChain, txPool *core.TxPool) { 657 log.Debug("call SetBackend()") 658 cbft.blockChain = blockChain 659 cbft.ppos.SetStartTimeOfEpoch(blockChain.Genesis().Time().Int64() / 1000) 660 cbft.ppos.ticketContext.SetChainConfig(blockChain.Config()) 661 662 currentBlock := blockChain.CurrentBlock() 663 664 genesisParentHash := bytes.Repeat([]byte{0x00}, 32) 665 if bytes.Equal(currentBlock.ParentHash().Bytes(), genesisParentHash) && currentBlock.Number() == nil { 666 currentBlock.Header().Number = big.NewInt(0) 667 } 668 669 log.Debug("init cbft.highestLogicalBlock", "hash", currentBlock.Hash(), "number", currentBlock.NumberU64()) 670 671 current := NewBlockExt(currentBlock, currentBlock.NumberU64()) 672 current.inTree = true 673 current.isExecuted = true 674 current.isSigned = true 675 current.isConfirmed = true 676 current.Number = currentBlock.NumberU64() 677 678 cbft.saveBlockExt(currentBlock.Hash(), current) 679 680 cbft.highestConfirmed.Store(current) 681 682 //cbft.highestLogical = current 683 cbft.setHighestLogical(current) 684 685 cbft.rootIrreversible.Store(current) 686 687 cbft.txPool = txPool 688 } 689 690 func SetPposOption(blockChain *core.BlockChain) { 691 cbft.ppos.setPPOS_Temp() 692 cbft.ppos.SetCandidateContextOption(blockChain, cbft.config.InitialNodes) 693 } 694 695 // BlockSynchronisation reset the cbft env, such as cbft.highestLogical, cbft.highestConfirmed. 696 // This function is invoked after that local has synced new blocks from other node. 697 func (cbft *Cbft) OnBlockSynced() { 698 log.Debug("call OnBlockSynced()", "cbft.dataReceiveCh.len", len(cbft.dataReceiveCh)) 699 cbft.dataReceiveCh <- &cbfttypes.BlockSynced{} 700 } 701 702 func (cbft *Cbft) blockSynced() { 703 currentBlock := cbft.blockChain.CurrentBlock() 704 log.Debug("=== call blockSynced() ===", 705 "highestLogicalHash", cbft.getHighestLogical().block.Hash(), 706 "highestLogicalNumber", cbft.getHighestLogical().Number, 707 "highestConfirmedHash", cbft.getHighestConfirmed().block.Hash(), 708 "highestConfirmedNumber", cbft.getHighestConfirmed().Number, 709 "rootIrreversibleHash", cbft.getRootIrreversible().block.Hash(), 710 "rootIrreversibleNumber", cbft.getRootIrreversible().Number, 711 "number", currentBlock.Number(), 712 "hash", currentBlock.Hash()) 713 714 if currentBlock.NumberU64() > cbft.getRootIrreversible().Number { 715 log.Debug("chain has a higher irreversible block", "hash", currentBlock.Hash(), "number", currentBlock.NumberU64()) 716 newRoot := cbft.findBlockExt(currentBlock.Hash()) 717 if newRoot == nil || newRoot.block == nil { 718 log.Debug("higher irreversible block is not existing in memory", "newTree", newRoot.toJson()) 719 //the block synced from other peer is a new block in local peer 720 //remove all blocks referenced in old tree after being cut off 721 cbft.cleanByTailoredTree(cbft.getRootIrreversible()) 722 723 newRoot = NewBlockExt(currentBlock, currentBlock.NumberU64()) 724 newRoot.inTree = true 725 newRoot.isExecuted = true 726 newRoot.isSigned = true 727 newRoot.isConfirmed = true 728 newRoot.Number = currentBlock.NumberU64() 729 newRoot.parent = nil 730 cbft.saveBlockExt(newRoot.block.Hash(), newRoot) 731 732 //only need to build up child 733 cbft.buildChildNode(newRoot) 734 735 if len(newRoot.Children) > 0{ 736 //the new root's children should re-execute base on new state 737 for _, child := range newRoot.Children { 738 if err := cbft.executeBlockAndDescendant(child, newRoot); err != nil { 739 log.Error("execute the block error", "err", err) 740 break 741 } 742 } 743 //there are some redundancy code for newRoot, but these codes are necessary for other logical blocks 744 cbft.signLogicalAndDescendant(newRoot) 745 } 746 747 } else if newRoot.block != nil { 748 log.Debug("higher irreversible block is existing in memory","newTree", newRoot.toJson()) 749 //the block synced from other peer exists in local peer 750 newRoot.isExecuted = true 751 newRoot.isSigned = true 752 newRoot.isConfirmed = true 753 newRoot.Number = currentBlock.NumberU64() 754 755 if newRoot.inTree == false { 756 newRoot.inTree = true 757 758 cbft.setDescendantInTree(newRoot) 759 760 if len(newRoot.Children) > 0{ 761 //the new root's children should re-execute base on new state 762 for _, child := range newRoot.Children { 763 if err := cbft.executeBlockAndDescendant(child, newRoot); err != nil { 764 log.Error("execute the block error", "err", err) 765 break 766 } 767 } 768 //there are some redundancy code for newRoot, but these codes are necessary for other logical blocks 769 cbft.signLogicalAndDescendant(newRoot) 770 } 771 }else{ 772 //cut off old tree from new root, 773 tailorTree(newRoot) 774 } 775 776 //remove all blocks referenced in old tree after been cut off 777 cbft.cleanByTailoredTree(cbft.getRootIrreversible()) 778 } 779 780 //remove all other blocks those their numbers are too low 781 cbft.cleanByNumber(newRoot.Number) 782 783 log.Debug("the cleared new tree in memory", "json", newRoot.toJson()) 784 785 //reset the new root irreversible 786 cbft.rootIrreversible.Store(newRoot) 787 788 log.Debug("reset the new root irreversible by synced", "hash", newRoot.block.Hash(), "number", newRoot.block.NumberU64()) 789 790 //reset highest confirmed block 791 highestConfirmed :=cbft.findLastClosestConfirmedIncludingSelf(newRoot) 792 cbft.highestConfirmed.Store(highestConfirmed) 793 if cbft.getHighestConfirmed() != nil { 794 log.Debug("reset the highestConfirmed by synced successful", "hash", highestConfirmed.block.Hash(), "number", highestConfirmed.block.NumberU64()) 795 796 //reset logical path 797 highestLogical := cbft.findHighest(highestConfirmed) 798 cbft.setHighestLogical(highestLogical) 799 log.Debug("reset the highestLogical by synced", "hash", highestLogical.block.Hash(), "number", highestLogical.block.NumberU64(), "newRoot.hash", newRoot.block.Hash(), "newRoot.number", newRoot.block.NumberU64() ) 800 801 if !cbft.flushReadyBlock() { 802 //remove all other blocks those their numbers are too low 803 cbft.cleanByNumber(cbft.getRootIrreversible().Number) 804 } 805 806 log.Debug("reset TxPool after block synced", "hash", currentBlock.Hash(), "number", currentBlock.NumberU64()) 807 cbft.txPool.Reset(currentBlock) 808 809 } else { 810 log.Debug("reset the highestConfirmed by synced failure because findLastClosestConfirmedIncludingSelf() returned nil", "newRoot.hash", newRoot.block.Hash(), "newRoot.number", newRoot.block.NumberU64()) 811 //cbft.highestConfirmed.Store(newRoot) 812 } 813 } 814 815 log.Debug("=== end of blockSynced() ===", 816 "highestLogicalHash", cbft.getHighestLogical().block.Hash(), 817 "highestLogicalNumber", cbft.getHighestLogical().Number, 818 "highestConfirmedHash", cbft.getHighestConfirmed().block.Hash(), 819 "highestConfirmedNumber", cbft.getHighestConfirmed().Number, 820 "rootIrreversibleHash", cbft.getRootIrreversible().block.Hash(), 821 "rootIrreversibleNumber", cbft.getRootIrreversible().Number, 822 "number", currentBlock.Number(), 823 "hash", currentBlock.Hash()) 824 } 825 826 // dataReceiverLoop is the main loop that handle the data from worker, or eth protocol's handler 827 // the new blocks packed by local in worker will be handled here; the other blocks and signs received by P2P will be handled here. 828 func (cbft *Cbft) dataReceiverLoop() { 829 for { 830 select { 831 case v := <-cbft.dataReceiveCh: 832 sign, ok := v.(*cbfttypes.BlockSignature) 833 if ok { 834 err := cbft.signReceiver(sign) 835 if err != nil { 836 log.Error("Error", "msg", err) 837 } 838 } else { 839 blockExt, ok := v.(*BlockExt) 840 if ok { 841 err := cbft.blockReceiver(blockExt) 842 if err != nil { 843 log.Error("Error", "msg", err) 844 } 845 } else { 846 _, ok := v.(*cbfttypes.BlockSynced) 847 if ok { 848 cbft.blockSynced() 849 } else { 850 log.Error("Received wrong data type") 851 } 852 } 853 } 854 case <-cbft.exitCh: 855 log.Debug("consensus engine exit") 856 return 857 } 858 } 859 } 860 861 // buildIntoTree inserts current BlockExt to the tree structure 862 func (cbft *Cbft) buildIntoTree(current *BlockExt) { 863 parent := cbft.findParent(current) 864 if parent != nil { 865 //catch up with parent 866 parent.Children = append(parent.Children, current) 867 current.parent = parent 868 current.inTree = parent.inTree 869 } else { 870 log.Warn("cannot find parent block", "hash", current.block.Hash(), "number", current.block.NumberU64()) 871 } 872 873 cbft.buildChildNode(current) 874 } 875 876 func (cbft *Cbft) buildChildNode(current *BlockExt) { 877 children := cbft.findChildren(current) 878 if len(children) > 0 { 879 current.Children = children 880 for _, child := range children { 881 //child should catch up with current 882 child.parent = current 883 } 884 cbft.setDescendantInTree(current) 885 } 886 } 887 888 func (cbft *Cbft) setDescendantInTree(child *BlockExt) { 889 log.Debug("set descendant inTree attribute", "hash", child.block.Hash(), "number", child.Number) 890 for _, grandchild := range child.Children { 891 grandchild.inTree = child.inTree 892 cbft.setDescendantInTree(grandchild) 893 } 894 } 895 896 // removeBadBlock removes bad block executed error from the tree structure and cbft.blockExtMap. 897 func (cbft *Cbft) removeBadBlock(badBlock *BlockExt) { 898 tailorTree(badBlock) 899 cbft.removeByTailored(badBlock) 900 /*for _, child := range badBlock.children { 901 child.parent = nil 902 } 903 cbft.blockExtMap.Delete(badBlock.block.Hash())*/ 904 //delete(cbft.blockExtMap, badBlock.block.Hash()) 905 } 906 907 func (cbft *Cbft) removeByTailored(badBlock *BlockExt) { 908 if len(badBlock.Children) > 0 { 909 for _, child := range badBlock.Children { 910 cbft.removeByTailored(child) 911 cbft.blockExtMap.Delete(child.block.Hash()) 912 } 913 } else { 914 cbft.blockExtMap.Delete(badBlock.block.Hash()) 915 } 916 } 917 918 // signReceiver handles the received block signature 919 func (cbft *Cbft) signReceiver(sig *cbfttypes.BlockSignature) error { 920 log.Debug("=== call signReceiver() ===", 921 "hash", sig.Hash, 922 "number", sig.Number.Uint64(), 923 "highestLogicalHash", cbft.getHighestLogical().block.Hash(), 924 "highestLogicalNumber", cbft.getHighestLogical().Number, 925 "highestConfirmedHash", cbft.getHighestConfirmed().block.Hash(), 926 "highestConfirmedNumber", cbft.getHighestConfirmed().Number, 927 "rootIrreversibleHash", cbft.getRootIrreversible().block.Hash(), 928 "rootIrreversibleNumber", cbft.getRootIrreversible().Number) 929 930 if sig.Number.Uint64() <= cbft.getRootIrreversible().Number { 931 log.Warn("block sign is too late") 932 return nil 933 } 934 935 current := cbft.findBlockExt(sig.Hash) 936 if current == nil { 937 log.Warn("have not received the corresponding block") 938 //the block is nil 939 current = NewEmptyExt(sig.Number.Uint64()) 940 current.inTree = false 941 current.isExecuted = false 942 current.isSigned = false 943 current.isConfirmed = false 944 945 cbft.saveBlockExt(sig.Hash, current) 946 } 947 948 cbft.collectSign(current, sig.Signature) 949 950 var hashLog interface{} 951 if current.block != nil { 952 hashLog = current.block.Hash() 953 } else { 954 hashLog = "sign received before block," 955 } 956 957 log.Debug("count signatures", 958 959 "hash", hashLog, 960 "number", current.Number, 961 "signCount", len(current.signs), 962 "inTree", current.inTree, 963 "isExecuted", current.isExecuted, 964 "isConfirmed", current.isConfirmed, 965 "isSigned", current.isSigned) 966 967 if current.inTree && current.isConfirmed && cbft.getRootIrreversible().isAncestor(current){ 968 //the current is new highestConfirmed on the same logical path 969 if current.Number > cbft.getHighestConfirmed().Number && cbft.getHighestConfirmed().isAncestor(current) { 970 cbft.highestConfirmed.Store(current) 971 newHighestLogical := cbft.findHighestLogical(current) 972 cbft.setHighestLogical(newHighestLogical) 973 } else if current.Number < cbft.getHighestConfirmed().Number && !current.isAncestor(cbft.getHighestConfirmed()) { 974 //only this case may cause a new fork 975 cbft.checkFork(current) 976 } 977 cbft.flushReadyBlock() 978 } 979 980 log.Debug("=== end of signReceiver() ===", 981 "hash", hashLog, 982 "number", current.Number, 983 "highestLogicalHash", cbft.getHighestLogical().block.Hash(), 984 "highestLogicalNumber", cbft.getHighestLogical().Number, 985 "highestConfirmedHash", cbft.getHighestConfirmed().block.Hash(), 986 "highestConfirmedNumber", cbft.getHighestConfirmed().Number, 987 "rootIrreversibleHash", cbft.getRootIrreversible().block.Hash(), 988 "rootIrreversibleNumber", cbft.getRootIrreversible().Number) 989 return nil 990 } 991 992 //blockReceiver handles the new block 993 func (cbft *Cbft) blockReceiver(tmp *BlockExt) error { 994 block := tmp.block 995 rcvTime := tmp.rcvTime 996 log.Debug("=== call blockReceiver() ===", 997 "hash", block.Hash(), 998 "number", block.NumberU64(), 999 "parentHash", block.ParentHash(), 1000 "ReceiptHash", block.ReceiptHash(), 1001 "highestLogicalHash", cbft.getHighestLogical().block.Hash(), 1002 "highestLogicalNumber", cbft.getHighestLogical().Number, 1003 "highestConfirmedHash", cbft.getHighestConfirmed().block.Hash(), 1004 "highestConfirmedNumber", cbft.getHighestConfirmed().Number, 1005 "rootIrreversibleHash", cbft.getRootIrreversible().block.Hash(), 1006 "rootIrreversibleNumber", cbft.getRootIrreversible().Number) 1007 1008 consensusNodes := cbft.ConsensusNodes(new(big.Int).Sub(block.Number(), common.Big1), block.ParentHash(), block.Number()) 1009 if consensusNodes != nil && len(consensusNodes) == 1 && cbft.config.NodeID==consensusNodes[0] { 1010 log.Debug("single node Mode") 1011 cbft.flushReadyBlock() 1012 return nil 1013 } 1014 1015 if block.NumberU64() <= 0 { 1016 return errGenesisBlock 1017 } 1018 1019 if block.NumberU64() <= cbft.getRootIrreversible().Number { 1020 return lateBlock 1021 } 1022 1023 //recover the producer's NodeID 1024 producerID, sign, err := ecrecover(block.Header()) 1025 if err != nil { 1026 return err 1027 } 1028 1029 // curTime := toMilliseconds(time.Now()) 1030 1031 // TODO 1032 //isLegal := cbft.isLegal(rcvTime, producerID) 1033 // TODO: the code that check if legal is commented temporarily. 1034 /*blockNumber := block.Number() 1035 parentNumber := new(big.Int).Sub(blockNumber, common.Big1) 1036 isLegal := cbft.isLegal(rcvTime, parentNumber, block.ParentHash(), blockNumber, producerID) 1037 log.Debug("check if block is legal", 1038 "result", isLegal, 1039 "hash", block.Hash(), 1040 "number", block.NumberU64(), 1041 "parentHash", block.ParentHash(), 1042 "rcvTime", rcvTime, 1043 "producerID", producerID) 1044 if !isLegal { 1045 return errIllegalBlock 1046 }*/ 1047 1048 //to check if there's a existing blockExt for received block 1049 //sometime we'll receive the block's sign before the block self. 1050 blockExt := cbft.findBlockExt(block.Hash()) 1051 if blockExt == nil { 1052 blockExt = tmp 1053 cbft.saveBlockExt(blockExt.block.Hash(), blockExt) 1054 } else if blockExt.block == nil { 1055 //received its sign before. 1056 blockExt.block = block 1057 blockExt.rcvTime = rcvTime 1058 } else { 1059 return errDuplicatedBlock 1060 } 1061 1062 //make tree node 1063 cbft.buildIntoTree(blockExt) 1064 1065 //collect the block's sign of producer 1066 cbft.collectSign(blockExt, common.NewBlockConfirmSign(sign)) 1067 1068 log.Debug("count signatures", 1069 1070 "hash", blockExt.block.Hash(), 1071 "number", blockExt.Number, 1072 "signCount", len(blockExt.signs), 1073 "inTree", blockExt.inTree, 1074 "isExecuted", blockExt.isExecuted, 1075 "isConfirmed", blockExt.isConfirmed, 1076 "isSigned", blockExt.isSigned) 1077 1078 if blockExt.inTree { 1079 if err := cbft.executeBlockAndDescendant(blockExt, blockExt.parent); err != nil { 1080 return err 1081 } 1082 1083 // TODO 1084 //inTurn := cbft.inTurnVerify(blockExt.rcvTime, producerID) 1085 blockNumber := block.Number() 1086 parentNumber := new(big.Int).Sub(blockNumber, common.Big1) 1087 inTurn := cbft.inTurnVerify(parentNumber, block.ParentHash(), blockNumber, blockExt.rcvTime, producerID) 1088 if !inTurn { 1089 log.Warn("not in turn", 1090 "RoutineID", common.CurrentGoRoutineID(), 1091 "hash", block.Hash(), 1092 "number", block.NumberU64(), 1093 "parentHash", block.ParentHash(), 1094 "curTime", rcvTime, 1095 "producerID", producerID) 1096 } 1097 1098 blockExt.inTurn = inTurn 1099 1100 //flowControl := flowControl.control(producerID, curTime) 1101 flowControl := true 1102 highestConfirmedIsAncestor := cbft.getHighestConfirmed().isAncestor(blockExt) 1103 1104 isLogical := inTurn && flowControl && highestConfirmedIsAncestor 1105 1106 log.Debug("check if block is logical", "result", isLogical, "hash", blockExt.block.Hash(), "number", blockExt.Number, "inTurn", inTurn, "flowControl", flowControl, "highestConfirmedIsAncestor", highestConfirmedIsAncestor) 1107 1108 if isLogical { 1109 cbft.signLogicalAndDescendant(blockExt) 1110 1111 newHighestConfirmed := cbft.findLastClosestConfirmedIncludingSelf(cbft.getHighestConfirmed()) 1112 if newHighestConfirmed != nil { 1113 cbft.highestConfirmed.Store(newHighestConfirmed) 1114 } 1115 1116 //rearrange logical path 1117 newHighestLogical := cbft.findHighestLogical(cbft.getHighestConfirmed()) 1118 if newHighestLogical != nil { 1119 cbft.setHighestLogical(newHighestLogical) 1120 } 1121 1122 } else { 1123 closestConfirmed := cbft.findClosestConfirmedIncludingSelf(blockExt) 1124 1125 //if closestConfirmed != nil && closestConfirmed.number < cbft.highestConfirmed.number && !closestConfirmed.isAncestor(cbft.highestConfirmed){ 1126 if closestConfirmed != nil && closestConfirmed.Number < cbft.getHighestConfirmed().Number { 1127 //only this case may cause a new fork 1128 cbft.checkFork(closestConfirmed) 1129 } 1130 } 1131 1132 cbft.flushReadyBlock() 1133 } 1134 log.Debug("=== end of blockReceiver() ===", 1135 "hash", block.Hash(), 1136 "number", block.NumberU64(), 1137 "parentHash", block.ParentHash(), 1138 "ReceiptHash", block.ReceiptHash(), 1139 "highestLogicalHash", cbft.getHighestLogical().block.Hash(), 1140 "highestLogicalNumber", cbft.getHighestLogical().Number, 1141 "highestConfirmedHash", cbft.getHighestConfirmed().block.Hash(), 1142 "highestConfirmedNumber", cbft.getHighestConfirmed().Number, 1143 "rootIrreversibleHash", cbft.getRootIrreversible().block.Hash(), 1144 "rootIrreversibleNumber", cbft.getRootIrreversible().Number) 1145 return nil 1146 } 1147 1148 // forked returns the blocks forked from original branch 1149 // original[0] == newFork[0] == cbft.rootIrreversible, len(origPath) > len(newPath) 1150 func (cbft *Cbft) forked(origPath []*BlockExt, newPath []*BlockExt) (oldTress, newTress []*BlockExt) { 1151 for i := 0; i < len(newPath); i++ { 1152 if newPath[i].block.Hash() != origPath[i].block.Hash() { 1153 return origPath[i:], newPath[i:] 1154 } 1155 } 1156 return nil, nil 1157 } 1158 1159 func extraBlocks(exts []*BlockExt) []*types.Block { 1160 blocks := make([]*types.Block, len(exts)) 1161 for idx, ext := range exts { 1162 blocks[idx] = ext.block 1163 } 1164 return blocks 1165 } 1166 1167 // checkFork checks if the logical path is changed cause the newConfirmed, if changed, this is a new fork. 1168 func (cbft *Cbft) checkFork(newConfirmed *BlockExt) { 1169 newHighestConfirmed := cbft.findLastClosestConfirmedIncludingSelf(newConfirmed) 1170 if newHighestConfirmed != nil && cbft.getRootIrreversible().isAncestor(newHighestConfirmed) && newHighestConfirmed.block.Hash() != cbft.getHighestConfirmed().block.Hash() { 1171 //forked 1172 newHighestLogical := cbft.findHighestLogical(newHighestConfirmed) 1173 newPath := cbft.backTrackBlocks(newHighestLogical, cbft.getRootIrreversible(), true) 1174 1175 origPath := cbft.backTrackBlocks(cbft.getHighestLogical(), cbft.getRootIrreversible(), true) 1176 1177 oldTress, newTress := cbft.forked(origPath, newPath) 1178 1179 if len(newTress) > 0 { 1180 1181 //fork 1182 log.Warn("the block chain in memory forked", "newHighestConfirmedHash", newHighestConfirmed.block.Hash(), "newHighestConfirmedNumber", newHighestConfirmed.Number) 1183 1184 cbft.txPool.ForkedReset(extraBlocks(oldTress), extraBlocks(newTress)) 1185 1186 //forkFrom to lower block 1187 cbft.highestConfirmed.Store(newHighestConfirmed) 1188 cbft.setHighestLogical(newHighestLogical) 1189 } 1190 } 1191 } 1192 1193 // flushReadyBlock finds ready blocks and flush them to chain 1194 func (cbft *Cbft) flushReadyBlock() bool { 1195 log.Debug("check if there's any block ready to flush to chain", "highestConfirmedNumber", cbft.getHighestConfirmed().Number, "rootIrreversibleNumber", cbft.getRootIrreversible().Number) 1196 1197 fallCount := int(cbft.getHighestConfirmed().Number - cbft.getRootIrreversible().Number) 1198 var newRoot *BlockExt 1199 if fallCount == 1 && cbft.getRootIrreversible().isParent(cbft.getHighestConfirmed().block) { 1200 cbft.storeBlocks([]*BlockExt{cbft.getHighestConfirmed()}) 1201 newRoot = cbft.getHighestConfirmed() 1202 } else if fallCount > windowSize { 1203 //find the completed path from root to highest logical 1204 logicalBlocks := cbft.backTrackBlocks(cbft.getHighestConfirmed(), cbft.getRootIrreversible(), false) 1205 total := len(logicalBlocks) 1206 toFlushs := logicalBlocks[:total-windowSize] 1207 1208 logicalBlocks = logicalBlocks[total-windowSize:] 1209 1210 for _, confirmed := range logicalBlocks { 1211 if confirmed.isConfirmed { 1212 toFlushs = append(toFlushs, confirmed) 1213 } else { 1214 break 1215 } 1216 } 1217 1218 cbft.storeBlocks(toFlushs) 1219 1220 for _, confirmed := range toFlushs { 1221 log.Debug("blocks should be flushed to chain ", "hash", confirmed.block.Hash(), "number", confirmed.Number) 1222 } 1223 1224 newRoot = toFlushs[len(toFlushs)-1] 1225 } 1226 if newRoot != nil { 1227 // blocks[0] == cbft.rootIrreversible 1228 oldRoot := cbft.getRootIrreversible() 1229 log.Debug("blockExt tree reorged, root info", "origHash", oldRoot.block.Hash(), "origNumber", oldRoot.Number, "newHash", newRoot.block.Hash(), "newNumber", newRoot.Number) 1230 //cut off old tree from new root, 1231 tailorTree(newRoot) 1232 1233 //set the new root as cbft.rootIrreversible 1234 cbft.rootIrreversible.Store(newRoot) 1235 1236 //remove all blocks referenced in old tree after being cut off 1237 cbft.cleanByTailoredTree(oldRoot) 1238 1239 //remove all other blocks those their numbers are too low 1240 cbft.cleanByNumber(cbft.getRootIrreversible().Number) 1241 return true 1242 } 1243 return false 1244 1245 /*if exceededCount := cbft.highestConfirmed.number - cbft.rootIrreversible.number; exceededCount > 0 { 1246 //find the completed path from root to highest logical 1247 logicalBlocks := cbft.backTrackBlocks(cbft.highestConfirmed, cbft.rootIrreversible, false) 1248 1249 total := len(logicalBlocks) 1250 1251 var newRoot *BlockExt 1252 1253 if total > 20 { 1254 forced := logicalBlocks[:total-20] 1255 log.Warn("force to flush blocks to chain", "blockCount", len(forced)) 1256 1257 cbft.storeBlocks(forced) 1258 1259 newRoot = forced[len(forced)-1] 1260 logicalBlocks = logicalBlocks[total-20:] 1261 } 1262 1263 count := 0 1264 for _, confirmed := range logicalBlocks { 1265 if confirmed.isConfirmed { 1266 newRoot = confirmed 1267 log.Debug("find confirmed block that can be flushed to chain ", "hash", newRoot.block.Hash(), "number", newRoot.number) 1268 count++ 1269 } else { 1270 break 1271 } 1272 } 1273 if count > 0 { 1274 cbft.storeBlocks(logicalBlocks[:count]) 1275 } 1276 if newRoot != nil { 1277 // blocks[0] == cbft.rootIrreversible 1278 oldRoot := cbft.rootIrreversible 1279 log.Debug("oldRoot", "hash", oldRoot.block.Hash(), "number", oldRoot.number) 1280 log.Debug("newRoot", "hash", newRoot.block.Hash(), "number", newRoot.number) 1281 //cut off old tree from new root, 1282 tailorTree(newRoot) 1283 1284 //set the new root as cbft.rootIrreversible 1285 cbft.rootIrreversible = newRoot 1286 1287 //remove all blocks referenced in old tree after being cut off 1288 cbft.cleanByTailoredTree(oldRoot) 1289 1290 //remove all other blocks those their numbers are too low 1291 cbft.cleanByNumber(cbft.rootIrreversible.number) 1292 } 1293 }*/ 1294 } 1295 1296 // tailorTree tailors the old tree from new root 1297 func tailorTree(newRoot *BlockExt) { 1298 if newRoot.parent != nil && newRoot.parent.Children != nil { 1299 for i := 0; i < len(newRoot.parent.Children); i++ { 1300 //remove newRoot from its parent's children list 1301 if newRoot.parent.Children[i].block.Hash() == newRoot.block.Hash() { 1302 newRoot.parent.Children = append(newRoot.parent.Children[:i], newRoot.parent.Children[i+1:]...) 1303 break 1304 } 1305 } 1306 newRoot.parent = nil 1307 } 1308 } 1309 1310 // cleanByTailoredTree removes all blocks in the tree which has been tailored. 1311 func (cbft *Cbft) cleanByTailoredTree(root *BlockExt) { 1312 log.Trace("call cleanByTailoredTree()", "rootHash", root.block.Hash(), "rootNumber", root.block.NumberU64()) 1313 if len(root.Children) > 0 { 1314 for _, child := range root.Children { 1315 cbft.cleanByTailoredTree(child) 1316 log.Debug("remove block in memory", "hash", root.block.Hash(), "number", root.block.NumberU64()) 1317 cbft.blockExtMap.Delete(root.block.Hash()) 1318 //delete(cbft.blockExtMap, root.block.Hash()) 1319 //delete(cbft.signedSet, root.block.NumberU64()) 1320 cbft.signedSet.Delete(root.block.NumberU64()) 1321 } 1322 } else { 1323 log.Debug("remove block in memory", "hash", root.block.Hash(), "number", root.block.NumberU64()) 1324 cbft.blockExtMap.Delete(root.block.Hash()) 1325 //delete(cbft.blockExtMap, root.block.Hash()) 1326 } 1327 } 1328 1329 // cleanByNumber removes all blocks lower than upperLimit in BlockExtMap. 1330 func (cbft *Cbft) cleanByNumber(upperLimit uint64) { 1331 log.Trace("call cleanByNumber()", "upperLimit", upperLimit) 1332 1333 f := func(k, v interface{}) bool { 1334 // The input and input types of this function are fixed and cannot be modified. 1335 // You can write your own code in the body of the function, call k, v in the map 1336 hash := k.(common.Hash) 1337 ext := v.(*BlockExt) 1338 if ext.Number < upperLimit { 1339 log.Debug("remove block in memory", "hash", hash, "number", ext.Number) 1340 cbft.blockExtMap.Delete(hash) 1341 } 1342 return true 1343 } 1344 cbft.blockExtMap.Range(f) 1345 1346 /*for number, _ := range cbft.signedSet { 1347 if number < upperLimit { 1348 delete(cbft.signedSet, number) 1349 } 1350 }*/ 1351 f2 := func(k, v interface{}) bool { 1352 number := k.(uint64) 1353 if number < upperLimit { 1354 cbft.signedSet.Delete(number) 1355 } 1356 return true 1357 } 1358 1359 cbft.signedSet.Range(f2) 1360 } 1361 1362 // Author implements consensus.Engine, returning the Ethereum address recovered 1363 // from the signature in the header's extra-data section. 1364 func (cbft *Cbft) Author(header *types.Header) (common.Address, error) { 1365 log.Trace("call Author()", "hash", header.Hash(), "number", header.Number.Uint64()) 1366 return header.Coinbase, nil 1367 } 1368 1369 // VerifyHeader checks whether a header conforms to the consensus rules. 1370 func (cbft *Cbft) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error { 1371 log.Trace("call VerifyHeader()", "hash", header.Hash(), "number", header.Number.Uint64(), "seal", seal) 1372 1373 if header.Number == nil { 1374 return errUnknownBlock 1375 } 1376 1377 if len(header.Extra) < extraSeal { 1378 return errMissingSignature 1379 } 1380 return nil 1381 } 1382 1383 // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The 1384 // method returns a quit channel to abort the operations and a results channel to 1385 // retrieve the async verifications (the order is that of the input slice). 1386 func (cbft *Cbft) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { 1387 log.Trace("call VerifyHeaders()", "Headers count", len(headers)) 1388 1389 abort := make(chan struct{}) 1390 results := make(chan error, len(headers)) 1391 1392 go func() { 1393 for _, header := range headers { 1394 err := cbft.VerifyHeader(chain, header, false) 1395 1396 select { 1397 case <-abort: 1398 return 1399 case results <- err: 1400 } 1401 } 1402 }() 1403 return abort, results 1404 } 1405 1406 // VerifyUncles implements consensus.Engine, always returning an error for any 1407 // uncles as this consensus mechanism doesn't permit uncles. 1408 func (cbft *Cbft) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { 1409 return nil 1410 } 1411 1412 // VerifySeal implements consensus.Engine, checking whether the signature contained 1413 // in the header satisfies the consensus protocol requirements. 1414 func (cbft *Cbft) VerifySeal(chain consensus.ChainReader, header *types.Header) error { 1415 log.Trace("call VerifySeal()", "hash", header.Hash(), "number", header.Number.String()) 1416 1417 return cbft.verifySeal(chain, header, nil) 1418 } 1419 1420 // Prepare implements consensus.Engine, preparing all the consensus fields of the 1421 // header for running the transactions on top. 1422 func (cbft *Cbft) Prepare(chain consensus.ChainReader, header *types.Header) error { 1423 log.Debug("call Prepare()", "hash", header.Hash(), "number", header.Number.Uint64()) 1424 1425 if cbft.getHighestLogical().block == nil || header.ParentHash != cbft.getHighestLogical().block.Hash() || header.Number.Uint64()-1 != cbft.getHighestLogical().block.NumberU64() { 1426 return consensus.ErrUnknownAncestor 1427 } 1428 1429 // header.Extra[0:32] to store block's version info etc. and right pad with 0x00; 1430 // header.Extra[32:97] to store block's sign of producer, the length of sign is 65. 1431 if len(header.Extra) < 32 { 1432 header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, 32-len(header.Extra))...) 1433 } 1434 header.Extra = header.Extra[:32] 1435 1436 //init header.Extra[32: 32+65] 1437 header.Extra = append(header.Extra, make([]byte, consensus.ExtraSeal)...) 1438 return nil 1439 } 1440 1441 // Finalize implements consensus.Engine, ensuring no uncles are set, nor block 1442 // rewards given, and returns the final block. 1443 func (cbft *Cbft) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { 1444 log.Debug("call Finalize()", "RoutineID", common.CurrentGoRoutineID(), "hash", header.Hash(), "number", header.Number.Uint64(), "txs", len(txs), "receipts", len(receipts), " extra: ", hexutil.Encode(header.Extra)) 1445 cbft.accumulateRewards(chain.Config(), state, header) 1446 cbft.IncreaseRewardPool(state, header.Number) 1447 1448 header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) 1449 header.UncleHash = types.CalcUncleHash(nil) 1450 return types.NewBlock(header, txs, nil, receipts), nil 1451 } 1452 1453 // to sign the block, and store the sign to header.Extra[32:97], send the sign to chanel to broadcast to other consensus nodes 1454 func (cbft *Cbft) Seal(chain consensus.ChainReader, block *types.Block, sealResultCh chan<- *types.Block, stopCh <-chan struct{}) error { 1455 log.Debug("call Seal()", "number", block.NumberU64(), "parentHash", block.ParentHash()) 1456 /*cbft.lock.Lock() 1457 defer cbft.lock.Unlock()*/ 1458 1459 header := block.Header() 1460 number := block.NumberU64() 1461 1462 if number == 0 { 1463 return errUnknownBlock 1464 } 1465 1466 if !cbft.getHighestLogical().isParent(block) { 1467 log.Error("Futile block cause highest logical block changed", "parentHash", block.ParentHash()) 1468 return errFutileBlock 1469 } 1470 1471 // sign the seal hash 1472 sign, err := cbft.signFn(header.SealHash().Bytes()) 1473 if err != nil { 1474 return err 1475 } 1476 1477 // store the sign into the header.Extra[32:97] 1478 copy(header.Extra[32:97], sign[:]) 1479 1480 sealedBlock := block.WithSeal(header) 1481 1482 current := NewBlockExt(sealedBlock, sealedBlock.NumberU64()) 1483 1484 //this block is produced by local node, so need not execute in cbft. 1485 current.inTree = true 1486 current.isExecuted = true 1487 current.isSigned = true 1488 1489 //save the block to cbft.blockExtMap 1490 cbft.saveBlockExt(sealedBlock.Hash(), current) 1491 1492 //collect the sign 1493 cbft.collectSign(current, common.NewBlockConfirmSign(sign)) 1494 1495 //log this signed block's number 1496 //cbft.signedSet[sealedBlock.NumberU64()] = struct{}{} 1497 cbft.signedSet.Store(sealedBlock.NumberU64(), struct{}{}) 1498 1499 //build tree node 1500 cbft.buildIntoTree(current) 1501 1502 log.Debug("seal complete", "hash", sealedBlock.Hash(), "number", block.NumberU64()) 1503 1504 // SetNodeCache 1505 blockNumber := current.block.Number() 1506 parentNumber := new(big.Int).Sub(blockNumber, common.Big1) 1507 sealhash := cbft.SealHash(current.block.Header()) 1508 state := cbft.blockChainCache.ReadStateDB(sealhash) 1509 log.Debug("setNodeCache", "parentNumber", parentNumber, "parentHash", current.block.ParentHash(), "blockNumber", blockNumber, "blockHash", current.block.Hash()) 1510 if state != nil { 1511 1512 genesis := cbft.blockChain.Genesis() 1513 1514 cbft.ppos.SetNodeCache(state, genesis.Number(), parentNumber, blockNumber, genesis.Hash(), block.ParentHash(), current.block.Hash()) 1515 blockInterval := new(big.Int).Sub(current.block.Number(), cbft.blockChain.CurrentBlock().Number()) 1516 cbft.ppos.Submit2Cache(state, blockNumber, blockInterval, current.block.Hash()) 1517 root := state.IntermediateRoot(cbft.blockChain.Config().IsEIP158(current.block.Number())) 1518 log.Debug("Consensus packaged, Finally, after Submit2Cache", "blockNumber", current.block.NumberU64(), "blockHash", current.block.Hash().Hex(), "block.root", current.block.Root().Hex(), "Realt-time state.root", root.Hex()) 1519 } else { 1520 log.Error("setNodeCache error") 1521 } 1522 1523 consensusNodes := cbft.ConsensusNodes(parentNumber, current.block.ParentHash(), blockNumber) 1524 1525 if consensusNodes != nil && len(consensusNodes) == 1 && cbft.config.NodeID==consensusNodes[0]{ 1526 log.Debug("single node Mode") 1527 //only one consensus node, so, each block is highestConfirmed. (lock is needless) 1528 current.rcvTime = toMilliseconds(time.Now()) 1529 current.inTree = true 1530 current.isExecuted = true 1531 current.isSigned = true 1532 current.isConfirmed = true 1533 1534 cbft.setHighestLogical(current) 1535 cbft.highestConfirmed.Store(current) 1536 1537 cbft.dataReceiveCh <- current 1538 1539 log.Debug("reset TxPool after block sealed", "hash", current.block.Hash(), "number", current.Number) 1540 cbft.txPool.Reset(current.block) 1541 return nil 1542 } 1543 1544 //reset cbft.highestLogicalBlockExt cause this block is produced by myself 1545 cbft.setHighestLogical(current) 1546 1547 go func() { 1548 select { 1549 case <-stopCh: 1550 return 1551 case sealResultCh <- sealedBlock: 1552 default: 1553 log.Warn("Sealing result is not ready by miner", "sealHash", header.SealHash()) 1554 } 1555 }() 1556 1557 log.Debug("reset TxPool after block sealed", "hash", current.block.Hash(), "number", current.Number) 1558 cbft.txPool.Reset(current.block) 1559 return nil 1560 } 1561 1562 // SealHash returns the hash of a block prior to it being sealed. 1563 func (b *Cbft) SealHash(header *types.Header) common.Hash { 1564 log.Debug("call SealHash()", "hash", header.Hash(), "number", header.Number.Uint64()) 1565 return header.SealHash() 1566 } 1567 1568 // Close implements consensus.Engine. It's a noop for cbft as there is are no background threads. 1569 func (cbft *Cbft) Close() error { 1570 log.Trace("call Close()") 1571 cbft.closeOnce.Do(func() { 1572 // Short circuit if the exit channel is not allocated. 1573 if cbft.exitCh == nil { 1574 return 1575 } 1576 cbft.exitCh <- struct{}{} 1577 close(cbft.exitCh) 1578 }) 1579 return nil 1580 } 1581 1582 // APIs implements consensus.Engine, returning the user facing RPC API to allow 1583 // controlling the signer voting. 1584 func (cbft *Cbft) APIs(chain consensus.ChainReader) []rpc.API { 1585 log.Trace("call APIs()") 1586 1587 return []rpc.API{{ 1588 Namespace: "cbft", 1589 Version: "1.0", 1590 Service: &API{chain: chain, cbft: cbft}, 1591 Public: false, 1592 }} 1593 } 1594 1595 // OnBlockSignature is called by by protocol handler when it received a new block signature by P2P. 1596 func (cbft *Cbft) OnBlockSignature(chain consensus.ChainReader, nodeID discover.NodeID, rcvSign *cbfttypes.BlockSignature) error { 1597 log.Debug("call OnBlockSignature()", "hash", rcvSign.Hash, "number", rcvSign.Number, "nodeID", hex.EncodeToString(nodeID.Bytes()[:8]), "signHash", rcvSign.SignHash, "cbft.dataReceiveCh.len", len(cbft.dataReceiveCh)) 1598 ok, err := verifySign(nodeID, rcvSign.SignHash, rcvSign.Signature[:]) 1599 if err != nil { 1600 log.Error("verify sign error", "errors", err) 1601 return err 1602 } 1603 1604 if !ok { 1605 log.Error("unauthorized signer") 1606 return errUnauthorizedSigner 1607 } 1608 cbft.dataReceiveCh <- rcvSign 1609 return nil 1610 } 1611 1612 // OnNewBlock is called by protocol handler when it received a new block by P2P. 1613 func (cbft *Cbft) OnNewBlock(chain consensus.ChainReader, rcvBlock *types.Block) error { 1614 log.Debug("call OnNewBlock()", "hash", rcvBlock.Hash(), "number", rcvBlock.NumberU64(), "ParentHash", rcvBlock.ParentHash(), "cbft.dataReceiveCh.len", len(cbft.dataReceiveCh)) 1615 tmp := NewBlockExt(rcvBlock, rcvBlock.NumberU64()) 1616 tmp.rcvTime = toMilliseconds(time.Now()) 1617 tmp.inTree = false 1618 tmp.isExecuted = false 1619 tmp.isSigned = false 1620 tmp.isConfirmed = false 1621 1622 cbft.dataReceiveCh <- tmp 1623 return nil 1624 } 1625 1626 // OnPong is called by protocol handler when it received a new Pong message by P2P. 1627 func (cbft *Cbft) OnPong(nodeID discover.NodeID, netLatency int64) error { 1628 log.Trace("call OnPong()", "nodeID", hex.EncodeToString(nodeID.Bytes()[:8]), "netLatency", netLatency) 1629 1630 cbft.netLatencyLock.Lock() 1631 defer cbft.netLatencyLock.Unlock() 1632 1633 if netLatency >= maxPingLatency { 1634 return nil 1635 } 1636 1637 latencyList, exist := cbft.netLatencyMap[nodeID] 1638 if !exist { 1639 cbft.netLatencyMap[nodeID] = list.New() 1640 cbft.netLatencyMap[nodeID].PushBack(netLatency) 1641 } else { 1642 if latencyList.Len() > 5 { 1643 e := latencyList.Front() 1644 cbft.netLatencyMap[nodeID].Remove(e) 1645 } 1646 cbft.netLatencyMap[nodeID].PushBack(netLatency) 1647 } 1648 return nil 1649 } 1650 1651 // RemovePeer remove the net latency info from netLatencyMap. 1652 func (cbft *Cbft) RemovePeer(nodeID discover.NodeID){ 1653 log.Trace("call RemovePeer()", "nodeID", hex.EncodeToString(nodeID.Bytes()[:8])) 1654 cbft.netLatencyLock.Lock() 1655 defer cbft.netLatencyLock.Unlock() 1656 delete(cbft.netLatencyMap, nodeID) 1657 } 1658 1659 // avgLatency statistics the net latency between local and other peers. 1660 func (cbft *Cbft) avgLatency(nodeID discover.NodeID) int64 { 1661 if latencyList, exist := cbft.netLatencyMap[nodeID]; exist { 1662 sum := int64(0) 1663 counts := int64(0) 1664 for e := latencyList.Front(); e != nil; e = e.Next() { 1665 if latency, ok := e.Value.(int64); ok { 1666 counts++ 1667 sum += latency 1668 } 1669 } 1670 if counts > 0 { 1671 return sum / counts 1672 } 1673 } 1674 return cbft.config.MaxLatency 1675 } 1676 1677 // HighestLogicalBlock returns the cbft.highestLogical.block. 1678 func (cbft *Cbft) HighestLogicalBlock() *types.Block { 1679 log.Debug("call HighestLogicalBlock() ...") 1680 if cbft.getHighestLogical() == nil { 1681 return nil 1682 } else { 1683 return cbft.getHighestLogical().block 1684 } 1685 } 1686 1687 // HighestConfirmedBlock returns the cbft.highestConfirmed.block. 1688 func (cbft *Cbft) HighestConfirmedBlock() *types.Block { 1689 log.Debug("call HighestConfirmedBlock() ...") 1690 if cbft.getHighestConfirmed() == nil { 1691 return nil 1692 } else { 1693 return cbft.getHighestConfirmed().block 1694 } 1695 } 1696 1697 // GetBlock returns the block in blockExtMap. 1698 func (cbft *Cbft) GetBlock(hash common.Hash, number uint64) *types.Block { 1699 /*if ext := cbft.findBlockExt(hash); ext!=nil { 1700 if ext.block != nil && ext.number == number { 1701 return ext.block 1702 } 1703 }*/ 1704 1705 if ext, ok := cbft.blockExtMap.Load(hash); ok { 1706 if ext.(*BlockExt).block != nil && ext.(*BlockExt).Number == number { 1707 return ext.(*BlockExt).block 1708 } 1709 } 1710 return nil 1711 } 1712 1713 // IsSignedBySelf returns if the block is signed by local. 1714 func IsSignedBySelf(sealHash common.Hash, signature []byte) bool { 1715 ok, err := verifySign(cbft.config.NodeID, sealHash, signature) 1716 if err != nil { 1717 log.Error("verify sign error", "errors", err) 1718 return false 1719 } 1720 return ok 1721 } 1722 1723 // storeBlocks sends the blocks to cbft.cbftResultOutCh, the receiver will write them into chain 1724 func (cbft *Cbft) storeBlocks(blocksToStore []*BlockExt) { 1725 for _, ext := range blocksToStore { 1726 cbftResult := &cbfttypes.CbftResult{ 1727 Block: ext.block, 1728 BlockConfirmSigns: ext.signs, 1729 } 1730 log.Debug("send consensus result to worker", "hash", ext.block.Hash(), "number", ext.block.NumberU64(), "signCount", len(ext.signs)) 1731 cbft.cbftResultOutCh <- cbftResult 1732 } 1733 } 1734 1735 // inTurn return if it is local's turn to package new block. 1736 //func (cbft *Cbft) inTurn() bool { 1737 // curTime := toMilliseconds(time.Now()) 1738 // inturn := cbft.calTurn(curTime, cbft.config.NodeID) 1739 // log.Debug("inTurn", "result", inturn) 1740 // return inturn 1741 //} 1742 1743 func (cbft *Cbft) inTurn(parentNumber *big.Int, parentHash common.Hash, commitNumber *big.Int) bool { 1744 curTime := toMilliseconds(time.Now()) 1745 inturn := cbft.calTurn(curTime-300, parentNumber, parentHash, commitNumber, cbft.config.NodeID, current) 1746 if inturn { 1747 inturn = cbft.calTurn(curTime+600, parentNumber, parentHash, commitNumber, cbft.config.NodeID, current) 1748 } 1749 log.Debug("inTurn", "result", inturn) 1750 return inturn 1751 } 1752 1753 // inTurnVerify verifies the time is in the time-window of the nodeID to package new block. 1754 //func (cbft *Cbft) inTurnVerify(curTime int64, nodeID discover.NodeID) bool { 1755 // latency := cbft.avgLatency(nodeID) 1756 // if latency >= maxAvgLatency { 1757 // log.Debug("inTurnVerify, return false cause of net latency", "result", false, "latency", latency) 1758 // return false 1759 // } 1760 // inTurnVerify := cbft.calTurn(curTime-latency, nodeID) 1761 // log.Debug("inTurnVerify", "result", inTurnVerify, "latency", latency) 1762 // return inTurnVerify 1763 //} 1764 func (cbft *Cbft) inTurnVerify(parentNumber *big.Int, parentHash common.Hash, blockNumber *big.Int, rcvTime int64, nodeID discover.NodeID) bool { 1765 latency := cbft.avgLatency(nodeID) 1766 if latency >= maxAvgLatency { 1767 log.Warn("check if peer's turn to commit block", "result", false, "peerID", nodeID, "high latency ", latency) 1768 return false 1769 } 1770 inTurnVerify := cbft.calTurn(rcvTime-latency, parentNumber, parentHash, blockNumber, nodeID, all) 1771 log.Debug("check if peer's turn to commit block", "result", inTurnVerify, "peerID", nodeID, "latency", latency) 1772 return inTurnVerify 1773 } 1774 1775 //shouldKeepIt verifies the time is legal to package new block for the nodeID. 1776 //func (cbft *Cbft) isLegal(rcvTime int64, producerID discover.NodeID) bool { 1777 // offset := 1000 * (cbft.config.Duration/2 - 1) 1778 // isLegal := cbft.calTurn(curTime-offset, producerID) 1779 // if !isLegal { 1780 // isLegal = cbft.calTurn(curTime+offset, producerID) 1781 // } 1782 // return isLegal 1783 //} 1784 func (cbft *Cbft) isLegal(rcvTime int64, parentNumber *big.Int, parentHash common.Hash, blockNumber *big.Int, producerID discover.NodeID) bool { 1785 offset := 1000 * (cbft.config.Duration/2 - 1) 1786 isLegal := cbft.calTurn(rcvTime-offset, parentNumber, parentHash, blockNumber, producerID, all) 1787 if !isLegal { 1788 isLegal = cbft.calTurn(rcvTime+offset, parentNumber, parentHash, blockNumber, producerID, all) 1789 } 1790 return isLegal 1791 } 1792 1793 //func (cbft *Cbft) calTurn(curTime int64, nodeID discover.NodeID) bool { 1794 // nodeIdx := cbft.dpos.NodeIndex(nodeID) 1795 // startEpoch := cbft.dpos.StartTimeOfEpoch() * 1000 1796 // 1797 // if nodeIdx >= 0 { 1798 // durationPerNode := cbft.config.Duration * 1000 1799 // durationPerTurn := durationPerNode * int64(len(cbft.dpos.primaryNodeList)) 1800 // 1801 // min := nodeIdx * (durationPerNode) 1802 // 1803 // value := (curTime - startEpoch) % durationPerTurn 1804 // 1805 // max := (nodeIdx + 1) * durationPerNode 1806 // 1807 // log.Debug("calTurn", "idx", nodeIdx, "min", min, "value", value, "max", max, "curTime", curTime, "startEpoch", startEpoch) 1808 // 1809 // if value > min && value < max { 1810 // return true 1811 // } 1812 // } 1813 // return false 1814 //} 1815 1816 func (cbft *Cbft) calTurn(timePoint int64, parentNumber *big.Int, parentHash common.Hash, blockNumber *big.Int, nodeID discover.NodeID, round int32) bool { 1817 1818 nodeIdx, consensusNodes := cbft.ppos.BlockProducerIndex(parentNumber, parentHash, blockNumber, nodeID, round) 1819 startEpoch := cbft.ppos.StartTimeOfEpoch() * 1000 1820 1821 if nodeIdx >= 0 { 1822 durationPerNode := cbft.config.Duration * 1000 1823 1824 //consensusNodes := cbft.ConsensusNodes(parentNumber, parentHash, blockNumber) 1825 if consensusNodes == nil || len(consensusNodes) <= 0 { 1826 log.Error("there is no consensus node", "number", blockNumber) 1827 return false 1828 } else if len(consensusNodes) == 1 { 1829 return true 1830 } 1831 1832 durationPerTurn := durationPerNode * int64(len(consensusNodes)) 1833 1834 min := nodeIdx * (durationPerNode) 1835 1836 value := (timePoint - startEpoch) % durationPerTurn 1837 1838 max := (nodeIdx + 1) * durationPerNode 1839 1840 if value > min && value < max { 1841 return true 1842 } 1843 }else{ 1844 log.Debug("local is not a consensus node", "localNode", nodeID.String(), "number", blockNumber) 1845 for idx, nid := range consensusNodes{ 1846 log.Debug("consensus node list", "idx", idx, "nodeID", nid.String()) 1847 } 1848 } 1849 return false 1850 } 1851 1852 // producer's signature = header.Extra[32:97] 1853 // public key can be recovered from signature, the length of public key is 65, 1854 // the length of NodeID is 64, nodeID = publicKey[1:] 1855 func ecrecover(header *types.Header) (discover.NodeID, []byte, error) { 1856 var nodeID discover.NodeID 1857 if len(header.Extra) < extraSeal { 1858 return nodeID, []byte{}, errMissingSignature 1859 } 1860 signature := header.Extra[32:97] 1861 sealHash := header.SealHash() 1862 1863 pubkey, err := crypto.Ecrecover(sealHash.Bytes(), signature) 1864 if err != nil { 1865 return nodeID, []byte{}, err 1866 } 1867 1868 nodeID, err = discover.BytesID(pubkey[1:]) 1869 if err != nil { 1870 return nodeID, []byte{}, err 1871 } 1872 return nodeID, signature, nil 1873 } 1874 1875 // verify sign, check the sign is from the right node. 1876 func verifySign(expectedNodeID discover.NodeID, sealHash common.Hash, signature []byte) (bool, error) { 1877 pubkey, err := crypto.SigToPub(sealHash.Bytes(), signature) 1878 1879 if err != nil { 1880 return false, err 1881 } 1882 1883 nodeID := discover.PubkeyID(pubkey) 1884 if bytes.Equal(nodeID.Bytes(), expectedNodeID.Bytes()) { 1885 return true, nil 1886 } 1887 return false, nil 1888 } 1889 1890 func (cbft *Cbft) verifySeal(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error { 1891 // Verifying the genesis block is not supported 1892 number := header.Number.Uint64() 1893 if number == 0 { 1894 return errUnknownBlock 1895 } 1896 return nil 1897 } 1898 1899 func (cbft *Cbft) signFn(headerHash []byte) (sign []byte, err error) { 1900 return crypto.Sign(headerHash, cbft.config.PrivateKey) 1901 } 1902 1903 //func (cbft *Cbft) getThreshold() int { 1904 // trunc := len(cbft.dpos.primaryNodeList) * 2 / 3 1905 // return int(trunc + 1) 1906 //} 1907 1908 1909 func (cbft *Cbft) getConnectedConsensusNodes(consensusNodes []discover.NodeID) int { 1910 cbft.netLatencyLock.RLock() 1911 defer cbft.netLatencyLock.RUnlock() 1912 1913 connected :=0 1914 for _,nodeId :=range consensusNodes { 1915 if _, exist := cbft.netLatencyMap[nodeId]; exist { 1916 connected++ 1917 } 1918 } 1919 return connected 1920 } 1921 1922 1923 func (cbft *Cbft) getThreshold(parentNumber *big.Int, parentHash common.Hash, blockNumber *big.Int) int { 1924 consensusNodes := cbft.ConsensusNodes(parentNumber, parentHash, blockNumber) 1925 if consensusNodes != nil { 1926 total := len(consensusNodes) 1927 log.Debug("consensus node quantity", "total", total) 1928 trunc := total * 2 / 3 1929 return int(trunc + 1) 1930 } 1931 return math.MaxInt16 1932 } 1933 1934 func (cbft *Cbft) calculateThreshold(consensusNodes []discover.NodeID) int { 1935 if consensusNodes != nil { 1936 trunc := len(consensusNodes) * 2 / 3 1937 return int(trunc + 1) 1938 } 1939 return math.MaxInt16 1940 } 1941 1942 1943 func toMilliseconds(t time.Time) int64 { 1944 return t.UnixNano() / 1e6 1945 } 1946 1947 func (cbft *Cbft) ShouldSeal(parentNumber *big.Int, parentHash common.Hash, commitNumber *big.Int) bool { 1948 log.Trace("call ShouldSeal()") 1949 1950 //consensusNodes := cbft.ConsensusNodes(parentNumber, parentHash, commitNumber) 1951 //if consensusNodes != nil && len(consensusNodes) == 1 { 1952 // return true 1953 //} 1954 1955 inturn := cbft.inTurn(parentNumber, parentHash, commitNumber) 1956 1957 if inturn { 1958 consensusNodes := cbft.ConsensusNodes(parentNumber, parentHash, commitNumber) 1959 threshold := cbft.calculateThreshold(consensusNodes) 1960 connected := cbft.getConnectedConsensusNodes(consensusNodes) 1961 1962 log.Debug("connected consensus peers(including self)", "count", connected+1, "threshold", threshold) 1963 if connected < threshold-1 { 1964 inturn = false 1965 } 1966 } 1967 log.Debug("end of ShouldSeal()", "result", inturn, "number", commitNumber) 1968 return inturn 1969 } 1970 1971 func (cbft *Cbft) CurrentNodes(parentNumber *big.Int, parentHash common.Hash, blockNumber *big.Int) []*discover.Node { 1972 return cbft.ppos.getCurrentNodes(parentNumber, parentHash, blockNumber) 1973 } 1974 1975 func (cbft *Cbft) IsCurrentNode(parentNumber *big.Int, parentHash common.Hash, blockNumber *big.Int) bool { 1976 currentNodes := cbft.ppos.getCurrentNodes(parentNumber, parentHash, blockNumber) 1977 nodeID := cbft.GetOwnNodeID() 1978 for _, n := range currentNodes { 1979 if nodeID == n.ID { 1980 return true 1981 } 1982 } 1983 return false 1984 } 1985 1986 func (cbft *Cbft) ConsensusNodes(parentNumber *big.Int, parentHash common.Hash, blockNumber *big.Int) []discover.NodeID { 1987 return cbft.ppos.consensusNodes(parentNumber, parentHash, blockNumber) 1988 } 1989 1990 // wether nodeID in former or current or next 1991 //func (cbft *Cbft) CheckConsensusNode(nodeID discover.NodeID) (bool, error) { 1992 // log.Debug("call CheckConsensusNode()", "nodeID", hex.EncodeToString(nodeID.Bytes()[:8])) 1993 // return cbft.ppos.AnyIndex(nodeID) >= 0, nil 1994 //} 1995 1996 // wether nodeID in former or current or next 1997 //func (cbft *Cbft) IsConsensusNode() (bool, error) { 1998 // log.Debug("call IsConsensusNode()") 1999 // return cbft.ppos.AnyIndex(cbft.config.NodeID) >= 0, nil 2000 //} 2001 2002 func (cbft *Cbft) Election(state *state.StateDB, parentHash common.Hash, blockNumber *big.Int) ([]*discover.Node, error) { 2003 return cbft.ppos.Election(state, parentHash, blockNumber) 2004 } 2005 2006 func (cbft *Cbft) Switch(state *state.StateDB, blockNumber *big.Int) bool { 2007 return cbft.ppos.Switch(state, blockNumber) 2008 } 2009 2010 func (cbft *Cbft) GetWitness(state *state.StateDB, flag int, blockNumber *big.Int) ([]*discover.Node, error) { 2011 return cbft.ppos.GetWitness(state, flag, blockNumber) 2012 } 2013 2014 func (cbft *Cbft) GetOwnNodeID() discover.NodeID { 2015 return cbft.config.NodeID 2016 } 2017 2018 func (cbft *Cbft) SetNodeCache(state *state.StateDB, parentNumber, currentNumber *big.Int, parentHash, currentHash common.Hash) error { 2019 log.Info("cbft SetNodeCache", "parentNumber", parentNumber, "parentHash", parentHash, "currentNumber", currentNumber, "currentHash", currentHash) 2020 genesis := cbft.blockChain.Genesis() 2021 return cbft.ppos.SetNodeCache(state, genesis.Number(), parentNumber, currentNumber, genesis.Hash(), parentHash, currentHash) 2022 } 2023 2024 func (cbft *Cbft) Notify(state vm.StateDB, blockNumber *big.Int) error { 2025 return cbft.ppos.Notify(state, blockNumber) 2026 } 2027 2028 func (cbft *Cbft) StoreHash(state *state.StateDB, blockNumber *big.Int, blockHash common.Hash) { 2029 cbft.ppos.StoreHash(state, blockNumber, blockHash) 2030 } 2031 2032 func (cbft *Cbft) Submit2Cache(state *state.StateDB, currBlocknumber *big.Int, blockInterval *big.Int, currBlockhash common.Hash) { 2033 cbft.ppos.Submit2Cache(state, currBlocknumber, blockInterval, currBlockhash) 2034 } 2035 2036 // AccumulateRewards for lucky tickets 2037 // Adjust rewards every 3600*24*365 blocks 2038 func (cbft *Cbft) accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header) { 2039 if len(header.Extra) < 64 { 2040 log.Error("Failed to Call accumulateRewards, header.Extra < 64", "blockNumber", header.Number, "blockHash", header.Hash(), "len(header.Extra):", len(header.Extra), "extra", hexutil.Encode(header.Extra)) 2041 return 2042 } 2043 2044 2045 appendEtraFunc := func(txHash common.Hash, isPackageNode bool) { 2046 if isPackageNode { 2047 var buffer bytes.Buffer 2048 buffer.Write(header.Extra) 2049 buffer.Write(txHash.Bytes()) 2050 header.Extra = buffer.Bytes() 2051 log.Info("Call accumulateRewards, When After Sets the Maybe lucky ticket into header.Extra", "len(header.Extra):", len(header.Extra), "extra", hexutil.Encode(header.Extra)) 2052 } 2053 } 2054 2055 2056 var nodeId discover.NodeID 2057 var err error 2058 log.Debug("Call accumulateRewards block header", "len(header.Extra):", len(header.Extra), "extra", hexutil.Encode(header.Extra)) 2059 2060 packageNodeFlag := bytes.Equal(header.Extra[32:97], make([]byte, 65)) 2061 2062 if packageNodeFlag { 2063 log.Warn("Call accumulateRewards block header extra[32:97] is empty!", "blockNumber", header.Number, "blockHash", header.Hash()) 2064 nodeId = cbft.config.NodeID 2065 } else { 2066 if nodeId, _, err = ecrecover(header); err != nil { 2067 2068 appendEtraFunc((common.Hash{}), packageNodeFlag) 2069 2070 log.Error("Failed to Call accumulateRewards, ecrecover faile", " err: ", err, "blockNumber", header.Number, "blockHash", header.Hash()) 2071 return 2072 } else { 2073 log.Info("Success ecrecover", "blockNumber", header.Number, "blockHash", header.Hash(), " nodeId: ", nodeId.String()) 2074 } 2075 } 2076 2077 //log.Info("Call accumulateRewards", "nodeid: ", nodeId.String()) 2078 var can *types.Candidate 2079 if big.NewInt(0).Cmp(new(big.Int).Rem(header.Number, big.NewInt(common.BaseSwitchWitness))) == 0 { 2080 can = cbft.ppos.GetWitnessCandidate(state, nodeId, -1, header.Number) 2081 } else { 2082 can = cbft.ppos.GetWitnessCandidate(state, nodeId, 0, header.Number) 2083 } 2084 if err != nil { 2085 2086 appendEtraFunc((common.Hash{}), packageNodeFlag) 2087 2088 log.Error("Failed to Call accumulateRewards, GetCandidate faile ", " err: ", err.Error(), 2089 "blockNumber", header.Number, "blockHash", header.Hash(), "nodeId", nodeId.String()) 2090 return 2091 } 2092 if can == nil { 2093 2094 appendEtraFunc((common.Hash{}), packageNodeFlag) 2095 2096 log.Warn("Call accumulateRewards, Witness's can is Empty !!!!!!!!!!!!!!!!!!!!!!!!", 2097 "blockNumber", header.Number, "blockHash", header.Hash(), "nodeId", nodeId.String()) 2098 return 2099 } 2100 2101 // store the lucky ticket into the header.Extra[97:129] 2102 appendEtraFunc(can.TxHash, packageNodeFlag) 2103 2104 //Calculate current block rewards 2105 var blockReward *big.Int 2106 preYearNumber := new(big.Int).Sub(header.Number, common.YearBlocks) 2107 yearReward := new(big.Int).Set(common.FirstYearReward) 2108 if preYearNumber.Cmp(common.YearBlocks) > 0 { // otherwise is 0 year and 1 year block reward 2109 yearReward = new(big.Int).Sub(GetAmount(header.Number), GetAmount(preYearNumber)) 2110 } 2111 blockReward = new(big.Int).Div(yearReward, common.YearBlocks) 2112 2113 nodeReward := blockReward 2114 //log.Info("Call accumulateRewards, GetTicket ", "TicketId: ", can.TicketId.Hex()) 2115 if can.TOwner != (common.Address{}) { 2116 nodeReward = new(big.Int).Div(new(big.Int).Mul(blockReward, new(big.Int).SetUint64(uint64(can.Fee))), common.FeeBase) 2117 ticketReward := new(big.Int).Sub(blockReward, nodeReward) 2118 2119 //log.Info("Call accumulateRewards, Rewards detail", "blockReward: ", blockReward, "nodeReward: ", nodeReward, "ticketReward: ", ticketReward) 2120 state.AddBalance(can.TOwner, ticketReward) 2121 log.Info("Ticket accumulateRewards", "txHash", can.TxHash.Hex(), "ticketOwner", can.TOwner.Hex(), "ticketReward", ticketReward, "ticketOwnerBalance", state.GetBalance(can.TOwner)) 2122 } 2123 state.AddBalance(header.Coinbase, nodeReward) 2124 state.SubBalance(common.RewardPoolAddr, blockReward) 2125 2126 log.Info("Call accumulateRewards SUCCESS !! ", "blockNumber", header.Number, "blockHash", header.Hash(), 2127 "nodeId", nodeId.String(), "ticketId", can.TxHash.Hex(), " yearReward: ", yearReward, " blockReward:", blockReward, 2128 " nodeReward: ", nodeReward, " RewardPoolAddr address: ", common.RewardPoolAddr.Hex(), 2129 " balance: ", state.GetBalance(common.RewardPoolAddr), " Fee: ", can.Fee, " Coinbase address: ", header.Coinbase.Hex(), 2130 " balance: ", state.GetBalance(header.Coinbase)) 2131 } 2132 2133 func (cbft *Cbft) IncreaseRewardPool(state *state.StateDB, number *big.Int) { 2134 //add balance to reward pool 2135 if new(big.Int).Rem(number, common.YearBlocks).Cmp(big.NewInt(0)) == 0 { 2136 num := GetAmount(number) 2137 log.Info("Call IncreaseRewardPool SUCCESS !! ", "addr", common.RewardPoolAddr.Hex(), "num", num.String()) 2138 state.AddBalance(common.RewardPoolAddr, num) 2139 } 2140 } 2141 2142 func GetAmount(number *big.Int) *big.Int { 2143 cycle := new(big.Int).Div(number, common.YearBlocks) 2144 rate := math2.BigPow(common.Rate.Int64(), cycle.Int64()) 2145 base := math2.BigPow(common.Base.Int64(), cycle.Int64()) 2146 //fmt.Println("number: ", number, " cycle: ", cycle, " rate: ", rate, " base: ", base) 2147 yearAmount := new(big.Int).Mul(common.InitAmount, rate) 2148 ret := new(big.Int).Div(yearAmount, base) 2149 return ret 2150 } 2151 2152 func (cbft *Cbft) FindTransaction(txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { 2153 return cbft.blockChain.GetTransactionByHash(txHash) 2154 } 2155 2156 func (cbft *Cbft) GetHeader(blockHash common.Hash, blockNumber uint64) *types.Header { 2157 return cbft.blockChain.GetHeader(blockHash, blockNumber) 2158 } 2159 2160 func (cbft *Cbft) GetBody(blockNumber uint64) *types.Body { 2161 return cbft.blockChain.GetBodyByNumber(blockNumber) 2162 } 2163 2164 func (cbft *Cbft) GetNewStateDB(root common.Hash, blockNumber *big.Int, blockHash common.Hash) (*state.StateDB, error) { 2165 return cbft.blockChain.GetNewStateDB(root, blockNumber, blockHash) 2166 }