Skip to content

Commit

Permalink
Fork event manager when creating EVM snapshots (sei-protocol#1831)
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored Aug 23, 2024
1 parent 4e175e9 commit dd7c43d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
4 changes: 3 additions & 1 deletion precompiles/pointer/pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestAddNative(t *testing.T) {
Aliases: []string{"DENOM"},
}},
})
statedb = state.NewDBImpl(ctx, &testApp.EvmKeeper, true)
statedb = state.NewDBImpl(ctx, &testApp.EvmKeeper, false)
evm = vm.NewEVM(*blockCtx, vm.TxContext{}, statedb, cfg, vm.Config{})
ret, g, err := p.RunAndCalculateGas(evm, caller, caller, append(p.GetExecutor().(*pointer.PrecompileExecutor).AddNativePointerID, args...), suppliedGas, nil, nil, false)
require.Nil(t, err)
Expand All @@ -64,6 +64,8 @@ func TestAddNative(t *testing.T) {
require.Equal(t, addr, pointerAddr)
require.Equal(t, native.CurrentVersion, version)
require.True(t, exists)
_, err = statedb.Finalize()
require.Nil(t, err)
hasRegisteredEvent := false
for _, e := range ctx.EventManager().Events() {
if e.Type != types.EventTypePointerRegistered {
Expand Down
2 changes: 1 addition & 1 deletion x/evm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s *DBImpl) HasSelfDestructed(acc common.Address) bool {
}

func (s *DBImpl) Snapshot() int {
newCtx := s.ctx.WithMultiStore(s.ctx.MultiStore().CacheMultiStore())
newCtx := s.ctx.WithMultiStore(s.ctx.MultiStore().CacheMultiStore()).WithEventManager(sdk.NewEventManager())
s.snapshottedCtxs = append(s.snapshottedCtxs, s.ctx)
s.ctx = newCtx
s.tempStatesHist = append(s.tempStatesHist, s.tempStateCurrent)
Expand Down
4 changes: 4 additions & 0 deletions x/evm/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func TestSnapshot(t *testing.T) {
k, ctx := testkeeper.MockEVMKeeper()
seiAddr, evmAddr := testkeeper.MockAddressPair()
k.SetAddressMapping(ctx, seiAddr, evmAddr)
eventCount := len(ctx.EventManager().Events())
statedb := state.NewDBImpl(ctx, k, false)
statedb.CreateAccount(evmAddr)
key := common.BytesToHash([]byte("abc"))
Expand All @@ -141,13 +142,15 @@ func TestSnapshot(t *testing.T) {
tval := common.BytesToHash([]byte("mno"))
statedb.SetState(evmAddr, key, val)
statedb.SetTransientState(evmAddr, tkey, tval)
statedb.Ctx().EventManager().EmitEvent(sdk.Event{})

rev := statedb.Snapshot()

newVal := common.BytesToHash([]byte("x"))
newTVal := common.BytesToHash([]byte("y"))
statedb.SetState(evmAddr, key, newVal)
statedb.SetTransientState(evmAddr, tkey, newTVal)
statedb.Ctx().EventManager().EmitEvent(sdk.Event{})

statedb.RevertToSnapshot(rev)

Expand All @@ -165,4 +168,5 @@ func TestSnapshot(t *testing.T) {
// prev state DB committed except for transient states
require.Equal(t, common.Hash{}, newStateDB.GetTransientState(evmAddr, tkey))
require.Equal(t, val, newStateDB.GetState(evmAddr, key))
require.Equal(t, eventCount+1, len(ctx.EventManager().Events()))
}
11 changes: 10 additions & 1 deletion x/evm/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ func (s *DBImpl) Finalize() (surplus sdk.Int, err error) {
for i := len(s.snapshottedCtxs) - 1; i > 0; i-- {
s.flushCtx(s.snapshottedCtxs[i])
}
// write all events in order
for i := 1; i < len(s.snapshottedCtxs); i++ {
s.flushEvents(s.snapshottedCtxs[i])
}
s.flushEvents(s.ctx)

surplus = s.tempStateCurrent.surplus
for _, ts := range s.tempStatesHist {
Expand All @@ -124,6 +129,10 @@ func (s *DBImpl) flushCtx(ctx sdk.Context) {
ctx.MultiStore().(sdk.CacheMultiStore).Write()
}

func (s *DBImpl) flushEvents(ctx sdk.Context) {
s.snapshottedCtxs[0].EventManager().EmitEvents(ctx.EventManager().Events())
}

// Backward-compatibility functions
func (s *DBImpl) Error() error {
return s.Err()
Expand All @@ -134,7 +143,7 @@ func (s *DBImpl) GetStorageRoot(common.Address) common.Hash {
}

func (s *DBImpl) Copy() vm.StateDB {
newCtx := s.ctx.WithMultiStore(s.ctx.MultiStore().CacheMultiStore())
newCtx := s.ctx.WithMultiStore(s.ctx.MultiStore().CacheMultiStore()).WithEventManager(sdk.NewEventManager())
return &DBImpl{
ctx: newCtx,
snapshottedCtxs: append(s.snapshottedCtxs, s.ctx),
Expand Down

0 comments on commit dd7c43d

Please sign in to comment.