|
| 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 state |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/ava-labs/libevm/common" |
| 23 | + "github.com/ava-labs/libevm/core/rawdb" |
| 24 | + "github.com/ava-labs/libevm/core/state/snapshot" |
| 25 | + "github.com/ava-labs/libevm/core/types" |
| 26 | + "github.com/ava-labs/libevm/libevm/stateconf" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | +) |
| 30 | + |
| 31 | +func TestStateDBCommitPropagatesOptions(t *testing.T) { |
| 32 | + var rec snapTreeRecorder |
| 33 | + sdb, err := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), &rec) |
| 34 | + require.NoError(t, err, "New()") |
| 35 | + |
| 36 | + // Ensures that rec.Update() will be called. |
| 37 | + sdb.SetNonce(common.Address{}, 42) |
| 38 | + |
| 39 | + const payload = "hello world" |
| 40 | + opt := stateconf.WithUpdatePayload(payload) |
| 41 | + _, err = sdb.Commit(0, false, opt) |
| 42 | + require.NoErrorf(t, err, "%T.Commit(..., %T)", sdb, opt) |
| 43 | + |
| 44 | + assert.Equalf(t, payload, rec.gotPayload, "%T payload propagated via %T.Commit() to %T.Update()", opt, sdb, rec) |
| 45 | +} |
| 46 | + |
| 47 | +type snapTreeRecorder struct { |
| 48 | + SnapshotTree |
| 49 | + gotPayload any |
| 50 | +} |
| 51 | + |
| 52 | +func (*snapTreeRecorder) Cap(common.Hash, int) error { |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func (r *snapTreeRecorder) Update( |
| 57 | + _, _ common.Hash, |
| 58 | + _ map[common.Hash]struct{}, _ map[common.Hash][]byte, _ map[common.Hash]map[common.Hash][]byte, |
| 59 | + opts ...stateconf.SnapshotUpdateOption, |
| 60 | +) error { |
| 61 | + r.gotPayload = stateconf.ExtractUpdatePayload(opts...) |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (*snapTreeRecorder) Snapshot(common.Hash) snapshot.Snapshot { |
| 66 | + return snapshotStub{} |
| 67 | +} |
| 68 | + |
| 69 | +type snapshotStub struct { |
| 70 | + snapshot.Snapshot |
| 71 | +} |
| 72 | + |
| 73 | +func (snapshotStub) Account(common.Hash) (*types.SlimAccount, error) { |
| 74 | + return &types.SlimAccount{}, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (snapshotStub) Root() common.Hash { |
| 78 | + return common.Hash{} |
| 79 | +} |
0 commit comments