Skip to content

Commit

Permalink
Rollback changes in sealing
Browse files Browse the repository at this point in the history
  • Loading branch information
whitebit-robot committed Jun 26, 2023
2 parents 6ca9efb + 8d5327d commit 2da6687
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (i *bbInput) sealEthash(block *types.Block) (*types.Block, error) {
// If the testmode is used, the sealer will return quickly, and complain
// "Sealing result is not read by miner" if it cannot write the result.
results := make(chan *types.Block, 1)
if err := engine.Seal(nil, block, results, nil, nil, nil); err != nil {
if err := engine.Seal(nil, block, results, nil); err != nil {
panic(fmt.Sprintf("failed to seal block: %v", err))
}
found := <-results
Expand Down
8 changes: 3 additions & 5 deletions consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ package beacon
import (
"errors"
"fmt"
"math/big"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc"
Expand All @@ -30,6 +27,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
"math/big"
)

// Proof-of-stake protocol constants.
Expand Down Expand Up @@ -353,9 +351,9 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
//
// Note, the method returns immediately and will send the result async. More
// than one result may also be returned depending on the consensus algorithm.
func (beacon *Beacon) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}, sealMu *sync.Mutex, lastSealedHeightCh chan<- uint64) error {
func (beacon *Beacon) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
if !beacon.IsPoSHeader(block.Header()) {
return beacon.ethone.Seal(chain, block, results, stop, sealMu, lastSealedHeightCh)
return beacon.ethone.Seal(chain, block, results, stop)
}
// The seal verification is done by the external consensus engine,
// return directly without pushing any block back. In another word
Expand Down
13 changes: 1 addition & 12 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func (c *Clique) Authorize(signer common.Address, signFn SignerFn) {

// Seal implements consensus.Engine, attempting to create a sealed block using
// the local signing credentials.
func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}, sealMu *sync.Mutex, lastSealedHeightCh chan<- uint64) error {
func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
header := block.Header()

// Sealing the genesis block is not supported
Expand Down Expand Up @@ -650,19 +650,8 @@ func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, res
case <-time.After(delay):
}

sealMu.Lock()
defer sealMu.Unlock()

select {
// if `stop` channel has been closed in worker, don't push new block to `results` channel
case <-stop:
return
default:
}

select {
case results <- block.WithSeal(header):
lastSealedHeightCh <- header.Number.Uint64()
default:
log.Warn("Sealing result is not read by miner", "sealhash", SealHash(header))
}
Expand Down
3 changes: 1 addition & 2 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"math/big"
"sync"
)

