Skip to content

Commit f0d666a

Browse files
holimanfjl
authored andcommitted
beacon/engine, eth/catalyst, miner: add beaconroot to miner and CL api surface
miner, beacon/engine: process systemtx, update json tag
1 parent 60ec41c commit f0d666a

File tree

8 files changed

+49
-2
lines changed

8 files changed

+49
-2
lines changed

beacon/engine/gen_blockparams.go

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

beacon/engine/gen_ed.go

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

beacon/engine/types.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type PayloadAttributes struct {
3535
Random common.Hash `json:"prevRandao" gencodec:"required"`
3636
SuggestedFeeRecipient common.Address `json:"suggestedFeeRecipient" gencodec:"required"`
3737
Withdrawals []*types.Withdrawal `json:"withdrawals"`
38+
BeaconRoot *common.Hash `json:"parentBeaconBlockRoot"`
3839
}
3940

4041
// JSON type overrides for PayloadAttributes.
@@ -63,6 +64,7 @@ type ExecutableData struct {
6364
Withdrawals []*types.Withdrawal `json:"withdrawals"`
6465
BlobGasUsed *uint64 `json:"blobGasUsed"`
6566
ExcessBlobGas *uint64 `json:"excessBlobGas"`
67+
BeaconRoot *common.Hash `json:"beaconRoot"`
6668
}
6769

6870
// JSON type overrides for executableData.
@@ -225,7 +227,7 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash)
225227
WithdrawalsHash: withdrawalsRoot,
226228
ExcessBlobGas: params.ExcessBlobGas,
227229
BlobGasUsed: params.BlobGasUsed,
228-
// TODO BeaconRoot
230+
BeaconRoot: params.BeaconRoot,
229231
}
230232
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */).WithWithdrawals(params.Withdrawals)
231233
if block.Hash() != params.BlockHash {
@@ -255,7 +257,7 @@ func BlockToExecutableData(block *types.Block, fees *big.Int, sidecars []*types.
255257
Withdrawals: block.Withdrawals(),
256258
BlobGasUsed: block.BlobGasUsed(),
257259
ExcessBlobGas: block.ExcessBlobGas(),
258-
// TODO BeaconRoot
260+
BeaconRoot: block.BeaconRoot(),
259261
}
260262
bundle := BlobsBundleV1{
261263
Commitments: make([]hexutil.Bytes, 0),

eth/catalyst/api.go

+4
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
345345
FeeRecipient: payloadAttributes.SuggestedFeeRecipient,
346346
Random: payloadAttributes.Random,
347347
Withdrawals: payloadAttributes.Withdrawals,
348+
BeaconRoot: payloadAttributes.BeaconRoot,
348349
}
349350
id := args.Id()
350351
// If we already are busy generating this work, then we do not need
@@ -457,6 +458,9 @@ func (api *ConsensusAPI) NewPayloadV3(params engine.ExecutableData, versionedHas
457458
if versionedHashes != nil {
458459
hashes = *versionedHashes
459460
}
461+
if params.BeaconRoot == nil {
462+
return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("nil beaconRoot post-cancun"))
463+
}
460464
return api.newPayload(params, hashes)
461465
}
462466

