forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 15
Remove Block RLP hooks #304
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
StephenButtolph
wants to merge
24
commits into
main
Choose a base branch
from
StephenButtolph/remove-block-rlp-hook
base: main
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
24 commits
Select commit
Hold shift + click to select a range
c96078f
AI slop
StephenButtolph 17a2dbc
wip simplify
StephenButtolph 0331e83
wip simplify
StephenButtolph 9516b73
wip simplify
StephenButtolph d97b5b7
wip
StephenButtolph 332ff9b
nit
StephenButtolph 54e9ebb
nit
StephenButtolph e2ff812
nit
StephenButtolph c63a15e
nit
StephenButtolph 03c95af
nit
StephenButtolph 9e05728
nits
StephenButtolph 50ec497
Merge branch 'main' into StephenButtolph/remove-block-rlp-hook
StephenButtolph 76e0af5
nit
StephenButtolph c681534
Merge branch 'StephenButtolph/remove-block-rlp-hook' of github.com:av…
StephenButtolph 8f27da7
nit
StephenButtolph 73c8af6
nit
StephenButtolph d9378ad
nit
StephenButtolph b9fdfd4
nit
StephenButtolph dbdbcfa
rename
StephenButtolph ececab1
add comment
StephenButtolph 161b4eb
reuse helpers
StephenButtolph fea5b35
Stop using NewBlock
StephenButtolph c7aa4e5
simplify
StephenButtolph d34e7ce
prefer typed values
StephenButtolph 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
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 |
|---|---|---|
|
|
@@ -207,13 +207,27 @@ func TestHeaderHooks(t *testing.T) { | |
|
|
||
| type blockPayload struct { | ||
| NOOPBlockBodyHooks | ||
| x int | ||
| x uint64 | ||
| } | ||
|
|
||
| func (p *blockPayload) Copy() *blockPayload { | ||
| return &blockPayload{x: p.x} | ||
| } | ||
|
|
||
| func (p *blockPayload) BodyRLPFieldsForEncoding(b *Body) *rlp.Fields { | ||
| return &rlp.Fields{ | ||
| Required: []any{b.Transactions, b.Uncles, p.x}, | ||
| Optional: []any{b.Withdrawals}, | ||
| } | ||
| } | ||
|
|
||
| func (p *blockPayload) BodyRLPFieldPointersForDecoding(b *Body) *rlp.Fields { | ||
| return &rlp.Fields{ | ||
| Required: []any{&b.Transactions, &b.Uncles, &p.x}, | ||
| Optional: []any{&b.Withdrawals}, | ||
| } | ||
| } | ||
|
|
||
| func TestBlockWithX(t *testing.T) { | ||
| TestOnlyClearRegisteredExtras() | ||
| t.Cleanup(TestOnlyClearRegisteredExtras) | ||
|
|
@@ -224,15 +238,15 @@ func TestBlockWithX(t *testing.T) { | |
| struct{}, | ||
| ]() | ||
|
|
||
| typ := reflect.TypeOf(&Block{}) | ||
| typ := reflect.TypeFor[*Block]() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not required - but my IDE lints these... So figured I might as well modernize this while I was here. |
||
| for i := 0; i < typ.NumMethod(); i++ { | ||
| method := typ.Method(i).Name | ||
| if method == "Withdrawals" || !strings.HasPrefix(method, "With") { | ||
| continue | ||
| } | ||
|
|
||
| block := NewBlockWithHeader(&Header{}) | ||
| const initialPayload = int(42) | ||
| const initialPayload uint64 = 42 | ||
| payload := &blockPayload{ | ||
| x: initialPayload, | ||
| } | ||
|
|
@@ -272,3 +286,160 @@ func TestBlockWithX(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestBodyExtraRoundTrip demonstrates that the body extra's round-trip | ||
| // correctly through RLP serialization. | ||
| func TestBodyExtraRoundTrip(t *testing.T) { | ||
| TestOnlyClearRegisteredExtras() | ||
| t.Cleanup(TestOnlyClearRegisteredExtras) | ||
|
|
||
| extras := RegisterExtras[ | ||
| NOOPHeaderHooks, *NOOPHeaderHooks, | ||
| blockPayload, *blockPayload, | ||
| struct{}, | ||
| ]() | ||
|
|
||
| rng := ethtest.NewPseudoRand(142857) | ||
| wantBlock := NewBlockWithHeader(newHeader(rng)) | ||
| want := extras.Block.Get(wantBlock) | ||
| want.x = rng.Uint64() | ||
|
|
||
| b, err := rlp.EncodeToBytes(wantBlock) | ||
| require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", wantBlock) | ||
|
|
||
| gotBlock := new(Block) | ||
| require.NoErrorf(t, rlp.DecodeBytes(b, gotBlock), "rlp.DecodeBytes(rlp.EncodeToBytes(%T), %T)", wantBlock, gotBlock) | ||
| got := extras.Block.Get(gotBlock) | ||
| assert.Equalf(t, want, got, "%T payload after RLP round trip", got) | ||
| } | ||
|
|
||
| // newHeader returns a [Header] with randomly populated fields. | ||
| func newHeader(rng *ethtest.PseudoRand) *Header { | ||
| return &Header{ | ||
| ParentHash: rng.Hash(), | ||
| UncleHash: rng.Hash(), | ||
| Coinbase: rng.Address(), | ||
| Root: rng.Hash(), | ||
| TxHash: rng.Hash(), | ||
| ReceiptHash: rng.Hash(), | ||
| Bloom: rng.Bloom(), | ||
| Difficulty: rng.BigUint64(), | ||
| Number: rng.BigUint64(), | ||
| GasLimit: rng.Uint64(), | ||
| GasUsed: rng.Uint64(), | ||
| Time: rng.Uint64(), | ||
| Extra: rng.Bytes(32), | ||
| MixDigest: rng.Hash(), | ||
| Nonce: rng.BlockNonce(), | ||
| BaseFee: rng.BigUint64(), | ||
| } | ||
| } | ||
|
|
||
| // bodySize describes the number of items in the [Body] of a test case. | ||
| type bodySize struct { | ||
| txs, uncles, withdrawals int | ||
| } | ||
|
|
||
| // bodySizes are the [Body] shapes covered by [FuzzBlockBytes] seeds and by | ||
| // [BenchmarkBlockBytes]. They cover every combination of empty and non-empty | ||
| // fields, the last of which is optional in RLP. | ||
| var bodySizes = []bodySize{ | ||
| {txs: 0, uncles: 0, withdrawals: 0}, | ||
| {txs: 1, uncles: 0, withdrawals: 0}, | ||
| {txs: 0, uncles: 1, withdrawals: 0}, | ||
| {txs: 0, uncles: 0, withdrawals: 1}, | ||
| {txs: 10, uncles: 0, withdrawals: 0}, | ||
| {txs: 10, uncles: 2, withdrawals: 4}, | ||
| {txs: 100, uncles: 0, withdrawals: 0}, | ||
| {txs: 100, uncles: 2, withdrawals: 16}, | ||
| } | ||
|
|
||
| // newBody returns a [Body] with randomly populated fields, holding the number | ||
| // of items described by `size`. | ||
| func newBody(rng *ethtest.PseudoRand, size bodySize) *Body { | ||
| body := &Body{} | ||
| for range size.txs { | ||
| body.Transactions = append(body.Transactions, NewTx(&LegacyTx{ | ||
| Nonce: rng.Uint64(), | ||
| GasPrice: rng.BigUint64(), | ||
| Gas: rng.Uint64(), | ||
| To: rng.AddressPtr(), | ||
| Value: rng.BigUint64(), | ||
| Data: rng.Bytes(64), | ||
| })) | ||
| } | ||
| for range size.uncles { | ||
| body.Uncles = append(body.Uncles, newHeader(rng)) | ||
| } | ||
| for range size.withdrawals { | ||
| body.Withdrawals = append(body.Withdrawals, &Withdrawal{ | ||
| Index: rng.Uint64(), | ||
| Validator: rng.Uint64(), | ||
| Address: rng.Address(), | ||
| Amount: rng.Uint64(), | ||
| }) | ||
| } | ||
|
StephenButtolph marked this conversation as resolved.
|
||
| return body | ||
| } | ||
|
|
||
| // encodeRLP RLP-encodes `v`, failing the test if it can't be encoded. | ||
| func encodeRLP(tb testing.TB, v any) []byte { | ||
| tb.Helper() | ||
| b, err := rlp.EncodeToBytes(v) | ||
| require.NoErrorf(tb, err, "rlp.EncodeToBytes(%T)", v) | ||
| return b | ||
| } | ||
|
|
||
| // referenceBlockBytes is the reference implementation against which | ||
| // [BlockBytes] is tested and benchmarked. | ||
| func referenceBlockBytes(headerBytes, bodyBytes []byte) ([]byte, error) { | ||
| header := new(Header) | ||
| if err := rlp.DecodeBytes(headerBytes, header); err != nil { | ||
| return nil, err | ||
| } | ||
| body := new(Body) | ||
| if err := rlp.DecodeBytes(bodyBytes, body); err != nil { | ||
| return nil, err | ||
| } | ||
| block := NewBlockWithHeader(header). | ||
| WithBody(*body). | ||
| WithWithdrawals(body.Withdrawals) | ||
| return rlp.EncodeToBytes(block) | ||
| } | ||
|
|
||
| // FuzzBlockBytes demonstrates that [BlockBytes] is equivalent to | ||
| // [referenceBlockBytes] for all inputs that the latter accepts. The seed corpus | ||
| // covers every shape in [bodySizes]. | ||
| func FuzzBlockBytes(f *testing.F) { | ||
| rng := ethtest.NewPseudoRand(20250806) | ||
| for _, size := range bodySizes { | ||
| f.Add( | ||
| encodeRLP(f, newHeader(rng)), | ||
| encodeRLP(f, newBody(rng, size)), | ||
| ) | ||
| } | ||
|
|
||
| f.Fuzz(func(t *testing.T, headerBytes, bodyBytes []byte) { | ||
| want, err := referenceBlockBytes(headerBytes, bodyBytes) | ||
| if err != nil { | ||
| t.Skip("invalid input bytes") | ||
| } | ||
|
|
||
| got, err := BlockBytes(headerBytes, bodyBytes) | ||
| require.NoError(t, err, "BlockBytes()") | ||
| assert.Equal(t, want, got, "referenceBlockBytes() == BlockBytes()") | ||
| }) | ||
| } | ||
|
|
||
| func BenchmarkBlockBytes(b *testing.B) { | ||
| for _, size := range bodySizes { | ||
| rng := ethtest.NewPseudoRand(2718281828) | ||
| headerBytes := encodeRLP(b, newHeader(rng)) | ||
| bodyBytes := encodeRLP(b, newBody(rng, size)) | ||
| b.Run(fmt.Sprintf("%d_txs_%d_uncles_%d_withdrawals", size.txs, size.uncles, size.withdrawals), func(b *testing.B) { | ||
| for b.Loop() { | ||
| _, _ = BlockBytes(headerBytes, bodyBytes) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was really the annoying part. It's kind of weird, since I don't think an implementation should actually use this... But I think we should guarantee that it is provided correctly. Thoughts on simplifying this @ARR4N?