// ChainHeaderReader defines a small collection of methods needed to access the local
Expand Down Expand Up @@ -105,7 +104,7 @@ type Engine interface {
//
// Note, the method returns immediately and will send the result async. More
// than one result may also be returned depending on the consensus algorithm.
Seal(chain ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}, sealMu *sync.Mutex, lastSealedHeightCh chan<- uint64) error
Seal(chain ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error

// SealHash returns the hash of a block prior to it being sealed.
SealHash(header *types.Header) common.Hash
Expand Down
6 changes: 3 additions & 3 deletions consensus/ethash/ethash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestTestMode(t *testing.T) {
defer ethash.Close()

results := make(chan *types.Block)
err := ethash.Seal(nil, types.NewBlockWithHeader(header), results, nil, nil, nil)
err := ethash.Seal(nil, types.NewBlockWithHeader(header), results, nil)
if err != nil {
t.Fatalf("failed to seal block: %v", err)
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestRemoteSealer(t *testing.T) {

// Push new work.
results := make(chan *types.Block)
ethash.Seal(nil, block, results, nil, nil, nil)
ethash.Seal(nil, block, results, nil)

var (
work [4]string
Expand All @@ -129,7 +129,7 @@ func TestRemoteSealer(t *testing.T) {
header = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)}
block = types.NewBlockWithHeader(header)
sealhash = ethash.SealHash(header)
ethash.Seal(nil, block, results, nil, nil, nil)
ethash.Seal(nil, block, results, nil)

if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
t.Error("expect to return the latest pushed work")
Expand Down
6 changes: 3 additions & 3 deletions consensus/ethash/sealer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (

// Seal implements consensus.Engine, attempting to find a nonce that satisfies
// the block's difficulty requirements.
func (ethash *Ethash) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}, sealMu *sync.Mutex, lastSealedHeightCh chan<- uint64) error {
func (ethash *Ethash) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
// If we're running a fake PoW, simply return a 0 nonce immediately
if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
header := block.Header()
Expand All @@ -62,7 +62,7 @@ func (ethash *Ethash) Seal(chain consensus.ChainHeaderReader, block *types.Block
}
// If we're running a shared PoW, delegate sealing to it
if ethash.shared != nil {
return ethash.shared.Seal(chain, block, results, stop, sealMu, lastSealedHeightCh)
return ethash.shared.Seal(chain, block, results, stop)
}
// Create a runner and the multiple search threads it directs
abort := make(chan struct{})
Expand Down Expand Up @@ -117,7 +117,7 @@ func (ethash *Ethash) Seal(chain consensus.ChainHeaderReader, block *types.Block
case <-ethash.update:
// Thread count was changed on user request, restart
close(abort)
if err := ethash.Seal(chain, block, results, stop, sealMu, lastSealedHeightCh); err != nil {
if err := ethash.Seal(chain, block, results, stop); err != nil {
ethash.config.Log.Error("Failed to restart sealing after update", "err", err)
}
}
Expand Down
10 changes: 5 additions & 5 deletions consensus/ethash/sealer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestRemoteNotify(t *testing.T) {
header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
block := types.NewBlockWithHeader(header)

ethash.Seal(nil, block, nil, nil, nil, nil)
ethash.Seal(nil, block, nil, nil)
select {
case work := <-sink:
if want := ethash.SealHash(header).Hex(); work[0] != want {
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestRemoteNotifyFull(t *testing.T) {
header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
block := types.NewBlockWithHeader(header)

ethash.Seal(nil, block, nil, nil, nil, nil)
ethash.Seal(nil, block, nil, nil)
select {
case work := <-sink:
if want := "0x" + strconv.FormatUint(header.Number.Uint64(), 16); work["number"] != want {
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestRemoteMultiNotify(t *testing.T) {
for i := 0; i < cap(sink); i++ {
header := &types.Header{Number: big.NewInt(int64(i)), Difficulty: big.NewInt(100)}
block := types.NewBlockWithHeader(header)
ethash.Seal(nil, block, results, nil, nil, nil)
ethash.Seal(nil, block, results, nil)
}

for i := 0; i < cap(sink); i++ {
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestRemoteMultiNotifyFull(t *testing.T) {
for i := 0; i < cap(sink); i++ {
header := &types.Header{Number: big.NewInt(int64(i)), Difficulty: big.NewInt(100)}
block := types.NewBlockWithHeader(header)
ethash.Seal(nil, block, results, nil, nil, nil)
ethash.Seal(nil, block, results, nil)
}

for i := 0; i < cap(sink); i++ {
Expand Down Expand Up @@ -266,7 +266,7 @@ func TestStaleSubmission(t *testing.T) {

for id, c := range testcases {
for _, h := range c.headers {
ethash.Seal(nil, types.NewBlockWithHeader(h), results, nil, nil, nil)
ethash.Seal(nil, types.NewBlockWithHeader(h), results, nil)
}
if res := api.SubmitWork(fakeNonce, ethash.SealHash(c.headers[c.submitIndex]), fakeDigest); res != c.submitRes {
t.Errorf("case %d submit result mismatch, want %t, get %t", id+1, c.submitRes, res)
Expand Down
22 changes: 2 additions & 20 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,6 @@ func (w *worker) taskLoop() {
prev common.Hash
)

sealMu := &sync.Mutex{}
lastSealedHeightCh := make(chan uint64, 1)

// interrupt aborts the in-flight sealing task.
interrupt := func() {
if stopCh != nil {
Expand All @@ -660,23 +657,8 @@ func (w *worker) taskLoop() {
continue
}

var lastSealedHeight uint64

sealMu.Lock()

// Interrupt previous sealing operation
interrupt()
select {
case lastSealedHeight = <-lastSealedHeightCh:
default:
}

sealMu.Unlock()

if lastSealedHeight == task.block.NumberU64() {
log.Info("Reject sealing block with already sealed height", "number", task.block.NumberU64(), "sealhash", sealHash)
continue
}

stopCh, prev = make(chan struct{}), sealHash

if w.skipSealHook != nil && w.skipSealHook(task) {
Expand All @@ -686,7 +668,7 @@ func (w *worker) taskLoop() {
w.pendingTasks[sealHash] = task
w.pendingMu.Unlock()

if err := w.engine.Seal(w.chain, task.block, w.resultCh, stopCh, sealMu, lastSealedHeightCh); err != nil {
if err := w.engine.Seal(w.chain, task.block, w.resultCh, stopCh); err != nil {
log.Warn("Block sealing failed", "err", err)
w.pendingMu.Lock()
delete(w.pendingTasks, sealHash)
Expand Down

0 comments on commit 2da6687

Please sign in to comment.