Skip to content

Commit

Permalink
add testcase to ensure contract address in the receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Nov 22, 2024
1 parent de36cfd commit 299b81e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
35 changes: 35 additions & 0 deletions indexer/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/big"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -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()
}
}
}()
Expand All @@ -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())
}
14 changes: 10 additions & 4 deletions indexer/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indexer_test
import (
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand All @@ -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{}
Expand Down
15 changes: 15 additions & 0 deletions indexer/tx_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 299b81e

Please sign in to comment.