2022-11-02 17:32:20 +08:00
|
|
|
// Copyright 2022 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
|
|
|
package miner
|
|
|
|
|
|
|
|
import (
|
2024-03-06 13:45:03 +01:00
|
|
|
"math/big"
|
2022-11-02 17:32:20 +08:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-02-06 16:37:58 +01:00
|
|
|
"github.com/ethereum/go-ethereum/beacon/engine"
|
2022-11-02 17:32:20 +08:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-03-06 13:45:03 +01:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/clique"
|
2022-11-02 17:32:20 +08:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2024-03-06 13:45:03 +01:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2022-11-02 17:32:20 +08:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2024-03-06 13:45:03 +01:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
|
|
|
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
|
2023-02-07 19:16:53 +01:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2024-03-06 13:45:03 +01:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2022-11-02 17:32:20 +08:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
2024-03-06 13:45:03 +01:00
|
|
|
var (
|
|
|
|
// Test chain configurations
|
|
|
|
testTxPoolConfig legacypool.Config
|
|
|
|
ethashChainConfig *params.ChainConfig
|
|
|
|
cliqueChainConfig *params.ChainConfig
|
|
|
|
|
|
|
|
// Test accounts
|
|
|
|
testBankKey, _ = crypto.GenerateKey()
|
|
|
|
testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey)
|
|
|
|
testBankFunds = big.NewInt(1000000000000000000)
|
|
|
|
|
|
|
|
testUserKey, _ = crypto.GenerateKey()
|
|
|
|
testUserAddress = crypto.PubkeyToAddress(testUserKey.PublicKey)
|
|
|
|
|
|
|
|
// Test transactions
|
|
|
|
pendingTxs []*types.Transaction
|
|
|
|
newTxs []*types.Transaction
|
|
|
|
|
|
|
|
testConfig = Config{
|
|
|
|
PendingFeeRecipient: testBankAddress,
|
|
|
|
Recommit: time.Second,
|
|
|
|
GasCeil: params.GenesisGasLimit,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
testTxPoolConfig = legacypool.DefaultConfig
|
|
|
|
testTxPoolConfig.Journal = ""
|
|
|
|
ethashChainConfig = new(params.ChainConfig)
|
|
|
|
*ethashChainConfig = *params.TestChainConfig
|
|
|
|
cliqueChainConfig = new(params.ChainConfig)
|
|
|
|
*cliqueChainConfig = *params.TestChainConfig
|
|
|
|
cliqueChainConfig.Clique = ¶ms.CliqueConfig{
|
|
|
|
Period: 10,
|
|
|
|
Epoch: 30000,
|
|
|
|
}
|
|
|
|
|
|
|
|
signer := types.LatestSigner(params.TestChainConfig)
|
|
|
|
tx1 := types.MustSignNewTx(testBankKey, signer, &types.AccessListTx{
|
|
|
|
ChainID: params.TestChainConfig.ChainID,
|
|
|
|
Nonce: 0,
|
|
|
|
To: &testUserAddress,
|
|
|
|
Value: big.NewInt(1000),
|
|
|
|
Gas: params.TxGas,
|
|
|
|
GasPrice: big.NewInt(params.InitialBaseFee),
|
|
|
|
})
|
|
|
|
pendingTxs = append(pendingTxs, tx1)
|
|
|
|
|
|
|
|
tx2 := types.MustSignNewTx(testBankKey, signer, &types.LegacyTx{
|
|
|
|
Nonce: 1,
|
|
|
|
To: &testUserAddress,
|
|
|
|
Value: big.NewInt(1000),
|
|
|
|
Gas: params.TxGas,
|
|
|
|
GasPrice: big.NewInt(params.InitialBaseFee),
|
|
|
|
})
|
|
|
|
newTxs = append(newTxs, tx2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testWorkerBackend implements worker.Backend interfaces and wraps all information needed during the testing.
|
|
|
|
type testWorkerBackend struct {
|
|
|
|
db ethdb.Database
|
|
|
|
txPool *txpool.TxPool
|
|
|
|
chain *core.BlockChain
|
|
|
|
genesis *core.Genesis
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, n int) *testWorkerBackend {
|
|
|
|
var gspec = &core.Genesis{
|
|
|
|
Config: chainConfig,
|
|
|
|
Alloc: types.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
|
|
|
|
}
|
|
|
|
switch e := engine.(type) {
|
|
|
|
case *clique.Clique:
|
|
|
|
gspec.ExtraData = make([]byte, 32+common.AddressLength+crypto.SignatureLength)
|
|
|
|
copy(gspec.ExtraData[32:32+common.AddressLength], testBankAddress.Bytes())
|
2024-10-21 09:24:28 +03:00
|
|
|
e.Authorize(testBankAddress)
|
2024-03-06 13:45:03 +01:00
|
|
|
case *ethash.Ethash:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected consensus engine type: %T", engine)
|
|
|
|
}
|
2024-09-04 15:03:06 +02:00
|
|
|
chain, err := core.NewBlockChain(db, &core.CacheConfig{TrieDirtyDisabled: true}, gspec, nil, engine, vm.Config{}, nil)
|
2024-03-06 13:45:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("core.NewBlockChain failed: %v", err)
|
|
|
|
}
|
|
|
|
pool := legacypool.New(testTxPoolConfig, chain)
|
|
|
|
txpool, _ := txpool.New(testTxPoolConfig.PriceLimit, chain, []txpool.SubPool{pool})
|
|
|
|
|
|
|
|
return &testWorkerBackend{
|
|
|
|
db: db,
|
|
|
|
chain: chain,
|
|
|
|
txPool: txpool,
|
|
|
|
genesis: gspec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *testWorkerBackend) BlockChain() *core.BlockChain { return b.chain }
|
|
|
|
func (b *testWorkerBackend) TxPool() *txpool.TxPool { return b.txPool }
|
|
|
|
|
|
|
|
func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, blocks int) (*Miner, *testWorkerBackend) {
|
|
|
|
backend := newTestWorkerBackend(t, chainConfig, engine, db, blocks)
|
2024-05-27 19:42:07 +08:00
|
|
|
backend.txPool.Add(pendingTxs, true, true)
|
2024-03-06 13:45:03 +01:00
|
|
|
w := New(backend, testConfig, engine)
|
|
|
|
return w, backend
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:32:20 +08:00
|
|
|
func TestBuildPayload(t *testing.T) {
|
|
|
|
var (
|
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
|
recipient = common.HexToAddress("0xdeadbeef")
|
|
|
|
)
|
|
|
|
w, b := newTestWorker(t, params.TestChainConfig, ethash.NewFaker(), db, 0)
|
|
|
|
|
|
|
|
timestamp := uint64(time.Now().Unix())
|
|
|
|
args := &BuildPayloadArgs{
|
|
|
|
Parent: b.chain.CurrentBlock().Hash(),
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Random: common.Hash{},
|
|
|
|
FeeRecipient: recipient,
|
|
|
|
}
|
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
|
|
|
payload, err := w.buildPayload(args, false)
|
2022-11-02 17:32:20 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to build payload %v", err)
|
|
|
|
}
|
2023-02-06 16:37:58 +01:00
|
|
|
verify := func(outer *engine.ExecutionPayloadEnvelope, txs int) {
|
2023-01-25 15:32:25 +01:00
|
|
|
payload := outer.ExecutionPayload
|
|
|
|
if payload.ParentHash != b.chain.CurrentBlock().Hash() {
|
2024-01-17 18:44:01 +08:00
|
|
|
t.Fatal("Unexpected parent hash")
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
2023-01-25 15:32:25 +01:00
|
|
|
if payload.Random != (common.Hash{}) {
|
2024-01-17 18:44:01 +08:00
|
|
|
t.Fatal("Unexpected random value")
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
2023-01-25 15:32:25 +01:00
|
|
|
if payload.Timestamp != timestamp {
|
2024-01-17 18:44:01 +08:00
|
|
|
t.Fatal("Unexpected timestamp")
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
2023-01-25 15:32:25 +01:00
|
|
|
if payload.FeeRecipient != recipient {
|
2024-01-17 18:44:01 +08:00
|
|
|
t.Fatal("Unexpected fee recipient")
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
2023-01-25 15:32:25 +01:00
|
|
|
if len(payload.Transactions) != txs {
|
2024-01-17 18:44:01 +08:00
|
|
|
t.Fatal("Unexpected transaction set")
|
2022-11-02 17:32:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
empty := payload.ResolveEmpty()
|
|
|
|
verify(empty, 0)
|
|
|
|
|
|
|
|
full := payload.ResolveFull()
|
|
|
|
verify(full, len(pendingTxs))
|
|
|
|
|
|
|
|
// Ensure resolve can be called multiple times and the
|
|
|
|
// result should be unchanged
|
|
|
|
dataOne := payload.Resolve()
|
|
|
|
dataTwo := payload.Resolve()
|
|
|
|
if !reflect.DeepEqual(dataOne, dataTwo) {
|
|
|
|
t.Fatal("Unexpected payload data")
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 19:16:53 +01:00
|
|
|
|
|
|
|
func TestPayloadId(t *testing.T) {
|
2023-11-15 15:46:32 +03:00
|
|
|
t.Parallel()
|
2023-02-07 19:16:53 +01:00
|
|
|
ids := make(map[string]int)
|
|
|
|
for i, tt := range []*BuildPayloadArgs{
|
2023-07-13 14:55:31 +08:00
|
|
|
{
|
2023-02-07 19:16:53 +01:00
|
|
|
Parent: common.Hash{1},
|
|
|
|
Timestamp: 1,
|
|
|
|
Random: common.Hash{0x1},
|
|
|
|
FeeRecipient: common.Address{0x1},
|
|
|
|
},
|
|
|
|
// Different parent
|
2023-07-13 14:55:31 +08:00
|
|
|
{
|
2023-02-07 19:16:53 +01:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 1,
|
|
|
|
Random: common.Hash{0x1},
|
|
|
|
FeeRecipient: common.Address{0x1},
|
|
|
|
},
|
|
|
|
// Different timestamp
|
2023-07-13 14:55:31 +08:00
|
|
|
{
|
2023-02-07 19:16:53 +01:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 2,
|
|
|
|
Random: common.Hash{0x1},
|
|
|
|
FeeRecipient: common.Address{0x1},
|
|
|
|
},
|
|
|
|
// Different Random
|
2023-07-13 14:55:31 +08:00
|
|
|
{
|
2023-02-07 19:16:53 +01:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 2,
|
|
|
|
Random: common.Hash{0x2},
|
|
|
|
FeeRecipient: common.Address{0x1},
|
|
|
|
},
|
|
|
|
// Different fee-recipient
|
2023-07-13 14:55:31 +08:00
|
|
|
{
|
2023-02-07 19:16:53 +01:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 2,
|
|
|
|
Random: common.Hash{0x2},
|
|
|
|
FeeRecipient: common.Address{0x2},
|
|
|
|
},
|
|
|
|
// Different withdrawals (non-empty)
|
2023-07-13 14:55:31 +08:00
|
|
|
{
|
2023-02-07 19:16:53 +01:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 2,
|
|
|
|
Random: common.Hash{0x2},
|
|
|
|
FeeRecipient: common.Address{0x2},
|
|
|
|
Withdrawals: []*types.Withdrawal{
|
2023-07-13 14:55:31 +08:00
|
|
|
{
|
2023-02-07 19:16:53 +01:00
|
|
|
Index: 0,
|
|
|
|
Validator: 0,
|
|
|
|
Address: common.Address{},
|
|
|
|
Amount: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Different withdrawals (non-empty)
|
2023-07-13 14:55:31 +08:00
|
|
|
{
|
2023-02-07 19:16:53 +01:00
|
|
|
Parent: common.Hash{2},
|
|
|
|
Timestamp: 2,
|
|
|
|
Random: common.Hash{0x2},
|
|
|
|
FeeRecipient: common.Address{0x2},
|
|
|
|
Withdrawals: []*types.Withdrawal{
|
2023-07-13 14:55:31 +08:00
|
|
|
{
|
2023-02-07 19:16:53 +01:00
|
|
|
Index: 2,
|
|
|
|
Validator: 0,
|
|
|
|
Address: common.Address{},
|
|
|
|
Amount: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
id := tt.Id().String()
|
|
|
|
if prev, exists := ids[id]; exists {
|
|
|
|
t.Errorf("ID collision, case %d and case %d: id %v", prev, i, id)
|
|
|
|
}
|
|
|
|
ids[id] = i
|
|
|
|
}
|
|
|
|
}
|