2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-07-06 22:08:16 -05:00
|
|
|
// Package miner implements Ethereum block creation and mining.
|
2014-10-31 08:56:42 -05:00
|
|
|
package miner
|
|
|
|
|
|
|
|
import (
|
2015-08-07 05:12:05 -05:00
|
|
|
"fmt"
|
2019-04-23 02:08:51 -05:00
|
|
|
"math/big"
|
2021-10-08 11:36:58 -05:00
|
|
|
"sync"
|
2018-08-21 14:56:54 -05:00
|
|
|
"time"
|
2014-10-31 08:56:42 -05:00
|
|
|
|
2015-03-18 07:00:01 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-04-23 02:08:51 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2017-04-04 17:16:29 -05:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
2015-02-17 05:24:51 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2015-04-07 05:32:55 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2022-10-24 08:13:55 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2015-04-07 05:32:55 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-08-07 05:12:05 -05:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2014-10-31 08:56:42 -05:00
|
|
|
)
|
|
|
|
|
2022-01-24 01:19:52 -06:00
|
|
|
// Backend wraps all methods required for mining. Only full node is capable
|
|
|
|
// to offer all the functions here.
|
2016-08-15 13:14:05 -05:00
|
|
|
type Backend interface {
|
|
|
|
BlockChain() *core.BlockChain
|
2022-10-24 08:13:55 -05:00
|
|
|
TxPool() *txpool.TxPool
|
2016-08-15 13:14:05 -05:00
|
|
|
}
|
|
|
|
|
2019-04-23 02:08:51 -05:00
|
|
|
// Config is the configuration parameters of mining.
|
|
|
|
type Config struct {
|
2024-03-06 06:45:03 -06:00
|
|
|
Etherbase common.Address `toml:"-"` // Deprecated
|
|
|
|
PendingFeeRecipient common.Address `toml:"-"` // Address for pending block rewards.
|
|
|
|
ExtraData hexutil.Bytes `toml:",omitempty"` // Block extra data set by the miner
|
|
|
|
GasCeil uint64 // Target gas ceiling for mined blocks.
|
|
|
|
GasPrice *big.Int // Minimum gas price for mining a transaction
|
|
|
|
Recommit time.Duration // The time interval for miner to re-create mining work.
|
2022-10-03 07:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConfig contains default settings for miner.
|
|
|
|
var DefaultConfig = Config{
|
2024-03-06 06:45:03 -06:00
|
|
|
GasCeil: 30_000_000,
|
2024-05-31 02:39:40 -05:00
|
|
|
GasPrice: big.NewInt(params.GWei / 1000),
|
2022-11-03 09:18:28 -05:00
|
|
|
|
|
|
|
// The default recommit time is chosen as two seconds since
|
|
|
|
// consensus-layer usually will wait a half slot of time(6s)
|
|
|
|
// for payload generation. It should be enough for Geth to
|
|
|
|
// run 3 rounds.
|
2024-03-06 06:45:03 -06:00
|
|
|
Recommit: 2 * time.Second,
|
2019-04-23 02:08:51 -05:00
|
|
|
}
|
|
|
|
|
2024-03-06 06:45:03 -06:00
|
|
|
// Miner is the main object which takes care of submitting new work to consensus
|
|
|
|
// engine and gathering the sealing result.
|
2014-10-31 08:56:42 -05:00
|
|
|
type Miner struct {
|
2024-03-06 06:45:03 -06:00
|
|
|
confMu sync.RWMutex // The lock used to protect the config fields: GasCeil, GasTip and Extradata
|
|
|
|
config *Config
|
|
|
|
chainConfig *params.ChainConfig
|
|
|
|
engine consensus.Engine
|
|
|
|
txpool *txpool.TxPool
|
|
|
|
chain *core.BlockChain
|
|
|
|
pending *pending
|
|
|
|
pendingMu sync.Mutex // Lock protects the pending block
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new miner with provided config.
|
|
|
|
func New(eth Backend, config Config, engine consensus.Engine) *Miner {
|
|
|
|
return &Miner{
|
|
|
|
config: &config,
|
|
|
|
chainConfig: eth.BlockChain().Config(),
|
|
|
|
engine: engine,
|
|
|
|
txpool: eth.TxPool(),
|
|
|
|
chain: eth.BlockChain(),
|
|
|
|
pending: &pending{},
|
2015-05-14 17:43:00 -05:00
|
|
|
}
|
2015-05-09 05:04:00 -05:00
|
|
|
}
|
|
|
|
|
2024-03-06 06:45:03 -06:00
|
|
|
// Pending returns the currently pending block and associated receipts, logs
|
|
|
|
// and statedb. The returned values can be nil in case the pending block is
|
|
|
|
// not initialized.
|
|
|
|
func (miner *Miner) Pending() (*types.Block, types.Receipts, *state.StateDB) {
|
|
|
|
pending := miner.getPending()
|
|
|
|
if pending == nil {
|
|
|
|
return nil, nil, nil
|
2015-08-06 04:58:14 -05:00
|
|
|
}
|
2024-03-06 06:45:03 -06:00
|
|
|
return pending.block, pending.receipts, pending.stateDB.Copy()
|
2014-11-07 05:18:48 -06:00
|
|
|
}
|
2015-04-05 11:57:03 -05:00
|
|
|
|
2024-03-06 06:45:03 -06:00
|
|
|
// SetExtra sets the content used to initialize the block extra field.
|
2019-11-24 13:46:34 -06:00
|
|
|
func (miner *Miner) SetExtra(extra []byte) error {
|
2017-01-04 13:17:24 -06:00
|
|
|
if uint64(len(extra)) > params.MaximumExtraDataSize {
|
2019-11-24 13:46:34 -06:00
|
|
|
return fmt.Errorf("extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
|
2015-08-07 05:12:05 -05:00
|
|
|
}
|
2024-03-06 06:45:03 -06:00
|
|
|
miner.confMu.Lock()
|
|
|
|
miner.config.ExtraData = extra
|
|
|
|
miner.confMu.Unlock()
|
2015-08-07 05:12:05 -05:00
|
|
|
return nil
|
2015-04-05 11:57:03 -05:00
|
|
|
}
|
2015-04-07 05:32:55 -05:00
|
|
|
|
2021-07-06 03:35:39 -05:00
|
|
|
// SetGasCeil sets the gaslimit to strive for when mining blocks post 1559.
|
|
|
|
// For pre-1559 blocks, it sets the ceiling.
|
|
|
|
func (miner *Miner) SetGasCeil(ceil uint64) {
|
2024-03-06 06:45:03 -06:00
|
|
|
miner.confMu.Lock()
|
|
|
|
miner.config.GasCeil = ceil
|
|
|
|
miner.confMu.Unlock()
|
2021-07-06 03:35:39 -05:00
|
|
|
}
|
|
|
|
|
2024-03-06 06:45:03 -06:00
|
|
|
// SetGasTip sets the minimum gas tip for inclusion.
|
|
|
|
func (miner *Miner) SetGasTip(tip *big.Int) error {
|
|
|
|
miner.confMu.Lock()
|
|
|
|
miner.config.GasPrice = tip
|
|
|
|
miner.confMu.Unlock()
|
|
|
|
return nil
|
2019-12-10 05:39:14 -06:00
|
|
|
}
|
2022-05-18 09:33:37 -05:00
|
|
|
|
2022-11-02 04:32:20 -05:00
|
|
|
// BuildPayload builds the payload according to the provided parameters.
|
beacon, core, eth, miner: integrate witnesses into production Geth (#30069)
This PR integrates witness-enabled block production, witness-creating
payload execution and stateless cross-validation into the `engine` API.
The purpose of the PR is to enable the following use-cases (for API
details, please see next section):
- Cross validating locally created blocks:
- Call `forkchoiceUpdatedWithWitness` instead of `forkchoiceUpdated` to
trigger witness creation too.
- Call `getPayload` as before to retrieve the new block and also the
above created witness.
- Call `executeStatelessPayload` against another client to
cross-validate the block.
- Cross validating locally processed blocks:
- Call `newPayloadWithWitness` instead of `newPayload` to trigger
witness creation too.
- Call `executeStatelessPayload` against another client to
cross-validate the block.
- Block production for stateless clients (local or MEV builders):
- Call `forkchoiceUpdatedWithWitness` instead of `forkchoiceUpdated` to
trigger witness creation too.
- Call `getPayload` as before to retrieve the new block and also the
above created witness.
- Propagate witnesses across the consensus libp2p network for stateless
Ethereum.
- Stateless validator validation:
- Call `executeStatelessPayload` with the propagated witness to
statelessly validate the block.
*Note, the various `WithWitness` methods could also *just be* an
additional boolean flag on the base methods, but this PR wanted to keep
the methods separate until a final consensus is reached on how to
integrate in production.*
---
The following `engine` API types are introduced:
```go
// StatelessPayloadStatusV1 is the result of a stateless payload execution.
type StatelessPayloadStatusV1 struct {
Status string `json:"status"`
StateRoot common.Hash `json:"stateRoot"`
ReceiptsRoot common.Hash `json:"receiptsRoot"`
ValidationError *string `json:"validationError"`
}
```
- Add `forkchoiceUpdatedWithWitnessV1,2,3` with same params and returns
as `forkchoiceUpdatedV1,2,3`, but triggering a stateless witness
building if block production is requested.
- Extend `getPayloadV2,3` to return `executionPayloadEnvelope` with an
additional `witness` field of type `bytes` iff created via
`forkchoiceUpdatedWithWitnessV2,3`.
- Add `newPayloadWithWitnessV1,2,3,4` with same params and returns as
`newPayloadV1,2,3,4`, but triggering a stateless witness creation during
payload execution to allow cross validating it.
- Extend `payloadStatusV1` with a `witness` field of type `bytes` if
returned by `newPayloadWithWitnessV1,2,3,4`.
- Add `executeStatelessPayloadV1,2,3,4` with same base params as
`newPayloadV1,2,3,4` and one more additional param (`witness`) of type
`bytes`. The method returns `statelessPayloadStatusV1`, which mirrors
`payloadStatusV1` but replaces `latestValidHash` with `stateRoot` and
`receiptRoot`.
2024-09-20 08:43:42 -05:00
|
|
|
func (miner *Miner) BuildPayload(args *BuildPayloadArgs, witness bool) (*Payload, error) {
|
|
|
|
return miner.buildPayload(args, witness)
|
2024-03-06 06:45:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// getPending retrieves the pending block based on the current head block.
|
|
|
|
// The result might be nil if pending generation is failed.
|
|
|
|
func (miner *Miner) getPending() *newPayloadResult {
|
|
|
|
header := miner.chain.CurrentHeader()
|
|
|
|
miner.pendingMu.Lock()
|
|
|
|
defer miner.pendingMu.Unlock()
|
|
|
|
if cached := miner.pending.resolve(header.Hash()); cached != nil {
|
|
|
|
return cached
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
timestamp = uint64(time.Now().Unix())
|
|
|
|
withdrawal types.Withdrawals
|
|
|
|
)
|
|
|
|
if miner.chainConfig.IsShanghai(new(big.Int).Add(header.Number, big.NewInt(1)), timestamp) {
|
|
|
|
withdrawal = []*types.Withdrawal{}
|
|
|
|
}
|
|
|
|
ret := miner.generateWork(&generateParams{
|
|
|
|
timestamp: timestamp,
|
|
|
|
forceTime: false,
|
|
|
|
parentHash: header.Hash(),
|
|
|
|
coinbase: miner.config.PendingFeeRecipient,
|
|
|
|
random: common.Hash{},
|
|
|
|
withdrawals: withdrawal,
|
|
|
|
beaconRoot: nil,
|
|
|
|
noTxs: false,
|
beacon, core, eth, miner: integrate witnesses into production Geth (#30069)
This PR integrates witness-enabled block production, witness-creating
payload execution and stateless cross-validation into the `engine` API.
The purpose of the PR is to enable the following use-cases (for API
details, please see next section):
- Cross validating locally created blocks:
- Call `forkchoiceUpdatedWithWitness` instead of `forkchoiceUpdated` to
trigger witness creation too.
- Call `getPayload` as before to retrieve the new block and also the
above created witness.
- Call `executeStatelessPayload` against another client to
cross-validate the block.
- Cross validating locally processed blocks:
- Call `newPayloadWithWitness` instead of `newPayload` to trigger
witness creation too.
- Call `executeStatelessPayload` against another client to
cross-validate the block.
- Block production for stateless clients (local or MEV builders):
- Call `forkchoiceUpdatedWithWitness` instead of `forkchoiceUpdated` to
trigger witness creation too.
- Call `getPayload` as before to retrieve the new block and also the
above created witness.
- Propagate witnesses across the consensus libp2p network for stateless
Ethereum.
- Stateless validator validation:
- Call `executeStatelessPayload` with the propagated witness to
statelessly validate the block.
*Note, the various `WithWitness` methods could also *just be* an
additional boolean flag on the base methods, but this PR wanted to keep
the methods separate until a final consensus is reached on how to
integrate in production.*
---
The following `engine` API types are introduced:
```go
// StatelessPayloadStatusV1 is the result of a stateless payload execution.
type StatelessPayloadStatusV1 struct {
Status string `json:"status"`
StateRoot common.Hash `json:"stateRoot"`
ReceiptsRoot common.Hash `json:"receiptsRoot"`
ValidationError *string `json:"validationError"`
}
```
- Add `forkchoiceUpdatedWithWitnessV1,2,3` with same params and returns
as `forkchoiceUpdatedV1,2,3`, but triggering a stateless witness
building if block production is requested.
- Extend `getPayloadV2,3` to return `executionPayloadEnvelope` with an
additional `witness` field of type `bytes` iff created via
`forkchoiceUpdatedWithWitnessV2,3`.
- Add `newPayloadWithWitnessV1,2,3,4` with same params and returns as
`newPayloadV1,2,3,4`, but triggering a stateless witness creation during
payload execution to allow cross validating it.
- Extend `payloadStatusV1` with a `witness` field of type `bytes` if
returned by `newPayloadWithWitnessV1,2,3,4`.
- Add `executeStatelessPayloadV1,2,3,4` with same base params as
`newPayloadV1,2,3,4` and one more additional param (`witness`) of type
`bytes`. The method returns `statelessPayloadStatusV1`, which mirrors
`payloadStatusV1` but replaces `latestValidHash` with `stateRoot` and
`receiptRoot`.
2024-09-20 08:43:42 -05:00
|
|
|
}, false) // we will never make a witness for a pending block
|
2024-03-06 06:45:03 -06:00
|
|
|
if ret.err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
miner.pending.update(header.Hash(), ret)
|
|
|
|
return ret
|
2022-05-18 09:33:37 -05:00
|
|
|
}
|