go-ethereum/libevm/pseudo/reflect.go
Arran Schlosberg 336a289f42
feat: pseudo.Type RLP round-tripping (#43)
All commits except the last two constitute PRs #43 and #44. The last two reverted files such that only changes to the `pseudo` and `ethtest` packages remain; once this is merged into the `libevm` branch then `libevm` will be merged into the branch for #44 too. Cherry-picking commits was not possible as some touched both halves of the changes; the squash-merges will, however, make this convoluted history irrelevant.

* feat: `types.StateAccount` pseudo-generic payload

* feat: registration of `StateAccount` payload type

* chore: mark `eth/tracers/logger` flaky

* chore: copyright header + `gci`

* test: lock default `types.SlimAccount` RLP encoding

* feat: `vm.SlimAccount.Extra` from `StateAccount` equiv

* chore: placate the linter

* test: `pseudo.Type.EncodeRLP()`

* test: `pseudo.Type.DecodeRLP()`

* fix: `pseudo.Type.DecodeRLP()` with non-pointer type

* feat: `pseudo.Type.IsZero()` and `Type.Equal(*Type)`

* feat: `types.StateAccountExtra.DecodeRLP()`

* chore: revert non-pseudo-package modifications

* chore: delete non-pseudo-package additions
2024-10-01 08:23:51 -07:00

60 lines
1.9 KiB
Go

// 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"
"github.com/ethereum/go-ethereum/rlp"
)
// 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]) isZero() bool {
// The alternative would require that T be comparable, which would bubble up
// and invade the rest of the code base.
return reflect.ValueOf(c.val).IsZero()
}
func (c *concrete[T]) equal(t *Type) bool {
d, ok := t.val.(*concrete[T])
if !ok {
return false
}
switch v := any(c.val).(type) {
case EqualityChecker[T]:
return v.Equal(d.val)
default:
// See rationale for reflection in [concrete.isZero].
return reflect.DeepEqual(c.val, d.val)
}
}
func (c *concrete[T]) DecodeRLP(s *rlp.Stream) error {
switch v := reflect.ValueOf(c.val); v.Kind() {
case reflect.Pointer:
if v.IsNil() {
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
}
return s.Decode(c.val)
default:
return s.Decode(&c.val)
}
}