From 767fe584b596ce45117e51b0de6514840ff99a09 Mon Sep 17 00:00:00 2001
From: atvanguard <93arpit@gmail.com>
Date: Mon, 18 May 2020 12:37:20 +0530
Subject: [PATCH] fix: revert cancel active seal logic

---
 consensus/bor/bor.go    | 52 ++++++-----------------------------------
 consensus/bor/errors.go | 11 ---------
 consensus/consensus.go  |  1 -
 core/blockchain.go      |  1 -
 4 files changed, 7 insertions(+), 58 deletions(-)

diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go
index 7ee2f52be..0d244548e 100644
--- a/consensus/bor/bor.go
+++ b/consensus/bor/bor.go
@@ -127,12 +127,6 @@ var (
 	errRecentlySigned = errors.New("recently signed")
 )
 
-// ActiveSealingOp keeps the context of the active sealing operation
-type ActiveSealingOp struct {
-	number uint64
-	cancel context.CancelFunc
-}
-
 // SignerFn is a signer callback function to request a header to be signed by a
 // backing account.
 type SignerFn func(accounts.Account, string, []byte) ([]byte, error)
@@ -238,9 +232,8 @@ type Bor struct {
 	stateReceiverABI abi.ABI
 	HeimdallClient   IHeimdallClient
 
-	stateDataFeed   event.Feed
-	scope           event.SubscriptionScope
-	activeSealingOp *ActiveSealingOp
+	stateDataFeed event.Feed
+	scope         event.SubscriptionScope
 	// The fields below are for testing only
 	fakeDiff bool // Skip difficulty verifications
 }
@@ -728,9 +721,6 @@ func (c *Bor) Authorize(signer common.Address, signFn SignerFn) {
 // Seal implements consensus.Engine, attempting to create a sealed block using
 // the local signing credentials.
 func (c *Bor) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
-	// if c.activeSealingOp != nil {
-	// 	return &SealingInFlightError{c.activeSealingOp.number}
-	// }
 	header := block.Header()
 
 	// Sealing the genesis block is not supported
@@ -777,17 +767,12 @@ func (c *Bor) Seal(chain consensus.ChainReader, block *types.Block, results chan
 
 	// Wait until sealing is terminated or delay timeout.
 	log.Trace("Waiting for slot to sign and propagate", "delay", common.PrettyDuration(delay))
-	shouldSeal := make(chan bool)
-	go c.WaitForSealingOp(number, shouldSeal, delay, stop)
 	go func() {
-		defer func() {
-			close(shouldSeal)
-			c.activeSealingOp = nil
-		}()
-		switch <-shouldSeal {
-		case false:
+		select {
+		case <-stop:
+			log.Debug("Discarding sealing operation for block", "number", number)
 			return
-		case true:
+		case <-time.After(delay):
 			if wiggle > 0 {
 				log.Info(
 					"Sealing out-of-turn",
@@ -803,38 +788,15 @@ func (c *Bor) Seal(chain consensus.ChainReader, block *types.Block, results chan
 				"headerDifficulty", header.Difficulty,
 			)
 		}
-
 		select {
 		case results <- block.WithSeal(header):
 		default:
-			log.Warn("Sealing result was not read by miner", "sealhash", SealHash(header))
+			log.Warn("Sealing result was not read by miner", "number", number, "sealhash", SealHash(header))
 		}
 	}()
 	return nil
 }
 
-// WaitForSealingOp blocks until delay elapses or stop signal is received
-func (c *Bor) WaitForSealingOp(number uint64, shouldSeal chan bool, delay time.Duration, stop <-chan struct{}) {
-	ctx, cancel := context.WithCancel(context.Background())
-	c.activeSealingOp = &ActiveSealingOp{number, cancel}
-	select {
-	case <-stop:
-		shouldSeal <- false
-	case <-ctx.Done():
-		shouldSeal <- false
-	case <-time.After(delay):
-		shouldSeal <- true
-	}
-}
-
-// CancelActiveSealingOp cancels in-flight sealing process
-func (c *Bor) CancelActiveSealingOp() {
-	if c.activeSealingOp != nil {
-		log.Debug("Discarding active sealing operation", "number", c.activeSealingOp.number)
-		c.activeSealingOp.cancel()
-	}
-}
-
 // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
 // that a new block should have based on the previous blocks in the chain and the
 // current signer.
diff --git a/consensus/bor/errors.go b/consensus/bor/errors.go
index f357705db..7e0b90941 100644
--- a/consensus/bor/errors.go
+++ b/consensus/bor/errors.go
@@ -69,17 +69,6 @@ func (e *MaxCheckpointLengthExceededError) Error() string {
 	)
 }
 
-type SealingInFlightError struct {
-	Number uint64
-}
-
-func (e *SealingInFlightError) Error() string {
-	return fmt.Sprintf(
-		"Requested concurrent block sealing. Sealing for block %d is already in progress",
-		e.Number,
-	)
-}
-
 // MismatchingValidatorsError is returned if a last block in sprint contains a
 // list of validators different from the one that local node calculated
 type MismatchingValidatorsError struct {
diff --git a/consensus/consensus.go b/consensus/consensus.go
index 0620cff05..037239113 100644
--- a/consensus/consensus.go
+++ b/consensus/consensus.go
@@ -120,7 +120,6 @@ type Engine interface {
 type Bor interface {
 	Engine
 	IsValidatorAction(chain ChainReader, from common.Address, tx *types.Transaction) bool
-	CancelActiveSealingOp()
 }
 
 // PoW is a consensus engine based on proof-of-work.
diff --git a/core/blockchain.go b/core/blockchain.go
index 3ce435692..e90ffbb30 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -1718,7 +1718,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
 	if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() {
 		events = append(events, ChainHeadEvent{lastCanon})
 	}
-	bc.engine.(consensus.Bor).CancelActiveSealingOp()
 	return it.index, events, coalescedLogs, err
 }
 
-- 
GitLab