Skip to content

Commit

Permalink
eth, miner: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Nov 29, 2021
1 parent 039f699 commit 20936a1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 38 deletions.
20 changes: 2 additions & 18 deletions eth/catalyst/api.go
Expand Up @@ -18,15 +18,13 @@
package catalyst

import (
"bytes"
"errors"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/les"
Expand Down Expand Up @@ -76,36 +74,23 @@ type ConsensusAPI struct {
light bool
eth *eth.Ethereum
les *les.LightEthereum
engine consensus.Engine // engine is the post-merge consensus engine, only for block creation
preparedBlocks map[int]*ExecutableData
}

func NewConsensusAPI(eth *eth.Ethereum, les *les.LightEthereum) *ConsensusAPI {
var engine consensus.Engine
if eth == nil {
if les.BlockChain().Config().TerminalTotalDifficulty == nil {
panic("Catalyst started without valid total difficulty")
}
if b, ok := les.Engine().(*beacon.Beacon); ok {
engine = beacon.New(b.InnerEngine())
} else {
engine = beacon.New(les.Engine())
}
} else {
if eth.BlockChain().Config().TerminalTotalDifficulty == nil {
panic("Catalyst started without valid total difficulty")
}
if b, ok := eth.Engine().(*beacon.Beacon); ok {
engine = beacon.New(b.InnerEngine())
} else {
engine = beacon.New(eth.Engine())
}
}
return &ConsensusAPI{
light: eth == nil,
eth: eth,
les: les,
engine: engine,
preparedBlocks: make(map[int]*ExecutableData),
}
}
Expand Down Expand Up @@ -143,8 +128,7 @@ func (api *ConsensusAPI) ConsensusValidated(params ConsensusValidatedParams) err
}

func (api *ConsensusAPI) ForkchoiceUpdated(params ForkChoiceParams) error {
var emptyHash = common.Hash{}
if !bytes.Equal(params.HeadBlockHash[:], emptyHash[:]) {
if params.HeadBlockHash != (common.Hash{}) {
if err := api.checkTerminalTotalDifficulty(params.HeadBlockHash); err != nil {
return err
}
Expand Down Expand Up @@ -201,7 +185,7 @@ func (api *ConsensusAPI) assembleBlock(params AssembleBlockParams) (*ExecutableD
return nil, errors.New("not supported")
}
log.Info("Producing block", "parentHash", params.ParentHash)
block, err := api.eth.Miner().GetSealingBlock(params.ParentHash, params.Timestamp)
block, err := api.eth.Miner().GetSealingBlock(params.ParentHash, params.Timestamp, params.FeeRecipient)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions miner/miner.go
Expand Up @@ -235,8 +235,8 @@ func (miner *Miner) DisablePreseal() {

// GetSealingBlock retrieves a sealing block based on the given parameters.
// The returned block is not sealed but all other fields should be filled.
func (miner *Miner) GetSealingBlock(parent common.Hash, timestamp uint64) (*types.Block, error) {
return miner.worker.getSealingBlock(parent, timestamp)
func (miner *Miner) GetSealingBlock(parent common.Hash, timestamp uint64, coinbase common.Address) (*types.Block, error) {
return miner.worker.getSealingBlock(parent, timestamp, coinbase)
}

// SubscribePendingLogs starts delivering logs from pending transactions
Expand Down
30 changes: 17 additions & 13 deletions miner/worker.go
Expand Up @@ -948,12 +948,12 @@ func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByP

// generateParams wraps various of settings for generating sealing task.
type generateParams struct {
timestamp uint64 // The timstamp for sealing task
forceTime bool // Flag whether the given timestamp is immutable or not
parentHash common.Hash // Parent block hash, empty means the latest chain head
coinbase bool // Flag whether the coinbase field is required
noUncle bool // Flag whether the uncle block inclusion is allowed
noExtra bool // Flag whether the extra field assignment is allowed
timestamp uint64 // The timstamp for sealing task
forceTime bool // Flag whether the given timestamp is immutable or not
parentHash common.Hash // Parent block hash, empty means the latest chain head
coinbase common.Address // The fee recipient address for including transaction
noUncle bool // Flag whether the uncle block inclusion is allowed
noExtra bool // Flag whether the extra field assignment is allowed
}

// prepareWork constructs the sealing task according to the given parameters,
Expand Down Expand Up @@ -1000,12 +1000,16 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
}
}
// Set the coinbase if the worker is running or it's required
if w.isRunning() || genParams.coinbase {
if w.coinbase == (common.Address{}) {
log.Error("Refusing to mine without etherbase")
return nil, errors.New("no etherbase specified")
if w.isRunning() || genParams.coinbase != (common.Address{}) {
if genParams.coinbase != (common.Address{}) {
header.Coinbase = genParams.coinbase
} else {
if w.coinbase == (common.Address{}) {
log.Error("Refusing to mine without etherbase")
return nil, errors.New("no etherbase specified")
}
header.Coinbase = w.coinbase
}
header.Coinbase = w.coinbase
}
// Run the consensus preparation with the default or customized consensus engine.
if err := w.engine.Prepare(w.chain, header); err != nil {
Expand Down Expand Up @@ -1142,13 +1146,13 @@ func (w *worker) commit(env *environment, interval func(), update bool, start ti
}

// getSealingBlock generates the sealing block based on the given parameters.
func (w *worker) getSealingBlock(parent common.Hash, timestamp uint64) (*types.Block, error) {
func (w *worker) getSealingBlock(parent common.Hash, timestamp uint64, coinbase common.Address) (*types.Block, error) {
req := &getWorkReq{
params: &generateParams{
timestamp: timestamp,
forceTime: true,
parentHash: parent,
coinbase: true,
coinbase: coinbase,
noUncle: true,
noExtra: true,
},
Expand Down
7 changes: 2 additions & 5 deletions miner/worker_test.go
Expand Up @@ -558,9 +558,6 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co
if len(block.Extra()) != 0 {
t.Error("Unexpected extra field")
}
if block.Coinbase() != w.coinbase {
t.Errorf("Invalid coinbase, want %x, get %x", w.coinbase, block.Coinbase())
}
}
if block.MixDigest() != (common.Hash{}) {
t.Error("Unexpected mix digest")
Expand Down Expand Up @@ -596,7 +593,7 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co

// This API should work even when the automatic sealing is not enabled
for _, c := range cases {
block, err := w.getSealingBlock(c.parent, timestamp)
block, err := w.getSealingBlock(c.parent, timestamp, common.Address{})
if c.expectErr {
if err == nil {
t.Error("Expect error but get nil")
Expand All @@ -612,7 +609,7 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co
// This API should work even when the automatic sealing is enabled
w.start()
for _, c := range cases {
block, err := w.getSealingBlock(c.parent, timestamp)
block, err := w.getSealingBlock(c.parent, timestamp, common.Address{})
if c.expectErr {
if err == nil {
t.Error("Expect error but get nil")
Expand Down

0 comments on commit 20936a1

Please sign in to comment.