forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: types.StateAccount pseudo-generic payload
#44
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
854fa2d
feat: `types.StateAccount` pseudo-generic payload
ARR4N 08e0f2a
Merge branch 'libevm' into arr4n/pseudo-type-rlp
ARR4N 10eb6f8
feat: registration of `StateAccount` payload type
ARR4N bb4c914
chore: mark `eth/tracers/logger` flaky
ARR4N 095415f
chore: copyright header + `gci`
ARR4N 0db9528
test: lock default `types.SlimAccount` RLP encoding
ARR4N cda5afb
feat: `vm.SlimAccount.Extra` from `StateAccount` equiv
ARR4N 780a592
chore: placate the linter
ARR4N 9925590
test: `pseudo.Type.EncodeRLP()`
ARR4N 0d19b34
test: `pseudo.Type.DecodeRLP()`
ARR4N 782f028
fix: `pseudo.Type.DecodeRLP()` with non-pointer type
ARR4N a981d54
feat: `pseudo.Type.IsZero()` and `Type.Equal(*Type)`
ARR4N 1602fdb
feat: `types.StateAccountExtra.DecodeRLP()`
ARR4N 5b15698
Merge branch 'libevm' into arr4n/state-account-extra-payload
ARR4N 17022ce
fix: remove unnecessary `StateAccountExtra.clone()`
ARR4N f1a1c67
refactor: readability
ARR4N e2af2a6
feat: `pseudo.Type.Format()` implements `fmt.Formatter`
ARR4N File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| // 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 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "github.com/ethereum/go-ethereum/libevm/pseudo" | ||
| "github.com/ethereum/go-ethereum/rlp" | ||
| ) | ||
|
|
||
| // RegisterExtras registers the type `SA` to be carried as an extra payload in | ||
| // [StateAccount] structs. It is expected to be called in an `init()` function | ||
| // and MUST NOT be called more than once. | ||
| // | ||
| // The payload will be treated as an extra struct field for the purposes of RLP | ||
| // encoding and decoding. RLP handling is plumbed through to the `SA` via the | ||
| // [StateAccountExtra] that holds it such that it acts as if there were a field | ||
| // of type `SA` in all StateAccount structs. | ||
| // | ||
| // The payload can be acced via the [ExtraPayloads.FromStateAccount] method of | ||
| // the accessor returned by RegisterExtras. | ||
| func RegisterExtras[SA any]() ExtraPayloads[SA] { | ||
| if registeredExtras != nil { | ||
| panic("re-registration of Extras") | ||
| } | ||
| var extra ExtraPayloads[SA] | ||
| registeredExtras = &extraConstructors{ | ||
| stateAccountType: func() string { | ||
| var x SA | ||
| return fmt.Sprintf("%T", x) | ||
| }(), | ||
| newStateAccount: pseudo.NewConstructor[SA]().Zero, | ||
| cloneStateAccount: extra.cloneStateAccount, | ||
| } | ||
| return extra | ||
| } | ||
|
|
||
| var registeredExtras *extraConstructors | ||
|
|
||
| type extraConstructors struct { | ||
| stateAccountType string | ||
| newStateAccount func() *pseudo.Type | ||
| cloneStateAccount func(*StateAccountExtra) *StateAccountExtra | ||
| } | ||
|
|
||
| func (e *StateAccountExtra) clone() *StateAccountExtra { | ||
| switch r := registeredExtras; { | ||
| case r == nil, e == nil: | ||
| return nil | ||
| default: | ||
| return r.cloneStateAccount(e) | ||
| } | ||
| } | ||
|
|
||
| // ExtraPayloads provides strongly typed access to the extra payload carried by | ||
| // [StateAccount] structs. The only valid way to construct an instance is by a | ||
| // call to [RegisterExtras]. | ||
| type ExtraPayloads[SA any] struct { | ||
| _ struct{} // make godoc show unexported fields so nobody tries to make their own instance ;) | ||
| } | ||
|
|
||
| func (ExtraPayloads[SA]) cloneStateAccount(s *StateAccountExtra) *StateAccountExtra { | ||
| v := pseudo.MustNewValue[SA](s.t) | ||
| return &StateAccountExtra{ | ||
| t: pseudo.From(v.Get()).Type, | ||
| } | ||
| } | ||
|
|
||
| // FromStateAccount returns the StateAccount's payload. | ||
| func (ExtraPayloads[SA]) FromStateAccount(a *StateAccount) SA { | ||
| return pseudo.MustNewValue[SA](a.extra().payload()).Get() | ||
| } | ||
|
|
||
| // PointerFromStateAccount returns a pointer to the StateAccounts's extra | ||
| // payload. This is guaranteed to be non-nil. | ||
| // | ||
| // Note that copying a StateAccount by dereferencing a pointer will result in a | ||
| // shallow copy and that the *SA returned here will therefore be shared by all | ||
| // copies. If this is not the desired behaviour, use | ||
| // [StateAccount.Copy] or [ExtraPayloads.SetOnStateAccount]. | ||
| func (ExtraPayloads[SA]) PointerFromStateAccount(a *StateAccount) *SA { | ||
| return pseudo.MustPointerTo[SA](a.extra().payload()).Value.Get() | ||
| } | ||
|
|
||
| // SetOnStateAccount sets the StateAccount's payload. | ||
| func (ExtraPayloads[SA]) SetOnStateAccount(a *StateAccount, val SA) { | ||
| a.extra().t = pseudo.From(val).Type | ||
| } | ||
|
|
||
| // A StateAccountExtra carries the extra payload, if any, registered with | ||
| // [RegisterExtras]. It SHOULD NOT be used directly; instead use the | ||
| // [ExtraPayloads] accessor returned by RegisterExtras. | ||
| type StateAccountExtra struct { | ||
| t *pseudo.Type | ||
| } | ||
|
|
||
| func (a *StateAccount) extra() *StateAccountExtra { | ||
| if a.Extra == nil { | ||
| a.Extra = &StateAccountExtra{ | ||
| t: registeredExtras.newStateAccount(), | ||
| } | ||
| } | ||
| return a.Extra | ||
| } | ||
|
|
||
| func (e *StateAccountExtra) payload() *pseudo.Type { | ||
| if e.t == nil { | ||
| e.t = registeredExtras.newStateAccount() | ||
| } | ||
| return e.t | ||
| } | ||
|
|
||
| var _ interface { | ||
| rlp.Encoder | ||
| rlp.Decoder | ||
| fmt.Formatter | ||
| } = (*StateAccountExtra)(nil) | ||
|
|
||
| // EncodeRLP implements the [rlp.Encoder] interface. | ||
| func (e *StateAccountExtra) EncodeRLP(w io.Writer) error { | ||
| switch r := registeredExtras; { | ||
| case r == nil: | ||
| return nil | ||
| case e == nil: | ||
| e = &StateAccountExtra{} | ||
| fallthrough | ||
| case e.t == nil: | ||
| e.t = r.newStateAccount() | ||
| } | ||
| return e.t.EncodeRLP(w) | ||
| } | ||
|
|
||
| // DecodeRLP implements the [rlp.Decoder] interface. | ||
| func (e *StateAccountExtra) DecodeRLP(s *rlp.Stream) error { | ||
| switch r := registeredExtras; { | ||
| case r == nil: | ||
| return nil | ||
| case e.t == nil: | ||
| e.t = r.newStateAccount() | ||
| fallthrough | ||
| default: | ||
| return s.Decode(e.t) | ||
| } | ||
| } | ||
|
|
||
| // Format implements the [fmt.Formatter] interface. | ||
| func (e *StateAccountExtra) Format(s fmt.State, verb rune) { | ||
| var out string | ||
| switch r := registeredExtras; { | ||
| case r == nil: | ||
| out = "<nil>" | ||
| case e == nil, e.t == nil: | ||
| out = fmt.Sprintf("<nil>[*StateAccountExtra[%s]]", r.stateAccountType) | ||
| default: | ||
| e.t.Format(s, verb) | ||
| return | ||
| } | ||
| _, _ = s.Write([]byte(out)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code is also removed upstream in upcoming revisions, fyi