diff --git a/indexer/abci_test.go b/indexer/abci_test.go index de2968c..dbc40d4 100644 --- a/indexer/abci_test.go +++ b/indexer/abci_test.go @@ -4,6 +4,7 @@ import ( "math/big" "sync" "testing" + "time" "github.com/stretchr/testify/require" @@ -108,6 +109,9 @@ func Test_ListenFinalizeBlock_Subscribe(t *testing.T) { wg.Done() } } + case <-time.After(10 * time.Second): + t.Error("timeout waiting for pending transaction") + wg.Done() } } }() @@ -117,3 +121,34 @@ func Test_ListenFinalizeBlock_Subscribe(t *testing.T) { wg.Wait() } + +func Test_ListenFinalizeBlock_ContractCreation(t *testing.T) { + app, indexer, _, privKeys := setupIndexer(t) + defer app.Close() + + tx, evmTxHash := generateCreateInitiaERC20Tx(t, app, privKeys[0]) + finalizeReq, finalizeRes := executeTxs(t, app, tx) + checkTxResult(t, finalizeRes.TxResults[0], true) + + events := finalizeRes.TxResults[0].Events + createEvent := events[len(events)-3] + require.Equal(t, evmtypes.EventTypeContractCreated, createEvent.GetType()) + + contractAddr, err := hexutil.Decode(createEvent.Attributes[0].Value) + require.NoError(t, err) + + // listen finalize block + ctx, err := app.CreateQueryContext(0, false) + require.NoError(t, err) + + err = indexer.ListenFinalizeBlock(ctx.WithBlockGasMeter(storetypes.NewInfiniteGasMeter()), *finalizeReq, *finalizeRes) + require.NoError(t, err) + + // check the tx is indexed + receipt, err := indexer.TxReceiptByHash(ctx, evmTxHash) + require.NoError(t, err) + require.NotNil(t, receipt) + + // contract creation should have contract address in receipt + require.Equal(t, contractAddr, receipt.ContractAddress.Bytes()) +} diff --git a/indexer/mempool_test.go b/indexer/mempool_test.go index 4ea97ed..82e205c 100644 --- a/indexer/mempool_test.go +++ b/indexer/mempool_test.go @@ -3,6 +3,7 @@ package indexer_test import ( "sync" "testing" + "time" "github.com/stretchr/testify/require" @@ -22,10 +23,15 @@ func Test_Mempool_Subscribe(t *testing.T) { wg := sync.WaitGroup{} wg.Add(1) go func() { - pendingTx := <-pendChan - require.NotNil(t, pendingTx) - require.Equal(t, evmTxHash, pendingTx.Hash) - wg.Done() + select { + case pendingTx := <-pendChan: + require.NotNil(t, pendingTx) + require.Equal(t, evmTxHash, pendingTx.Hash) + wg.Done() + case <-time.After(5 * time.Second): + t.Error("timeout waiting for pending transaction") + wg.Done() + } }() noopMempool := &mempool.NoOpMempool{} diff --git a/indexer/tx_helper_test.go b/indexer/tx_helper_test.go index 0479a8b..c67e4a8 100644 --- a/indexer/tx_helper_test.go +++ b/indexer/tx_helper_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" coretypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -22,6 +23,7 @@ import ( minitiaapp "github.com/initia-labs/minievm/app" "github.com/initia-labs/minievm/x/evm/contracts/erc20" "github.com/initia-labs/minievm/x/evm/contracts/erc20_factory" + "github.com/initia-labs/minievm/x/evm/contracts/initia_erc20" evmkeeper "github.com/initia-labs/minievm/x/evm/keeper" evmtypes "github.com/initia-labs/minievm/x/evm/types" ) @@ -98,6 +100,19 @@ func generateTx( return sdkTx, signedTx.Hash() } +func generateCreateInitiaERC20Tx(t *testing.T, app *minitiaapp.MinitiaApp, privKey *ecdsa.PrivateKey, seqNum ...uint64) (sdk.Tx, common.Hash) { + abi, err := initia_erc20.InitiaErc20MetaData.GetAbi() + require.NoError(t, err) + + bin, err := hexutil.Decode(initia_erc20.InitiaErc20MetaData.Bin) + require.NoError(t, err) + + inputBz, err := abi.Pack("", "foo", "foo", uint8(6)) + require.NoError(t, err) + + return generateTx(t, app, privKey, nil, append(bin, inputBz...), seqNum...) +} + func generateCreateERC20Tx(t *testing.T, app *minitiaapp.MinitiaApp, privKey *ecdsa.PrivateKey, seqNum ...uint64) (sdk.Tx, common.Hash) { ctx, err := app.CreateQueryContext(0, false) require.NoError(t, err)