Skip to content

Commit

Permalink
test: pseudo.Type.DecodeRLP()
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N committed Oct 1, 2024
1 parent 9925590 commit 0d19b34
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
32 changes: 32 additions & 0 deletions libevm/pseudo/reflect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 pseudo

import "reflect"

// Reflection is used as a last resort in pseudo types so is limited to this
// file to avoid being seen as the norm. If you are adding to this file, please
// try to achieve the same results with type parameters.

func (c *concrete[T]) ensureNonNilPointer() {
v := reflect.ValueOf(c.val)
if v.Kind() != reflect.Pointer || !v.IsNil() {
return
}
el := v.Type().Elem()
c.val = reflect.New(el).Interface().(T) //nolint:forcetypeassert // Invariant scoped to the last few lines of code so simple to verify
}
10 changes: 8 additions & 2 deletions libevm/pseudo/rlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ func TestRLPEquivalence(t *testing.T) {
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", hdr)

typ := pseudo.From(hdr).Type
got, err := rlp.EncodeToBytes(typ)
gotRLP, err := rlp.EncodeToBytes(typ)
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", typ)

require.Equalf(t, want, got, "RLP encoding of %T (canonical) vs %T (under test)", hdr, typ)
require.Equalf(t, want, gotRLP, "RLP encoding of %T (canonical) vs %T (under test)", hdr, typ)

t.Run("decode", func(t *testing.T) {
pseudo := pseudo.Zero[*types.Header]()
require.NoError(t, rlp.DecodeBytes(gotRLP, pseudo.Type))
require.Equal(t, hdr, pseudo.Value.Get())
})
})
}
}
5 changes: 1 addition & 4 deletions libevm/pseudo/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ func (c *concrete[T]) UnmarshalJSON(b []byte) error {
func (c *concrete[T]) EncodeRLP(w io.Writer) error { return rlp.Encode(w, c.val) }

func (c *concrete[T]) DecodeRLP(s *rlp.Stream) error {
if _, _, err := s.Kind(); err != nil {
// DO NOT MERGE: is calling Kind() a necessary step?
return err
}
c.ensureNonNilPointer()
return s.Decode(c.val)
}

0 comments on commit 0d19b34

Please sign in to comment.