Skip to content

Commit 71d9367

Browse files
committed
Merge pull request bnb-chain#1151 from fjl/parallel-nonce-2
core: re-add parallel nonce checks
2 parents 122d2db + 55b60e6 commit 71d9367

1 file changed

Lines changed: 49 additions & 67 deletions

File tree

‎core/chain_manager.go‎

Lines changed: 49 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,14 @@ type queueEvent struct {
522522
}
523523

524524
func (self *ChainManager) procFutureBlocks() {
525-
blocks := []*types.Block{}
525+
var blocks []*types.Block
526526
self.futureBlocks.Each(func(i int, block *types.Block) {
527527
blocks = append(blocks, block)
528528
})
529-
530-
types.BlockBy(types.Number).Sort(blocks)
531-
self.InsertChain(blocks)
529+
if len(blocks) > 0 {
530+
types.BlockBy(types.Number).Sort(blocks)
531+
self.InsertChain(blocks)
532+
}
532533
}
533534

534535
// InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned
@@ -540,17 +541,34 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
540541
self.chainmu.Lock()
541542
defer self.chainmu.Unlock()
542543

543-
// A queued approach to delivering events. This is generally faster than direct delivery and requires much less mutex acquiring.
544+
// A queued approach to delivering events. This is generally
545+
// faster than direct delivery and requires much less mutex
546+
// acquiring.
544547
var (
545548
queue = make([]interface{}, len(chain))
546549
queueEvent = queueEvent{queue: queue}
547550
stats struct{ queued, processed, ignored int }
548551
tstart = time.Now()
552+
553+
nonceDone = make(chan nonceResult, len(chain))
554+
nonceQuit = make(chan struct{})
555+
nonceChecked = make([]bool, len(chain))
549556
)
550557

558+
// Start the parallel nonce verifier.
559+
go verifyNonces(self.pow, chain, nonceQuit, nonceDone)
560+
defer close(nonceQuit)
561+
551562
for i, block := range chain {
552-
if block == nil {
553-
continue
563+
// Wait for block i's nonce to be verified before processing
564+
// its state transition.
565+
for nonceChecked[i] {
566+
r := <-nonceDone
567+
nonceChecked[r.i] = true
568+
if !r.valid {
569+
block := chain[i]
570+
return i, ValidationError("Block (#%v / %x) nonce is invalid (= %x)", block.Number(), block.Hash(), block.Nonce)
571+
}
554572
}
555573

556574
if BadHashes[block.Hash()] {
@@ -559,10 +577,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
559577
return i, err
560578
}
561579

562-
// create a nonce channel for parallisation of the nonce check
563-
nonceErrCh := make(chan error)
564-
go verifyBlockNonce(self.pow, block, nonceErrCh)
565-
566580
// Setting block.Td regardless of error (known for example) prevents errors down the line
567581
// in the protocol handler
568582
block.Td = new(big.Int).Set(CalcTD(block, self.GetBlock(block.ParentHash())))
@@ -571,9 +585,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
571585
// all others will fail too (unless a known block is returned).
572586
logs, err := self.processor.Process(block)
573587
if err != nil {
574-
// empty the nonce channel
575-
<-nonceErrCh
576-
577588
if IsKnownBlockErr(err) {
578589
stats.ignored++
579590
continue
@@ -597,11 +608,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
597608

598609
return i, err
599610
}
600-
// Wait and check nonce channel and make sure it checks out fine
601-
// otherwise return the error
602-
if err := <-nonceErrCh; err != nil {
603-
return i, err
604-
}
605611

606612
cblock := self.currentBlock
607613
// Compare the TD of the last known block in the canonical chain to make sure it's greater.
@@ -776,66 +782,42 @@ func blockErr(block *types.Block, err error) {
776782
h := block.Header()
777783
glog.V(logger.Error).Infof("Bad block #%v (%x)\n", h.Number, h.Hash().Bytes())
778784
glog.V(logger.Error).Infoln(err)
779-
glog.V(logger.Debug).Infoln(block)
785+
glog.V(logger.Debug).Infoln(verifyNonces)
780786
}
781787

782-
// verifyNonces verifies nonces of the given blocks in parallel and returns
788+
type nonceResult struct {
789+
i int
790+
valid bool
791+
}
792+
793+
// block verifies nonces of the given blocks in parallel and returns
783794
// an error if one of the blocks nonce verifications failed.
784-
func verifyNonces(pow pow.PoW, blocks []*types.Block) error {
795+
func verifyNonces(pow pow.PoW, blocks []*types.Block, quit <-chan struct{}, done chan<- nonceResult) {
785796
// Spawn a few workers. They listen for blocks on the in channel
786797
// and send results on done. The workers will exit in the
787798
// background when in is closed.
788799
var (
789-
in = make(chan *types.Block)
790-
done = make(chan error, runtime.GOMAXPROCS(0))
800+
in = make(chan int)
801+
nworkers = runtime.GOMAXPROCS(0)
791802
)
792803
defer close(in)
793-
for i := 0; i < cap(done); i++ {
794-
go verifyNonce(pow, in, done)
804+
if len(blocks) < nworkers {
805+
nworkers = len(blocks)
795806
}
796-
// Feed blocks to the workers, aborting at the first invalid nonce.
797-
var (
798-
running, i int
799-
block *types.Block
800-
sendin = in
801-
)
802-
for i < len(blocks) || running > 0 {
803-
if i == len(blocks) {
804-
// Disable sending to in.
805-
sendin = nil
806-
} else {
807-
block = blocks[i]
808-
i++
809-
}
810-
select {
811-
case sendin <- block:
812-
running++
813-
case err := <-done:
814-
running--
815-
if err != nil {
816-
return err
807+
for i := 0; i < nworkers; i++ {
808+
go func() {
809+
for i := range in {
810+
done <- nonceResult{i: i, valid: pow.Verify(blocks[i])}
817811
}
818-
}
812+
}()
819813
}
820-
return nil
821-
}
822-
823-
// verifyNonce is a worker for the verifyNonces method. It will run until
824-
// in is closed.
825-
func verifyNonce(pow pow.PoW, in <-chan *types.Block, done chan<- error) {
826-
for block := range in {
827-
if !pow.Verify(block) {
828-
done <- ValidationError("Block (#%v / %x) nonce is invalid (= %x)", block.Number(), block.Hash(), block.Nonce)
829-
} else {
830-
done <- nil
814+
// Feed block indices to the workers.
815+
for i := range blocks {
816+
select {
817+
case in <- i:
818+
continue
819+
case <-quit:
820+
return
831821
}
832822
}
833823
}
834-
835-
func verifyBlockNonce(pow pow.PoW, block *types.Block, done chan<- error) {
836-
if !pow.Verify(block) {
837-
done <- ValidationError("Block (#%v / %x) nonce is invalid (= %x)", block.Number(), block.Hash(), block.Nonce)
838-
} else {
839-
done <- nil
840-
}
841-
}

0 commit comments

Comments
 (0)