-
Notifications
You must be signed in to change notification settings - Fork 2
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
test: StateAccount.Extra
via trie.StateTrie.{Update,Get}Account()
#45
Changes from all commits
bd5a36a
4789f88
5e43398
ccbb544
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// 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_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/holiman/uint256" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/rawdb" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/libevm/ethtest" | ||
"github.com/ethereum/go-ethereum/trie" | ||
"github.com/ethereum/go-ethereum/triedb" | ||
) | ||
|
||
func TestStateAccountExtraViaTrieStorage(t *testing.T) { | ||
rng := ethtest.NewPseudoRand(1984) | ||
addr := rng.Address() | ||
|
||
type arbitraryPayload struct { | ||
Data string | ||
} | ||
const arbitraryData = "Hello, RLP world!" | ||
|
||
var ( | ||
// The specific trie hashes after inserting the account are irrelevant; | ||
// what's important is that: (a) they are all different; and (b) tests | ||
// of implicit and explicit zero-value payloads have the same hash. | ||
vanillaGeth = common.HexToHash("0x2108846aaec8a88cfa02887527ad8c1beffc11b5ec428b68f15d9ce4e71e4ce1") | ||
trueBool = common.HexToHash("0x665576885e52711e4cf90b72750fc1c17c80c5528bc54244e327414d486a10a4") | ||
falseBool = common.HexToHash("0xa53fcb27d01347e202fb092d0af2a809cb84390c6001cbc151052ee29edc2294") | ||
arbitrary = common.HexToHash("0x94eecff1444ab69437636630918c15596e001b30b973f03e06006ae20aa6e307") | ||
) | ||
|
||
tests := []struct { | ||
name string | ||
registerAndSetExtra func(*types.StateAccount) *types.StateAccount | ||
assertExtra func(*testing.T, *types.StateAccount) | ||
wantTrieHash common.Hash | ||
}{ | ||
{ | ||
name: "vanilla geth", | ||
registerAndSetExtra: func(a *types.StateAccount) *types.StateAccount { | ||
return a | ||
}, | ||
assertExtra: func(t *testing.T, a *types.StateAccount) { | ||
t.Helper() | ||
assert.Truef(t, a.Extra.Equal(nil), "%T.%T.IsEmpty()", a, a.Extra) | ||
}, | ||
wantTrieHash: vanillaGeth, | ||
}, | ||
{ | ||
name: "true-boolean payload", | ||
registerAndSetExtra: func(a *types.StateAccount) *types.StateAccount { | ||
types.RegisterExtras[bool]().SetOnStateAccount(a, true) | ||
return a | ||
}, | ||
assertExtra: func(t *testing.T, sa *types.StateAccount) { | ||
t.Helper() | ||
assert.Truef(t, types.ExtraPayloads[bool]{}.FromStateAccount(sa), "") | ||
}, | ||
wantTrieHash: trueBool, | ||
}, | ||
{ | ||
name: "explicit false-boolean payload", | ||
registerAndSetExtra: func(a *types.StateAccount) *types.StateAccount { | ||
p := types.RegisterExtras[bool]() | ||
p.SetOnStateAccount(a, false) // the explicit part | ||
return a | ||
}, | ||
assertExtra: func(t *testing.T, sa *types.StateAccount) { | ||
t.Helper() | ||
assert.Falsef(t, types.ExtraPayloads[bool]{}.FromStateAccount(sa), "") | ||
}, | ||
wantTrieHash: falseBool, | ||
}, | ||
{ | ||
name: "implicit false-boolean payload", | ||
registerAndSetExtra: func(a *types.StateAccount) *types.StateAccount { | ||
types.RegisterExtras[bool]() | ||
// Note that `a` is reflected, unchanged (the implicit part). | ||
return a | ||
}, | ||
assertExtra: func(t *testing.T, sa *types.StateAccount) { | ||
t.Helper() | ||
assert.Falsef(t, types.ExtraPayloads[bool]{}.FromStateAccount(sa), "") | ||
}, | ||
wantTrieHash: falseBool, | ||
}, | ||
{ | ||
name: "arbitrary payload", | ||
registerAndSetExtra: func(a *types.StateAccount) *types.StateAccount { | ||
p := arbitraryPayload{arbitraryData} | ||
types.RegisterExtras[arbitraryPayload]().SetOnStateAccount(a, p) | ||
return a | ||
}, | ||
assertExtra: func(t *testing.T, sa *types.StateAccount) { | ||
t.Helper() | ||
got := types.ExtraPayloads[arbitraryPayload]{}.FromStateAccount(sa) | ||
assert.Equalf(t, arbitraryPayload{arbitraryData}, got, "") | ||
}, | ||
wantTrieHash: arbitrary, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
types.TestOnlyClearRegisteredExtras() | ||
t.Cleanup(types.TestOnlyClearRegisteredExtras) | ||
|
||
acct := tt.registerAndSetExtra(&types.StateAccount{ | ||
Nonce: 42, | ||
Balance: uint256.NewInt(314159), | ||
Root: types.EmptyRootHash, | ||
CodeHash: types.EmptyCodeHash[:], | ||
}) | ||
|
||
db := triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil) | ||
id := trie.TrieID(types.EmptyRootHash) | ||
state, err := trie.NewStateTrie(id, db) | ||
require.NoError(t, err, "trie.NewStateTrie(types.EmptyRootHash, ...)") | ||
|
||
require.NoErrorf(t, state.UpdateAccount(addr, acct), "%T.UpdateAccount(...)", state) | ||
assert.Equalf(t, tt.wantTrieHash, state.Hash(), "%T.Hash() after UpdateAccount()", state) | ||
Comment on lines
+142
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can can we also add testing for Copy & Snapshot/Revert being handled properly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Snapshot/Revert covered in #48 I'm about to test Copy too. If it fails then I'll see how much needs to change and might start a new PR if the fix spreads too far. |
||
|
||
got, err := state.GetAccount(addr) | ||
require.NoError(t, err, "state.GetAccount({account updated earlier})") | ||
if diff := cmp.Diff(acct, got); diff != "" { | ||
t.Errorf("%T.GetAccount() not equal to value passed to %[1]T.UpdateAccount(); diff (-want +got):\n%s", state, diff) | ||
} | ||
tt.assertExtra(t, got) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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 testonly enforces functionality that MUST be limited to tests. | ||
package testonly | ||
|
||
import ( | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
// OrPanic runs `fn` i.f.f. called from within a testing environment. | ||
func OrPanic(fn func()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was abstracted from |
||
pc := make([]uintptr, 64) | ||
runtime.Callers(0, pc) | ||
frames := runtime.CallersFrames(pc) | ||
for { | ||
f, more := frames.Next() | ||
if strings.Contains(f.File, "/testing/") || strings.HasSuffix(f.File, "_test.go") { | ||
fn() | ||
return | ||
} | ||
if !more { | ||
panic("no _test.go file in call stack") | ||
} | ||
} | ||
} |
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 was moved from the test file, unchanged.