-
Couldn't load subscription status.
- Fork 5
refactor(core/types): simplify Body RLP override
#120
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
Changes from 8 commits
7103781
ddfca36
d4c671b
b3f6c5b
c6f199d
6e350cf
c44b1d9
64d39ca
13e3e76
b9eeaca
f0045f4
ef97380
95e2a91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,8 +116,8 @@ func TestBodyRLPBackwardsCompatibility(t *testing.T) { | |
| newHdr := func(hashLow byte) *Header { return &Header{ParentHash: common.Hash{hashLow}} } | ||
| newWithdraw := func(idx uint64) *Withdrawal { return &Withdrawal{Index: idx} } | ||
|
|
||
| // We build up test-case [Body] instances from the power set of each of | ||
| // these components. | ||
| // We build up test-case [Body] instances from the Cartesian product of each | ||
| // of these components. | ||
| txMatrix := [][]*Transaction{ | ||
| nil, {}, // Must be equivalent for non-optional field | ||
| {newTx(1)}, | ||
|
|
@@ -197,35 +197,27 @@ type cChainBodyExtras struct { | |
|
|
||
| var _ BodyHooks = (*cChainBodyExtras)(nil) | ||
|
|
||
| func (e *cChainBodyExtras) AppendRLPFields(b rlp.EncoderBuffer, _ bool) error { | ||
| b.WriteUint64(uint64(e.Version)) | ||
|
|
||
| var data []byte | ||
| if e.ExtData != nil { | ||
| data = *e.ExtData | ||
| } | ||
| b.WriteBytes(data) | ||
|
|
||
| return nil | ||
| func (e *cChainBodyExtras) RLPFieldsForEncoding(b *Body) ([]any, []any) { | ||
ARR4N marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // The Avalanche C-Chain uses all of the geth required fields (but none of | ||
| // the optional ones) so there's no need to explicitly list them. This | ||
| // pattern might not be ideal for readability but is used here for | ||
| // demonstrative purposes. | ||
| // | ||
| // All new fields will always be tagged as optional for backwards | ||
| // compatibility so this is safe to do, but only for the required fields. | ||
| req, _ /*drop all optional*/ := NOOPBodyHooks{}.RLPFieldsForEncoding(b) | ||
| return append(req, e.Version, e.ExtData), nil | ||
| } | ||
|
|
||
| func (e *cChainBodyExtras) DecodeExtraRLPFields(s *rlp.Stream) error { | ||
| if err := s.Decode(&e.Version); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| buf, err := s.Bytes() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if len(buf) > 0 { | ||
| e.ExtData = &buf | ||
| } else { | ||
| // Respect the `rlp:"nil"` field tag. | ||
| e.ExtData = nil | ||
| } | ||
|
|
||
| return nil | ||
| func (e *cChainBodyExtras) RLPFieldPointersForDecoding(b *Body) ([]any, []any) { | ||
| // An alternative to the pattern used above is to explicitly list all | ||
| // fields for better introspection. | ||
| return []any{ | ||
| &b.Transactions, | ||
| &b.Uncles, | ||
| &e.Version, | ||
| rlp.Nillable(&e.ExtData), // equivalent to `rlp:"nil"` | ||
|
||
| }, nil | ||
| } | ||
|
|
||
| func TestBodyRLPCChainCompat(t *testing.T) { | ||
|
|
@@ -256,12 +248,14 @@ func TestBodyRLPCChainCompat(t *testing.T) { | |
| wantRLPHex string | ||
| }{ | ||
| { | ||
| name: "nil ExtData", | ||
ARR4N marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| extra: &cChainBodyExtras{ | ||
| Version: version, | ||
| }, | ||
| wantRLPHex: `e5dedd2a80809400000000000000000000000000decafc0ffeebad8080808080c08304cb2f80`, | ||
| }, | ||
| { | ||
| name: "non-nil ExtData", | ||
ARR4N marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| extra: &cChainBodyExtras{ | ||
| Version: version, | ||
| ExtData: &[]byte{1, 4, 2, 8, 5, 7}, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.