-
Notifications
You must be signed in to change notification settings - Fork 692
/
main.go
230 lines (208 loc) · 6.83 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
package main
import (
"context"
"math/big"
"os"
"time"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/pol"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonrollupmanager"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevm"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/test/operations"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/urfave/cli/v2"
)
const (
flagL1URLName = "url"
flagZkevmAddrName = "zkevm"
flagRollupManagerAddrName = "rollupmanager"
miningTimeout = 180
)
var (
flagL1URL = cli.StringFlag{
Name: flagL1URLName,
Aliases: []string{"u"},
Usage: "L1 node url",
Required: true,
}
flagZkevmAddr = cli.StringFlag{
Name: flagZkevmAddrName,
Aliases: []string{"zk"},
Usage: "Zkevm smart contract address",
Required: true,
}
flagRollupManagerAddr = cli.StringFlag{
Name: flagRollupManagerAddrName,
Aliases: []string{"r"},
Usage: "RollupmManager smart contract address",
Required: true,
}
)
func main() {
fbatchsender := cli.NewApp()
fbatchsender.Name = "forcedBatchsender"
fbatchsender.Usage = "send forced batch transactions to L1"
fbatchsender.DefaultCommand = "send"
flags := []cli.Flag{&flagL1URL, &flagZkevmAddr, &flagRollupManagerAddr}
fbatchsender.Commands = []*cli.Command{
{
Before: setLogLevel,
Name: "send",
Aliases: []string{},
Flags: flags,
Action: sendForcedBatches,
},
}
err := fbatchsender.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func setLogLevel(ctx *cli.Context) error {
logLevel := "debug"
log.Init(log.Config{
Level: logLevel,
Outputs: []string{"stderr"},
})
return nil
}
func transferERC20Pol(ctx context.Context, ethClient *ethclient.Client, authSequencer, authForcedBatch *bind.TransactOpts, zkevmAddr common.Address) error {
log.Infof("Transfering POL from sequencer to forcedBatchesAddress")
polSmc, err := pol.NewPol(common.HexToAddress(operations.DefaultL1PolSmartContract), ethClient)
if err != nil {
return err
}
polAmount, _ := big.NewInt(0).SetString("9999999999999999999999", 0)
log.Debugf("Charging pol from sequencer -> forcedBatchesAddress")
txValue, err := polSmc.Transfer(authSequencer, common.HexToAddress(operations.DefaultForcedBatchesAddress), polAmount)
if err != nil {
return err
}
log.Debugf("Waiting for tx %s to be mined (transfer of pol from sequencer -> forcedBatches)", txValue.Hash().String())
err = operations.WaitTxToBeMined(ctx, ethClient, txValue, operations.DefaultTimeoutTxToBeMined)
if err != nil {
return err
}
balance, err := polSmc.BalanceOf(&bind.CallOpts{Pending: false}, common.HexToAddress(operations.DefaultSequencerAddress))
if err != nil {
return err
}
log.Debugf("Account (sequencer) %s pol balance %s", operations.DefaultSequencerAddress, balance.String())
balance, err = polSmc.BalanceOf(&bind.CallOpts{Pending: false}, common.HexToAddress(operations.DefaultForcedBatchesAddress))
if err != nil {
return err
}
log.Debugf("Account (force_batches) %s pol balance %s", operations.DefaultForcedBatchesAddress, balance.String())
log.Debugf("Approve to zkEVM SMC (%s) to spend %s pol", zkevmAddr, polAmount.String())
_, err = polSmc.Approve(authForcedBatch, zkevmAddr, polAmount)
if err != nil {
return err
}
return nil
}
func sendForcedBatches(cliCtx *cli.Context) error {
ctx := cliCtx.Context
url := cliCtx.String(flagL1URLName)
// Connect to ethereum node
ethClient, err := ethclient.Dial(url)
if err != nil {
log.Errorf("error connecting to %s: %+v", url, err)
return err
}
// Create smc client
zkevmAddr := common.HexToAddress(cliCtx.String(flagZkevmAddrName))
zkevm, err := polygonzkevm.NewPolygonzkevm(zkevmAddr, ethClient)
if err != nil {
return err
}
rollupManagerAddr := common.HexToAddress(cliCtx.String(flagRollupManagerAddrName))
rollupManager, err := polygonrollupmanager.NewPolygonrollupmanager(rollupManagerAddr, ethClient)
if err != nil {
return err
}
authSeq, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL1ChainID)
if err != nil {
return err
}
auth, err := operations.GetAuth(operations.DefaultForcedBatchesPrivateKey, operations.DefaultL1ChainID)
if err != nil {
return err
}
err = transferERC20Pol(ctx, ethClient, authSeq, auth, zkevmAddr)
if err != nil {
log.Error("error transfering pol. Error: ", err)
return err
}
log.Info("Using address: ", auth.From)
num, err := zkevm.LastForceBatch(&bind.CallOpts{Pending: false})
if err != nil {
log.Error("error getting lastForBatch number. Error : ", err)
return err
}
log.Info("Number of forceBatches in the smc: ", num)
currentBlock, err := ethClient.BlockByNumber(ctx, nil)
if err != nil {
log.Error("error getting blockByNumber. Error: ", err)
return err
}
log.Debug("currentBlock.Time(): ", currentBlock.Time())
// Get tip
tip, err := rollupManager.GetForcedBatchFee(&bind.CallOpts{Pending: false})
if err != nil {
log.Error("error getting tip. Error: ", err)
return err
}
log.Info("Tip: ", tip)
tx, err := zkevm.SetForceBatchAddress(authSeq, common.Address{})
if err != nil {
log.Error("error sending SetForceBatchAddress. Error: ", err)
return err
}
err = operations.WaitTxToBeMined(ctx, ethClient, tx, operations.DefaultTimeoutTxToBeMined)
if err != nil {
log.Error("error waiting tx to be mined. Error: ", err)
return err
}
txs := "ee80843b9aca00830186a0944d5cf5032b2a844602278b01199ed191a86c93ff88016345785d8a0000808203e980801186622d03b6b8da7cf111d1ccba5bb185c56deae6a322cebc6dda0556f3cb9700910c26408b64b51c5da36ba2f38ef55ba1cee719d5a6c012259687999074321bff"
// Send forceBatch
data := common.Hex2Bytes(txs)
log.Info("Data: ", data)
tx, err = zkevm.ForceBatch(auth, data, tip)
if err != nil {
log.Error("error sending forceBatch. Error: ", err)
return err
}
log.Info("TxHash: ", tx.Hash())
time.Sleep(1 * time.Second)
err = operations.WaitTxToBeMined(ctx, ethClient, tx, miningTimeout*time.Second)
if err != nil {
return err
}
query := ethereum.FilterQuery{
FromBlock: currentBlock.Number(),
Addresses: []common.Address{zkevmAddr},
}
logs, err := ethClient.FilterLogs(ctx, query)
if err != nil {
return err
}
for _, vLog := range logs {
fb, err := zkevm.ParseForceBatch(vLog)
if err == nil {
log.Debugf("log decoded: %+v", fb)
ger := fb.LastGlobalExitRoot
log.Info("GlobalExitRoot: ", ger)
log.Info("Transactions: ", common.Bytes2Hex(fb.Transactions))
fullBlock, err := ethClient.BlockByHash(ctx, vLog.BlockHash)
if err != nil {
log.Errorf("error getting hashParent. BlockNumber: %d. Error: %v", vLog.BlockNumber, err)
return err
}
log.Info("MinForcedTimestamp: ", fullBlock.Time())
}
}
return nil
}