|
| 1 | +// Copyright 2024 the libevm authors. |
| 2 | +// |
| 3 | +// The libevm additions to go-ethereum are free software: you can redistribute |
| 4 | +// them and/or modify them under the terms of the GNU Lesser General Public License |
| 5 | +// as published by the Free Software Foundation, either version 3 of the License, |
| 6 | +// or (at your option) any later version. |
| 7 | +// |
| 8 | +// The libevm additions are distributed in the hope that they will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
| 11 | +// General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU Lesser General Public License |
| 14 | +// along with the go-ethereum library. If not, see |
| 15 | +// <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package types_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "io" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + |
| 27 | + . "github.com/ava-labs/libevm/core/types" |
| 28 | + "github.com/ava-labs/libevm/crypto" |
| 29 | + "github.com/ava-labs/libevm/libevm/ethtest" |
| 30 | + "github.com/ava-labs/libevm/rlp" |
| 31 | +) |
| 32 | + |
| 33 | +type stubHeaderHooks struct { |
| 34 | + rlpSuffix []byte |
| 35 | + gotRawRLPToDecode []byte |
| 36 | + setHeaderToOnDecode Header |
| 37 | + |
| 38 | + errEncode, errDecode error |
| 39 | +} |
| 40 | + |
| 41 | +func fakeHeaderRLP(h *Header, suffix []byte) []byte { |
| 42 | + return append(crypto.Keccak256(h.ParentHash[:]), suffix...) |
| 43 | +} |
| 44 | + |
| 45 | +func (hh *stubHeaderHooks) EncodeRLP(h *Header, w io.Writer) error { |
| 46 | + if _, err := w.Write(fakeHeaderRLP(h, hh.rlpSuffix)); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + return hh.errEncode |
| 50 | +} |
| 51 | + |
| 52 | +func (hh *stubHeaderHooks) DecodeRLP(h *Header, s *rlp.Stream) error { |
| 53 | + r, err := s.Raw() |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + hh.gotRawRLPToDecode = r |
| 58 | + *h = hh.setHeaderToOnDecode |
| 59 | + return hh.errDecode |
| 60 | +} |
| 61 | + |
| 62 | +func TestHeaderHooks(t *testing.T) { |
| 63 | + TestOnlyClearRegisteredExtras() |
| 64 | + defer TestOnlyClearRegisteredExtras() |
| 65 | + |
| 66 | + extras := RegisterExtras[stubHeaderHooks, *stubHeaderHooks, struct{}]() |
| 67 | + rng := ethtest.NewPseudoRand(13579) |
| 68 | + |
| 69 | + t.Run("EncodeRLP", func(t *testing.T) { |
| 70 | + suffix := rng.Bytes(8) |
| 71 | + |
| 72 | + hdr := &Header{ |
| 73 | + ParentHash: rng.Hash(), |
| 74 | + } |
| 75 | + extras.Header.Get(hdr).rlpSuffix = append([]byte{}, suffix...) |
| 76 | + |
| 77 | + got, err := rlp.EncodeToBytes(hdr) |
| 78 | + require.NoError(t, err, "rlp.EncodeToBytes(%T)", hdr) |
| 79 | + assert.Equal(t, fakeHeaderRLP(hdr, suffix), got) |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("DecodeRLP", func(t *testing.T) { |
| 83 | + input, err := rlp.EncodeToBytes(rng.Bytes(8)) |
| 84 | + require.NoError(t, err) |
| 85 | + |
| 86 | + hdr := new(Header) |
| 87 | + stub := &stubHeaderHooks{ |
| 88 | + setHeaderToOnDecode: Header{ |
| 89 | + Extra: []byte("arr4n was here"), |
| 90 | + }, |
| 91 | + } |
| 92 | + extras.Header.Set(hdr, stub) |
| 93 | + err = rlp.DecodeBytes(input, hdr) |
| 94 | + require.NoErrorf(t, err, "rlp.DecodeBytes(%#x)", input) |
| 95 | + |
| 96 | + assert.Equal(t, input, stub.gotRawRLPToDecode, "raw RLP received by hooks") |
| 97 | + assert.Equalf(t, &stub.setHeaderToOnDecode, hdr, "%T after RLP decoding with hook", hdr) |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("error_propagation", func(t *testing.T) { |
| 101 | + errEncode := errors.New("uh oh") |
| 102 | + errDecode := errors.New("something bad happened") |
| 103 | + |
| 104 | + hdr := new(Header) |
| 105 | + extras.Header.Set(hdr, &stubHeaderHooks{ |
| 106 | + errEncode: errEncode, |
| 107 | + errDecode: errDecode, |
| 108 | + }) |
| 109 | + |
| 110 | + assert.Equal(t, errEncode, rlp.Encode(io.Discard, hdr), "via rlp.Encode()") |
| 111 | + assert.Equal(t, errDecode, rlp.DecodeBytes([]byte{0}, hdr), "via rlp.DecodeBytes()") |
| 112 | + }) |
| 113 | +} |
0 commit comments