-
Notifications
You must be signed in to change notification settings - Fork 865
catalyst/api: centralize OPStack validation into helper functions #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
geoknee
wants to merge
7
commits into
optimism
Choose a base branch
from
gk/568
base: optimism
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c4f5a94
catalyist/api: centralize OPStack validation into helper functions
geoknee d24ba1e
add test coverage for optimism validation checks
geoknee 003cd64
lint
geoknee c266ce2
use engine.InvalidPayloadAttributes.With() for all failed optimism FC…
geoknee 8daaba7
typos
geoknee 6dd6374
Merge branch 'optimism' into gk/568
geoknee d84dde2
Merge branch 'optimism' into gk/568
geoknee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package catalyst | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/ethereum/go-ethereum/beacon/engine" | ||
"github.com/ethereum/go-ethereum/consensus/misc/eip1559" | ||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
// checkOptimismPayload performs Optimism-specific checks on the payload data (called during [(*ConsensusAPI).newPayload]). | ||
func checkOptimismPayload(params engine.ExecutableData, cfg *params.ChainConfig) error { | ||
// Holocene | ||
if cfg.IsHolocene(params.Timestamp) { | ||
if err := eip1559.ValidateHoloceneExtraData(params.ExtraData); err != nil { | ||
return err | ||
} | ||
} else { | ||
if len(params.ExtraData) > 0 { | ||
return errors.New("extraData must be empty before Holocene") | ||
} | ||
} | ||
|
||
// Isthmus | ||
if cfg.IsIsthmus(params.Timestamp) { | ||
if params.Withdrawals == nil || len(params.Withdrawals) != 0 { | ||
return errors.New("non-empty or nil withdrawals post-isthmus") | ||
} | ||
if params.WithdrawalsRoot == nil { | ||
return errors.New("nil withdrawalsRoot post-isthmus") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// checkOptimismPayloadAttributes performs Optimism-specific checks on the payload attributes (called during [(*ConsensusAPI).forkChoiceUpdated]. | ||
func checkOptimismPayloadAttributes(payloadAttributes *engine.PayloadAttributes, cfg *params.ChainConfig) error { | ||
if payloadAttributes.GasLimit == nil { | ||
return errors.New("gasLimit parameter is required") | ||
} | ||
|
||
// Holocene | ||
if cfg.IsHolocene(payloadAttributes.Timestamp) { | ||
if err := eip1559.ValidateHolocene1559Params(payloadAttributes.EIP1559Params); err != nil { | ||
return err | ||
} | ||
} else if len(payloadAttributes.EIP1559Params) != 0 { | ||
return errors.New("eip155Params not supported prior to Holocene upgrade") | ||
} | ||
|
||
// Isthmus | ||
if cfg.IsIsthmus(payloadAttributes.Timestamp) { | ||
if payloadAttributes.Withdrawals == nil || len(payloadAttributes.Withdrawals) != 0 { | ||
return errors.New("non-empty or nil withdrawals post-isthmus") | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package catalyst | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/beacon/engine" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/params" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func preHolocene() *params.ChainConfig { | ||
cfg := new(params.ChainConfig) | ||
return cfg | ||
} | ||
|
||
func postHolocene() *params.ChainConfig { | ||
cfg := new(params.ChainConfig) | ||
cfg.HoloceneTime = new(uint64) | ||
return cfg | ||
} | ||
|
||
func postIsthmus() *params.ChainConfig { | ||
cfg := new(params.ChainConfig) | ||
cfg.HoloceneTime = new(uint64) | ||
cfg.IsthmusTime = new(uint64) | ||
return cfg | ||
} | ||
|
||
var valid1559Params = []byte{0, 1, 2, 3, 4, 5, 6, 7} | ||
var validExtraData = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8} | ||
|
||
func TestCheckOptimismPayload(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
params engine.ExecutableData | ||
cfg *params.ChainConfig | ||
expected error | ||
}{ | ||
{ | ||
name: "valid payload pre-Holocene", | ||
params: engine.ExecutableData{ | ||
Timestamp: 0, | ||
ExtraData: []byte{}, | ||
}, | ||
cfg: preHolocene(), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "invalid payload pre-Holocene with extraData", | ||
params: engine.ExecutableData{ | ||
Timestamp: 0, | ||
ExtraData: []byte{1, 2, 3}, | ||
}, | ||
cfg: preHolocene(), | ||
expected: errors.New("extraData must be empty before Holocene"), | ||
}, | ||
{ | ||
name: "invalid payload pre-Holocene with extraData", | ||
params: engine.ExecutableData{ | ||
Timestamp: 0, | ||
ExtraData: []byte{1, 2, 3}, | ||
}, | ||
cfg: postHolocene(), | ||
expected: errors.New("holocene extraData should be 9 bytes, got 3"), | ||
}, | ||
{ | ||
name: "valid payload post-Holocene with extraData", | ||
params: engine.ExecutableData{ | ||
Timestamp: 0, | ||
ExtraData: validExtraData, | ||
}, | ||
cfg: postHolocene(), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "nil withdrawals post-isthmus", | ||
params: engine.ExecutableData{ | ||
Timestamp: 0, | ||
Withdrawals: nil, | ||
ExtraData: validExtraData, | ||
}, | ||
cfg: postIsthmus(), | ||
expected: errors.New("non-empty or nil withdrawals post-isthmus"), | ||
}, | ||
{ | ||
name: "non-empty withdrawals post-isthmus", | ||
params: engine.ExecutableData{ | ||
Timestamp: 0, | ||
Withdrawals: make([]*types.Withdrawal, 1), | ||
ExtraData: validExtraData, | ||
}, | ||
cfg: postIsthmus(), | ||
expected: errors.New("non-empty or nil withdrawals post-isthmus"), | ||
}, | ||
{ | ||
name: "nil withdrawals root post-isthmus", | ||
params: engine.ExecutableData{ | ||
Timestamp: 0, | ||
Withdrawals: make([]*types.Withdrawal, 0), | ||
ExtraData: validExtraData, | ||
}, | ||
cfg: postIsthmus(), | ||
expected: errors.New("nil withdrawalsRoot post-isthmus"), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := checkOptimismPayload(test.params, test.cfg) | ||
if test.expected == nil { | ||
require.NoError(t, err) | ||
} else { | ||
require.EqualError(t, err, test.expected.Error()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCheckOptimismPayloadAttributes(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
payloadAttributes *engine.PayloadAttributes | ||
cfg *params.ChainConfig | ||
expected error | ||
}{ | ||
{ | ||
name: "valid payload attributes pre-Holocene", | ||
payloadAttributes: &engine.PayloadAttributes{ | ||
Timestamp: 0, | ||
GasLimit: new(uint64), | ||
}, | ||
cfg: preHolocene(), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "invalid payload attributes pre-Holocene with gasLimit", | ||
payloadAttributes: &engine.PayloadAttributes{ | ||
Timestamp: 0, | ||
GasLimit: nil, | ||
}, | ||
cfg: preHolocene(), | ||
expected: errors.New("gasLimit parameter is required"), | ||
}, | ||
{ | ||
name: "invalid payload attributes pre-Holocene with eip1559Params", | ||
payloadAttributes: &engine.PayloadAttributes{ | ||
Timestamp: 0, | ||
GasLimit: new(uint64), | ||
EIP1559Params: valid1559Params, | ||
}, | ||
cfg: preHolocene(), | ||
expected: errors.New("eip155Params not supported prior to Holocene upgrade"), | ||
}, | ||
{ | ||
name: "valid payload attributes post-Holocene with gasLimit", | ||
payloadAttributes: &engine.PayloadAttributes{ | ||
Timestamp: 0, | ||
GasLimit: new(uint64), | ||
EIP1559Params: valid1559Params, | ||
}, | ||
cfg: postHolocene(), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "non-empty withdrawals post-isthmus", | ||
payloadAttributes: &engine.PayloadAttributes{ | ||
Timestamp: 0, | ||
GasLimit: new(uint64), | ||
EIP1559Params: valid1559Params, | ||
Withdrawals: make([]*types.Withdrawal, 1), | ||
}, | ||
cfg: postIsthmus(), | ||
expected: errors.New("non-empty or nil withdrawals post-isthmus"), | ||
}, | ||
{ | ||
name: "nil withdrawals post-isthmus", | ||
payloadAttributes: &engine.PayloadAttributes{ | ||
Timestamp: 0, | ||
GasLimit: new(uint64), | ||
EIP1559Params: valid1559Params, | ||
Withdrawals: nil, | ||
}, | ||
cfg: postIsthmus(), | ||
expected: errors.New("non-empty or nil withdrawals post-isthmus"), | ||
}, | ||
{ | ||
name: "empty withdrawals post-isthmus", | ||
payloadAttributes: &engine.PayloadAttributes{ | ||
Timestamp: 0, | ||
GasLimit: new(uint64), | ||
EIP1559Params: valid1559Params, | ||
Withdrawals: make([]*types.Withdrawal, 0), | ||
}, | ||
cfg: postIsthmus(), | ||
expected: nil, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := checkOptimismPayloadAttributes(test.payloadAttributes, test.cfg) | ||
if test.expected == nil { | ||
require.NoError(t, err) | ||
} else { | ||
require.EqualError(t, err, test.expected.Error()) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.