From 8111ba50b1b43581751a129b36ebf4b1833df3b3 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Fri, 1 Apr 2022 18:48:58 +0200 Subject: [PATCH 01/14] Remove the waitForBlock method in TestTxPool_TransactionCoalescing --- e2e/txpool_test.go | 75 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 15 deletions(-) diff --git a/e2e/txpool_test.go b/e2e/txpool_test.go index 76160c5c33..9fc945a5bf 100644 --- a/e2e/txpool_test.go +++ b/e2e/txpool_test.go @@ -202,16 +202,22 @@ func TestTxPool_TransactionCoalescing(t *testing.T) { referenceKey, referenceAddr := tests.GenerateKeyAndAddr(t) defaultBalance := framework.EthToWei(10) - devInterval := 5 - // Set up the test server - srvs := framework.NewTestServers(t, 1, func(config *framework.TestServerConfig) { - config.SetConsensus(framework.ConsensusDev) - config.SetSeal(true) - config.SetDevInterval(devInterval) - config.Premine(referenceAddr, defaultBalance) - }) - srv := srvs[0] + ibftManager := framework.NewIBFTServersManager( + t, + 1, + IBFTDirPrefix, + func(i int, config *framework.TestServerConfig) { + config.SetSeal(true) + config.Premine(referenceAddr, defaultBalance) + }, + ) + + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + ibftManager.StartServers(ctx) + + srv := ibftManager.GetServer(0) client := srv.JSONRPC() // Required default values @@ -251,19 +257,40 @@ func TestTxPool_TransactionCoalescing(t *testing.T) { return msg } + // testTransaction is a helper structure for + // keeping track of test transaction execution + type testTransaction struct { + txHash web3.Hash // the transaction hash + block *uint64 // the block the transaction was included in + } + + testTransactions := make([]*testTransaction, 0) + // Add the transactions with the following nonce order nonces := []uint64{0, 2} for i := 0; i < len(nonces); i++ { addReq := generateReq(nonces[i]) - _, addErr := clt.AddTxn(context.Background(), addReq) + addResp, addErr := clt.AddTxn(context.Background(), addReq) if addErr != nil { t.Fatalf("Unable to add txn, %v", addErr) } + + testTransactions = append(testTransactions, &testTransaction{ + txHash: web3.HexToHash(addResp.TxHash), + }) + } + + // Wait for the first transaction to go through + ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) + defer cancelFn() + + receipt, receiptErr := tests.WaitForReceipt(ctx, client.Eth(), testTransactions[0].txHash) + if receiptErr != nil { + t.Fatalf("unable to wait for receipt, %v", receiptErr) } - // Wait for the state transition to be executed - _ = waitForBlock(t, srv, 1, 0) + testTransactions[0].block = &receipt.BlockNumber // Get to account balance // Only the first tx should've gone through @@ -277,13 +304,28 @@ func TestTxPool_TransactionCoalescing(t *testing.T) { // Add the transaction with the gap nonce value addReq := generateReq(1) - _, addErr := clt.AddTxn(context.Background(), addReq) + addResp, addErr := clt.AddTxn(context.Background(), addReq) if addErr != nil { t.Fatalf("Unable to add txn, %v", addErr) } - // Wait for the state transition to be executed - _ = waitForBlock(t, srv, 1, 0) + testTransactions = append(testTransactions, &testTransaction{ + txHash: web3.HexToHash(addResp.TxHash), + }) + + for i := 1; i < len(testTransactions); i++ { + // Wait for the first transaction to go through + ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) + + receipt, receiptErr := tests.WaitForReceipt(ctx, client.Eth(), testTransactions[i].txHash) + if receiptErr != nil { + t.Fatalf("unable to wait for receipt, %v", receiptErr) + } + + testTransactions[i].block = &receipt.BlockNumber + + cancelFn() + } // Now both the added tx and the shelved tx should've gone through toAccountBalance = framework.GetAccountBalance(t, toAddress, client) @@ -292,6 +334,9 @@ func TestTxPool_TransactionCoalescing(t *testing.T) { toAccountBalance.String(), "To address balance mismatch after gap transaction", ) + + // Make sure the first transaction and the last transaction didn't get included in the same block + assert.NotEqual(t, *(testTransactions[0].block), *(testTransactions[2].block)) } type testAccount struct { From 7666002b01c1a406434df0db3cd71691abf6acf5 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Fri, 1 Apr 2022 18:55:00 +0200 Subject: [PATCH 02/14] Add configurable block time for the test server --- e2e/framework/config.go | 5 +++++ e2e/framework/testserver.go | 4 ++++ e2e/txpool_test.go | 1 + 3 files changed, 10 insertions(+) diff --git a/e2e/framework/config.go b/e2e/framework/config.go index 09a838119d..db182e7128 100644 --- a/e2e/framework/config.go +++ b/e2e/framework/config.go @@ -48,6 +48,7 @@ type TestServerConfig struct { Signer *crypto.EIP155Signer // Signer used for transactions MinValidatorCount uint64 // Min validator count MaxValidatorCount uint64 // Max validator count + BlockTime uint64 // Minimum block generation time (in s) } // DataDir returns path of data directory server uses @@ -64,6 +65,10 @@ func (t *TestServerConfig) SetSigner(signer *crypto.EIP155Signer) { t.Signer = signer } +func (t *TestServerConfig) SetBlockTime(blockTime uint64) { + t.BlockTime = blockTime +} + // PrivateKey returns a private key in data directory func (t *TestServerConfig) PrivateKey() (*ecdsa.PrivateKey, error) { return crypto.GenerateOrReadPrivateKey(filepath.Join(t.DataDir(), "consensus", ibft.IbftKeyName)) diff --git a/e2e/framework/testserver.go b/e2e/framework/testserver.go index 1b50293a7c..4d295f95ee 100644 --- a/e2e/framework/testserver.go +++ b/e2e/framework/testserver.go @@ -355,6 +355,10 @@ func (t *TestServer) Start(ctx context.Context) error { args = append(args, "--block-gas-target", *types.EncodeUint64(t.Config.BlockGasTarget)) } + if t.Config.BlockTime != 0 { + args = append(args, "--block-time", strconv.FormatUint(t.Config.BlockTime, 10)) + } + t.ReleaseReservedPorts() // Start the server diff --git a/e2e/txpool_test.go b/e2e/txpool_test.go index 9fc945a5bf..5b4138e797 100644 --- a/e2e/txpool_test.go +++ b/e2e/txpool_test.go @@ -210,6 +210,7 @@ func TestTxPool_TransactionCoalescing(t *testing.T) { func(i int, config *framework.TestServerConfig) { config.SetSeal(true) config.Premine(referenceAddr, defaultBalance) + config.SetBlockTime(1) }, ) From ce4db861b0703eaef005738fa13b41a91a23c691 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Fri, 1 Apr 2022 19:03:35 +0200 Subject: [PATCH 03/14] Remove waitForBlock in TestPoS_UnstakeExploit --- e2e/pos_test.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/e2e/pos_test.go b/e2e/pos_test.go index 2aa46e2ad7..74cf189b04 100644 --- a/e2e/pos_test.go +++ b/e2e/pos_test.go @@ -375,6 +375,8 @@ func TestPoS_UnstakeExploit(t *testing.T) { return signedTx } + txHashes := make([]web3.Hash, 0) + for i := 0; i < numTransactions; i++ { var msg *txpoolOp.AddTxnReq @@ -387,22 +389,32 @@ func TestPoS_UnstakeExploit(t *testing.T) { From: types.ZeroAddress.String(), } - _, addErr := clt.AddTxn(context.Background(), msg) + addResp, addErr := clt.AddTxn(context.Background(), msg) if addErr != nil { t.Fatalf("Unable to add txn, %v", addErr) } + + txHashes = append(txHashes, web3.HexToHash(addResp.TxHash)) } - // Set up the blockchain listener to catch the added block event - blockNum := waitForBlock(t, srv, 1, 0) + // Wait for the transactions to go through + totalGasUsed := uint64(0) - block, blockErr := client.Eth().GetBlockByNumber(web3.BlockNumber(blockNum), true) - if blockErr != nil { - t.Fatalf("Unable to fetch block") + for _, txHash := range txHashes { + ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) + + receipt, receiptErr := tests.WaitForReceipt(ctx, client.Eth(), txHash) + if receiptErr != nil { + t.Fatalf("unable to wait for receipt, %v", receiptErr) + } + + cancelFn() + + totalGasUsed += receipt.GasUsed } // Find how much the address paid for all the transactions in this block - paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(block.GasUsed))) + paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(totalGasUsed))) // Check the balances actualAccountBalance := framework.GetAccountBalance(t, senderAddr, client) From d5d2cb4450de68be05a3f3b6427391d02b8b0026 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Fri, 1 Apr 2022 19:15:20 +0200 Subject: [PATCH 04/14] Remove waitForBlock in TestPoS_StakeUnstakeExploit --- e2e/pos_test.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/e2e/pos_test.go b/e2e/pos_test.go index 74cf189b04..74af48efe6 100644 --- a/e2e/pos_test.go +++ b/e2e/pos_test.go @@ -529,6 +529,7 @@ func TestPoS_StakeUnstakeExploit(t *testing.T) { oneEth := framework.EthToWei(1) zeroEth := framework.EthToWei(0) + txHashes := make([]web3.Hash, 0) for i := 0; i < numTransactions; i++ { var msg *txpoolOp.AddTxnReq @@ -551,22 +552,32 @@ func TestPoS_StakeUnstakeExploit(t *testing.T) { } } - _, addErr := txpoolClient.AddTxn(context.Background(), msg) + addResp, addErr := txpoolClient.AddTxn(context.Background(), msg) if addErr != nil { t.Fatalf("Unable to add txn, %v", addErr) } + + txHashes = append(txHashes, web3.HexToHash(addResp.TxHash)) } // Set up the blockchain listener to catch the added block event - blockNum := waitForBlock(t, srv, 1, 0) + totalGasUsed := uint64(0) - block, blockErr := client.Eth().GetBlockByNumber(web3.BlockNumber(blockNum), true) - if blockErr != nil { - t.Fatalf("Unable to fetch block") + for _, txHash := range txHashes { + ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) + + receipt, receiptErr := tests.WaitForReceipt(ctx, client.Eth(), txHash) + if receiptErr != nil { + t.Fatalf("unable to wait for receipt, %v", receiptErr) + } + + cancelFn() + + totalGasUsed += receipt.GasUsed } // Find how much the address paid for all the transactions in this block - paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(block.GasUsed))) + paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(totalGasUsed))) // Check the balances actualAccountBalance := framework.GetAccountBalance(t, senderAddr, client) From 1573d142baa64fe4100d6cc11eb0a09920a935ad Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Fri, 1 Apr 2022 19:25:30 +0200 Subject: [PATCH 05/14] Extract commong gas total wait logic, remove waitForBlock in TestPoS_StakeUnstakeWithinSameBlock --- e2e/framework/testserver.go | 21 +++++++++++++++++ e2e/pos_test.go | 47 ++++++++----------------------------- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/e2e/framework/testserver.go b/e2e/framework/testserver.go index 4d295f95ee..641611c7a4 100644 --- a/e2e/framework/testserver.go +++ b/e2e/framework/testserver.go @@ -542,6 +542,27 @@ func (t *TestServer) WaitForReceipt(ctx context.Context, hash web3.Hash) (*web3. return data.receipt, data.err } +// WaitForGasTotal waits for the total gas used sum for the passed in +// transactions +func (t *TestServer) WaitForGasTotal(txHashes []web3.Hash) uint64 { + totalGasUsed := uint64(0) + + for _, txHash := range txHashes { + ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) + + receipt, receiptErr := tests.WaitForReceipt(ctx, t.JSONRPC().Eth(), txHash) + if receiptErr != nil { + t.t.Fatalf("unable to wait for receipt, %v", receiptErr) + } + + cancelFn() + + totalGasUsed += receipt.GasUsed + } + + return totalGasUsed +} + func (t *TestServer) WaitForReady(ctx context.Context) error { _, err := tests.RetryUntilTimeout(ctx, func() (interface{}, bool) { num, err := t.GetLatestBlockHeight() diff --git a/e2e/pos_test.go b/e2e/pos_test.go index 74af48efe6..02a8f536de 100644 --- a/e2e/pos_test.go +++ b/e2e/pos_test.go @@ -398,20 +398,7 @@ func TestPoS_UnstakeExploit(t *testing.T) { } // Wait for the transactions to go through - totalGasUsed := uint64(0) - - for _, txHash := range txHashes { - ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) - - receipt, receiptErr := tests.WaitForReceipt(ctx, client.Eth(), txHash) - if receiptErr != nil { - t.Fatalf("unable to wait for receipt, %v", receiptErr) - } - - cancelFn() - - totalGasUsed += receipt.GasUsed - } + totalGasUsed := srv.WaitForGasTotal(txHashes) // Find how much the address paid for all the transactions in this block paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(totalGasUsed))) @@ -561,20 +548,7 @@ func TestPoS_StakeUnstakeExploit(t *testing.T) { } // Set up the blockchain listener to catch the added block event - totalGasUsed := uint64(0) - - for _, txHash := range txHashes { - ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) - - receipt, receiptErr := tests.WaitForReceipt(ctx, client.Eth(), txHash) - if receiptErr != nil { - t.Fatalf("unable to wait for receipt, %v", receiptErr) - } - - cancelFn() - - totalGasUsed += receipt.GasUsed - } + totalGasUsed := srv.WaitForGasTotal(txHashes) // Find how much the address paid for all the transactions in this block paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(totalGasUsed))) @@ -673,6 +647,8 @@ func TestPoS_StakeUnstakeWithinSameBlock(t *testing.T) { } zeroEth := framework.EthToWei(0) + txHashes := make([]web3.Hash, 0) + // addTxn is a helper method for generating and adding a transaction // through the operator command addTxn := func(value *big.Int, methodName string) { @@ -684,10 +660,12 @@ func TestPoS_StakeUnstakeWithinSameBlock(t *testing.T) { From: types.ZeroAddress.String(), } - _, addErr := txpoolClient.AddTxn(context.Background(), txnMsg) + addResp, addErr := txpoolClient.AddTxn(context.Background(), txnMsg) if addErr != nil { t.Fatalf("Unable to add txn, %v", addErr) } + + txHashes = append(txHashes, web3.HexToHash(addResp.TxHash)) } // Stake transaction @@ -696,16 +674,11 @@ func TestPoS_StakeUnstakeWithinSameBlock(t *testing.T) { // Unstake transaction addTxn(zeroEth, "unstake") - // Set up the blockchain listener to catch the added block event - blockNum := waitForBlock(t, srv, 1, 0) - - block, blockErr := client.Eth().GetBlockByNumber(web3.BlockNumber(blockNum), true) - if blockErr != nil { - t.Fatalf("Unable to fetch block") - } + // Wait for the transactions to go through + totalGasUsed := srv.WaitForGasTotal(txHashes) // Find how much the address paid for all the transactions in this block - paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(block.GasUsed))) + paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(totalGasUsed))) // Check the balances actualAccountBalance := framework.GetAccountBalance(t, senderAddr, client) From a014fdb903a1aeb37f4fe68737d16cbe3dd567a3 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Fri, 1 Apr 2022 19:26:06 +0200 Subject: [PATCH 06/14] Remove waitForBlock --- e2e/txpool_test.go | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/e2e/txpool_test.go b/e2e/txpool_test.go index 5b4138e797..ca5f0924ea 100644 --- a/e2e/txpool_test.go +++ b/e2e/txpool_test.go @@ -5,7 +5,6 @@ import ( "crypto/ecdsa" "errors" "fmt" - "io" "math/big" "sync" "testing" @@ -18,7 +17,6 @@ import ( txpoolOp "github.com/0xPolygon/polygon-edge/txpool/proto" "github.com/0xPolygon/polygon-edge/types" "github.com/golang/protobuf/ptypes/any" - "github.com/golang/protobuf/ptypes/empty" "github.com/stretchr/testify/assert" "github.com/umbracle/go-web3" ) @@ -28,36 +26,6 @@ var ( signer = crypto.NewEIP155Signer(100) ) -func waitForBlock(t *testing.T, srv *framework.TestServer, expectedBlocks int, index int) int64 { - t.Helper() - - systemClient := srv.Operator() - ctx, cancelFn := context.WithCancel(context.Background()) - stream, err := systemClient.Subscribe(ctx, &empty.Empty{}) - - if err != nil { - cancelFn() - t.Fatalf("Unable to subscribe to blockchain events") - } - - evnt, err := stream.Recv() - if errors.Is(err, io.EOF) { - t.Fatalf("Invalid stream close") - } - - if err != nil { - t.Fatalf("Unable to read blockchain event") - } - - if len(evnt.Added) != expectedBlocks { - t.Fatalf("Invalid number of blocks added") - } - - cancelFn() - - return evnt.Added[index].Number -} - type generateTxReqParams struct { nonce uint64 referenceAddr types.Address From a1ae785881df2f888aef968e3c384ce94ad27345 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Fri, 1 Apr 2022 19:38:52 +0200 Subject: [PATCH 07/14] Make the gas usage wait func concurrent --- e2e/framework/testserver.go | 43 ++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/e2e/framework/testserver.go b/e2e/framework/testserver.go index 641611c7a4..12955eb178 100644 --- a/e2e/framework/testserver.go +++ b/e2e/framework/testserver.go @@ -13,6 +13,8 @@ import ( "path/filepath" "strconv" "strings" + "sync" + "sync/atomic" "testing" "time" @@ -545,19 +547,44 @@ func (t *TestServer) WaitForReceipt(ctx context.Context, hash web3.Hash) (*web3. // WaitForGasTotal waits for the total gas used sum for the passed in // transactions func (t *TestServer) WaitForGasTotal(txHashes []web3.Hash) uint64 { - totalGasUsed := uint64(0) + var ( + totalGasUsed = uint64(0) + receiptErrs = make([]error, 0) + receiptErrsLock sync.Mutex + wg sync.WaitGroup + ) + + appendReceiptErr := func(receiptErr error) { + receiptErrsLock.Lock() + defer receiptErrsLock.Unlock() + + receiptErrs = append(receiptErrs, receiptErr) + } for _, txHash := range txHashes { - ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) + wg.Add(1) - receipt, receiptErr := tests.WaitForReceipt(ctx, t.JSONRPC().Eth(), txHash) - if receiptErr != nil { - t.t.Fatalf("unable to wait for receipt, %v", receiptErr) - } + go func(txHash web3.Hash) { + defer wg.Done() + + ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) + defer cancelFn() + + receipt, receiptErr := tests.WaitForReceipt(ctx, t.JSONRPC().Eth(), txHash) + if receiptErr != nil { + appendReceiptErr(fmt.Errorf("unable to wait for receipt, %w", receiptErr)) + + return + } + + atomic.AddUint64(&totalGasUsed, receipt.GasUsed) + }(txHash) + } - cancelFn() + wg.Wait() - totalGasUsed += receipt.GasUsed + if len(receiptErrs) > 0 { + t.t.Fatalf("unable to wait for receipts, %v", receiptErrs) } return totalGasUsed From 8a2425fbbd74889d0de9e2822d502416982ee361 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Thu, 28 Apr 2022 09:28:36 +0200 Subject: [PATCH 08/14] Rename method to be more clear --- e2e/framework/testserver.go | 4 ++-- e2e/pos_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/e2e/framework/testserver.go b/e2e/framework/testserver.go index 12955eb178..06cd6e1a31 100644 --- a/e2e/framework/testserver.go +++ b/e2e/framework/testserver.go @@ -544,9 +544,9 @@ func (t *TestServer) WaitForReceipt(ctx context.Context, hash web3.Hash) (*web3. return data.receipt, data.err } -// WaitForGasTotal waits for the total gas used sum for the passed in +// GetGasTotal waits for the total gas used sum for the passed in // transactions -func (t *TestServer) WaitForGasTotal(txHashes []web3.Hash) uint64 { +func (t *TestServer) GetGasTotal(txHashes []web3.Hash) uint64 { var ( totalGasUsed = uint64(0) receiptErrs = make([]error, 0) diff --git a/e2e/pos_test.go b/e2e/pos_test.go index 02a8f536de..52ab2f1583 100644 --- a/e2e/pos_test.go +++ b/e2e/pos_test.go @@ -398,7 +398,7 @@ func TestPoS_UnstakeExploit(t *testing.T) { } // Wait for the transactions to go through - totalGasUsed := srv.WaitForGasTotal(txHashes) + totalGasUsed := srv.GetGasTotal(txHashes) // Find how much the address paid for all the transactions in this block paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(totalGasUsed))) @@ -548,7 +548,7 @@ func TestPoS_StakeUnstakeExploit(t *testing.T) { } // Set up the blockchain listener to catch the added block event - totalGasUsed := srv.WaitForGasTotal(txHashes) + totalGasUsed := srv.GetGasTotal(txHashes) // Find how much the address paid for all the transactions in this block paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(totalGasUsed))) @@ -675,7 +675,7 @@ func TestPoS_StakeUnstakeWithinSameBlock(t *testing.T) { addTxn(zeroEth, "unstake") // Wait for the transactions to go through - totalGasUsed := srv.WaitForGasTotal(txHashes) + totalGasUsed := srv.GetGasTotal(txHashes) // Find how much the address paid for all the transactions in this block paidFee := big.NewInt(0).Mul(bigGasPrice, big.NewInt(int64(totalGasUsed))) From 908c772d4171b1d1f27aa0077b3e254d6b337e7e Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Thu, 28 Apr 2022 09:30:55 +0200 Subject: [PATCH 09/14] Add context to TestPoS_UnstakeExploit --- e2e/pos_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/e2e/pos_test.go b/e2e/pos_test.go index 52ab2f1583..f1b622093c 100644 --- a/e2e/pos_test.go +++ b/e2e/pos_test.go @@ -389,12 +389,16 @@ func TestPoS_UnstakeExploit(t *testing.T) { From: types.ZeroAddress.String(), } - addResp, addErr := clt.AddTxn(context.Background(), msg) + addCtx, addCtxCn := context.WithTimeout(context.Background(), time.Second*10) + + addResp, addErr := clt.AddTxn(addCtx, msg) if addErr != nil { t.Fatalf("Unable to add txn, %v", addErr) } txHashes = append(txHashes, web3.HexToHash(addResp.TxHash)) + + addCtxCn() } // Wait for the transactions to go through From 1ab4ac5dc3c5bb958170efe056a3d0238c1861f5 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Thu, 28 Apr 2022 09:32:17 +0200 Subject: [PATCH 10/14] Add context to TestTxPool_TransactionCoalescing --- e2e/txpool_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/e2e/txpool_test.go b/e2e/txpool_test.go index ca5f0924ea..2c0f90cc92 100644 --- a/e2e/txpool_test.go +++ b/e2e/txpool_test.go @@ -240,7 +240,9 @@ func TestTxPool_TransactionCoalescing(t *testing.T) { for i := 0; i < len(nonces); i++ { addReq := generateReq(nonces[i]) - addResp, addErr := clt.AddTxn(context.Background(), addReq) + addCtx, addCtxCn := context.WithTimeout(context.Background(), time.Second*10) + + addResp, addErr := clt.AddTxn(addCtx, addReq) if addErr != nil { t.Fatalf("Unable to add txn, %v", addErr) } @@ -248,6 +250,8 @@ func TestTxPool_TransactionCoalescing(t *testing.T) { testTransactions = append(testTransactions, &testTransaction{ txHash: web3.HexToHash(addResp.TxHash), }) + + addCtxCn() } // Wait for the first transaction to go through From cb32a270372f1812ec401c110d88ceb81bccdeca Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Thu, 28 Apr 2022 09:36:05 +0200 Subject: [PATCH 11/14] Add timeout to TestTxPool_TransactionCoalescing --- e2e/txpool_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/e2e/txpool_test.go b/e2e/txpool_test.go index 2c0f90cc92..2d100bc679 100644 --- a/e2e/txpool_test.go +++ b/e2e/txpool_test.go @@ -277,7 +277,10 @@ func TestTxPool_TransactionCoalescing(t *testing.T) { // Add the transaction with the gap nonce value addReq := generateReq(1) - addResp, addErr := clt.AddTxn(context.Background(), addReq) + addCtx, addCtxCn := context.WithTimeout(context.Background(), time.Second*10) + defer addCtxCn() + + addResp, addErr := clt.AddTxn(addCtx, addReq) if addErr != nil { t.Fatalf("Unable to add txn, %v", addErr) } From c2e592563352dfd8bec020d3954450c5ac0b32ff Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Thu, 28 Apr 2022 09:38:43 +0200 Subject: [PATCH 12/14] Add comment to TestTxPool_TransactionCoalescing --- e2e/txpool_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e/txpool_test.go b/e2e/txpool_test.go index 2d100bc679..6e8aaa8fe1 100644 --- a/e2e/txpool_test.go +++ b/e2e/txpool_test.go @@ -289,6 +289,7 @@ func TestTxPool_TransactionCoalescing(t *testing.T) { txHash: web3.HexToHash(addResp.TxHash), }) + // Start from 1 since there was previously a txn with nonce 0 for i := 1; i < len(testTransactions); i++ { // Wait for the first transaction to go through ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) From 5f8c638d1ca97acef59fd8895ba51974adceffd2 Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Thu, 28 Apr 2022 09:41:37 +0200 Subject: [PATCH 13/14] Add t.Helper() in GetGasTotal --- e2e/framework/testserver.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e/framework/testserver.go b/e2e/framework/testserver.go index 06cd6e1a31..953b86b4d7 100644 --- a/e2e/framework/testserver.go +++ b/e2e/framework/testserver.go @@ -547,6 +547,8 @@ func (t *TestServer) WaitForReceipt(ctx context.Context, hash web3.Hash) (*web3. // GetGasTotal waits for the total gas used sum for the passed in // transactions func (t *TestServer) GetGasTotal(txHashes []web3.Hash) uint64 { + t.t.Helper() + var ( totalGasUsed = uint64(0) receiptErrs = make([]error, 0) From 5200166563d9b84f20bbbab77a352ac2fe5eb96f Mon Sep 17 00:00:00 2001 From: Milos Zivkovic Date: Thu, 28 Apr 2022 09:50:39 +0200 Subject: [PATCH 14/14] Increase block gas limit for TestPoS_UnstakeExploit --- e2e/pos_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e/pos_test.go b/e2e/pos_test.go index f1b622093c..f3b5287c05 100644 --- a/e2e/pos_test.go +++ b/e2e/pos_test.go @@ -328,6 +328,7 @@ func TestPoS_UnstakeExploit(t *testing.T) { config.Premine(senderAddr, defaultBalance) config.SetDevStakingAddresses(append(generateStakingAddresses(numDummyValidators), senderAddr)) config.SetIBFTPoS(true) + config.SetBlockLimit(5000000000) }) srv := srvs[0] client := srv.JSONRPC()