Skip to content

Commit e7e2cbf

Browse files
committed
core: re-add parallel nonce checks
In this incancation, the processor waits until the nonce has been verified before handling the block.
1 parent 5b14fdb commit e7e2cbf

1 file changed

Lines changed: 44 additions & 63 deletions

File tree

‎core/chain_manager.go‎

Lines changed: 44 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -540,17 +540,34 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
540540
self.chainmu.Lock()
541541
defer self.chainmu.Unlock()
542542

543-
// A queued approach to delivering events. This is generally faster than direct delivery and requires much less mutex acquiring.
543+
// A queued approach to delivering events. This is generally
544+
// faster than direct delivery and requires much less mutex
545+
// acquiring.
544546
var (
545547
queue = make([]interface{}, len(chain))
546548
queueEvent = queueEvent{queue: queue}
547549
stats struct{ queued, processed, ignored int }
548550
tstart = time.Now()
551+
552+
nonceDone = make(chan nonceResult, len(chain))
553+
nonceQuit = make(chan struct{})
554+
nonceChecked = make([]bool, len(chain))
549555
)
550556

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

556573
if BadHashes[block.Hash()] {
@@ -559,10 +576,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
559576
return i, err
560577
}
561578

562-
// create a nonce channel for parallisation of the nonce check
563-
nonceErrCh := make(chan error)
564-
go verifyBlockNonce(self.pow, block, nonceErrCh)
565-
566579
// Setting block.Td regardless of error (known for example) prevents errors down the line
567580
// in the protocol handler
568581
block.Td = new(big.Int).Set(CalcTD(block, self.GetBlock(block.ParentHash())))
@@ -571,9 +584,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
571584
// all others will fail too (unless a known block is returned).
572585
logs, err := self.processor.Process(block)
573586
if err != nil {
574-
// empty the nonce channel
575-
<-nonceErrCh
576-
577587
if IsKnownBlockErr(err) {
578588
stats.ignored++
579589
continue
@@ -597,11 +607,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
597607

598608
return i, err
599609
}
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-
}
605610

606611
cblock := self.currentBlock
607612
// Compare the TD of the last known block in the canonical chain to make sure it's greater.
@@ -776,66 +781,42 @@ func blockErr(block *types.Block, err error) {
776781
h := block.Header()
777782
glog.V(logger.Error).Infof("Bad block #%v (%x)\n", h.Number, h.Hash().Bytes())
778783
glog.V(logger.Error).Infoln(err)
779-
glog.V(logger.Debug).Infoln(block)
784+
glog.V(logger.Debug).Infoln(verifyNonces)
785+
}
786+
787+
type nonceResult struct {
788+
i int
789+
valid bool
780790
}
781791

782-
// verifyNonces verifies nonces of the given blocks in parallel and returns
792+
// block verifies nonces of the given blocks in parallel and returns
783793
// an error if one of the blocks nonce verifications failed.
784-
func verifyNonces(pow pow.PoW, blocks []*types.Block) error {
794+
func verifyNonces(pow pow.PoW, blocks []*types.Block, quit <-chan struct{}, done chan<- nonceResult) {
785795
// Spawn a few workers. They listen for blocks on the in channel
786796
// and send results on done. The workers will exit in the
787797
// background when in is closed.
788798
var (
789-
in = make(chan *types.Block)
790-
done = make(chan error, runtime.GOMAXPROCS(0))
799+
in = make(chan int)
800+
nworkers = runtime.GOMAXPROCS(0)
791801
)
792802
defer close(in)
793-
for i := 0; i < cap(done); i++ {
794-
go verifyNonce(pow, in, done)
803+
if len(blocks) < nworkers {
804+
nworkers = len(blocks)
795805
}
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
806+
for i := 0; i < nworkers; i++ {
807+
go func() {
808+
for i := range in {
809+
done <- nonceResult{i: i, valid: pow.Verify(blocks[i])}
817810
}
818-
}
811+
}()
819812
}
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
813+
// Feed block indices to the workers.
814+
for i := range blocks {
815+
select {
816+
case in <- i:
817+
continue
818+
case <-quit:
819+
return
831820
}
832821
}
833822
}
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)