Skip to content

Commit dab746b

Browse files
eth/catalyst: support earlier forks in SimulatedBeacon (#31084)
Co-authored-by: Marius van der Wijden <[email protected]>
1 parent ef00a6e commit dab746b

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

eth/catalyst/simulated_beacon.go

+40-14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"github.com/ethereum/go-ethereum/event"
3434
"github.com/ethereum/go-ethereum/log"
3535
"github.com/ethereum/go-ethereum/node"
36+
"github.com/ethereum/go-ethereum/params"
37+
"github.com/ethereum/go-ethereum/params/forks"
3638
"github.com/ethereum/go-ethereum/rpc"
3739
)
3840

@@ -95,6 +97,16 @@ type SimulatedBeacon struct {
9597
lastBlockTime uint64
9698
}
9799

100+
func payloadVersion(config *params.ChainConfig, time uint64) engine.PayloadVersion {
101+
switch config.LatestFork(time) {
102+
case forks.Prague, forks.Cancun:
103+
return engine.PayloadV3
104+
case forks.Paris, forks.Shanghai:
105+
return engine.PayloadV2
106+
}
107+
panic("invalid fork, simulated beacon needs to be started post-merge")
108+
}
109+
98110
// NewSimulatedBeacon constructs a new simulated beacon chain.
99111
func NewSimulatedBeacon(period uint64, eth *eth.Ethereum) (*SimulatedBeacon, error) {
100112
block := eth.BlockChain().CurrentBlock()
@@ -107,7 +119,8 @@ func NewSimulatedBeacon(period uint64, eth *eth.Ethereum) (*SimulatedBeacon, err
107119

108120
// if genesis block, send forkchoiceUpdated to trigger transition to PoS
109121
if block.Number.Sign() == 0 {
110-
if _, err := engineAPI.ForkchoiceUpdatedV3(current, nil); err != nil {
122+
version := payloadVersion(eth.BlockChain().Config(), block.Time)
123+
if _, err := engineAPI.forkchoiceUpdated(current, nil, version, false); err != nil {
111124
return nil, err
112125
}
113126
}
@@ -171,6 +184,8 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u
171184
return fmt.Errorf("failed to sync txpool: %w", err)
172185
}
173186

187+
version := payloadVersion(c.eth.BlockChain().Config(), timestamp)
188+
174189
var random [32]byte
175190
rand.Read(random[:])
176191
fcResponse, err := c.engineAPI.forkchoiceUpdated(c.curForkchoiceState, &engine.PayloadAttributes{
@@ -179,7 +194,7 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u
179194
Withdrawals: withdrawals,
180195
Random: random,
181196
BeaconRoot: &common.Hash{},
182-
}, engine.PayloadV3, false)
197+
}, version, false)
183198
if err != nil {
184199
return err
185200
}
@@ -204,28 +219,39 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u
204219
}
205220
}
206221

207-
// Independently calculate the blob hashes from sidecars.
208-
blobHashes := make([]common.Hash, 0)
209-
if envelope.BlobsBundle != nil {
210-
hasher := sha256.New()
211-
for _, commit := range envelope.BlobsBundle.Commitments {
212-
var c kzg4844.Commitment
213-
if len(commit) != len(c) {
214-
return errors.New("invalid commitment length")
222+
var (
223+
blobHashes []common.Hash
224+
beaconRoot *common.Hash
225+
requests [][]byte
226+
)
227+
// Compute post-shanghai fields
228+
if version > engine.PayloadV2 {
229+
// Independently calculate the blob hashes from sidecars.
230+
blobHashes = make([]common.Hash, 0)
231+
if envelope.BlobsBundle != nil {
232+
hasher := sha256.New()
233+
for _, commit := range envelope.BlobsBundle.Commitments {
234+
var c kzg4844.Commitment
235+
if len(commit) != len(c) {
236+
return errors.New("invalid commitment length")
237+
}
238+
copy(c[:], commit)
239+
blobHashes = append(blobHashes, kzg4844.CalcBlobHashV1(hasher, &c))
215240
}
216-
copy(c[:], commit)
217-
blobHashes = append(blobHashes, kzg4844.CalcBlobHashV1(hasher, &c))
218241
}
242+
beaconRoot = &common.Hash{}
243+
requests = envelope.Requests
219244
}
245+
220246
// Mark the payload as canon
221-
_, err = c.engineAPI.newPayload(*payload, blobHashes, &common.Hash{}, envelope.Requests, false)
247+
_, err = c.engineAPI.newPayload(*payload, blobHashes, beaconRoot, requests, false)
222248
if err != nil {
223249
return err
224250
}
225251
c.setCurrentState(payload.BlockHash, finalizedHash)
226252

227253
// Mark the block containing the payload as canonical
228-
if _, err = c.engineAPI.ForkchoiceUpdatedV3(c.curForkchoiceState, nil); err != nil {
254+
if _, err = c.engineAPI.forkchoiceUpdated(c.curForkchoiceState, nil, version, false); err != nil {
229255
return err
230256
}
231257
c.lastBlockTime = payload.Timestamp

0 commit comments

Comments
 (0)