github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/benchmarks/sequencer/common/transactions/transactions.go (about) 1 package transactions 2 3 import ( 4 "context" 5 "math/big" 6 "strconv" 7 "time" 8 9 "github.com/0xPolygon/supernets2-node/log" 10 "github.com/0xPolygon/supernets2-node/pool" 11 "github.com/0xPolygon/supernets2-node/test/benchmarks/sequencer/common/params" 12 "github.com/0xPolygon/supernets2-node/test/contracts/bin/ERC20" 13 "github.com/0xPolygon/supernets2-node/test/operations" 14 "github.com/ethereum/go-ethereum/accounts/abi/bind" 15 "github.com/ethereum/go-ethereum/ethclient" 16 ) 17 18 // SendAndWait sends a number of transactions and waits for them to be marked as pending in the pool 19 func SendAndWait( 20 ctx context.Context, 21 auth *bind.TransactOpts, 22 client *ethclient.Client, 23 countByStatusFunc func(ctx context.Context, status pool.TxStatus) (uint64, error), 24 nTxs int, 25 erc20SC *ERC20.ERC20, 26 txSenderFunc func(l2Client *ethclient.Client, gasPrice *big.Int, nonce uint64, auth *bind.TransactOpts, erc20SC *ERC20.ERC20) error, 27 ) error { 28 auth.GasLimit = 2100000 29 log.Debugf("Sending %d txs ...", nTxs) 30 startingNonce := auth.Nonce.Uint64() 31 maxNonce := uint64(nTxs) + startingNonce 32 initialPendingCount, err := countByStatusFunc(params.Ctx, pool.TxStatusPending) 33 if err != nil { 34 panic(err) 35 } 36 37 for nonce := startingNonce; nonce < maxNonce; nonce++ { 38 err = txSenderFunc(client, auth.GasPrice, nonce, auth, erc20SC) 39 if err != nil { 40 for err != nil && err.Error() == "nonce intrinsic error" { 41 log.Warnf("nonce intrinsic error, retrying with nonce %d", nonce) 42 err = txSenderFunc(client, auth.GasPrice, nonce, auth, erc20SC) 43 } 44 if err == nil { 45 continue 46 } 47 return err 48 } 49 } 50 log.Debug("All txs were sent!") 51 log.Debug("Waiting pending transactions To be added in the pool ...") 52 err = operations.Poll(1*time.Second, params.DefaultDeadline, func() (bool, error) { 53 // using a closure here To capture st and currentBatchNumber 54 count, err := countByStatusFunc(ctx, pool.TxStatusPending) 55 if err != nil { 56 return false, err 57 } 58 59 log.Debugf("amount of pending txs: %d\n", count) 60 done := count-initialPendingCount <= 0 61 return done, nil 62 }) 63 if err != nil { 64 return err 65 } 66 67 log.Debug("All pending txs are added in the pool!") 68 69 return nil 70 } 71 72 // WaitStatusSelected waits for a number of transactions to be marked as selected in the pool 73 func WaitStatusSelected(countByStatusFunc func(ctx context.Context, status pool.TxStatus) (uint64, error), initialCount uint64, nTxs uint64) error { 74 log.Debug("Wait for sequencer to select all txs from the pool") 75 pollingInterval := 1 * time.Second 76 77 prevCount := uint64(0) 78 txsPerSecond := 0 79 txsPerSecondAsStr := "N/A" 80 estimatedTimeToFinish := "N/A" 81 err := operations.Poll(pollingInterval, params.DefaultDeadline, func() (bool, error) { 82 selectedCount, err := countByStatusFunc(params.Ctx, pool.TxStatusSelected) 83 if err != nil { 84 return false, err 85 } 86 currCount := selectedCount - initialCount 87 remainingTxs := nTxs - currCount 88 if prevCount > 0 { 89 txsPerSecond = int(currCount - prevCount) 90 if txsPerSecond == 0 { 91 estimatedTimeToFinish = "N/A" 92 } else { 93 estimatedTimeToFinish = (time.Duration(int(remainingTxs)/txsPerSecond) * time.Second).String() 94 } 95 txsPerSecondAsStr = strconv.Itoa(txsPerSecond) 96 } 97 log.Debugf("amount of selected txs: %d/%d, estimated txs per second: %s, time to finish: %s", selectedCount-initialCount, nTxs, txsPerSecondAsStr, estimatedTimeToFinish) 98 prevCount = currCount 99 100 done := (int64(selectedCount) - int64(initialCount)) >= int64(nTxs) 101 return done, nil 102 }) 103 104 return err 105 }