eth/catalyst/api_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func TestEth2PrepareAndGetPayload(t *testing.T) {
204204
Timestamp: blockParams.Timestamp,
205205
FeeRecipient: blockParams.SuggestedFeeRecipient,
206206
Random: blockParams.Random,
207+
BeaconRoot: blockParams.BeaconRoot,
207208
}).Id()
208209
execData, err := api.GetPayloadV1(payloadID)
209210
if err != nil {
@@ -667,6 +668,7 @@ func assembleBlock(api *ConsensusAPI, parentHash common.Hash, params *engine.Pay
667668
FeeRecipient: params.SuggestedFeeRecipient,
668669
Random: params.Random,
669670
Withdrawals: params.Withdrawals,
671+
BeaconRoot: params.BeaconRoot,
670672
}
671673
payload, err := api.eth.Miner().BuildPayload(args)
672674
if err != nil {
@@ -1068,6 +1070,7 @@ func TestWithdrawals(t *testing.T) {
10681070
FeeRecipient: blockParams.SuggestedFeeRecipient,
10691071
Random: blockParams.Random,
10701072
Withdrawals: blockParams.Withdrawals,
1073+
BeaconRoot: blockParams.BeaconRoot,
10711074
}).Id()
10721075
execData, err := api.GetPayloadV2(payloadID)
10731076
if err != nil {
@@ -1115,6 +1118,7 @@ func TestWithdrawals(t *testing.T) {
11151118
FeeRecipient: blockParams.SuggestedFeeRecipient,
11161119
Random: blockParams.Random,
11171120
Withdrawals: blockParams.Withdrawals,
1121+
BeaconRoot: blockParams.BeaconRoot,
11181122
}).Id()
11191123
execData, err = api.GetPayloadV2(payloadID)
11201124
if err != nil {
@@ -1245,6 +1249,7 @@ func TestNilWithdrawals(t *testing.T) {
12451249
Timestamp: test.blockParams.Timestamp,
12461250
FeeRecipient: test.blockParams.SuggestedFeeRecipient,
12471251
Random: test.blockParams.Random,
1252+
BeaconRoot: test.blockParams.BeaconRoot,
12481253
}).Id()
12491254
execData, err := api.GetPayloadV2(payloadID)
12501255
if err != nil {

miner/payload_building.go

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type BuildPayloadArgs struct {
4040
FeeRecipient common.Address // The provided recipient address for collecting transaction fee
4141
Random common.Hash // The provided randomness value
4242
Withdrawals types.Withdrawals // The provided withdrawals
43+
BeaconRoot *common.Hash // The provided beaconRoot (Cancun)
4344
}
4445

4546
// Id computes an 8-byte identifier by hashing the components of the payload arguments.
@@ -51,6 +52,9 @@ func (args *BuildPayloadArgs) Id() engine.PayloadID {
5152
hasher.Write(args.Random[:])
5253
hasher.Write(args.FeeRecipient[:])
5354
rlp.Encode(hasher, args.Withdrawals)
55+
if args.BeaconRoot != nil {
56+
hasher.Write(args.BeaconRoot[:])
57+
}
5458
var out engine.PayloadID
5559
copy(out[:], hasher.Sum(nil)[:8])
5660
return out
@@ -182,6 +186,7 @@ func (w *worker) buildPayload(args *BuildPayloadArgs) (*Payload, error) {
182186
coinbase: args.FeeRecipient,
183187
random: args.Random,
184188
withdrawals: args.Withdrawals,
189+
beaconRoot: args.BeaconRoot,
185190
noTxs: true,
186191
}
187192
empty := w.getSealingBlock(emptyParams)
@@ -212,6 +217,7 @@ func (w *worker) buildPayload(args *BuildPayloadArgs) (*Payload, error) {
212217
coinbase: args.FeeRecipient,
213218
random: args.Random,
214219
withdrawals: args.Withdrawals,
220+
beaconRoot: args.BeaconRoot,
215221
noTxs: false,
216222
}
217223

miner/worker.go

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/ethereum/go-ethereum/core/state"
3333
"github.com/ethereum/go-ethereum/core/txpool"
3434
"github.com/ethereum/go-ethereum/core/types"
35+
"github.com/ethereum/go-ethereum/core/vm"
3536
"github.com/ethereum/go-ethereum/event"
3637
"github.com/ethereum/go-ethereum/log"
3738
"github.com/ethereum/go-ethereum/params"
@@ -860,6 +861,7 @@ type generateParams struct {
860861
coinbase common.Address // The fee recipient address for including transaction
861862
random common.Hash // The randomness generated by beacon chain, empty before the merge
862863
withdrawals types.Withdrawals // List of withdrawals to include in block.
864+
beaconRoot *common.Hash // The beacon root (cancun field).
863865
noTxs bool // Flag whether an empty block without any transaction is expected
864866
}
865867

@@ -912,6 +914,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
912914
header.GasLimit = core.CalcGasLimit(parentGasLimit, w.config.GasCeil)
913915
}
914916
}
917+
// Apply EIP-4844, EIP-4788.
915918
if w.chainConfig.IsCancun(header.Number, header.Time) {
916919
var excessBlobGas uint64
917920
if w.chainConfig.IsCancun(parent.Number, parent.Time) {
@@ -921,6 +924,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
921924
excessBlobGas = eip4844.CalcExcessBlobGas(0, 0)
922925
}
923926
header.ExcessBlobGas = &excessBlobGas
927+
header.BeaconRoot = genParams.beaconRoot
924928
}
925929
// Run the consensus preparation with the default or customized consensus engine.
926930
if err := w.engine.Prepare(w.chain, header); err != nil {
@@ -935,6 +939,18 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
935939
log.Error("Failed to create sealing context", "err", err)
936940
return nil, err
937941
}
942+
if w.chainConfig.IsCancun(header.Number, header.Time) {
943+
if header.BeaconRoot == nil {
944+
// Failure: we are unable to construct a block if we do not have
945+
// the beacon root.
946+
err = errors.New("missing parentBeaconRoot")
947+
log.Error("Failed to construct block", "err", err)
948+
return nil, err
949+
}
950+
context := core.NewEVMBlockContext(header, w.chain, nil)
951+
vmenv := vm.NewEVM(context, vm.TxContext{}, env.state, w.chainConfig, vm.Config{})
952+
core.ProcessBeaconBlockRoot(*header.BeaconRoot, vmenv, env.state)
953+
}
938954
return env, nil
939955
}
940956

miner/worker_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co
458458
coinbase: c.coinbase,
459459
random: c.random,
460460
withdrawals: nil,
461+
beaconRoot: nil,
461462
noTxs: false,
462463
forceTime: true,
463464
})
@@ -482,6 +483,7 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co
482483
coinbase: c.coinbase,
483484
random: c.random,
484485
withdrawals: nil,
486+
beaconRoot: nil,
485487
noTxs: false,
486488
forceTime: true,
487489
})

0 commit comments

Comments
 (0)