-
Couldn't load subscription status.
- Fork 5
feat: types.HeaderHooks for RLP overrides
#89
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 3 commits
aee5be0
6bc6fa6
27886ea
c73cbde
5d37968
825f3c2
eae5083
0bb10a1
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 |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright 2024 the libevm authors. | ||
| // | ||
| // The libevm additions to go-ethereum are free software: you can redistribute | ||
| // them and/or modify them under the terms of the GNU Lesser General Public License | ||
| // as published by the Free Software Foundation, either version 3 of the License, | ||
| // or (at your option) any later version. | ||
| // | ||
| // The libevm additions are distributed in the hope that they will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | ||
| // General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Lesser General Public License | ||
| // along with the go-ethereum library. If not, see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| package types_test | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| . "github.com/ava-labs/libevm/core/types" | ||
| "github.com/ava-labs/libevm/crypto" | ||
| "github.com/ava-labs/libevm/libevm/ethtest" | ||
| "github.com/ava-labs/libevm/rlp" | ||
| ) | ||
|
|
||
| type stubHeaderHooks struct { | ||
| rlpSuffix []byte | ||
| gotRawRLPToDecode []byte | ||
| setHeaderToOnDecode Header | ||
|
|
||
| errEncode, errDecode error | ||
| } | ||
|
|
||
| func fakeHeaderRLP(h *Header, suffix []byte) []byte { | ||
| return append(crypto.Keccak256(h.ParentHash[:]), suffix...) | ||
| } | ||
|
|
||
| func (hh *stubHeaderHooks) EncodeRLP(h *Header, w io.Writer) error { | ||
| if _, err := w.Write(fakeHeaderRLP(h, hh.rlpSuffix)); err != nil { | ||
| return err | ||
| } | ||
| return hh.errEncode | ||
| } | ||
|
|
||
| func (hh *stubHeaderHooks) DecodeRLP(h *Header, s *rlp.Stream) error { | ||
| r, err := s.Raw() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| hh.gotRawRLPToDecode = r | ||
| *h = hh.setHeaderToOnDecode | ||
| return hh.errDecode | ||
| } | ||
|
|
||
| func TestHeaderHooks(t *testing.T) { | ||
| TestOnlyClearRegisteredExtras() | ||
| defer TestOnlyClearRegisteredExtras() | ||
qdm12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| extras := RegisterExtras[stubHeaderHooks, *stubHeaderHooks, struct{}]() | ||
| rng := ethtest.NewPseudoRand(13579) | ||
|
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. Any particular reason for 13579? Or just smashed digits on the keyboard? ⌨️ I feel we should really just seed everything with 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.
If there's a particular reason then I'm happy to consider it as the only reason I choose numbers is for some fun. Practically I think it's all the same, no? 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. It does look like a magic number although it's not, at least with Maybe we could address those in a chore PR later finding all numbers with a regex, I can create a super low priority issue for this 😸 |
||
|
|
||
| t.Run("EncodeRLP", func(t *testing.T) { | ||
| suffix := rng.Bytes(8) | ||
|
|
||
| hdr := &Header{ | ||
| ParentHash: rng.Hash(), | ||
| } | ||
| extras.Header.Get(hdr).rlpSuffix = append([]byte{}, suffix...) | ||
|
|
||
| got, err := rlp.EncodeToBytes(hdr) | ||
| require.NoError(t, err, "rlp.EncodeToBytes(%T)", hdr) | ||
| assert.Equal(t, fakeHeaderRLP(hdr, suffix), got) | ||
| }) | ||
|
|
||
| t.Run("DecodeRLP", func(t *testing.T) { | ||
| input, err := rlp.EncodeToBytes(rng.Bytes(8)) | ||
| require.NoError(t, err) | ||
|
|
||
| hdr := new(Header) | ||
| stub := &stubHeaderHooks{ | ||
| setHeaderToOnDecode: Header{ | ||
| Extra: []byte("arr4n was here"), | ||
| }, | ||
| } | ||
| extras.Header.Set(hdr, stub) | ||
| require.NoErrorf(t, rlp.DecodeBytes(input, hdr), "rlp.DecodeBytes(%#x)", input) | ||
ARR4N marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| assert.Equal(t, input, stub.gotRawRLPToDecode, "raw RLP received by hooks") | ||
| assert.Equalf(t, &stub.setHeaderToOnDecode, hdr, "%T after RLP decoding with hook", hdr) | ||
| }) | ||
|
|
||
| t.Run("error propagation", func(t *testing.T) { | ||
ARR4N marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| errEncode := errors.New("uh oh") | ||
| errDecode := errors.New("something bad happened") | ||
|
|
||
| hdr := new(Header) | ||
| extras.Header.Set(hdr, &stubHeaderHooks{ | ||
| errEncode: errEncode, | ||
| errDecode: errDecode, | ||
| }) | ||
|
|
||
| assert.Equal(t, errEncode, rlp.Encode(io.Discard, hdr), "via rlp.Encode()") | ||
| assert.Equal(t, errDecode, rlp.DecodeBytes([]byte{0}, hdr), "via rlp.DecodeBytes()") | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.