Skip to content

Commit

Permalink
Merge MiningResultPOSCh with MiningResultCh (#12463)
Browse files Browse the repository at this point in the history
Cherry pick #11612 into `release/2.61`
  • Loading branch information
yperbasis authored Oct 24, 2024
1 parent 1502825 commit 24d8d9b
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 89 deletions.
2 changes: 1 addition & 1 deletion cmd/integration/commands/state_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/aura/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/clique/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions consensus/ethash/ethash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 (
Expand All @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions consensus/ethash/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down
28 changes: 15 additions & 13 deletions consensus/ethash/sealer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
crand "crypto/rand"
"errors"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"math"
"math/big"
"math/rand"
Expand All @@ -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"
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down
24 changes: 15 additions & 9 deletions consensus/ethash/sealer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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())
}
Expand Down
16 changes: 14 additions & 2 deletions consensus/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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():
Expand Down
Loading

0 comments on commit 24d8d9b

Please sign in to comment.