diff --git a/precompiles/pointer/pointer_test.go b/precompiles/pointer/pointer_test.go index e75c09f7fb..f5bd4e1c5d 100644 --- a/precompiles/pointer/pointer_test.go +++ b/precompiles/pointer/pointer_test.go @@ -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) @@ -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 { diff --git a/x/evm/state/state.go b/x/evm/state/state.go index 7d88551767..a6ead6bfa5 100644 --- a/x/evm/state/state.go +++ b/x/evm/state/state.go @@ -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) diff --git a/x/evm/state/state_test.go b/x/evm/state/state_test.go index 1b165d63ff..28cc4c8610 100644 --- a/x/evm/state/state_test.go +++ b/x/evm/state/state_test.go @@ -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")) @@ -141,6 +142,7 @@ 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() @@ -148,6 +150,7 @@ func TestSnapshot(t *testing.T) { newTVal := common.BytesToHash([]byte("y")) statedb.SetState(evmAddr, key, newVal) statedb.SetTransientState(evmAddr, tkey, newTVal) + statedb.Ctx().EventManager().EmitEvent(sdk.Event{}) statedb.RevertToSnapshot(rev) @@ -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())) } diff --git a/x/evm/state/statedb.go b/x/evm/state/statedb.go index 101735618c..9ef3f52eb7 100644 --- a/x/evm/state/statedb.go +++ b/x/evm/state/statedb.go @@ -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 { @@ -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() @@ -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),