Skip to content

Commit 0d19b34

Browse files
committed
test: pseudo.Type.DecodeRLP()
1 parent 9925590 commit 0d19b34

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

libevm/pseudo/reflect.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 pseudo
18+
19+
import "reflect"
20+
21+
// Reflection is used as a last resort in pseudo types so is limited to this
22+
// file to avoid being seen as the norm. If you are adding to this file, please
23+
// try to achieve the same results with type parameters.
24+
25+
func (c *concrete[T]) ensureNonNilPointer() {
26+
v := reflect.ValueOf(c.val)
27+
if v.Kind() != reflect.Pointer || !v.IsNil() {
28+
return
29+
}
30+
el := v.Type().Elem()
31+
c.val = reflect.New(el).Interface().(T) //nolint:forcetypeassert // Invariant scoped to the last few lines of code so simple to verify
32+
}

libevm/pseudo/rlp_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ func TestRLPEquivalence(t *testing.T) {
5959
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", hdr)
6060

6161
typ := pseudo.From(hdr).Type
62-
got, err := rlp.EncodeToBytes(typ)
62+
gotRLP, err := rlp.EncodeToBytes(typ)
6363
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", typ)
6464

65-
require.Equalf(t, want, got, "RLP encoding of %T (canonical) vs %T (under test)", hdr, typ)
65+
require.Equalf(t, want, gotRLP, "RLP encoding of %T (canonical) vs %T (under test)", hdr, typ)
66+
67+
t.Run("decode", func(t *testing.T) {
68+
pseudo := pseudo.Zero[*types.Header]()
69+
require.NoError(t, rlp.DecodeBytes(gotRLP, pseudo.Type))
70+
require.Equal(t, hdr, pseudo.Value.Get())
71+
})
6672
})
6773
}
6874
}

libevm/pseudo/type.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,6 @@ func (c *concrete[T]) UnmarshalJSON(b []byte) error {
233233
func (c *concrete[T]) EncodeRLP(w io.Writer) error { return rlp.Encode(w, c.val) }
234234

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

0 commit comments

Comments
 (0)