From 24d8d9b964c2dd2c7b7543e1d7457d5e61cddb4c Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Thu, 24 Oct 2024 16:27:19 +0200 Subject: [PATCH] Merge MiningResultPOSCh with MiningResultCh (#12463) Cherry pick #11612 into `release/2.61` --- cmd/integration/commands/state_stages.go | 2 +- cmd/rpcdaemon/cli/config.go | 2 +- consensus/aura/aura.go | 4 +-- consensus/clique/clique.go | 9 +++---- consensus/clique/verifier.go | 2 +- consensus/consensus.go | 2 +- consensus/ethash/ethash_test.go | 10 +++++--- consensus/ethash/fake.go | 6 +++-- consensus/ethash/sealer.go | 28 +++++++++++---------- consensus/ethash/sealer_test.go | 24 +++++++++++------- consensus/merge/merge.go | 16 ++++++++++-- eth/backend.go | 8 +++--- eth/stagedsync/stage_mining_create_block.go | 21 ++++------------ eth/stagedsync/stage_mining_finish.go | 8 ++---- eth/stagedsync/stagedsynctest/harness.go | 8 +++--- polygon/bor/bor.go | 10 ++++---- polygon/bor/bor_test.go | 20 ++++++++------- turbo/stages/mock/mock_sentry.go | 6 ++--- turbo/stages/mock/sentry_mock_test.go | 2 +- 19 files changed, 99 insertions(+), 89 deletions(-) diff --git a/cmd/integration/commands/state_stages.go b/cmd/integration/commands/state_stages.go index 5e342009b0b..9792bf0aef8 100644 --- a/cmd/integration/commands/state_stages.go +++ b/cmd/integration/commands/state_stages.go @@ -383,7 +383,7 @@ func syncBySmallSteps(db kv.RwDB, miningConfig params.MiningConfig, ctx context. } defer tx.Rollback() minedBlock := <-miner.MiningResultCh - checkMinedBlock(nextBlock, minedBlock, chainConfig) + checkMinedBlock(nextBlock, minedBlock.Block, chainConfig) } // Unwind all stages to `execStage - unwind` block diff --git a/cmd/rpcdaemon/cli/config.go b/cmd/rpcdaemon/cli/config.go index 398772ecfac..626ac992074 100644 --- a/cmd/rpcdaemon/cli/config.go +++ b/cmd/rpcdaemon/cli/config.go @@ -1044,7 +1044,7 @@ func (e *remoteConsensusEngine) FinalizeAndAssemble(_ *chain.Config, _ *types.He panic("remoteConsensusEngine.FinalizeAndAssemble not supported") } -func (e *remoteConsensusEngine) Seal(_ consensus.ChainHeaderReader, _ *types.Block, _ chan<- *types.Block, _ <-chan struct{}) error { +func (e *remoteConsensusEngine) Seal(_ consensus.ChainHeaderReader, _ *types.BlockWithReceipts, _ chan<- *types.BlockWithReceipts, _ <-chan struct{}) error { panic("remoteConsensusEngine.Seal not supported") } diff --git a/consensus/aura/aura.go b/consensus/aura/aura.go index 2da332fc9c3..df889aaf4a0 100644 --- a/consensus/aura/aura.go +++ b/consensus/aura/aura.go @@ -874,8 +874,8 @@ func (c *AuRa) GenesisEpochData(header *types.Header, caller consensus.SystemCal return res, nil } -func (c *AuRa) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { - return nil +func (c *AuRa) Seal(chain consensus.ChainHeaderReader, block *types.BlockWithReceipts, results chan<- *types.BlockWithReceipts, stop <-chan struct{}) error { + panic("AuRa block production is not implemented") //header := block.Header() // /// Sealing the genesis block is not supported diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 060d267c932..2a08d4f4dee 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -67,8 +67,6 @@ var ( NonceAuthVote = hexutil.MustDecode("0xffffffffffffffff") // Magic nonce number to vote on adding a new signer nonceDropVote = hexutil.MustDecode("0x0000000000000000") // Magic nonce number to vote on removing a signer. - emptyUncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW. - DiffInTurn = big.NewInt(2) // Block difficulty for in-turn signatures diffNoTurn = big.NewInt(1) // Block difficulty for out-of-turn signatures ) @@ -405,8 +403,9 @@ func (c *Clique) Authorize(signer libcommon.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{}) error { - +func (c *Clique) Seal(chain consensus.ChainHeaderReader, blockWithReceipts *types.BlockWithReceipts, results chan<- *types.BlockWithReceipts, stop <-chan struct{}) error { + block := blockWithReceipts.Block + receipts := blockWithReceipts.Receipts header := block.Header() // Sealing the genesis block is not supported @@ -468,7 +467,7 @@ func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, res } select { - case results <- block.WithSeal(header): + case results <- &types.BlockWithReceipts{Block: block.WithSeal(header), Receipts: receipts}: default: c.logger.Warn("Sealing result is not read by miner", "sealhash", SealHash(header)) } diff --git a/consensus/clique/verifier.go b/consensus/clique/verifier.go index ceeb52f98a3..e7443744104 100644 --- a/consensus/clique/verifier.go +++ b/consensus/clique/verifier.go @@ -67,7 +67,7 @@ func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.H return errInvalidMixDigest } // Ensure that the block doesn't contain any uncles which are meaningless in PoA - if header.UncleHash != emptyUncleHash { + if header.UncleHash != types.EmptyUncleHash { return errInvalidUncleHash } // Ensure that the block's difficulty is meaningful (may not be correct at this point) diff --git a/consensus/consensus.go b/consensus/consensus.go index e3600e39ed3..4810bee7953 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -174,7 +174,7 @@ type EngineWriter 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{}) error + Seal(chain ChainHeaderReader, block *types.BlockWithReceipts, results chan<- *types.BlockWithReceipts, stop <-chan struct{}) error // SealHash returns the hash of a block prior to it being sealed. SealHash(header *types.Header) libcommon.Hash diff --git a/consensus/ethash/ethash_test.go b/consensus/ethash/ethash_test.go index c0566237c68..db987c51276 100644 --- a/consensus/ethash/ethash_test.go +++ b/consensus/ethash/ethash_test.go @@ -17,12 +17,12 @@ package ethash import ( - "github.com/ledgerwatch/erigon-lib/common/hexutil" "math/big" "testing" "time" libcommon "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon-lib/common/hexutil" "github.com/ledgerwatch/erigon/core/types" ) @@ -37,11 +37,12 @@ func TestRemoteSealer(t *testing.T) { } header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)} block := types.NewBlockWithHeader(header) + blockWithReceipts := &types.BlockWithReceipts{Block: block} sealhash := ethash.SealHash(header) // Push new work. - results := make(chan *types.Block) - if err := ethash.Seal(nil, block, results, nil); err != nil { + results := make(chan *types.BlockWithReceipts) + if err := ethash.Seal(nil, blockWithReceipts, results, nil); err != nil { t.Fatal(err) } var ( @@ -58,8 +59,9 @@ func TestRemoteSealer(t *testing.T) { // Push new block with same block number to replace the original one. header = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)} block = types.NewBlockWithHeader(header) + blockWithReceipts = &types.BlockWithReceipts{Block: block} sealhash = ethash.SealHash(header) - err = ethash.Seal(nil, block, results, nil) + err = ethash.Seal(nil, blockWithReceipts, results, nil) if err != nil { t.Fatal(err) } diff --git a/consensus/ethash/fake.go b/consensus/ethash/fake.go index 70899866d27..52737aebc05 100644 --- a/consensus/ethash/fake.go +++ b/consensus/ethash/fake.go @@ -113,12 +113,14 @@ func (f *FakeEthash) VerifySeal(_ consensus.ChainHeaderReader, header *types.Hea } // If we're running a fake PoW, simply return a 0 nonce immediately -func (f *FakeEthash) Seal(_ consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { +func (f *FakeEthash) Seal(_ consensus.ChainHeaderReader, blockWithReceipts *types.BlockWithReceipts, results chan<- *types.BlockWithReceipts, stop <-chan struct{}) error { + block := blockWithReceipts.Block + receipts := blockWithReceipts.Receipts header := block.Header() header.Nonce, header.MixDigest = types.BlockNonce{}, libcommon.Hash{} select { - case results <- block.WithSeal(header): + case results <- &types.BlockWithReceipts{Block: block.WithSeal(header), Receipts: receipts}: default: f.Ethash.config.Log.Warn("Sealing result is not read by miner", "mode", "fake", "sealhash", f.SealHash(block.Header())) } diff --git a/consensus/ethash/sealer.go b/consensus/ethash/sealer.go index d020147ff5a..048b79f4760 100644 --- a/consensus/ethash/sealer.go +++ b/consensus/ethash/sealer.go @@ -21,7 +21,6 @@ import ( "context" crand "crypto/rand" "errors" - "github.com/ledgerwatch/erigon-lib/common/hexutil" "math" "math/big" "math/rand" @@ -30,7 +29,9 @@ import ( "time" "github.com/goccy/go-json" + libcommon "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon-lib/common/hexutil" "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/consensus" @@ -49,7 +50,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{}) error { +func (ethash *Ethash) Seal(chain consensus.ChainHeaderReader, block *types.BlockWithReceipts, results chan<- *types.BlockWithReceipts, stop <-chan struct{}) error { // If we're running a shared PoW, delegate sealing to it if ethash.shared != nil { return ethash.shared.Seal(chain, block, results, stop) @@ -73,7 +74,7 @@ func (ethash *Ethash) Seal(chain consensus.ChainHeaderReader, block *types.Block const remoteSealerTimeout = 1 * time.Second type remoteSealer struct { - works map[libcommon.Hash]*types.Block + works map[libcommon.Hash]*types.BlockWithReceipts rates map[libcommon.Hash]hashrate currentBlock *types.Block currentWork [4]string @@ -84,7 +85,7 @@ type remoteSealer struct { ethash *Ethash noverify bool notifyURLs []string - results chan<- *types.Block + results chan<- *types.BlockWithReceipts workCh chan *sealTask // Notification channel to push new work and relative result channel to remote sealer fetchWorkCh chan *sealWork // Channel used for remote sealer to fetch mining work submitWorkCh chan *mineResult // Channel used for remote sealer to submit their mining result @@ -96,8 +97,8 @@ type remoteSealer struct { // sealTask wraps a seal block with relative result channel for remote sealer thread. type sealTask struct { - block *types.Block - results chan<- *types.Block + block *types.BlockWithReceipts + results chan<- *types.BlockWithReceipts } // mineResult wraps the pow solution parameters for the specified block. @@ -132,7 +133,7 @@ func startRemoteSealer(ethash *Ethash, urls []string, noverify bool) *remoteSeal notifyURLs: urls, notifyCtx: ctx, cancelNotify: cancel, - works: make(map[libcommon.Hash]*types.Block), + works: make(map[libcommon.Hash]*types.BlockWithReceipts), rates: make(map[libcommon.Hash]hashrate), workCh: make(chan *sealTask), fetchWorkCh: make(chan *sealWork), @@ -206,7 +207,7 @@ func (s *remoteSealer) loop() { // Clear stale pending blocks if s.currentBlock != nil { for hash, block := range s.works { - if block.NumberU64()+staleThreshold <= s.currentBlock.NumberU64() { + if block.Block.NumberU64()+staleThreshold <= s.currentBlock.NumberU64() { delete(s.works, hash) } } @@ -226,7 +227,8 @@ func (s *remoteSealer) loop() { // result[1], 32 bytes hex encoded seed hash used for DAG // result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty // result[3], hex encoded block number -func (s *remoteSealer) makeWork(block *types.Block) { +func (s *remoteSealer) makeWork(blockWithReceipts *types.BlockWithReceipts) { + block := blockWithReceipts.Block hash := s.ethash.SealHash(block.Header()) s.currentWork[0] = hash.Hex() s.currentWork[1] = libcommon.BytesToHash(SeedHash(block.NumberU64())).Hex() @@ -235,7 +237,7 @@ func (s *remoteSealer) makeWork(block *types.Block) { // Trace the seal work fetched by remote sealer. s.currentBlock = block - s.works[hash] = block + s.works[hash] = blockWithReceipts } // notifyWork notifies all the specified mining endpoints of the availability of @@ -295,7 +297,7 @@ func (s *remoteSealer) submitWork(nonce types.BlockNonce, mixDigest libcommon.Ha return false } // Verify the correctness of submitted result. - header := block.Header() + header := block.Block.Header() header.Nonce = nonce header.MixDigest = mixDigest @@ -314,12 +316,12 @@ func (s *remoteSealer) submitWork(nonce types.BlockNonce, mixDigest libcommon.Ha s.ethash.config.Log.Trace("Verified correct proof-of-work", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start))) // Solutions seems to be valid, return to the miner and notify acceptance. - solution := block.WithSeal(header) + solution := block.Block.WithSeal(header) // The submitted solution is within the scope of acceptance. if solution.NumberU64()+staleThreshold > s.currentBlock.NumberU64() { select { - case s.results <- solution: + case s.results <- &types.BlockWithReceipts{Block: solution, Receipts: block.Receipts}: s.ethash.config.Log.Trace("Work submitted is acceptable", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash()) return true default: diff --git a/consensus/ethash/sealer_test.go b/consensus/ethash/sealer_test.go index 72bcf16fda2..4dd32601523 100644 --- a/consensus/ethash/sealer_test.go +++ b/consensus/ethash/sealer_test.go @@ -60,8 +60,9 @@ func TestRemoteNotify(t *testing.T) { // Stream a work task and ensure the notification bubbles out. header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)} block := types.NewBlockWithHeader(header) + blockWithReceipts := &types.BlockWithReceipts{Block: block} - if err := ethash.Seal(nil, block, nil, nil); err != nil { + if err := ethash.Seal(nil, blockWithReceipts, nil, nil); err != nil { t.Fatal(err) } select { @@ -110,8 +111,9 @@ func TestRemoteNotifyFull(t *testing.T) { // Stream a work task and ensure the notification bubbles out. header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)} block := types.NewBlockWithHeader(header) + blockWithReceipts := &types.BlockWithReceipts{Block: block} - if err := ethash.Seal(nil, block, nil, nil); err != nil { + if err := ethash.Seal(nil, blockWithReceipts, nil, nil); err != nil { t.Fatal(err) } select { @@ -155,13 +157,14 @@ func TestRemoteMultiNotify(t *testing.T) { // Provide a results reader. // Otherwise the unread results will be logged asynchronously // and this can happen after the test is finished, causing a panic. - results := make(chan *types.Block, cap(sink)) + results := make(chan *types.BlockWithReceipts, cap(sink)) // Stream a lot of work task and ensure all the notifications bubble out. for i := 0; i < cap(sink); i++ { header := &types.Header{Number: big.NewInt(int64(i)), Difficulty: big.NewInt(100)} block := types.NewBlockWithHeader(header) - err := ethash.Seal(nil, block, results, nil) + blockWithReceipts := &types.BlockWithReceipts{Block: block} + err := ethash.Seal(nil, blockWithReceipts, results, nil) if err != nil { t.Fatal(err) } @@ -208,13 +211,14 @@ func TestRemoteMultiNotifyFull(t *testing.T) { // Provide a results reader. // Otherwise the unread results will be logged asynchronously // and this can happen after the test is finished, causing a panic. - results := make(chan *types.Block, cap(sink)) + results := make(chan *types.BlockWithReceipts, cap(sink)) // Stream a lot of work task and ensure all the notifications bubble out. for i := 0; i < cap(sink); i++ { header := &types.Header{Number: big.NewInt(int64(i)), Difficulty: big.NewInt(100)} block := types.NewBlockWithHeader(header) - err := ethash.Seal(nil, block, results, nil) + blockWithReceipts := &types.BlockWithReceipts{Block: block} + err := ethash.Seal(nil, blockWithReceipts, results, nil) if err != nil { t.Fatal(err) } @@ -279,11 +283,12 @@ func TestStaleSubmission(t *testing.T) { false, }, } - results := make(chan *types.Block, 16) + results := make(chan *types.BlockWithReceipts, 16) for id, c := range testcases { for _, h := range c.headers { - err := ethash.Seal(nil, types.NewBlockWithHeader(h), results, nil) + blockWithReceipts := &types.BlockWithReceipts{Block: types.NewBlockWithHeader(h)} + err := ethash.Seal(nil, blockWithReceipts, results, nil) if err != nil { t.Fatal(err) } @@ -295,7 +300,8 @@ func TestStaleSubmission(t *testing.T) { continue } select { - case res := <-results: + case resWithReceipts := <-results: + res := resWithReceipts.Block if res.Nonce() != fakeNonce { t.Errorf("case %d block nonce mismatch, want %x, get %x", id+1, fakeNonce, res.Nonce()) } diff --git a/consensus/merge/merge.go b/consensus/merge/merge.go index c09624755d1..36c86a79b7e 100644 --- a/consensus/merge/merge.go +++ b/consensus/merge/merge.go @@ -306,10 +306,22 @@ func (s *Merge) verifyHeader(chain consensus.ChainHeaderReader, header, parent * return nil } -func (s *Merge) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { +func (s *Merge) Seal(chain consensus.ChainHeaderReader, blockWithReceipts *types.BlockWithReceipts, results chan<- *types.BlockWithReceipts, stop <-chan struct{}) error { + block := blockWithReceipts.Block + receipts := blockWithReceipts.Receipts if !misc.IsPoSHeader(block.HeaderNoCopy()) { - return s.eth1Engine.Seal(chain, block, results, stop) + return s.eth1Engine.Seal(chain, blockWithReceipts, results, stop) } + + header := block.Header() + header.Nonce = ProofOfStakeNonce + + select { + case results <- &types.BlockWithReceipts{Block: block.WithSeal(header), Receipts: receipts}: + default: + log.Warn("Sealing result is not read", "sealhash", block.Hash()) + } + return nil } diff --git a/eth/backend.go b/eth/backend.go index 355b4346297..6034fd32eb2 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -672,7 +672,7 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger // proof-of-stake mining assembleBlockPOS := func(param *core.BlockBuilderParameters, interrupt *int32) (*types.BlockWithReceipts, error) { - miningStatePos := stagedsync.NewProposingState(&config.Miner) + miningStatePos := stagedsync.NewMiningState(&config.Miner) miningStatePos.MiningConfig.Etherbase = param.SuggestedFeeRecipient proposingSync := stagedsync.New( config.Sync, @@ -689,7 +689,7 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger if err := stages2.MiningStep(ctx, backend.chainDB, proposingSync, tmpdir, logger); err != nil { return nil, err } - block := <-miningStatePos.MiningResultPOSCh + block := <-miningStatePos.MiningResultCh return block, nil } @@ -1248,8 +1248,8 @@ func (s *Ethereum) StartMining(ctx context.Context, db kv.RwDB, stateDiffClient select { case block := <-miner.MiningResultCh: if block != nil { - s.logger.Debug("Mined block", "block", block.Number()) - s.minedBlocks <- block + s.logger.Debug("Mined block", "block", block.Block.Number()) + s.minedBlocks <- block.Block } return case <-workCtx.Done(): diff --git a/eth/stagedsync/stage_mining_create_block.go b/eth/stagedsync/stage_mining_create_block.go index 4a2b88ce90d..6624c0257a7 100644 --- a/eth/stagedsync/stage_mining_create_block.go +++ b/eth/stagedsync/stage_mining_create_block.go @@ -36,32 +36,21 @@ type MiningBlock struct { } type MiningState struct { - MiningConfig *params.MiningConfig - PendingResultCh chan *types.Block - MiningResultCh chan *types.Block - MiningResultPOSCh chan *types.BlockWithReceipts - MiningBlock *MiningBlock + MiningConfig *params.MiningConfig + PendingResultCh chan *types.Block + MiningResultCh chan *types.BlockWithReceipts + MiningBlock *MiningBlock } func NewMiningState(cfg *params.MiningConfig) MiningState { return MiningState{ MiningConfig: cfg, PendingResultCh: make(chan *types.Block, 1), - MiningResultCh: make(chan *types.Block, 1), + MiningResultCh: make(chan *types.BlockWithReceipts, 1), MiningBlock: &MiningBlock{}, } } -func NewProposingState(cfg *params.MiningConfig) MiningState { - return MiningState{ - MiningConfig: cfg, - PendingResultCh: make(chan *types.Block, 1), - MiningResultCh: make(chan *types.Block, 1), - MiningResultPOSCh: make(chan *types.BlockWithReceipts, 1), - MiningBlock: &MiningBlock{}, - } -} - type MiningCreateBlockCfg struct { db kv.RwDB miner MiningState diff --git a/eth/stagedsync/stage_mining_finish.go b/eth/stagedsync/stage_mining_finish.go index f57576d40ed..fad148fa414 100644 --- a/eth/stagedsync/stage_mining_finish.go +++ b/eth/stagedsync/stage_mining_finish.go @@ -64,16 +64,12 @@ func SpawnMiningFinishStage(s *StageState, tx kv.RwTx, cfg MiningFinishCfg, quit //} //prev = sealHash cfg.latestBlockBuiltStore.AddBlockBuilt(block) - if cfg.miningState.MiningResultPOSCh != nil { - cfg.miningState.MiningResultPOSCh <- blockWithReceipts - return nil - } // Tests may set pre-calculated nonce if block.NonceU64() != 0 { // Note: To propose a new signer for Clique consensus, the block nonce should be set to 0xFFFFFFFFFFFFFFFF. if cfg.engine.Type() != chain.CliqueConsensus { - cfg.miningState.MiningResultCh <- block + cfg.miningState.MiningResultCh <- blockWithReceipts return nil } } @@ -96,7 +92,7 @@ func SpawnMiningFinishStage(s *StageState, tx kv.RwTx, cfg MiningFinishCfg, quit logger.Trace("No in-flight sealing task.") } chain := ChainReader{Cfg: cfg.chainConfig, Db: tx, BlockReader: cfg.blockReader, Logger: logger} - if err := cfg.engine.Seal(chain, block, cfg.miningState.MiningResultCh, cfg.sealCancel); err != nil { + if err := cfg.engine.Seal(chain, blockWithReceipts, cfg.miningState.MiningResultCh, cfg.sealCancel); err != nil { logger.Warn("Block sealing failed", "err", err) } diff --git a/eth/stagedsync/stagedsynctest/harness.go b/eth/stagedsync/stagedsynctest/harness.go index d283fafc896..06a8227f2bb 100644 --- a/eth/stagedsync/stagedsynctest/harness.go +++ b/eth/stagedsync/stagedsynctest/harness.go @@ -47,7 +47,7 @@ func InitHarness(ctx context.Context, t *testing.T, cfg HarnessCfg) Harness { borConsensusDB := memdb.NewTestDB(t) ctrl := gomock.NewController(t) heimdallClient := heimdall.NewMockHeimdallClient(ctrl) - miningState := stagedsync.NewProposingState(ðconfig.Defaults.Miner) + miningState := stagedsync.NewMiningState(ðconfig.Defaults.Miner) bhCfg := stagedsync.StageBorHeimdallCfg( chainDataDB, borConsensusDB, @@ -481,13 +481,13 @@ func (h *Harness) generateChain(ctx context.Context, t *testing.T, ctrl *gomock. func (h *Harness) seal(t *testing.T, chr consensus.ChainHeaderReader, eng consensus.Engine, block *types.Block) { h.logger.Info("Sealing mock block", "blockNum", block.Number()) - sealRes, sealStop := make(chan *types.Block, 1), make(chan struct{}, 1) - if err := eng.Seal(chr, block, sealRes, sealStop); err != nil { + sealRes, sealStop := make(chan *types.BlockWithReceipts, 1), make(chan struct{}, 1) + if err := eng.Seal(chr, &types.BlockWithReceipts{Block: block}, sealRes, sealStop); err != nil { t.Fatal(err) } sealedParentBlock := <-sealRes - h.sealedHeaders[sealedParentBlock.Number().Uint64()] = sealedParentBlock.Header() + h.sealedHeaders[sealedParentBlock.Block.Number().Uint64()] = sealedParentBlock.Block.Header() } func (h *Harness) consensusEngine(t *testing.T, cfg HarnessCfg) consensus.Engine { diff --git a/polygon/bor/bor.go b/polygon/bor/bor.go index d0e653d31ef..a2a8c3a3bcf 100644 --- a/polygon/bor/bor.go +++ b/polygon/bor/bor.go @@ -68,8 +68,6 @@ var ( "0": 64, } // Default number of blocks after which to checkpoint and reset the pending votes - emptyUncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW. - // diffInTurn = big.NewInt(2) // Block difficulty for in-turn signatures // diffNoTurn = big.NewInt(1) // Block difficulty for out-of-turn signatures @@ -545,7 +543,7 @@ func ValidateHeaderUnusedFields(header *types.Header) error { } // Ensure that the block doesn't contain any uncles which are meaningless in PoA - if header.UncleHash != emptyUncleHash { + if header.UncleHash != types.EmptyUncleHash { return errInvalidUncleHash } @@ -1114,7 +1112,9 @@ func (c *Bor) Authorize(currentSigner libcommon.Address, signFn SignerFn) { // Seal implements consensus.Engine, attempting to create a sealed block using // the local signing credentials. -func (c *Bor) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { +func (c *Bor) Seal(chain consensus.ChainHeaderReader, blockWithReceipts *types.BlockWithReceipts, results chan<- *types.BlockWithReceipts, stop <-chan struct{}) error { + block := blockWithReceipts.Block + receipts := blockWithReceipts.Receipts header := block.HeaderNoCopy() // Sealing the genesis block is not supported number := header.Number.Uint64() @@ -1192,7 +1192,7 @@ func (c *Bor) Seal(chain consensus.ChainHeaderReader, block *types.Block, result } } select { - case results <- block.WithSeal(header): + case results <- &types.BlockWithReceipts{Block: block.WithSeal(header), Receipts: receipts}: default: c.logger.Warn("Sealing result was not read by miner", "number", number, "sealhash", SealHash(header, c.config)) } diff --git a/polygon/bor/bor_test.go b/polygon/bor/bor_test.go index 5f694160845..a3d94f55dc7 100644 --- a/polygon/bor/bor_test.go +++ b/polygon/bor/bor_test.go @@ -239,12 +239,12 @@ func (v validator) IsProposer(block *types.Block) (bool, error) { return v.Engine.(*bor.Bor).IsProposer(block.Header()) } -func (v validator) sealBlocks(blocks []*types.Block) ([]*types.Block, error) { +func (v validator) sealBlocks(blocks []*types.Block, receipts []types.Receipts) ([]*types.Block, error) { sealedBlocks := make([]*types.Block, 0, len(blocks)) hr := headerReader{v} - for _, block := range blocks { + for i, block := range blocks { header := block.HeaderNoCopy() if err := v.Engine.Prepare(hr, header, nil); err != nil { @@ -255,15 +255,16 @@ func (v validator) sealBlocks(blocks []*types.Block) ([]*types.Block, error) { header.ParentHash = parent.Hash() } - sealResults := make(chan *types.Block, 1) + sealResults := make(chan *types.BlockWithReceipts, 1) - if err := v.Engine.Seal(hr, block, sealResults, nil); err != nil { + blockWithReceipts := &types.BlockWithReceipts{Block: block, Receipts: receipts[i]} + if err := v.Engine.Seal(hr, blockWithReceipts, sealResults, nil); err != nil { return nil, err } sealedBlock := <-sealResults - v.blocks[sealedBlock.NumberU64()] = sealedBlock - sealedBlocks = append(sealedBlocks, sealedBlock) + v.blocks[sealedBlock.Block.NumberU64()] = sealedBlock.Block + sealedBlocks = append(sealedBlocks, sealedBlock.Block) } return sealedBlocks, nil @@ -349,7 +350,7 @@ func TestVerifyHeader(t *testing.T) { t.Fatalf("generate blocks failed: %v", err) } - sealedBlocks, err := v.sealBlocks(chain.Blocks) + sealedBlocks, err := v.sealBlocks(chain.Blocks, chain.Receipts) if err != nil { t.Fatalf("seal block failed: %v", err) @@ -403,6 +404,7 @@ func testVerify(t *testing.T, noValidators int, chainLength int) { for bi := 0; bi < chainLength; bi++ { for vi, v := range validators { block := chains[vi].Blocks[bi] + receipts := chains[vi].Receipts[bi] isProposer, err := v.IsProposer(block) @@ -420,7 +422,7 @@ func testVerify(t *testing.T, noValidators int, chainLength int) { lastProposerIndex = vi } - sealedBlocks, err := v.sealBlocks([]*types.Block{block}) + sealedBlocks, err := v.sealBlocks([]*types.Block{block}, []types.Receipts{receipts}) if err != nil { t.Fatalf("seal block failed: %v", err) @@ -449,7 +451,7 @@ func TestSendBlock(t *testing.T) { t.Fatalf("generate blocks failed: %v", err) } - sealedBlocks, err := s.sealBlocks(chain.Blocks) + sealedBlocks, err := s.sealBlocks(chain.Blocks, chain.Receipts) if err != nil { t.Fatalf("seal block failed: %v", err) diff --git a/turbo/stages/mock/mock_sentry.go b/turbo/stages/mock/mock_sentry.go index b357bd85503..1b4ffd58a24 100644 --- a/turbo/stages/mock/mock_sentry.go +++ b/turbo/stages/mock/mock_sentry.go @@ -87,7 +87,7 @@ type MockSentry struct { Sync *stagedsync.Sync MiningSync *stagedsync.Sync PendingBlocks chan *types.Block - MinedBlocks chan *types.Block + MinedBlocks chan *types.BlockWithReceipts sentriesClient *sentry_multi_client.MultiClient Key *ecdsa.PrivateKey Genesis *types.Block @@ -414,7 +414,7 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK } // proof-of-stake mining assembleBlockPOS := func(param *core.BlockBuilderParameters, interrupt *int32) (*types.BlockWithReceipts, error) { - miningStatePos := stagedsync.NewProposingState(&cfg.Miner) + miningStatePos := stagedsync.NewMiningState(&cfg.Miner) miningStatePos.MiningConfig.Etherbase = param.SuggestedFeeRecipient proposingSync := stagedsync.New( cfg.Sync, @@ -431,7 +431,7 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK if err := stages2.MiningStep(ctx, mock.DB, proposingSync, tmpdir, logger); err != nil { return nil, err } - block := <-miningStatePos.MiningResultPOSCh + block := <-miningStatePos.MiningResultCh return block, nil } diff --git a/turbo/stages/mock/sentry_mock_test.go b/turbo/stages/mock/sentry_mock_test.go index 2e7e1539cbe..31385243e6a 100644 --- a/turbo/stages/mock/sentry_mock_test.go +++ b/turbo/stages/mock/sentry_mock_test.go @@ -127,7 +127,7 @@ func TestMineBlockWith1Tx(t *testing.T) { got := <-m.PendingBlocks require.Equal(chain.TopBlock.Transactions().Len(), got.Transactions().Len()) got2 := <-m.MinedBlocks - require.Equal(chain.TopBlock.Transactions().Len(), got2.Transactions().Len()) + require.Equal(chain.TopBlock.Transactions().Len(), got2.Block.Transactions().Len()) } func TestReorg(t *testing.T) {