-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core/types): header JSON and RLP serialization hooks (#746)
- Implement `HeaderExtra` RLP and JSON serialization methods - remove `BlockNonce` - remove `EncodeNonce` - new functions `GetHeaderExtra` and `SetHeaderExtra` - Migrate existing custom `Header` to `HeaderSerializable` in block_ext.go with only `Hash` method for RLP code generation - Rename files gen_header_json.go to gen_header_serializable_json.go - Rename files gen_header_rlp.go to gen_header_serializable_rlp.go Signed-off-by: Quentin McGaw <quentin.mcgaw@gmail.com> Co-authored-by: Quentin Mc Gaw <quentin.mcgaw@avalabs.org> Co-authored-by: Arran Schlosberg <519948+ARR4N@users.noreply.github.com> Co-authored-by: Ceyhun Onur <ceyhun.onur@avalabs.org>
- Loading branch information
1 parent
6448d1c
commit 0657877
Showing
21 changed files
with
650 additions
and
247 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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) { | ||
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), | ||
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) { | ||
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. | ||
} |
Oops, something went wrong.