2015-07-07 02:54:22 +02:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 18:48:40 +02:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 02:54:22 +02:00
|
|
|
//
|
2015-07-23 18:35:11 +02:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 02:54:22 +02: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 18:48:40 +02:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 02:54:22 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 18:48:40 +02:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 02:54:22 +02: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 18:48:40 +02:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 02:54:22 +02:00
|
|
|
|
2015-02-04 05:52:59 -08:00
|
|
|
package miner
|
|
|
|
|
|
|
|
import (
|
2018-08-28 16:48:20 +03:00
|
|
|
"errors"
|
2022-01-24 15:19:52 +08:00
|
|
|
"fmt"
|
2015-02-04 05:52:59 -08:00
|
|
|
"math/big"
|
2015-03-26 17:45:03 +01:00
|
|
|
"sync/atomic"
|
2015-06-15 11:33:08 +02:00
|
|
|
"time"
|
2015-02-04 05:52:59 -08:00
|
|
|
|
2015-03-18 13:00:01 +01:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-08-01 17:58:45 +08:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
|
2023-08-23 15:16:14 -06:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
|
2015-02-04 05:52:59 -08:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2015-03-23 18:27:05 +01:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
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 16:43:42 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/stateless"
|
2023-07-27 13:45:35 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2015-02-04 05:52:59 -08:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-08-26 04:52:12 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2017-02-22 14:10:07 +02:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-07-08 13:00:37 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2024-02-17 13:37:14 +02:00
|
|
|
"github.com/holiman/uint256"
|
2015-02-04 05:52:59 -08:00
|
|
|
)
|
|
|
|
|
2022-05-06 11:19:30 +02:00
|
|
|
var (
|
|
|
|
errBlockInterruptedByNewHead = errors.New("new head arrived while building block")
|
|
|
|
errBlockInterruptedByRecommit = errors.New("recommit interrupt while building block")
|
2022-10-03 20:10:00 +08:00
|
|
|
errBlockInterruptedByTimeout = errors.New("timeout while building block")
|
2022-05-06 11:19:30 +02:00
|
|
|
)
|
|
|
|
|
2022-01-24 15:19:52 +08:00
|
|
|
// environment is the worker's current environment and holds all
|
|
|
|
// information of the sealing block generation.
|
2018-08-16 19:14:33 +08:00
|
|
|
type environment struct {
|
2023-05-31 15:09:49 +08:00
|
|
|
signer types.Signer
|
|
|
|
state *state.StateDB // apply state changes here
|
|
|
|
tcount int // tx count in cycle
|
|
|
|
gasPool *core.GasPool // available gas used to pack transactions
|
|
|
|
coinbase common.Address
|
2015-02-04 05:52:59 -08:00
|
|
|
|
2015-06-16 12:41:50 +02:00
|
|
|
header *types.Header
|
|
|
|
txs []*types.Transaction
|
|
|
|
receipts []*types.Receipt
|
2023-08-23 15:16:14 -06:00
|
|
|
sidecars []*types.BlobTxSidecar
|
|
|
|
blobs int
|
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 16:43:42 +03:00
|
|
|
|
|
|
|
witness *stateless.Witness
|
2022-01-24 15:19:52 +08:00
|
|
|
}
|
|
|
|
|
2018-08-16 19:14:33 +08:00
|
|
|
const (
|
|
|
|
commitInterruptNone int32 = iota
|
|
|
|
commitInterruptNewHead
|
|
|
|
commitInterruptResubmit
|
2022-10-03 20:10:00 +08:00
|
|
|
commitInterruptTimeout
|
2018-08-16 19:14:33 +08:00
|
|
|
)
|
|
|
|
|
2023-08-23 15:16:14 -06:00
|
|
|
// newPayloadResult is the result of payload generation.
|
2022-11-02 17:32:20 +08:00
|
|
|
type newPayloadResult struct {
|
2023-08-23 15:16:14 -06:00
|
|
|
err error
|
|
|
|
block *types.Block
|
|
|
|
fees *big.Int // total block fees
|
|
|
|
sidecars []*types.BlobTxSidecar // collected blobs of blob transactions
|
2024-03-06 13:45:03 +01:00
|
|
|
stateDB *state.StateDB // StateDB after executing the transactions
|
|
|
|
receipts []*types.Receipt // Receipts collected during construction
|
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 16:43:42 +03:00
|
|
|
witness *stateless.Witness // Witness is an optional stateless proof
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
|
|
|
|
2024-04-08 18:59:17 +08:00
|
|
|
// generateParams wraps various settings for generating sealing task.
|
2024-03-06 13:45:03 +01:00
|
|
|
type generateParams struct {
|
|
|
|
timestamp uint64 // The timestamp 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
|
|
|
|
random common.Hash // The randomness generated by beacon chain, empty before the merge
|
|
|
|
withdrawals types.Withdrawals // List of withdrawals to include in block (shanghai field)
|
|
|
|
beaconRoot *common.Hash // The beacon root (cancun field).
|
|
|
|
noTxs bool // Flag whether an empty block without any transaction is expected
|
2015-02-04 05:52:59 -08:00
|
|
|
}
|
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
// generateWork generates a sealing block based on the given 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 16:43:42 +03:00
|
|
|
func (miner *Miner) generateWork(params *generateParams, witness bool) *newPayloadResult {
|
|
|
|
work, err := miner.prepareWork(params, witness)
|
2024-03-06 13:45:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return &newPayloadResult{err: err}
|
2022-10-03 20:10:00 +08:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
if !params.noTxs {
|
|
|
|
interrupt := new(atomic.Int32)
|
|
|
|
timer := time.AfterFunc(miner.config.Recommit, func() {
|
|
|
|
interrupt.Store(commitInterruptTimeout)
|
|
|
|
})
|
|
|
|
defer timer.Stop()
|
2015-04-07 12:32:55 +02:00
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
err := miner.fillTransactions(interrupt, work)
|
|
|
|
if errors.Is(err, errBlockInterruptedByTimeout) {
|
|
|
|
log.Warn("Block building is interrupted", "allowance", common.PrettyDuration(miner.config.Recommit))
|
|
|
|
}
|
2019-11-20 18:36:41 +08:00
|
|
|
}
|
2024-09-04 14:33:51 +02:00
|
|
|
|
2024-03-20 14:58:47 +01:00
|
|
|
body := types.Body{Transactions: work.txs, Withdrawals: params.withdrawals}
|
2024-09-04 14:33:51 +02:00
|
|
|
allLogs := make([]*types.Log, 0)
|
|
|
|
for _, r := range work.receipts {
|
|
|
|
allLogs = append(allLogs, r.Logs...)
|
|
|
|
}
|
|
|
|
// Read requests if Prague is enabled.
|
|
|
|
if miner.chainConfig.IsPrague(work.header.Number, work.header.Time) {
|
|
|
|
requests, err := core.ParseDepositLogs(allLogs, miner.chainConfig)
|
|
|
|
if err != nil {
|
|
|
|
return &newPayloadResult{err: err}
|
|
|
|
}
|
|
|
|
body.Requests = requests
|
|
|
|
}
|
2024-03-20 14:58:47 +01:00
|
|
|
block, err := miner.engine.FinalizeAndAssemble(miner.chain, work.header, work.state, &body, work.receipts)
|
2024-03-06 13:45:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return &newPayloadResult{err: err}
|
2022-01-24 15:19:52 +08:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
return &newPayloadResult{
|
|
|
|
block: block,
|
|
|
|
fees: totalFees(block, work.receipts),
|
|
|
|
sidecars: work.sidecars,
|
|
|
|
stateDB: work.state,
|
|
|
|
receipts: work.receipts,
|
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 16:43:42 +03:00
|
|
|
witness: work.witness,
|
2018-08-14 23:34:33 +08:00
|
|
|
}
|
2018-04-16 00:56:20 -07:00
|
|
|
}
|
2016-11-30 10:48:48 +01:00
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
// prepareWork constructs the sealing task according to the given parameters,
|
|
|
|
// either based on the last chain head or specified parent. In this function
|
|
|
|
// the pending transactions are not filled yet, only the empty task returned.
|
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 16:43:42 +03:00
|
|
|
func (miner *Miner) prepareWork(genParams *generateParams, witness bool) (*environment, error) {
|
2024-03-06 13:45:03 +01:00
|
|
|
miner.confMu.RLock()
|
|
|
|
defer miner.confMu.RUnlock()
|
2015-02-04 05:52:59 -08:00
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
// Find the parent block for sealing task
|
|
|
|
parent := miner.chain.CurrentBlock()
|
|
|
|
if genParams.parentHash != (common.Hash{}) {
|
|
|
|
block := miner.chain.GetBlockByHash(genParams.parentHash)
|
|
|
|
if block == nil {
|
2024-04-08 18:59:17 +08:00
|
|
|
return nil, errors.New("missing parent")
|
2020-07-28 18:16:49 +02:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
parent = block.Header()
|
2020-07-28 18:16:49 +02:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
// Sanity check the timestamp correctness, recap the timestamp
|
|
|
|
// to parent+1 if the mutation is allowed.
|
|
|
|
timestamp := genParams.timestamp
|
|
|
|
if parent.Time >= timestamp {
|
|
|
|
if genParams.forceTime {
|
|
|
|
return nil, fmt.Errorf("invalid timestamp, parent %d given %d", parent.Time, timestamp)
|
miner, test: fix potential goroutine leak (#21989)
In miner/worker.go, there are two goroutine using channel w.newWorkCh: newWorkerLoop() sends to this channel, and mainLoop() receives from this channel. Only the receive operation is in a select.
However, w.exitCh may be closed by another goroutine. This is fine for the receive since receive is in select, but if the send operation is blocking, then it will block forever. This commit puts the send in a select, so it won't block even if w.exitCh is closed.
Similarly, there are two goroutines using channel errc: the parent that runs the test receives from it, and the child created at line 573 sends to it. If the parent goroutine exits too early by calling t.Fatalf() at line 614, then the child goroutine will be blocked at line 574 forever. This commit adds 1 buffer to errc. Now send will not block, and receive is not influenced because receive still needs to wait for the send.
2020-12-11 04:29:42 -05:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
timestamp = parent.Time + 1
|
2018-08-22 03:56:54 +08:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
// Construct the sealing block header.
|
|
|
|
header := &types.Header{
|
|
|
|
ParentHash: parent.Hash(),
|
|
|
|
Number: new(big.Int).Add(parent.Number, common.Big1),
|
|
|
|
GasLimit: core.CalcGasLimit(parent.GasLimit, miner.config.GasCeil),
|
|
|
|
Time: timestamp,
|
|
|
|
Coinbase: genParams.coinbase,
|
2018-08-23 21:02:57 +08:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
// Set the extra field.
|
|
|
|
if len(miner.config.ExtraData) != 0 {
|
|
|
|
header.Extra = miner.config.ExtraData
|
2018-08-16 19:14:33 +08:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
// Set the randomness field from the beacon chain if it's available.
|
|
|
|
if genParams.random != (common.Hash{}) {
|
|
|
|
header.MixDigest = genParams.random
|
2015-02-04 05:52:59 -08:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
// Set baseFee and GasLimit if we are on an EIP-1559 chain
|
|
|
|
if miner.chainConfig.IsLondon(header.Number) {
|
|
|
|
header.BaseFee = eip1559.CalcBaseFee(miner.chainConfig, parent)
|
|
|
|
if !miner.chainConfig.IsLondon(parent.Number) {
|
|
|
|
parentGasLimit := parent.GasLimit * miner.chainConfig.ElasticityMultiplier()
|
|
|
|
header.GasLimit = core.CalcGasLimit(parentGasLimit, miner.config.GasCeil)
|
2018-08-14 23:34:33 +08:00
|
|
|
}
|
|
|
|
}
|
2024-03-19 22:23:55 +08:00
|
|
|
// Run the consensus preparation with the default or customized consensus engine.
|
|
|
|
// Note that the `header.Time` may be changed.
|
|
|
|
if err := miner.engine.Prepare(miner.chain, header); err != nil {
|
|
|
|
log.Error("Failed to prepare header for sealing", "err", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
// Apply EIP-4844, EIP-4788.
|
|
|
|
if miner.chainConfig.IsCancun(header.Number, header.Time) {
|
|
|
|
var excessBlobGas uint64
|
|
|
|
if miner.chainConfig.IsCancun(parent.Number, parent.Time) {
|
|
|
|
excessBlobGas = eip4844.CalcExcessBlobGas(*parent.ExcessBlobGas, *parent.BlobGasUsed)
|
|
|
|
} else {
|
|
|
|
// For the first post-fork block, both parent.data_gas_used and parent.excess_data_gas are evaluated as 0
|
|
|
|
excessBlobGas = eip4844.CalcExcessBlobGas(0, 0)
|
2018-08-14 23:34:33 +08:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
header.BlobGasUsed = new(uint64)
|
|
|
|
header.ExcessBlobGas = &excessBlobGas
|
|
|
|
header.ParentBeaconRoot = genParams.beaconRoot
|
2018-08-14 23:34:33 +08:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
// Could potentially happen if starting to mine in an odd state.
|
|
|
|
// Note genParams.coinbase can be different with header.Coinbase
|
|
|
|
// since clique algorithm can modify the coinbase field in header.
|
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 16:43:42 +03:00
|
|
|
env, err := miner.makeEnv(parent, header, genParams.coinbase, witness)
|
2024-03-06 13:45:03 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to create sealing context", "err", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if header.ParentBeaconRoot != nil {
|
|
|
|
context := core.NewEVMBlockContext(header, miner.chain, nil)
|
|
|
|
vmenv := vm.NewEVM(context, vm.TxContext{}, env.state, miner.chainConfig, vm.Config{})
|
|
|
|
core.ProcessBeaconBlockRoot(*header.ParentBeaconRoot, vmenv, env.state)
|
2015-02-04 05:52:59 -08:00
|
|
|
}
|
2024-08-26 10:39:35 +02:00
|
|
|
if miner.chainConfig.IsPrague(header.Number, header.Time) {
|
|
|
|
context := core.NewEVMBlockContext(header, miner.chain, nil)
|
|
|
|
vmenv := vm.NewEVM(context, vm.TxContext{}, env.state, miner.chainConfig, vm.Config{})
|
|
|
|
core.ProcessParentBlockHash(header.ParentHash, vmenv, env.state)
|
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
return env, nil
|
2015-02-04 05:52:59 -08:00
|
|
|
}
|
|
|
|
|
2022-01-24 15:19:52 +08:00
|
|
|
// makeEnv creates a new environment for the sealing block.
|
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 16:43:42 +03:00
|
|
|
func (miner *Miner) makeEnv(parent *types.Header, header *types.Header, coinbase common.Address, witness bool) (*environment, error) {
|
2024-08-01 07:06:43 -07:00
|
|
|
// Retrieve the parent state to execute on top.
|
2024-03-06 13:45:03 +01:00
|
|
|
state, err := miner.chain.StateAt(parent.Root)
|
2022-01-24 15:19:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-06 16:35:55 +02:00
|
|
|
}
|
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 16:43:42 +03:00
|
|
|
if witness {
|
|
|
|
bundle, err := stateless.NewWitness(header, miner.chain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state.StartPrefetcher("miner", bundle)
|
|
|
|
}
|
2022-01-24 15:19:52 +08:00
|
|
|
// Note the passed coinbase may be different with header.Coinbase.
|
2024-03-06 13:45:03 +01:00
|
|
|
return &environment{
|
|
|
|
signer: types.MakeSigner(miner.chainConfig, header.Number, header.Time),
|
2023-05-31 15:09:49 +08:00
|
|
|
state: state,
|
|
|
|
coinbase: coinbase,
|
|
|
|
header: header,
|
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 16:43:42 +03:00
|
|
|
witness: state.Witness(),
|
2024-03-06 13:45:03 +01:00
|
|
|
}, nil
|
2015-04-07 12:32:55 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
func (miner *Miner) commitTransaction(env *environment, tx *types.Transaction) error {
|
2023-08-26 04:52:12 +02:00
|
|
|
if tx.Type() == types.BlobTxType {
|
2024-03-06 13:45:03 +01:00
|
|
|
return miner.commitBlobTransaction(env, tx)
|
2023-08-26 04:52:12 +02:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
receipt, err := miner.applyTransaction(env, tx)
|
2023-08-26 04:52:12 +02:00
|
|
|
if err != nil {
|
2024-03-06 13:45:03 +01:00
|
|
|
return err
|
2023-08-26 04:52:12 +02:00
|
|
|
}
|
|
|
|
env.txs = append(env.txs, tx)
|
|
|
|
env.receipts = append(env.receipts, receipt)
|
2024-03-06 13:45:03 +01:00
|
|
|
env.tcount++
|
|
|
|
return nil
|
2023-08-26 04:52:12 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
func (miner *Miner) commitBlobTransaction(env *environment, tx *types.Transaction) error {
|
2023-08-26 04:52:12 +02:00
|
|
|
sc := tx.BlobTxSidecar()
|
|
|
|
if sc == nil {
|
|
|
|
panic("blob transaction without blobs in miner")
|
|
|
|
}
|
2023-08-23 15:16:14 -06:00
|
|
|
// Checking against blob gas limit: It's kind of ugly to perform this check here, but there
|
|
|
|
// isn't really a better place right now. The blob gas limit is checked at block validation time
|
|
|
|
// and not during execution. This means core.ApplyTransaction will not return an error if the
|
|
|
|
// tx has too many blobs. So we have to explicitly check it here.
|
2023-08-26 04:52:12 +02:00
|
|
|
if (env.blobs+len(sc.Blobs))*params.BlobTxBlobGasPerBlob > params.MaxBlobGasPerBlock {
|
2024-03-06 13:45:03 +01:00
|
|
|
return errors.New("max data blobs reached")
|
2023-08-23 15:16:14 -06:00
|
|
|
}
|
2024-03-06 13:45:03 +01:00
|
|
|
receipt, err := miner.applyTransaction(env, tx)
|
2018-08-16 19:14:33 +08:00
|
|
|
if err != nil {
|
2024-03-06 13:45:03 +01:00
|
|
|
return err
|
2018-08-16 19:14:33 +08:00
|
|
|
}
|
2023-08-23 15:16:14 -06:00
|
|
|
env.txs = append(env.txs, tx.WithoutBlobTxSidecar())
|
2022-01-24 15:19:52 +08:00
|
|
|
env.receipts = append(env.receipts, receipt)
|
2023-08-26 04:52:12 +02:00
|
|
|
env.sidecars = append(env.sidecars, sc)
|
|
|
|
env.blobs += len(sc.Blobs)
|
|
|
|
*env.header.BlobGasUsed += receipt.BlobGasUsed
|
2024-03-06 13:45:03 +01:00
|
|
|
env.tcount++
|
|
|
|
return nil
|
2023-08-26 04:52:12 +02:00
|
|
|
}
|
2018-08-16 19:14:33 +08:00
|
|
|
|
2023-08-26 04:52:12 +02:00
|
|
|
// applyTransaction runs the transaction. If execution fails, state and gas pool are reverted.
|
2024-03-06 13:45:03 +01:00
|
|
|
func (miner *Miner) applyTransaction(env *environment, tx *types.Transaction) (*types.Receipt, error) {
|
2023-08-26 04:52:12 +02:00
|
|
|
var (
|
|
|
|
snap = env.state.Snapshot()
|
|
|
|
gp = env.gasPool.Gas()
|
|
|
|
)
|
2024-03-22 18:53:53 +01:00
|
|
|
receipt, err := core.ApplyTransaction(miner.chainConfig, miner.chain, &env.coinbase, env.gasPool, env.state, env.header, tx, &env.header.GasUsed, vm.Config{})
|
2023-08-26 04:52:12 +02:00
|
|
|
if err != nil {
|
|
|
|
env.state.RevertToSnapshot(snap)
|
|
|
|
env.gasPool.SetGas(gp)
|
2023-08-23 15:16:14 -06:00
|
|
|
}
|
2023-08-26 04:52:12 +02:00
|
|
|
return receipt, err
|
2018-08-16 19:14:33 +08:00
|
|
|
}
|
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
func (miner *Miner) commitTransactions(env *environment, plainTxs, blobTxs *transactionsByPriceAndNonce, interrupt *atomic.Int32) error {
|
2022-01-24 15:19:52 +08:00
|
|
|
gasLimit := env.header.GasLimit
|
|
|
|
if env.gasPool == nil {
|
|
|
|
env.gasPool = new(core.GasPool).AddGas(gasLimit)
|
2018-08-16 19:14:33 +08:00
|
|
|
}
|
|
|
|
for {
|
2022-10-03 20:10:00 +08:00
|
|
|
// Check interruption signal and abort building if it's fired.
|
|
|
|
if interrupt != nil {
|
2023-03-31 14:32:47 +08:00
|
|
|
if signal := interrupt.Load(); signal != commitInterruptNone {
|
2022-10-03 20:10:00 +08:00
|
|
|
return signalToErr(signal)
|
2018-08-22 03:56:54 +08:00
|
|
|
}
|
2018-08-16 19:14:33 +08:00
|
|
|
}
|
2022-10-03 20:10:00 +08:00
|
|
|
// If we don't have enough gas for any further transactions then we're done.
|
2022-01-24 15:19:52 +08:00
|
|
|
if env.gasPool.Gas() < params.TxGas {
|
|
|
|
log.Trace("Not enough gas for further transactions", "have", env.gasPool, "want", params.TxGas)
|
2018-08-16 19:14:33 +08:00
|
|
|
break
|
|
|
|
}
|
2024-02-20 11:37:23 +02:00
|
|
|
// If we don't have enough blob space for any further blob transactions,
|
|
|
|
// skip that list altogether
|
|
|
|
if !blobTxs.Empty() && env.blobs*params.BlobTxBlobGasPerBlob >= params.MaxBlobGasPerBlock {
|
|
|
|
log.Trace("Not enough blob space for further blob transactions")
|
|
|
|
blobTxs.Clear()
|
|
|
|
// Fall though to pick up any plain txs
|
|
|
|
}
|
2022-10-03 20:10:00 +08:00
|
|
|
// Retrieve the next transaction and abort if all done.
|
2024-02-20 11:37:23 +02:00
|
|
|
var (
|
|
|
|
ltx *txpool.LazyTransaction
|
|
|
|
txs *transactionsByPriceAndNonce
|
|
|
|
)
|
|
|
|
pltx, ptip := plainTxs.Peek()
|
|
|
|
bltx, btip := blobTxs.Peek()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case pltx == nil:
|
|
|
|
txs, ltx = blobTxs, bltx
|
|
|
|
case bltx == nil:
|
|
|
|
txs, ltx = plainTxs, pltx
|
|
|
|
default:
|
|
|
|
if ptip.Lt(btip) {
|
|
|
|
txs, ltx = blobTxs, bltx
|
|
|
|
} else {
|
|
|
|
txs, ltx = plainTxs, pltx
|
|
|
|
}
|
|
|
|
}
|
2023-07-27 13:45:35 +03:00
|
|
|
if ltx == nil {
|
2018-08-16 19:14:33 +08:00
|
|
|
break
|
|
|
|
}
|
2023-10-04 12:36:36 +03:00
|
|
|
// If we don't have enough space for the next transaction, skip the account.
|
|
|
|
if env.gasPool.Gas() < ltx.Gas {
|
|
|
|
log.Trace("Not enough gas left for transaction", "hash", ltx.Hash, "left", env.gasPool.Gas(), "needed", ltx.Gas)
|
|
|
|
txs.Pop()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if left := uint64(params.MaxBlobGasPerBlock - env.blobs*params.BlobTxBlobGasPerBlob); left < ltx.BlobGas {
|
|
|
|
log.Trace("Not enough blob gas left for transaction", "hash", ltx.Hash, "left", left, "needed", ltx.BlobGas)
|
|
|
|
txs.Pop()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Transaction seems to fit, pull it up from the pool
|
2023-07-27 13:45:35 +03:00
|
|
|
tx := ltx.Resolve()
|
|
|
|
if tx == nil {
|
2023-10-04 12:36:36 +03:00
|
|
|
log.Trace("Ignoring evicted transaction", "hash", ltx.Hash)
|
2023-07-27 13:45:35 +03:00
|
|
|
txs.Pop()
|
|
|
|
continue
|
|
|
|
}
|
2018-08-16 19:14:33 +08:00
|
|
|
// Error may be ignored here. The error has already been checked
|
2024-04-23 18:06:25 +08:00
|
|
|
// during transaction acceptance in the transaction pool.
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 10:13:34 +02:00
|
|
|
from, _ := types.Sender(env.signer, tx)
|
2022-10-03 20:10:00 +08:00
|
|
|
|
2018-08-16 19:14:33 +08:00
|
|
|
// Check whether the tx is replay protected. If we're not in the EIP155 hf
|
|
|
|
// phase, start ignoring the sender until we do.
|
2024-03-06 13:45:03 +01:00
|
|
|
if tx.Protected() && !miner.chainConfig.IsEIP155(env.header.Number) {
|
|
|
|
log.Trace("Ignoring replay protected transaction", "hash", ltx.Hash, "eip155", miner.chainConfig.EIP155Block)
|
2018-08-16 19:14:33 +08:00
|
|
|
txs.Pop()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Start executing the transaction
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 10:13:34 +02:00
|
|
|
env.state.SetTxContext(tx.Hash(), env.tcount)
|
2018-08-16 19:14:33 +08:00
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
err := miner.commitTransaction(env, tx)
|
2020-12-04 12:22:19 +01:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, core.ErrNonceTooLow):
|
2018-08-16 19:14:33 +08:00
|
|
|
// New head notification data race between the transaction pool and miner, shift
|
2023-10-04 12:36:36 +03:00
|
|
|
log.Trace("Skipping transaction with low nonce", "hash", ltx.Hash, "sender", from, "nonce", tx.Nonce())
|
2018-08-16 19:14:33 +08:00
|
|
|
txs.Shift()
|
|
|
|
|
2020-12-04 12:22:19 +01:00
|
|
|
case errors.Is(err, nil):
|
2018-08-16 19:14:33 +08:00
|
|
|
// Everything ok, collect the logs and shift in the next transaction from the same account
|
|
|
|
txs.Shift()
|
|
|
|
|
|
|
|
default:
|
2023-04-05 19:09:25 +08:00
|
|
|
// Transaction is regarded as invalid, drop all consecutive transactions from
|
|
|
|
// the same sender because of `nonce-too-high` clause.
|
2023-10-04 12:36:36 +03:00
|
|
|
log.Debug("Transaction failed, account skipped", "hash", ltx.Hash, "err", err)
|
2023-04-05 19:09:25 +08:00
|
|
|
txs.Pop()
|
2018-08-16 19:14:33 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-06 11:19:30 +02:00
|
|
|
return nil
|
2018-08-16 19:14:33 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 15:19:52 +08:00
|
|
|
// fillTransactions retrieves the pending transactions from the txpool and fills them
|
|
|
|
// into the given sealing block. The transaction selection and ordering strategy can
|
|
|
|
// be customized with the plugin in the future.
|
2024-03-06 13:45:03 +01:00
|
|
|
func (miner *Miner) fillTransactions(interrupt *atomic.Int32, env *environment) error {
|
|
|
|
miner.confMu.RLock()
|
|
|
|
tip := miner.config.GasPrice
|
|
|
|
miner.confMu.RUnlock()
|
2024-02-17 13:37:14 +02:00
|
|
|
|
|
|
|
// Retrieve the pending transactions pre-filtered by the 1559/4844 dynamic fees
|
2024-02-20 11:37:23 +02:00
|
|
|
filter := txpool.PendingFilter{
|
2024-03-06 13:45:03 +01:00
|
|
|
MinTip: uint256.MustFromBig(tip),
|
2024-02-20 11:37:23 +02:00
|
|
|
}
|
2024-02-17 13:37:14 +02:00
|
|
|
if env.header.BaseFee != nil {
|
2024-02-20 11:37:23 +02:00
|
|
|
filter.BaseFee = uint256.MustFromBig(env.header.BaseFee)
|
2024-02-17 13:37:14 +02:00
|
|
|
}
|
|
|
|
if env.header.ExcessBlobGas != nil {
|
2024-02-20 11:37:23 +02:00
|
|
|
filter.BlobFee = uint256.MustFromBig(eip4844.CalcBlobFee(*env.header.ExcessBlobGas))
|
2024-02-17 13:37:14 +02:00
|
|
|
}
|
2024-02-20 11:37:23 +02:00
|
|
|
filter.OnlyPlainTxs, filter.OnlyBlobTxs = true, false
|
2024-03-06 13:45:03 +01:00
|
|
|
pendingPlainTxs := miner.txpool.Pending(filter)
|
2024-02-20 11:37:23 +02:00
|
|
|
|
|
|
|
filter.OnlyPlainTxs, filter.OnlyBlobTxs = false, true
|
2024-03-06 13:45:03 +01:00
|
|
|
pendingBlobTxs := miner.txpool.Pending(filter)
|
2023-06-16 15:29:40 +03:00
|
|
|
|
2023-08-23 15:16:14 -06:00
|
|
|
// Split the pending transactions into locals and remotes.
|
2024-02-20 11:37:23 +02:00
|
|
|
localPlainTxs, remotePlainTxs := make(map[common.Address][]*txpool.LazyTransaction), pendingPlainTxs
|
|
|
|
localBlobTxs, remoteBlobTxs := make(map[common.Address][]*txpool.LazyTransaction), pendingBlobTxs
|
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
for _, account := range miner.txpool.Locals() {
|
2024-02-20 11:37:23 +02:00
|
|
|
if txs := remotePlainTxs[account]; len(txs) > 0 {
|
|
|
|
delete(remotePlainTxs, account)
|
|
|
|
localPlainTxs[account] = txs
|
|
|
|
}
|
|
|
|
if txs := remoteBlobTxs[account]; len(txs) > 0 {
|
|
|
|
delete(remoteBlobTxs, account)
|
|
|
|
localBlobTxs[account] = txs
|
2018-08-21 20:30:06 +03:00
|
|
|
}
|
|
|
|
}
|
2023-08-23 15:16:14 -06:00
|
|
|
// Fill the block with all available pending transactions.
|
2024-02-20 11:37:23 +02:00
|
|
|
if len(localPlainTxs) > 0 || len(localBlobTxs) > 0 {
|
|
|
|
plainTxs := newTransactionsByPriceAndNonce(env.signer, localPlainTxs, env.header.BaseFee)
|
|
|
|
blobTxs := newTransactionsByPriceAndNonce(env.signer, localBlobTxs, env.header.BaseFee)
|
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
if err := miner.commitTransactions(env, plainTxs, blobTxs, interrupt); err != nil {
|
2022-05-06 11:19:30 +02:00
|
|
|
return err
|
2018-08-21 20:30:06 +03:00
|
|
|
}
|
|
|
|
}
|
2024-02-20 11:37:23 +02:00
|
|
|
if len(remotePlainTxs) > 0 || len(remoteBlobTxs) > 0 {
|
|
|
|
plainTxs := newTransactionsByPriceAndNonce(env.signer, remotePlainTxs, env.header.BaseFee)
|
|
|
|
blobTxs := newTransactionsByPriceAndNonce(env.signer, remoteBlobTxs, env.header.BaseFee)
|
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
if err := miner.commitTransactions(env, plainTxs, blobTxs, interrupt); err != nil {
|
2022-05-06 11:19:30 +02:00
|
|
|
return err
|
2018-08-21 20:30:06 +03:00
|
|
|
}
|
2018-08-16 19:14:33 +08:00
|
|
|
}
|
2022-05-06 11:19:30 +02:00
|
|
|
return nil
|
2018-08-15 19:09:17 +08:00
|
|
|
}
|
|
|
|
|
2022-11-02 17:32:20 +08:00
|
|
|
// totalFees computes total consumed miner fees in Wei. Block transactions and receipts have to have the same order.
|
|
|
|
func totalFees(block *types.Block, receipts []*types.Receipt) *big.Int {
|
2020-07-28 18:16:49 +02:00
|
|
|
feesWei := new(big.Int)
|
|
|
|
for i, tx := range block.Transactions() {
|
2021-06-08 13:05:41 +03:00
|
|
|
minerFee, _ := tx.EffectiveGasTip(block.BaseFee())
|
2021-05-21 09:59:26 +02:00
|
|
|
feesWei.Add(feesWei, new(big.Int).Mul(new(big.Int).SetUint64(receipts[i].GasUsed), minerFee))
|
2020-07-28 18:16:49 +02:00
|
|
|
}
|
2022-11-02 17:32:20 +08:00
|
|
|
return feesWei
|
2020-07-28 18:16:49 +02:00
|
|
|
}
|
2022-10-03 20:10:00 +08:00
|
|
|
|
|
|
|
// signalToErr converts the interruption signal to a concrete error type for return.
|
|
|
|
// The given signal must be a valid interruption signal.
|
|
|
|
func signalToErr(signal int32) error {
|
|
|
|
switch signal {
|
|
|
|
case commitInterruptNewHead:
|
|
|
|
return errBlockInterruptedByNewHead
|
|
|
|
case commitInterruptResubmit:
|
|
|
|
return errBlockInterruptedByRecommit
|
|
|
|
case commitInterruptTimeout:
|
|
|
|
return errBlockInterruptedByTimeout
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("undefined signal %d", signal))
|
|
|
|
}
|
|
|
|
}
|