Skip to content

Commit fcac8c8

Browse files
lightclientDergarcon
authored andcommitted
eth/catalyst: prefix payload id with version (ethereum#28246)
GetPayloadVX should only return payloads which match its version. GetPayloadV2 is a special snowflake that supports v1 and v2 payloads. This change uses a a version-specific prefix within in the payload id, basically a namespace for the version number.
1 parent 48debb4 commit fcac8c8

File tree

5 files changed

+58
-13
lines changed

5 files changed

+58
-13
lines changed

beacon/engine/types.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ import (
2626
"github.com/ethereum/go-ethereum/trie"
2727
)
2828

29+
// PayloadVersion denotes the version of PayloadAttributes used to request the
30+
// building of the payload to commence.
31+
type PayloadVersion byte
32+
33+
var (
34+
PayloadV1 PayloadVersion = 0x1
35+
PayloadV2 PayloadVersion = 0x2
36+
PayloadV3 PayloadVersion = 0x3
37+
)
38+
2939
//go:generate go run github.com/fjl/gencodec -type PayloadAttributes -field-override payloadAttributesMarshaling -out gen_blockparams.go
3040

3141
// PayloadAttributes describes the environment context in which a block should
@@ -115,6 +125,21 @@ type TransitionConfigurationV1 struct {
115125
// PayloadID is an identifier of the payload build process
116126
type PayloadID [8]byte
117127

128+
// Version returns the payload version associated with the identifier.
129+
func (b PayloadID) Version() PayloadVersion {
130+
return PayloadVersion(b[0])
131+
}
132+
133+
// Is returns whether the identifier matches any of provided payload versions.
134+
func (b PayloadID) Is(versions ...PayloadVersion) bool {
135+
for _, v := range versions {
136+
if v == b.Version() {
137+
return true
138+
}
139+
}
140+
return false
141+
}
142+
118143
func (b PayloadID) String() string {
119144
return hexutil.Encode(b[:])
120145
}

eth/catalyst/api.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV1(update engine.ForkchoiceStateV1, pa
180180
return engine.STATUS_INVALID, engine.InvalidParams.With(errors.New("forkChoiceUpdateV1 called post-shanghai"))
181181
}
182182
}
183-
return api.forkchoiceUpdated(update, payloadAttributes, false)
183+
return api.forkchoiceUpdated(update, payloadAttributes, engine.PayloadV1, false)
184184
}
185185

186186
// ForkchoiceUpdatedV2 is equivalent to V1 with the addition of withdrawals in the payload attributes.
@@ -196,7 +196,7 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV2(update engine.ForkchoiceStateV1, pa
196196
return engine.STATUS_INVALID, engine.UnsupportedFork.With(errors.New("forkchoiceUpdatedV2 must only be called for shanghai payloads"))
197197
}
198198
}
199-
return api.forkchoiceUpdated(update, params, false)
199+
return api.forkchoiceUpdated(update, params, engine.PayloadV2, false)
200200
}
201201

202202
// ForkchoiceUpdatedV3 is equivalent to V2 with the addition of parent beacon block root in the payload attributes.
@@ -220,10 +220,10 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV3(update engine.ForkchoiceStateV1, pa
220220
// hash, even if params are wrong. To do this we need to split up
221221
// forkchoiceUpdate into a function that only updates the head and then a
222222
// function that kicks off block construction.
223-
return api.forkchoiceUpdated(update, params, false)
223+
return api.forkchoiceUpdated(update, params, engine.PayloadV3, false)
224224
}
225225

