Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Nov 28, 2024
1 parent 3364153 commit de591a9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
9 changes: 5 additions & 4 deletions indexer/abci_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package indexer_test

import (
"context"
"math/big"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
}
}()
Expand All @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions indexer/mempool_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package indexer_test

import (
"context"
"sync"
"testing"
"time"

"github.com/initia-labs/minievm/tests"
"github.com/stretchr/testify/require"
Expand All @@ -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() {
Expand All @@ -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
}
}()

Expand All @@ -50,4 +50,5 @@ func Test_Mempool_Subscribe(t *testing.T) {
require.Equal(t, evmTxHash, rpcTx.Hash)

wg.Wait()
done()
}
3 changes: 0 additions & 3 deletions jsonrpc/namespaces/eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package filters
import (
"context"
"errors"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -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...)
Expand Down
60 changes: 59 additions & 1 deletion jsonrpc/namespaces/eth/filters/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

0 comments on commit de591a9

Please sign in to comment.