-
Notifications
You must be signed in to change notification settings - Fork 159
chore(core/types): header JSON and RLP serialization hooks (4) #746
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
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
ce204e6
chore(core/types): header hooks JSON and RLP serialization
qdm12 aa2e380
Add `TestCopyHeader`
qdm12 0ef64fb
Add TestHeaderExtraGetWith
qdm12 94ccf1d
Add `TestHeaderSerializable_updates`
qdm12 2c672c2
`TestHeaderExtraRLP`
qdm12 96f13ba
TestHeaderExtraJSON
qdm12 0bac977
Add comments on exported symbols
qdm12 70124de
Remove test import on libevm/pseudo
qdm12 cbd644a
Simplify changes in Block.ExtDataGasUsed()
qdm12 4dbb36e
Simplify changes in Block.BlockGasCost()
qdm12 59cbb97
Apply PR review suggestions
qdm12 0ecd53e
WithHeaderExtra -> SetHeaderExtra
qdm12 15638ec
Add missing SetHeaderExtra calls in plugin/evm/header tests
qdm12 718e9f7
plugin/evm/header: add missing extra nil checks
qdm12 d58c459
Minor improvements (feedback)
qdm12 12811e5
Chery pick ARR4N commit with modifications
ARR4N 4ae3869
PR feedback round 3
qdm12 7a05828
scripts/lint_allowed_eth_imports.sh: use `-name` instead of `-path` f…
qdm12 aefb9c1
PR feedback nits
qdm12 089432e
Remove core/types from scripts/eth-allowed-packages.txt
qdm12 3b9ef2a
Add comment to HeaderSerializable.Hash method
qdm12 728cbfe
Re-generate JSON for HeaderSerializable
qdm12 aab9d7e
Rename generated codec files
qdm12 56b296b
Add note on custom generators used to support type aliases
qdm12 a7f9917
remove vmerrs (#829)
ceyonur 4d2f143
Simplify assertDifferentPointers to not rely on testify/assert
qdm12 57a6734
Revert to using assertNonZero function in `allExportedFieldsSet`
qdm12 d360c2c
Improve `testHeaderEncodeDecode` signature definition
qdm12 949ce6d
Merge branch 'libevm' into libevm-upstream-types
qdm12 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
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,108 @@ | ||
// (c) 2025, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package types | ||
|
||
import ( | ||
"math/big" | ||
"reflect" | ||
"testing" | ||
"unsafe" | ||
|
||
"github.com/ava-labs/libevm/common" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCopyHeader(t *testing.T) { | ||
qdm12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Parallel() | ||
|
||
t.Run("empty_header", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
empty := &Header{} | ||
|
||
headerExtra := &HeaderExtra{} | ||
extras.Header.Set(empty, headerExtra) | ||
|
||
cpy := CopyHeader(empty) | ||
|
||
want := &Header{ | ||
Difficulty: new(big.Int), | ||
qdm12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Number: new(big.Int), | ||
} | ||
|
||
headerExtra = &HeaderExtra{} | ||
extras.Header.Set(want, headerExtra) | ||
|
||
assert.Equal(t, want, cpy) | ||
}) | ||
|
||
t.Run("filled_header", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
header, _ := headerWithNonZeroFields() // the header carries the [HeaderExtra] so we can ignore it | ||
|
||
gotHeader := CopyHeader(header) | ||
gotExtra := GetHeaderExtra(gotHeader) | ||
|
||
wantHeader, wantExtra := headerWithNonZeroFields() | ||
assert.Equal(t, wantHeader, gotHeader) | ||
assert.Equal(t, wantExtra, gotExtra) | ||
|
||
exportedFieldsPointToDifferentMemory(t, header, gotHeader) | ||
exportedFieldsPointToDifferentMemory(t, GetHeaderExtra(header), gotExtra) | ||
}) | ||
} | ||
|
||
func exportedFieldsPointToDifferentMemory[T interface { | ||
Header | HeaderExtra | ||
}](t *testing.T, original, cpy *T) { | ||
t.Helper() | ||
|
||
v := reflect.ValueOf(*original) | ||
typ := v.Type() | ||
cp := reflect.ValueOf(*cpy) | ||
for i := range v.NumField() { | ||
field := typ.Field(i) | ||
if !field.IsExported() { | ||
continue | ||
} | ||
switch field.Type.Kind() { | ||
case reflect.Array, reflect.Uint64: | ||
// Not pointers, but using explicit Kinds for safety | ||
continue | ||
} | ||
|
||
t.Run(field.Name, func(t *testing.T) { | ||
darioush marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fieldCp := cp.Field(i).Interface() | ||
switch f := v.Field(i).Interface().(type) { | ||
case *big.Int: | ||
assertDifferentPointers(t, f, fieldCp) | ||
case *common.Hash: | ||
assertDifferentPointers(t, f, fieldCp) | ||
case *uint64: | ||
assertDifferentPointers(t, f, fieldCp) | ||
case []uint8: | ||
assertDifferentPointers(t, unsafe.SliceData(f), unsafe.SliceData(fieldCp.([]uint8))) | ||
default: | ||
t.Errorf("field %q type %T needs to be added to switch cases of exportedFieldsDeepCopied", field.Name, f) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// assertDifferentPointers asserts that `a` and `b` are both non-nil | ||
// pointers pointing to different memory locations. | ||
func assertDifferentPointers[T any](t *testing.T, a *T, b any) { | ||
t.Helper() | ||
switch { | ||
case a == nil: | ||
t.Errorf("a (%T) cannot be nil", a) | ||
case b == nil: | ||
t.Errorf("b (%T) cannot be nil", b) | ||
case a == b: | ||
t.Errorf("pointers to same memory") | ||
} | ||
// Note: no need to check `b` is of the same type as `a`, otherwise | ||
// the memory address would be different as well. | ||
ARR4N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.