Skip to content

Commit

Permalink
fix: TestJournalReversion
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Jan 22, 2025
1 parent 0249319 commit c66d0cf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 17 deletions.
49 changes: 48 additions & 1 deletion x/evm/precompile/test/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasm "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/stretchr/testify/suite"

"github.com/NibiruChain/nibiru/v2/app"
Expand Down Expand Up @@ -152,6 +153,52 @@ func AssertWasmCounterState(
s.EqualValues(deps.Sender.NibiruAddr.String(), typedResp.Owner)
}

func AssertWasmCounterStateWithEvm(
s *suite.Suite,
deps evmtest.TestDeps,
evmObj *vm.EVM,
wasmContract sdk.AccAddress,
wantCount int64,
) {
msgArgsBz := []byte(`
{
"count": {}
}
`)

contractInput, err := embeds.SmartContract_Wasm.ABI.Pack(
string(precompile.WasmMethod_query),
wasmContract.String(),
msgArgsBz,
)
s.Require().NoError(err)

evmResp, err := deps.EvmKeeper.CallContractWithInput(
deps.Ctx,
evmObj,
deps.Sender.EthAddr,
&precompile.PrecompileAddr_Wasm,
false,
contractInput,
WasmGasLimitQuery,
)
s.Require().NoError(err)
s.Require().NotEmpty(evmResp.Ret)

var queryResp []byte
s.NoError(embeds.SmartContract_Wasm.ABI.UnpackIntoInterface(
&queryResp,
string(precompile.WasmMethod_query),
evmResp.Ret,
))

var typedResp QueryMsgCountResp
s.NoError(json.Unmarshal(queryResp, &typedResp))

s.EqualValues(wantCount, typedResp.Count)
s.EqualValues(deps.Sender.NibiruAddr.String(), typedResp.Owner)
}

// Result of QueryMsg::Count from the [hello_world_counter] Wasm contract:
//
// ```rust
Expand Down Expand Up @@ -198,6 +245,7 @@ type QueryMsgCountResp struct {
func IncrementWasmCounterWithExecuteMulti(
s *suite.Suite,
deps *evmtest.TestDeps,
evmObj *vm.EVM,
wasmContract sdk.AccAddress,
times uint,
commit bool,
Expand Down Expand Up @@ -229,7 +277,6 @@ func IncrementWasmCounterWithExecuteMulti(
)
s.Require().NoError(err)

evmObj, _ := deps.NewEVM()
ethTxResp, err := deps.EvmKeeper.CallContractWithInput(
deps.Ctx,
evmObj,
Expand Down
36 changes: 20 additions & 16 deletions x/evm/statedb/journal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ func (s *Suite) TestContractCallsAnotherContract() {

func (s *Suite) TestJournalReversion() {
deps := evmtest.NewTestDeps()
evmObj, _ := deps.NewEVM()
stateDB := evmObj.StateDB.(*statedb.StateDB)
s.Require().NoError(testapp.FundAccount(
deps.App.BankKeeper,
deps.Ctx,
Expand All @@ -176,42 +174,48 @@ func (s *Suite) TestJournalReversion() {
fmt.Printf("wasmContract: %s\n", helloWorldCounterWasm)

s.T().Log("commitEvmTx=true, expect 0 dirty journal entries")
evmObj, stateDB := deps.NewEVM()
test.IncrementWasmCounterWithExecuteMulti(
&s.Suite, &deps, helloWorldCounterWasm, 7, true,
&s.Suite, &deps, evmObj, helloWorldCounterWasm, 7, true,
)
if stateDB.DebugDirtiesCount() != 0 {
debugDirtiesCountMismatch(stateDB, s.T())
s.FailNowf("statedb dirty count mismatch", "expected 0 dirty journal changes, but instead got: %d", stateDB.DebugDirtiesCount())
}

s.T().Log("commitEvmTx=false, expect dirty journal entries")
evmObj, stateDB = deps.NewEVM()
test.IncrementWasmCounterWithExecuteMulti(
&s.Suite, &deps, helloWorldCounterWasm, 5, false,
&s.Suite, &deps, evmObj, helloWorldCounterWasm, 5, false,
)
s.T().Log("Expect exactly 1 dirty journal entry for the precompile snapshot")
if stateDB.DebugDirtiesCount() != 1 {
debugDirtiesCountMismatch(stateDB, s.T())
s.FailNowf("statedb dirty count mismatch", "expected 1 dirty journal change, but instead got: %d", stateDB.DebugDirtiesCount())
}

s.T().Log("Expect to see the pending changes included")
s.T().Log("Expect to see the pending changes included in the EVM context")
test.AssertWasmCounterStateWithEvm(
&s.Suite, deps, evmObj, helloWorldCounterWasm, 7+5,
)
s.T().Log("Expect to see the pending changes not included in cosmos ctx")
test.AssertWasmCounterState(
&s.Suite, deps, helloWorldCounterWasm, 12, // 12 = 7 + 5
&s.Suite, deps, helloWorldCounterWasm, 7,
)

// NOTE: that the [StateDB.Commit] fn has not been called yet. We're still
// mid-transaction.

s.T().Log("EVM revert operation should bring about the old state")
test.IncrementWasmCounterWithExecuteMulti(
&s.Suite, &deps, helloWorldCounterWasm, 50, false,
&s.Suite, &deps, evmObj, helloWorldCounterWasm, 50, false,
)
s.T().Log(heredoc.Doc(`At this point, 2 precompile calls have succeeded.
One that increments the counter to 7 + 5, and another for +50.
The StateDB has not been committed. We expect to be able to revert to both
snapshots and see the prior states.`))
test.AssertWasmCounterState(
&s.Suite, deps, helloWorldCounterWasm, 7+5+50,
test.AssertWasmCounterStateWithEvm(
&s.Suite, deps, evmObj, helloWorldCounterWasm, 7+5+50,
)

errFn := common.TryCatch(func() {
Expand All @@ -220,20 +224,20 @@ snapshots and see the prior states.`))
})
s.Require().ErrorContains(errFn(), "revision id 9000 cannot be reverted")

stateDB.RevertToSnapshot(5)
test.AssertWasmCounterState(
&s.Suite, deps, helloWorldCounterWasm, 7+5,
stateDB.RevertToSnapshot(2)
test.AssertWasmCounterStateWithEvm(
&s.Suite, deps, evmObj, helloWorldCounterWasm, 7+5,
)

stateDB.RevertToSnapshot(3)
test.AssertWasmCounterState(
&s.Suite, deps, helloWorldCounterWasm, 7, // state before precompile called
stateDB.RevertToSnapshot(0)
test.AssertWasmCounterStateWithEvm(
&s.Suite, deps, evmObj, helloWorldCounterWasm, 7,
)

s.Require().NoError(stateDB.Commit())
s.Require().EqualValues(0, stateDB.DebugDirtiesCount())
test.AssertWasmCounterState(
&s.Suite, deps, helloWorldCounterWasm, 7, // state before precompile called
&s.Suite, deps, helloWorldCounterWasm, 7,
)
}

Expand Down

0 comments on commit c66d0cf

Please sign in to comment.