-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.go
117 lines (103 loc) · 3.72 KB
/
transactions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package transactions
import (
"context"
"errors"
"fmt"
"math/big"
"strconv"
"time"
"github.com/hoaleee/zkevm-node/pool"
"github.com/hoaleee/zkevm-node/state"
"github.com/hoaleee/zkevm-node/test/benchmarks/sequencer/common/params"
"github.com/hoaleee/zkevm-node/test/operations"
"github.com/hoaleee/go-ethereum/accounts/abi/bind"
"github.com/hoaleee/go-ethereum/core/types"
"github.com/hoaleee/go-ethereum/ethclient"
"github.com/hoaleee/zkevm-node/test/contracts/bin/ERC20"
"github.com/hoaleee/zkevm-node/test/scripts/uniswap/pkg"
)
// SendAndWait sends a number of transactions and waits for them to be marked as pending in the pool
func SendAndWait(
auth *bind.TransactOpts,
client *ethclient.Client,
getTxsByStatus func(ctx context.Context, status pool.TxStatus, limit uint64) ([]pool.Transaction, error),
nTxs uint64,
erc20SC *ERC20.ERC20,
uniswapDeployments *pkg.Deployments,
txSenderFunc func(l2Client *ethclient.Client, gasPrice *big.Int, auth *bind.TransactOpts, erc20SC *ERC20.ERC20, uniswapDeployments *pkg.Deployments) ([]*types.Transaction, error),
) ([]*types.Transaction, error) {
auth.GasLimit = 2100000
fmt.Printf("Sending %d txs ...\n", nTxs)
if auth.Nonce != nil {
auth.Nonce = nil
}
allTxs := make([]*types.Transaction, 0, nTxs)
for i := 0; i < int(nTxs); i++ {
txs, err := txSenderFunc(client, auth.GasPrice, auth, erc20SC, uniswapDeployments)
if err != nil {
return nil, err
}
allTxs = append(allTxs, txs...)
}
fmt.Println("All txs were sent!")
fmt.Println("Waiting pending transactions To be added in the pool ...")
err := operations.Poll(1*time.Second, params.DefaultDeadline, func() (bool, error) {
// using a closure here To capture st and currentBatchNumber
pendingTxs, err := getTxsByStatus(params.Ctx, pool.TxStatusPending, 0)
if err != nil {
panic(err)
}
pendingTxsCount := 0
for _, tx := range pendingTxs {
sender, err := state.GetSender(tx.Transaction)
if err != nil {
panic(err)
}
if sender == auth.From {
pendingTxsCount++
}
}
fmt.Printf("amount of pending txs: %d\n\n", pendingTxsCount)
done := pendingTxsCount <= 0
return done, nil
})
if err != nil {
return nil, err
}
fmt.Println("All pending txs are added in the pool!")
return allTxs, nil
}
// WaitStatusSelected waits for a number of transactions to be marked as selected in the pool
func WaitStatusSelected(countByStatusFunc func(ctx context.Context, status ...pool.TxStatus) (uint64, error), initialCount uint64, nTxs uint64) error {
fmt.Println("Wait for sequencer to select all txs from the pool")
pollingInterval := 1 * time.Second
prevCount := uint64(0)
txsPerSecond := 0
txsPerSecondAsStr := "N/A"
estimatedTimeToFinish := "N/A"
err := operations.Poll(pollingInterval, params.DefaultDeadline, func() (bool, error) {
selectedCount, err := countByStatusFunc(params.Ctx, pool.TxStatusSelected)
if err != nil {
return false, err
}
currCount := selectedCount - initialCount
remainingTxs := nTxs - currCount
if prevCount > 0 {
txsPerSecond = int(currCount - prevCount)
if txsPerSecond == 0 {
estimatedTimeToFinish = "N/A"
} else {
estimatedTimeToFinish = (time.Duration(int(remainingTxs)/txsPerSecond) * time.Second).String()
}
txsPerSecondAsStr = strconv.Itoa(txsPerSecond)
}
fmt.Printf("amount of selected txs: %d/%d, estimated txs per second: %s, time to finish: %s\n", selectedCount-initialCount, nTxs, txsPerSecondAsStr, estimatedTimeToFinish)
prevCount = currCount
done := (int64(selectedCount) - int64(initialCount)) >= int64(nTxs)
return done, nil
})
return err
}
func ShouldRetryError(err error) bool {
return errors.Is(err, state.ErrStateNotSynchronized) || errors.Is(err, state.ErrInsufficientFunds) || errors.Is(err, pool.ErrNonceTooHigh)
}