2023-07-06 03:42:34 -05:00
|
|
|
// Copyright 2023 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 catalyst
|
|
|
|
|
|
|
|
import (
|
2023-08-31 13:37:16 -05:00
|
|
|
"crypto/rand"
|
2024-02-29 06:17:32 -06:00
|
|
|
"crypto/sha256"
|
2023-07-06 03:42:34 -05:00
|
|
|
"errors"
|
2024-08-19 16:32:15 -05:00
|
|
|
"fmt"
|
2023-07-06 03:42:34 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/beacon/engine"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-02-20 03:37:23 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2023-07-06 03:42:34 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2024-02-29 06:17:32 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/kzg4844"
|
2023-07-06 03:42:34 -05:00
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2024-08-19 16:32:15 -05:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2023-07-06 03:42:34 -05:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
2023-08-21 12:33:37 -05:00
|
|
|
const devEpochLength = 32
|
|
|
|
|
2023-07-06 03:42:34 -05:00
|
|
|
// withdrawalQueue implements a FIFO queue which holds withdrawals that are
|
|
|
|
// pending inclusion.
|
|
|
|
type withdrawalQueue struct {
|
2024-08-19 16:32:15 -05:00
|
|
|
pending types.Withdrawals
|
|
|
|
mu sync.Mutex
|
|
|
|
feed event.Feed
|
|
|
|
subs event.SubscriptionScope
|
2023-07-06 03:42:34 -05:00
|
|
|
}
|
|
|
|
|
2024-08-19 16:32:15 -05:00
|
|
|
type newWithdrawalsEvent struct{ Withdrawals types.Withdrawals }
|
|
|
|
|
2023-07-06 03:42:34 -05:00
|
|
|
// add queues a withdrawal for future inclusion.
|
|
|
|
func (w *withdrawalQueue) add(withdrawal *types.Withdrawal) error {
|
2024-08-19 16:32:15 -05:00
|
|
|
w.mu.Lock()
|
|
|
|
w.pending = append(w.pending, withdrawal)
|
|
|
|
w.mu.Unlock()
|
|
|
|
|
|
|
|
w.feed.Send(newWithdrawalsEvent{types.Withdrawals{withdrawal}})
|
2023-07-06 03:42:34 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-19 16:32:15 -05:00
|
|
|
// pop dequeues the specified number of withdrawals from the queue.
|
|
|
|
func (w *withdrawalQueue) pop(count int) types.Withdrawals {
|
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
|
|
|
|
count = min(count, len(w.pending))
|
|
|
|
popped := w.pending[0:count]
|
|
|
|
w.pending = w.pending[count:]
|
|
|
|
|
|
|
|
return popped
|
|
|
|
}
|
|
|
|
|
|
|
|
// subscribe allows a listener to be updated when new withdrawals are added to
|
|
|
|
// the queue.
|
|
|
|
func (w *withdrawalQueue) subscribe(ch chan<- newWithdrawalsEvent) event.Subscription {
|
|
|
|
sub := w.feed.Subscribe(ch)
|
|
|
|
return w.subs.Track(sub)
|
2023-07-06 03:42:34 -05:00
|
|
|
}
|
|
|
|
|
2024-08-19 16:32:15 -05:00
|
|
|
// SimulatedBeacon drives an Ethereum instance as if it were a real beacon
|
|
|
|
// client. It can run in period mode where it mines a new block every period
|
|
|
|
// (seconds) or on every transaction via Commit, Fork and AdjustTime.
|
2023-07-06 03:42:34 -05:00
|
|
|
type SimulatedBeacon struct {
|
|
|
|
shutdownCh chan struct{}
|
|
|
|
eth *eth.Ethereum
|
|
|
|
period uint64
|
|
|
|
withdrawals withdrawalQueue
|
|
|
|
|
|
|
|
feeRecipient common.Address
|
|
|
|
feeRecipientLock sync.Mutex // lock gates concurrent access to the feeRecipient
|
|
|
|
|
|
|
|
engineAPI *ConsensusAPI
|
|
|
|
curForkchoiceState engine.ForkchoiceStateV1
|
|
|
|
lastBlockTime uint64
|
|
|
|
}
|
|
|
|
|
2024-01-10 09:45:08 -06:00
|
|
|
// NewSimulatedBeacon constructs a new simulated beacon chain.
|
2023-07-06 03:42:34 -05:00
|
|
|
func NewSimulatedBeacon(period uint64, eth *eth.Ethereum) (*SimulatedBeacon, error) {
|
|
|
|
block := eth.BlockChain().CurrentBlock()
|
|
|
|
current := engine.ForkchoiceStateV1{
|
|
|
|
HeadBlockHash: block.Hash(),
|
|
|
|
SafeBlockHash: block.Hash(),
|
|
|
|
FinalizedBlockHash: block.Hash(),
|
|
|
|
}
|
2023-08-24 03:48:09 -05:00
|
|
|
engineAPI := newConsensusAPIWithoutHeartbeat(eth)
|
2023-07-06 03:42:34 -05:00
|
|
|
|
|
|
|
// if genesis block, send forkchoiceUpdated to trigger transition to PoS
|
|
|
|
if block.Number.Sign() == 0 {
|
2024-09-04 07:33:51 -05:00
|
|
|
if _, err := engineAPI.ForkchoiceUpdatedV3(current, nil); err != nil {
|
2023-07-06 03:42:34 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &SimulatedBeacon{
|
|
|
|
eth: eth,
|
|
|
|
period: period,
|
|
|
|
shutdownCh: make(chan struct{}),
|
|
|
|
engineAPI: engineAPI,
|
|
|
|
lastBlockTime: block.Time,
|
|
|
|
curForkchoiceState: current,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SimulatedBeacon) setFeeRecipient(feeRecipient common.Address) {
|
|
|
|
c.feeRecipientLock.Lock()
|
|
|
|
c.feeRecipient = feeRecipient
|
|
|
|
c.feeRecipientLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start invokes the SimulatedBeacon life-cycle function in a goroutine.
|
|
|
|
func (c *SimulatedBeacon) Start() error {
|
|
|
|
if c.period == 0 {
|
2024-01-10 09:45:08 -06:00
|
|
|
// if period is set to 0, do not mine at all
|
|
|
|
// this is used in the simulated backend where blocks
|
|
|
|
// are explicitly mined via Commit, AdjustTime and Fork
|
2023-07-06 03:42:34 -05:00
|
|
|
} else {
|
|
|
|
go c.loop()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop halts the SimulatedBeacon service.
|
|
|
|
func (c *SimulatedBeacon) Stop() error {
|
|
|
|
close(c.shutdownCh)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sealBlock initiates payload building for a new block and creates a new block
|
|
|
|
// with the completed payload.
|
2024-01-10 09:45:08 -06:00
|
|
|
func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp uint64) error {
|
|
|
|
if timestamp <= c.lastBlockTime {
|
|
|
|
timestamp = c.lastBlockTime + 1
|
2023-07-06 03:42:34 -05:00
|
|
|
}
|
|
|
|
c.feeRecipientLock.Lock()
|
|
|
|
feeRecipient := c.feeRecipient
|
|
|
|
c.feeRecipientLock.Unlock()
|
|
|
|
|
2023-08-25 14:38:27 -05:00
|
|
|
// Reset to CurrentBlock in case of the chain was rewound
|
|
|
|
if header := c.eth.BlockChain().CurrentBlock(); c.curForkchoiceState.HeadBlockHash != header.Hash() {
|
|
|
|
finalizedHash := c.finalizedBlockHash(header.Number.Uint64())
|
|
|
|
c.setCurrentState(header.Hash(), *finalizedHash)
|
|
|
|
}
|
|
|
|
|
2024-08-19 16:32:15 -05:00
|
|
|
// Because transaction insertion, block insertion, and block production will
|
|
|
|
// happen without any timing delay between them in simulator mode and the
|
|
|
|
// transaction pool will be running its internal reset operation on a
|
|
|
|
// background thread, flaky executions can happen. To avoid the racey
|
|
|
|
// behavior, the pool will be explicitly blocked on its reset before
|
|
|
|
// continuing to the block production below.
|
|
|
|
if err := c.eth.APIBackend.TxPool().Sync(); err != nil {
|
|
|
|
return fmt.Errorf("failed to sync txpool: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-31 13:37:16 -05:00
|
|
|
var random [32]byte
|
|
|
|
rand.Read(random[:])
|
2024-01-23 13:59:38 -06:00
|
|
|
fcResponse, err := c.engineAPI.forkchoiceUpdated(c.curForkchoiceState, &engine.PayloadAttributes{
|
2024-01-10 09:45:08 -06:00
|
|
|
Timestamp: timestamp,
|
2023-07-06 03:42:34 -05:00
|
|
|
SuggestedFeeRecipient: feeRecipient,
|
|
|
|
Withdrawals: withdrawals,
|
2023-08-31 13:37:16 -05:00
|
|
|
Random: random,
|
2024-02-29 06:17:32 -06:00
|
|
|
BeaconRoot: &common.Hash{},
|
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
|
|
|
}, engine.PayloadV3, false)
|
2023-07-06 03:42:34 -05:00
|
|
|
if err != nil {
|
2023-08-25 14:38:27 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fcResponse == engine.STATUS_SYNCING {
|
|
|
|
return errors.New("chain rewind prevented invocation of payload creation")
|
2023-07-06 03:42:34 -05:00
|
|
|
}
|
2024-08-19 16:32:15 -05:00
|
|
|
|
2023-08-23 07:14:04 -05:00
|
|
|
envelope, err := c.engineAPI.getPayload(*fcResponse.PayloadID, true)
|
2023-07-06 03:42:34 -05:00
|
|
|
if err != nil {
|
2023-08-25 14:38:27 -05:00
|
|
|
return err
|
2023-07-06 03:42:34 -05:00
|
|
|
}
|
|
|
|
payload := envelope.ExecutionPayload
|
|
|
|
|
2023-08-21 12:33:37 -05:00
|
|
|
var finalizedHash common.Hash
|
|
|
|
if payload.Number%devEpochLength == 0 {
|
|
|
|
finalizedHash = payload.BlockHash
|
|
|
|
} else {
|
2023-08-25 14:38:27 -05:00
|
|
|
if fh := c.finalizedBlockHash(payload.Number); fh == nil {
|
|
|
|
return errors.New("chain rewind interrupted calculation of finalized block hash")
|
|
|
|
} else {
|
|
|
|
finalizedHash = *fh
|
|
|
|
}
|
2023-08-21 12:33:37 -05:00
|
|
|
}
|
|
|
|
|
2024-02-29 06:17:32 -06:00
|
|
|
// Independently calculate the blob hashes from sidecars.
|
|
|
|
blobHashes := make([]common.Hash, 0)
|
|
|
|
if envelope.BlobsBundle != nil {
|
|
|
|
hasher := sha256.New()
|
|
|
|
for _, commit := range envelope.BlobsBundle.Commitments {
|
|
|
|
var c kzg4844.Commitment
|
|
|
|
if len(commit) != len(c) {
|
|
|
|
return errors.New("invalid commitment length")
|
|
|
|
}
|
|
|
|
copy(c[:], commit)
|
|
|
|
blobHashes = append(blobHashes, kzg4844.CalcBlobHashV1(hasher, &c))
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 14:38:27 -05:00
|
|
|
// Mark the payload as canon
|
2024-02-29 06:17:32 -06:00
|
|
|
if _, err = c.engineAPI.NewPayloadV3(*payload, blobHashes, &common.Hash{}); err != nil {
|
2023-08-25 14:38:27 -05:00
|
|
|
return err
|
2023-07-06 03:42:34 -05:00
|
|
|
}
|
2023-08-25 14:38:27 -05:00
|
|
|
c.setCurrentState(payload.BlockHash, finalizedHash)
|
2024-01-10 09:45:08 -06:00
|
|
|
|
2023-08-25 14:38:27 -05:00
|
|
|
// Mark the block containing the payload as canonical
|
2024-09-04 07:33:51 -05:00
|
|
|
if _, err = c.engineAPI.ForkchoiceUpdatedV3(c.curForkchoiceState, nil); err != nil {
|
2023-08-25 14:38:27 -05:00
|
|
|
return err
|
2023-07-06 03:42:34 -05:00
|
|
|
}
|
|
|
|
c.lastBlockTime = payload.Timestamp
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-25 14:38:27 -05:00
|
|
|
// loop runs the block production loop for non-zero period configuration
|
2023-07-06 03:42:34 -05:00
|
|
|
func (c *SimulatedBeacon) loop() {
|
|
|
|
timer := time.NewTimer(0)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.shutdownCh:
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
2024-08-19 16:32:15 -05:00
|
|
|
if err := c.sealBlock(c.withdrawals.pop(10), uint64(time.Now().Unix())); err != nil {
|
2023-08-25 14:38:27 -05:00
|
|
|
log.Warn("Error performing sealing work", "err", err)
|
|
|
|
} else {
|
|
|
|
timer.Reset(time.Second * time.Duration(c.period))
|
2023-07-06 03:42:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 09:45:08 -06:00
|
|
|
// finalizedBlockHash returns the block hash of the finalized block corresponding
|
|
|
|
// to the given number or nil if doesn't exist in the chain.
|
2023-08-25 14:38:27 -05:00
|
|
|
func (c *SimulatedBeacon) finalizedBlockHash(number uint64) *common.Hash {
|
|
|
|
var finalizedNumber uint64
|
|
|
|
if number%devEpochLength == 0 {
|
|
|
|
finalizedNumber = number
|
|
|
|
} else {
|
|
|
|
finalizedNumber = (number - 1) / devEpochLength * devEpochLength
|
|
|
|
}
|
|
|
|
if finalizedBlock := c.eth.BlockChain().GetBlockByNumber(finalizedNumber); finalizedBlock != nil {
|
|
|
|
fh := finalizedBlock.Hash()
|
|
|
|
return &fh
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setCurrentState sets the current forkchoice state
|
|
|
|
func (c *SimulatedBeacon) setCurrentState(headHash, finalizedHash common.Hash) {
|
|
|
|
c.curForkchoiceState = engine.ForkchoiceStateV1{
|
|
|
|
HeadBlockHash: headHash,
|
|
|
|
SafeBlockHash: headHash,
|
|
|
|
FinalizedBlockHash: finalizedHash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 09:45:08 -06:00
|
|
|
// Commit seals a block on demand.
|
|
|
|
func (c *SimulatedBeacon) Commit() common.Hash {
|
2024-08-19 16:32:15 -05:00
|
|
|
withdrawals := c.withdrawals.pop(10)
|
2024-01-10 09:45:08 -06:00
|
|
|
if err := c.sealBlock(withdrawals, uint64(time.Now().Unix())); err != nil {
|
|
|
|
log.Warn("Error performing sealing work", "err", err)
|
|
|
|
}
|
|
|
|
return c.eth.BlockChain().CurrentBlock().Hash()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback un-sends previously added transactions.
|
|
|
|
func (c *SimulatedBeacon) Rollback() {
|
2024-09-26 04:08:36 -05:00
|
|
|
c.eth.TxPool().FlushAllTransactions()
|
2024-01-10 09:45:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fork sets the head to the provided hash.
|
|
|
|
func (c *SimulatedBeacon) Fork(parentHash common.Hash) error {
|
2024-05-29 08:56:52 -05:00
|
|
|
// Ensure no pending transactions.
|
|
|
|
c.eth.TxPool().Sync()
|
2024-02-20 03:37:23 -06:00
|
|
|
if len(c.eth.TxPool().Pending(txpool.PendingFilter{})) != 0 {
|
2024-01-10 09:45:08 -06:00
|
|
|
return errors.New("pending block dirty")
|
|
|
|
}
|
2024-05-29 08:56:52 -05:00
|
|
|
|
2024-01-10 09:45:08 -06:00
|
|
|
parent := c.eth.BlockChain().GetBlockByHash(parentHash)
|
|
|
|
if parent == nil {
|
|
|
|
return errors.New("parent not found")
|
|
|
|
}
|
|
|
|
return c.eth.BlockChain().SetHead(parent.NumberU64())
|
|
|
|
}
|
|
|
|
|
|
|
|
// AdjustTime creates a new block with an adjusted timestamp.
|
|
|
|
func (c *SimulatedBeacon) AdjustTime(adjustment time.Duration) error {
|
2024-02-20 03:37:23 -06:00
|
|
|
if len(c.eth.TxPool().Pending(txpool.PendingFilter{})) != 0 {
|
2024-01-10 09:45:08 -06:00
|
|
|
return errors.New("could not adjust time on non-empty block")
|
|
|
|
}
|
|
|
|
parent := c.eth.BlockChain().CurrentBlock()
|
|
|
|
if parent == nil {
|
|
|
|
return errors.New("parent not found")
|
|
|
|
}
|
2024-08-19 16:32:15 -05:00
|
|
|
withdrawals := c.withdrawals.pop(10)
|
2024-07-16 04:47:11 -05:00
|
|
|
return c.sealBlock(withdrawals, parent.Time+uint64(adjustment/time.Second))
|
2024-01-10 09:45:08 -06:00
|
|
|
}
|
|
|
|
|
2024-08-19 16:32:15 -05:00
|
|
|
// RegisterSimulatedBeaconAPIs registers the simulated beacon's API with the
|
|
|
|
// stack.
|
2023-07-06 03:42:34 -05:00
|
|
|
func RegisterSimulatedBeaconAPIs(stack *node.Node, sim *SimulatedBeacon) {
|
2024-08-19 16:32:15 -05:00
|
|
|
api := newSimulatedBeaconAPI(sim)
|
2023-07-06 03:42:34 -05:00
|
|
|
stack.RegisterAPIs([]rpc.API{
|
|
|
|
{
|
|
|
|
Namespace: "dev",
|
2024-01-10 09:45:08 -06:00
|
|
|
Service: api,
|
2023-07-06 03:42:34 -05:00
|
|
|
Version: "1.0",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|