226-
func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payloadAttributes *engine.PayloadAttributes, simulatorMode bool) (engine.ForkChoiceResponse, error) {
226+
func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payloadAttributes *engine.PayloadAttributes, payloadVersion engine.PayloadVersion, simulatorMode bool) (engine.ForkChoiceResponse, error) {
227227
api.forkchoiceLock.Lock()
228228
defer api.forkchoiceLock.Unlock()
229229

@@ -367,6 +367,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
367367
Random: payloadAttributes.Random,
368368
Withdrawals: payloadAttributes.Withdrawals,
369369
BeaconRoot: payloadAttributes.BeaconRoot,
370+
Version: payloadVersion,
370371
}
371372
id := args.Id()
372373
// If we already are busy generating this work, then we do not need
@@ -430,6 +431,9 @@ func (api *ConsensusAPI) ExchangeTransitionConfigurationV1(config engine.Transit
430431

431432
// GetPayloadV1 returns a cached payload by id.
432433
func (api *ConsensusAPI) GetPayloadV1(payloadID engine.PayloadID) (*engine.ExecutableData, error) {
434+
if !payloadID.Is(engine.PayloadV1) {
435+
return nil, engine.UnsupportedFork
436+
}
433437
data, err := api.getPayload(payloadID, false)
434438
if err != nil {
435439
return nil, err
@@ -439,11 +443,17 @@ func (api *ConsensusAPI) GetPayloadV1(payloadID engine.PayloadID) (*engine.Execu
439443

440444
// GetPayloadV2 returns a cached payload by id.
441445
func (api *ConsensusAPI) GetPayloadV2(payloadID engine.PayloadID) (*engine.ExecutionPayloadEnvelope, error) {
446+
if !payloadID.Is(engine.PayloadV1, engine.PayloadV2) {
447+
return nil, engine.UnsupportedFork
448+
}
442449
return api.getPayload(payloadID, false)
443450
}
444451

445452
// GetPayloadV3 returns a cached payload by id.
446453
func (api *ConsensusAPI) GetPayloadV3(payloadID engine.PayloadID) (*engine.ExecutionPayloadEnvelope, error) {
454+
if !payloadID.Is(engine.PayloadV3) {
455+
return nil, engine.UnsupportedFork
456+
}
447457
return api.getPayload(payloadID, false)
448458
}
449459

eth/catalyst/api_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func TestEth2PrepareAndGetPayload(t *testing.T) {
210210
FeeRecipient: blockParams.SuggestedFeeRecipient,
211211
Random: blockParams.Random,
212212
BeaconRoot: blockParams.BeaconRoot,
213+
Version: engine.PayloadV1,
213214
}).Id()
214215
execData, err := api.GetPayloadV1(payloadID)
215216
if err != nil {
@@ -1076,6 +1077,7 @@ func TestWithdrawals(t *testing.T) {
10761077
Random: blockParams.Random,
10771078
Withdrawals: blockParams.Withdrawals,
10781079
BeaconRoot: blockParams.BeaconRoot,
1080+
Version: engine.PayloadV2,
10791081
}).Id()
10801082
execData, err := api.GetPayloadV2(payloadID)
10811083
if err != nil {
@@ -1124,6 +1126,7 @@ func TestWithdrawals(t *testing.T) {
11241126
Random: blockParams.Random,
11251127
Withdrawals: blockParams.Withdrawals,
11261128
BeaconRoot: blockParams.BeaconRoot,
1129+
Version: engine.PayloadV2,
11271130
}).Id()
11281131
execData, err = api.GetPayloadV2(payloadID)
11291132
if err != nil {
@@ -1238,12 +1241,15 @@ func TestNilWithdrawals(t *testing.T) {
12381241

12391242
for _, test := range tests {
12401243
var (
1241-
err error
1242-
shanghai = genesis.Config.IsShanghai(genesis.Config.LondonBlock, test.blockParams.Timestamp)
1244+
err error
1245+
payloadVersion engine.PayloadVersion
1246+
shanghai = genesis.Config.IsShanghai(genesis.Config.LondonBlock, test.blockParams.Timestamp)
12431247
)
12441248
if !shanghai {
1249+
payloadVersion = engine.PayloadV1
12451250
_, err = api.ForkchoiceUpdatedV1(fcState, &test.blockParams)
12461251
} else {
1252+
payloadVersion = engine.PayloadV2
12471253
_, err = api.ForkchoiceUpdatedV2(fcState, &test.blockParams)
12481254
}
12491255
if test.wantErr {
@@ -1262,6 +1268,7 @@ func TestNilWithdrawals(t *testing.T) {
12621268
Timestamp: test.blockParams.Timestamp,
12631269
FeeRecipient: test.blockParams.SuggestedFeeRecipient,
12641270
Random: test.blockParams.Random,
1271+
Version: payloadVersion,
12651272
}).Id()
12661273
execData, err := api.GetPayloadV2(payloadID)
12671274
if err != nil {
@@ -1616,6 +1623,7 @@ func TestParentBeaconBlockRoot(t *testing.T) {
16161623
Random: blockParams.Random,
16171624
Withdrawals: blockParams.Withdrawals,
16181625
BeaconRoot: blockParams.BeaconRoot,
1626+
Version: engine.PayloadV3,
16191627
}).Id()
16201628
execData, err := api.GetPayloadV3(payloadID)
16211629
if err != nil {

eth/catalyst/simulated_beacon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u
160160
SuggestedFeeRecipient: feeRecipient,
161161
Withdrawals: withdrawals,
162162
Random: random,
163-
}, true)
163+
}, engine.PayloadV2, true)
164164
if err != nil {
165165
return err
166166
}

miner/payload_building.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ import (
3535
// Check engine-api specification for more details.
3636
// https://github.com/ethereum/execution-apis/blob/main/src/engine/cancun.md#payloadattributesv3
3737
type BuildPayloadArgs struct {
38-
Parent common.Hash // The parent block to build payload on top
39-
Timestamp uint64 // The provided timestamp of generated payload
40-
FeeRecipient common.Address // The provided recipient address for collecting transaction fee
41-
Random common.Hash // The provided randomness value
42-
Withdrawals types.Withdrawals // The provided withdrawals
43-
BeaconRoot *common.Hash // The provided beaconRoot (Cancun)
38+
Parent common.Hash // The parent block to build payload on top
39+
Timestamp uint64 // The provided timestamp of generated payload
40+
FeeRecipient common.Address // The provided recipient address for collecting transaction fee
41+
Random common.Hash // The provided randomness value
42+
Withdrawals types.Withdrawals // The provided withdrawals
43+
BeaconRoot *common.Hash // The provided beaconRoot (Cancun)
44+
Version engine.PayloadVersion // Versioning byte for payload id calculation.
4445
}
4546

4647
// Id computes an 8-byte identifier by hashing the components of the payload arguments.
@@ -57,6 +58,7 @@ func (args *BuildPayloadArgs) Id() engine.PayloadID {
5758
}
5859
var out engine.PayloadID
5960
copy(out[:], hasher.Sum(nil)[:8])
61+
out[0] = byte(args.Version)
6062
return out
6163
}
6264

0 commit comments

Comments
 (0)