/
main.go
341 lines (302 loc) · 8.65 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"bytes"
"errors"
"fmt"
"math/big"
"os"
"strconv"
"time"
"github.com/0xPolygonHermez/zkevm-node/config"
"github.com/0xPolygonHermez/zkevm-node/etherman"
ethmanTypes "github.com/0xPolygonHermez/zkevm-node/etherman/types"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/test/operations"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
)
const (
flagSequencesName = "sequences"
flagWaitName = "wait"
flagVerboseName = "verbose"
)
var (
flagSequences = cli.Uint64Flag{
Name: flagSequencesName,
Aliases: []string{"s"},
Usage: "send batches for the provided number of sequences.",
Required: false,
}
flagWait = cli.BoolFlag{
Name: flagWaitName,
Aliases: []string{"w"},
Usage: "wait batch transaction to be confirmed",
Required: false,
}
flagVerbose = cli.BoolFlag{
Name: flagVerboseName,
Aliases: []string{"v"},
Usage: "output verbose logs",
Required: false,
}
)
func main() {
batchsender := cli.NewApp()
batchsender.Name = "batchsender"
batchsender.Usage = "send batch transactions to L1"
batchsender.Description = `This tool allows to send a specified number of batch transactions to L1.
Optionally it can wait for the batches to be validated.`
batchsender.DefaultCommand = "send"
batchsender.Flags = []cli.Flag{&flagSequences, &flagWait, &flagVerbose}
batchsender.Commands = []*cli.Command{
{
Before: setLogLevel,
Name: "send",
Aliases: []string{},
Usage: "Sends the specified number of batch transactions to L1",
Description: `This command sends the specified number of batches to L1.
If --sequences flag is used, the number of batches is repeated for the number of sequences provided.
If --wait flag is used, it waits for the corresponding validation transaction.`,
ArgsUsage: "number of batches to be sent (default: 1)",
Action: sendBatches,
},
}
err := batchsender.Run(os.Args)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}
func setLogLevel(ctx *cli.Context) error {
logLevel := "info"
if ctx.Bool(flagVerboseName) {
logLevel = "debug"
}
log.Init(log.Config{
Level: logLevel,
Outputs: []string{"stderr"},
})
return nil
}
func sendBatches(cliCtx *cli.Context) error {
ctx := cliCtx.Context
// retrieve default configuration
var cfg config.Config
viper.SetConfigType("toml")
err := viper.ReadConfig(bytes.NewBuffer([]byte(config.DefaultValues)))
if err != nil {
return err
}
err = viper.Unmarshal(&cfg, viper.DecodeHook(mapstructure.TextUnmarshallerHookFunc()))
if err != nil {
return err
}
auth, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL1ChainID)
if err != nil {
return err
}
ethMan, err := etherman.NewClient(cfg.Etherman, cfg.NetworkConfig.L1Config)
if err != nil {
return err
}
err = ethMan.AddOrReplaceAuth(*auth)
if err != nil {
return err
}
log.Info("Using address: ", auth.From)
wait := cliCtx.Bool(flagWaitName)
nBatches := 1 // send 1 batch by default
if cliCtx.NArg() > 0 {
nBatchesArgStr := cliCtx.Args().Get(0)
nBatchesArg, err := strconv.Atoi(nBatchesArgStr)
if err == nil {
nBatches = nBatchesArg
}
}
nSequences := int(cliCtx.Uint64(flagSequencesName))
var sentTxs []*ethTypes.Transaction
sentTxsMap := make(map[common.Hash]struct{})
var duration time.Duration
if nBatches > 1 {
duration = 500
}
// here the behavior is different:
// - if the `--sequences` flag is used we send ns sequences filled with nb batches each
// - if the flag is not used we send one sequence for each batch
var ns, nb int
if nSequences == 0 {
ns = nBatches
nb = 1
} else {
ns = nSequences
nb = nBatches
}
nonce, err := ethMan.CurrentNonce(ctx, auth.From)
if err != nil {
err := fmt.Errorf("failed to get current nonce: %w", err)
log.Error(err.Error())
return err
}
for i := 0; i < ns; i++ {
currentBlock, err := ethMan.EthClient.BlockByNumber(ctx, nil)
if err != nil {
return err
}
log.Debug("currentBlock.Time(): ", currentBlock.Time())
seqs := make([]ethmanTypes.Sequence, 0, nBatches)
for i := 0; i < nb; i++ {
// empty rollup
seqs = append(seqs, ethmanTypes.Sequence{
BatchNumber: uint64(i),
GlobalExitRoot: common.HexToHash("0x"),
BatchL2Data: []byte{},
LastL2BLockTimestamp: int64(currentBlock.Time() - 1), // fit in latest-sequence < > current-block rage
})
}
// send to L1
firstSequence := seqs[0]
lastSequence := seqs[len(seqs)-1]
to, data, err := ethMan.BuildSequenceBatchesTxData(auth.From, seqs, uint64(lastSequence.LastL2BLockTimestamp), firstSequence.BatchNumber, auth.From)
if err != nil {
return err
}
tx := ethTypes.NewTx(ðTypes.LegacyTx{
To: to,
Data: data,
})
signedTx, err := ethMan.SignTx(ctx, auth.From, tx)
if err != nil {
return err
}
err = ethMan.SendTx(ctx, signedTx)
if err != nil {
return err
}
gas, err := ethMan.EstimateGas(ctx, auth.From, to, nil, data)
if err != nil {
err := fmt.Errorf("failed to estimate gas: %w", err)
log.Error(err.Error())
return err
}
// get gas price
gasPrice, err := ethMan.SuggestedGasPrice(ctx)
if err != nil {
err := fmt.Errorf("failed to get suggested gas price: %w", err)
log.Error(err.Error())
return err
}
tx = ethTypes.NewTx(ðTypes.LegacyTx{
Nonce: nonce,
Gas: gas + uint64(i),
GasPrice: gasPrice,
To: to,
Data: data,
})
signedTx, err = ethMan.SignTx(ctx, auth.From, tx)
if err != nil {
log.Error(err.Error())
return err
}
err = ethMan.SendTx(ctx, signedTx)
if err != nil {
log.Error(err.Error())
return err
}
log.Info("TxHash: ", signedTx.Hash())
sentTxs = append(sentTxs, signedTx)
sentTxsMap[signedTx.Hash()] = struct{}{}
time.Sleep(duration * time.Millisecond)
}
sentBatches := len(sentTxs)
if wait { // wait proofs
log.Info("Waiting for transactions to be confirmed...")
time.Sleep(time.Second)
virtualBatches := make(map[uint64]common.Hash)
verifiedBatches := make(map[uint64]struct{})
loggedBatches := make(map[uint64]struct{})
miningTimeout := 180 * time.Second //nolint:gomnd
waitTimeout := time.Duration(180*len(sentTxs)) * time.Second //nolint:gomnd
done := make(chan struct{})
for _, tx := range sentTxs {
err := operations.WaitTxToBeMined(ctx, ethMan.EthClient, tx, miningTimeout)
if err != nil {
return err
}
}
for {
select {
case <-time.After(waitTimeout):
return errors.New("deadline exceeded")
case <-done:
success(sentBatches)
return nil
default:
txLoop:
for _, tx := range sentTxs {
// get rollup tx block number
receipt, err := ethMan.EthClient.TransactionReceipt(ctx, tx.Hash())
if err != nil {
return err
}
fromBlock := receipt.BlockNumber
toBlock := new(big.Int).Add(fromBlock, new(big.Int).SetUint64(cfg.Synchronizer.SyncChunkSize))
query := ethereum.FilterQuery{
FromBlock: fromBlock,
ToBlock: toBlock,
Addresses: ethMan.SCAddresses,
}
logs, err := ethMan.EthClient.FilterLogs(ctx, query)
if err != nil {
return err
}
for _, vLog := range logs {
switch vLog.Topics[0] {
case etherman.SequencedBatchesSigHash():
if vLog.TxHash == tx.Hash() { // ignore other txs happening on L1
sb, err := ethMan.ZkEVM.ParseSequenceBatches(vLog)
if err != nil {
return err
}
virtualBatches[sb.NumBatch] = vLog.TxHash
if _, logged := loggedBatches[sb.NumBatch]; !logged {
log.Infof("Batch [%d] virtualized in TxHash [%v]", sb.NumBatch, vLog.TxHash)
loggedBatches[sb.NumBatch] = struct{}{}
}
}
case etherman.TrustedVerifyBatchesSigHash():
vb, err := ethMan.ZkEVM.ParseVerifyBatches(vLog)
if err != nil {
return err
}
if _, verified := verifiedBatches[vb.NumBatch]; !verified {
log.Infof("Batch [%d] verified in TxHash [%v]", vb.NumBatch, vLog.TxHash)
verifiedBatches[vb.NumBatch] = struct{}{}
}
// batch is verified, remove it from the txs set
delete(sentTxsMap, virtualBatches[vb.NumBatch])
if len(sentTxsMap) == 0 {
close(done)
break txLoop
}
}
}
// wait for verifications
time.Sleep(time.Second) //nolint:gomnd
}
}
}
}
success(sentBatches)
return nil
}
func success(nBatches int) {
if nBatches > 1 {
log.Infof("Successfully sent %d batches", nBatches)
} else {
log.Info("Successfully sent 1 batch")
}
}