Skip to content

Commit

Permalink
Remove concurrency from Filter e2e tests (0xPolygon#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos authored May 10, 2022
1 parent 61e5829 commit 568a985
Showing 1 changed file with 53 additions and 59 deletions.
112 changes: 53 additions & 59 deletions e2e/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package e2e
import (
"context"
"math/big"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -48,89 +47,84 @@ func TestNewFilter_Logs(t *testing.T) {

txpoolClient := srv.TxnPoolOperator()
jsonRPCClient := srv.JSONRPC()

id, err := jsonRPCClient.Eth().NewFilter(&web3.LogFilter{})
assert.NoError(t, err)

numCalls := 10
txn, err := tests.GenerateAddTxnReq(tests.GenerateTxReqParams{
Nonce: 1, // The first transaction was a contract deployment
ReferenceAddr: addr,
ReferenceKey: key,
ToAddress: castedContractAddr,
GasPrice: big.NewInt(framework.DefaultGasPrice),
Input: framework.MethodSig("setA1"),
})
if err != nil {
return
}

var (
wg sync.WaitGroup
)
addTxnContext, addTxnCancelFn := context.WithTimeout(context.Background(), framework.DefaultTimeout)
defer addTxnCancelFn()

for i := 1; i <= numCalls; i++ {
wg.Add(1)

go func(nonce uint64) {
defer wg.Done()

txn, err := tests.GenerateAddTxnReq(tests.GenerateTxReqParams{
Nonce: nonce,
ReferenceAddr: addr,
ReferenceKey: key,
ToAddress: castedContractAddr,
GasPrice: big.NewInt(framework.DefaultGasPrice),
Input: framework.MethodSig("setA1"),
})
if err != nil {
return
}

addTxnContext, cancelFn := context.WithTimeout(context.Background(), framework.DefaultTimeout)
defer cancelFn()

addResp, addErr := txpoolClient.AddTxn(addTxnContext, txn)
if addErr != nil {
return
}

receiptContext, cancelFn := context.WithTimeout(context.Background(), framework.DefaultTimeout)
defer cancelFn()

txHash := web3.Hash(types.StringToHash(addResp.TxHash))
if _, receiptErr := srv.WaitForReceipt(receiptContext, txHash); receiptErr != nil {
return
}
}(uint64(i))
addResp, addErr := txpoolClient.AddTxn(addTxnContext, txn)
if addErr != nil {
t.Fatalf("Unable to add transaction, %v", addErr)
}

wg.Wait()
receiptContext, cancelFn := context.WithTimeout(context.Background(), framework.DefaultTimeout)
defer cancelFn()

txHash := web3.Hash(types.StringToHash(addResp.TxHash))
if _, receiptErr := srv.WaitForReceipt(receiptContext, txHash); receiptErr != nil {
t.Fatalf("Unable to wait for receipt, %v", receiptErr)
}

res, err := jsonRPCClient.Eth().GetFilterChanges(id)

assert.NoError(t, err)
assert.Equal(t, numCalls, len(res))
assert.Equal(t, 1, len(res))
}

func TestNewFilter_Block(t *testing.T) {
fromKey, from := tests.GenerateKeyAndAddr(t)
_, to := tests.GenerateKeyAndAddr(t)

srvs := framework.NewTestServers(t, 1, func(config *framework.TestServerConfig) {
config.SetConsensus(framework.ConsensusDev)
config.Premine(from, framework.EthToWei(10))
config.SetSeal(true)
})
srv := srvs[0]
ibftManager := framework.NewIBFTServersManager(
t,
1,
IBFTDirPrefix,
func(i int, config *framework.TestServerConfig) {
config.Premine(from, framework.EthToWei(10))
config.SetBlockTime(1)
},
)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

ibftManager.StartServers(ctx)
srv := ibftManager.GetServer(0)

client := srv.JSONRPC()

id, err := client.Eth().NewBlockFilter()
assert.NoError(t, err)

for i := 0; i < 3; i++ {
ctx, cancel := context.WithTimeout(context.Background(), framework.DefaultTimeout)
_, err := srv.SendRawTx(ctx, &framework.PreparedTransaction{
From: from,
To: &to,
GasPrice: big.NewInt(10000),
Gas: 1000000,
Value: big.NewInt(10000),
}, fromKey)

assert.NoError(t, err)
ctx, cancelFn := context.WithTimeout(context.Background(), framework.DefaultTimeout)
defer cancelFn()

cancel()
if _, sendErr := srv.SendRawTx(ctx, &framework.PreparedTransaction{
From: from,
To: &to,
GasPrice: big.NewInt(10000),
Gas: 1000000,
Value: big.NewInt(10000),
}, fromKey); err != nil {
t.Fatalf("Unable to send transaction %v", sendErr)
}

assert.NoError(t, err)

// verify filter picked up block changes
blocks, err := client.Eth().GetFilterChangesBlock(id)
assert.NoError(t, err)
Expand Down

0 comments on commit 568a985

Please sign in to comment.