From de591a9a84d1bf126b3fa6802fdc365e5b8fe02b Mon Sep 17 00:00:00 2001 From: beer-1 Date: Thu, 28 Nov 2024 17:56:52 +0900 Subject: [PATCH] fix test --- indexer/abci_test.go | 9 ++-- indexer/mempool_test.go | 9 ++-- jsonrpc/namespaces/eth/filters/api.go | 3 -- jsonrpc/namespaces/eth/filters/api_test.go | 60 +++++++++++++++++++++- 4 files changed, 69 insertions(+), 12 deletions(-) diff --git a/indexer/abci_test.go b/indexer/abci_test.go index 96d832a8..960e0b62 100644 --- a/indexer/abci_test.go +++ b/indexer/abci_test.go @@ -1,10 +1,10 @@ package indexer_test import ( + "context" "math/big" "sync" "testing" - "time" "github.com/stretchr/testify/require" @@ -74,6 +74,7 @@ func Test_ListenFinalizeBlock_Subscribe(t *testing.T) { tx, evmTxHash := tests.GenerateCreateERC20Tx(t, app, privKeys[0]) + ctx, done := context.WithCancel(context.Background()) reqHeight := app.LastBlockHeight() + 1 wg := sync.WaitGroup{} wg.Add(2) @@ -93,9 +94,8 @@ func Test_ListenFinalizeBlock_Subscribe(t *testing.T) { } wg.Done() - case <-time.After(10 * time.Second): - t.Error("timeout waiting for pending transaction") - wg.Done() + case <-ctx.Done(): + return } } }() @@ -109,6 +109,7 @@ func Test_ListenFinalizeBlock_Subscribe(t *testing.T) { require.Equal(t, evmtypes.EventTypeContractCreated, createEvent.GetType()) wg.Wait() + done() } func Test_ListenFinalizeBlock_ContractCreation(t *testing.T) { diff --git a/indexer/mempool_test.go b/indexer/mempool_test.go index 7fe1bd57..c16c3eeb 100644 --- a/indexer/mempool_test.go +++ b/indexer/mempool_test.go @@ -1,9 +1,9 @@ package indexer_test import ( + "context" "sync" "testing" - "time" "github.com/initia-labs/minievm/tests" "github.com/stretchr/testify/require" @@ -23,6 +23,7 @@ func Test_Mempool_Subscribe(t *testing.T) { tx, evmTxHash := tests.GenerateCreateERC20Tx(t, app, privKeys[0]) + ctx, done := context.WithCancel(context.Background()) wg := sync.WaitGroup{} wg.Add(1) go func() { @@ -31,9 +32,8 @@ func Test_Mempool_Subscribe(t *testing.T) { 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() + case <-ctx.Done(): + return } }() @@ -50,4 +50,5 @@ func Test_Mempool_Subscribe(t *testing.T) { require.Equal(t, evmTxHash, rpcTx.Hash) wg.Wait() + done() } diff --git a/jsonrpc/namespaces/eth/filters/api.go b/jsonrpc/namespaces/eth/filters/api.go index 4b3346ea..4e6b28e0 100644 --- a/jsonrpc/namespaces/eth/filters/api.go +++ b/jsonrpc/namespaces/eth/filters/api.go @@ -3,7 +3,6 @@ package filters import ( "context" "errors" - "fmt" "sync" "time" @@ -340,9 +339,7 @@ func (api *FilterAPI) NewFilter(crit ethfilters.FilterCriteria) (rpc.ID, error) for { select { case logs := <-logsChan: - fmt.Println(logs) logs = filterLogs(logs, s.crit.FromBlock, s.crit.ToBlock, s.crit.Addresses, s.crit.Topics) - fmt.Println(logs) api.filtersMut.Lock() if f, found := api.filters[id]; found { f.logs = append(f.logs, logs...) diff --git a/jsonrpc/namespaces/eth/filters/api_test.go b/jsonrpc/namespaces/eth/filters/api_test.go index 3dbb7630..6982603d 100644 --- a/jsonrpc/namespaces/eth/filters/api_test.go +++ b/jsonrpc/namespaces/eth/filters/api_test.go @@ -226,6 +226,9 @@ func Test_NewBlockFilter(t *testing.T) { _, finalizeRes = tests.ExecuteTxs(t, app, tx2) tests.CheckTxResult(t, finalizeRes.TxResults[0], true) + // wait txs to be indexed + time.Sleep(2 * time.Second) + // there should be 2 changes changes, err := input.filterAPI.GetFilterChanges(filterID) require.NoError(t, err) @@ -281,11 +284,66 @@ func Test_NewFilter(t *testing.T) { _, finalizeRes = tests.ExecuteTxs(t, app, tx2) tests.CheckTxResult(t, finalizeRes.TxResults[0], true) + // wait txs to be indexed + time.Sleep(2 * time.Second) + changes, err := input.filterAPI.GetFilterChanges(filterID) require.NoError(t, err) require.NotEmpty(t, changes) for _, change := range changes.([]*coretypes.Log) { require.Equal(t, txHash2, change.TxHash) - t.Logf("%v", change) + } +} + +func Test_GetLogs(t *testing.T) { + input := setupFilterAPI(t) + defer input.app.Close() + + app, addrs, privKeys := input.app, input.addrs, input.privKeys + + // invalid block range + _, err := input.filterAPI.NewFilter(ethfilters.FilterCriteria{ + FromBlock: big.NewInt(100), + ToBlock: big.NewInt(10), + }) + require.Error(t, err) + + tx, txHash1 := tests.GenerateCreateERC20Tx(t, app, privKeys[0]) + _, finalizeRes := tests.ExecuteTxs(t, app, tx) + tests.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) + + // mint 1_000_000 tokens to the first address + tx2, txHash2 := tests.GenerateMintERC20Tx(t, app, privKeys[0], common.BytesToAddress(contractAddr), addrs[0], new(big.Int).SetUint64(1_000_000_000_000)) + _, finalizeRes = tests.ExecuteTxs(t, app, tx2) + tests.CheckTxResult(t, finalizeRes.TxResults[0], true) + + // wait txs to be indexed + time.Sleep(2 * time.Second) + + logs, err := input.filterAPI.GetLogs(context.Background(), ethfilters.FilterCriteria{ + FromBlock: big.NewInt(app.LastBlockHeight()), + ToBlock: big.NewInt(app.LastBlockHeight()), + }) + require.NoError(t, err) + require.NotEmpty(t, logs) + for _, log := range logs { + require.Equal(t, txHash2, log.TxHash) + } + + logs, err = input.filterAPI.GetLogs(context.Background(), ethfilters.FilterCriteria{ + FromBlock: big.NewInt(app.LastBlockHeight() - 1), + ToBlock: big.NewInt(app.LastBlockHeight() - 1), + }) + require.NoError(t, err) + require.NotEmpty(t, logs) + for _, log := range logs { + require.Equal(t, txHash1, log.TxHash) } }