Skip to content

Commit

Permalink
miner: update
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Dec 9, 2021
1 parent ced98b5 commit e22dadd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
46 changes: 25 additions & 21 deletions miner/worker.go
Expand Up @@ -592,7 +592,7 @@ func (w *worker) mainLoop() {
}
txset := types.NewTransactionsByPriceAndNonce(w.current.signer, txs, w.current.header.BaseFee)
tcount := w.current.tcount
w.commitTransactions(w.current, txset, w.current.coinbase, nil)
w.commitTransactions(w.current, txset, nil)

// Only update the snapshot if any new transactions were added
// to the pending block
Expand Down Expand Up @@ -823,10 +823,10 @@ func (w *worker) updateSnapshot(env *environment) {
w.snapshotState = env.state.Copy()
}

func (w *worker) commitTransaction(env *environment, tx *types.Transaction, coinbase common.Address) ([]*types.Log, error) {
func (w *worker) commitTransaction(env *environment, tx *types.Transaction) ([]*types.Log, error) {
snap := env.state.Snapshot()

receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &coinbase, env.gasPool, env.state, env.header, tx, &env.header.GasUsed, *w.chain.GetVMConfig())
receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &env.coinbase, env.gasPool, env.state, env.header, tx, &env.header.GasUsed, *w.chain.GetVMConfig())
if err != nil {
env.state.RevertToSnapshot(snap)
return nil, err
Expand All @@ -837,7 +837,7 @@ func (w *worker) commitTransaction(env *environment, tx *types.Transaction, coin
return receipt.Logs, nil
}

func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByPriceAndNonce, coinbase common.Address, interrupt *int32) bool {
func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByPriceAndNonce, interrupt *int32) bool {
gasLimit := env.header.GasLimit
if env.gasPool == nil {
env.gasPool = new(core.GasPool).AddGas(gasLimit)
Expand Down Expand Up @@ -891,7 +891,7 @@ func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByP
// Start executing the transaction
env.state.Prepare(tx.Hash(), env.tcount)

logs, err := w.commitTransaction(env, tx, coinbase)
logs, err := w.commitTransaction(env, tx)
switch {
case errors.Is(err, core.ErrGasLimitReached):
// Pop the current out-of-gas transaction without shifting in the next from the account
Expand Down Expand Up @@ -991,6 +991,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
Number: num.Add(num, common.Big1),
GasLimit: core.CalcGasLimit(parent.GasLimit(), w.config.GasCeil),
Time: timestamp,
Coinbase: genParams.coinbase,
}
if !genParams.noExtra && len(w.extra) != 0 {
header.Extra = w.extra
Expand All @@ -1003,25 +1004,15 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
header.GasLimit = core.CalcGasLimit(parentGasLimit, w.config.GasCeil)
}
}
// Set the coinbase if the worker is running or it's required
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
}
}
// Run the consensus preparation with the default or customized consensus engine.
if err := w.engine.Prepare(w.chain, header); err != nil {
log.Error("Failed to prepare header for sealing", "err", err)
return nil, err
}
// Could potentially happen if starting to mine in an odd state.
env, err := w.makeEnv(parent, header, w.coinbase)
// Note genParams.coinbase can be different with header.Coinbase
// since clique algorithm can modify the coinbase field in header.
env, err := w.makeEnv(parent, header, genParams.coinbase)
if err != nil {
log.Error("Failed to create sealing context", "err", err)
return nil, err
Expand Down Expand Up @@ -1063,13 +1054,13 @@ func (w *worker) fillTransactions(interrupt *int32, env *environment) error {
}
if len(localTxs) > 0 {
txs := types.NewTransactionsByPriceAndNonce(env.signer, localTxs, env.header.BaseFee)
if w.commitTransactions(env, txs, env.coinbase, interrupt) {
if w.commitTransactions(env, txs, interrupt) {
return nil
}
}
if len(remoteTxs) > 0 {
txs := types.NewTransactionsByPriceAndNonce(env.signer, remoteTxs, env.header.BaseFee)
if w.commitTransactions(env, txs, env.coinbase, interrupt) {
if w.commitTransactions(env, txs, interrupt) {
return nil
}
}
Expand All @@ -1094,7 +1085,20 @@ func (w *worker) generateWork(params *generateParams) (*types.Block, error) {
// and submit them to the sealer.
func (w *worker) commitWork(interrupt *int32, noempty bool, timestamp int64) {
start := time.Now()
work, err := w.prepareWork(&generateParams{timestamp: uint64(timestamp)})

// Set the coinbase if the worker is running or it's required
var coinbase common.Address
if w.isRunning() {
if w.coinbase == (common.Address{}) {
log.Error("Refusing to mine without etherbase")
return
}
coinbase = w.coinbase // Use the preset address as the fee recipient
}
work, err := w.prepareWork(&generateParams{
timestamp: uint64(timestamp),
coinbase: coinbase,
})
if err != nil {
return
}
Expand Down
27 changes: 22 additions & 5 deletions miner/worker_test.go
Expand Up @@ -551,7 +551,7 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co
time.Sleep(100 * time.Millisecond)
}
timestamp := uint64(time.Now().Unix())
assertBlock := func(block *types.Block, number uint64) {
assertBlock := func(block *types.Block, number uint64, coinbase common.Address) {
if block.Time() != timestamp {
// Sometime the timestamp will be mutated if the timestamp
// is even smaller than parent block's. It's OK.
Expand All @@ -565,6 +565,13 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co
if len(block.Extra()) != 0 {
t.Error("Unexpected extra field")
}
if block.Coinbase() != coinbase {
t.Errorf("Unexpected coinbase got %x want %x", block.Coinbase(), coinbase)
}
} else {
if block.Coinbase() != (common.Address{}) {
t.Error("Unexpected coinbase")
}
}
if block.MixDigest() != (common.Hash{}) {
t.Error("Unexpected mix digest")
Expand All @@ -578,29 +585,39 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co
}
var cases = []struct {
parent common.Hash
coinbase common.Address
expectNumber uint64
expectErr bool
}{
{
b.chain.Genesis().Hash(),
common.HexToAddress("0xdeadbeef"),
uint64(1),
false,
},
{
b.chain.CurrentBlock().Hash(),
common.HexToAddress("0xdeadbeef"),
b.chain.CurrentBlock().NumberU64() + 1,
false,
},
{
b.chain.CurrentBlock().Hash(),
common.Address{},
b.chain.CurrentBlock().NumberU64() + 1,
false,
},
{
common.HexToHash("0xdeadbeef"),
common.HexToAddress("0xdeadbeef"),
0,
true,
},
}

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

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

0 comments on commit e22dadd

Please sign in to comment.