github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/scripts/sendForcedBatch/main.go (about) 1 package main 2 3 import ( 4 "os" 5 "time" 6 7 "github.com/0xPolygon/supernets2-node/etherman/smartcontracts/supernets2" 8 "github.com/0xPolygon/supernets2-node/log" 9 "github.com/0xPolygon/supernets2-node/test/operations" 10 "github.com/ethereum/go-ethereum" 11 "github.com/ethereum/go-ethereum/accounts/abi/bind" 12 "github.com/ethereum/go-ethereum/common" 13 "github.com/ethereum/go-ethereum/ethclient" 14 "github.com/urfave/cli/v2" 15 ) 16 17 const ( 18 flagL1URLName = "url" 19 flagSmcAddrName = "smc" 20 miningTimeout = 180 21 ) 22 23 var ( 24 flagL1URL = cli.StringFlag{ 25 Name: flagL1URLName, 26 Aliases: []string{"u"}, 27 Usage: "L1 node url", 28 Required: true, 29 } 30 flagSmcAddr = cli.StringFlag{ 31 Name: flagSmcAddrName, 32 Aliases: []string{"a"}, 33 Usage: "Smart contract address", 34 Required: true, 35 } 36 ) 37 38 func main() { 39 fbatchsender := cli.NewApp() 40 fbatchsender.Name = "forcedBatchsender" 41 fbatchsender.Usage = "send forced batch transactions to L1" 42 fbatchsender.DefaultCommand = "send" 43 flags := []cli.Flag{&flagL1URL, &flagSmcAddr} 44 fbatchsender.Commands = []*cli.Command{ 45 { 46 Before: setLogLevel, 47 Name: "send", 48 Aliases: []string{}, 49 Flags: flags, 50 Action: sendForcedBatches, 51 }, 52 } 53 54 err := fbatchsender.Run(os.Args) 55 if err != nil { 56 log.Fatal(err) 57 } 58 } 59 60 func setLogLevel(ctx *cli.Context) error { 61 logLevel := "debug" 62 log.Init(log.Config{ 63 Level: logLevel, 64 Outputs: []string{"stderr"}, 65 }) 66 return nil 67 } 68 69 func sendForcedBatches(cliCtx *cli.Context) error { 70 ctx := cliCtx.Context 71 72 url := cliCtx.String(flagL1URLName) 73 // Connect to ethereum node 74 ethClient, err := ethclient.Dial(url) 75 if err != nil { 76 log.Errorf("error connecting to %s: %+v", url, err) 77 return err 78 } 79 // Create smc client 80 poeAddr := common.HexToAddress(cliCtx.String(flagSmcAddrName)) 81 poe, err := supernets2.NewSupernets2(poeAddr, ethClient) 82 if err != nil { 83 return err 84 } 85 86 auth, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL1ChainID) 87 if err != nil { 88 return err 89 } 90 91 log.Info("Using address: ", auth.From) 92 93 num, err := poe.LastForceBatch(&bind.CallOpts{Pending: false}) 94 if err != nil { 95 log.Error("error getting lastForBatch number. Error : ", err) 96 return err 97 } 98 log.Info("Number of forceBatches in the smc: ", num) 99 100 currentBlock, err := ethClient.BlockByNumber(ctx, nil) 101 if err != nil { 102 log.Error("error getting blockByNumber. Error: ", err) 103 return err 104 } 105 log.Debug("currentBlock.Time(): ", currentBlock.Time()) 106 107 // Get tip 108 tip, err := poe.GetForcedBatchFee(&bind.CallOpts{Pending: false}) 109 if err != nil { 110 log.Error("error getting tip. Error: ", err) 111 return err 112 } 113 114 // Allow forced batches in smart contract if disallowed 115 disallowed, err := poe.IsForcedBatchDisallowed(&bind.CallOpts{Pending: false}) 116 if err != nil { 117 log.Error("error getting isForcedBatchDisallowed. Error: ", err) 118 return err 119 } 120 if disallowed { 121 tx, err := poe.ActivateForceBatches(auth) 122 if err != nil { 123 log.Error("error sending activateForceBatches. Error: ", err) 124 return err 125 } 126 err = operations.WaitTxToBeMined(ctx, ethClient, tx, operations.DefaultTimeoutTxToBeMined) 127 if err != nil { 128 129 log.Error("error waiting tx to be mined. Error: ", err) 130 return err 131 } 132 } 133 134 // Send forceBatch 135 tx, err := poe.ForceBatch(auth, []byte{}, tip) 136 if err != nil { 137 log.Error("error sending forceBatch. Error: ", err) 138 return err 139 } 140 141 log.Info("TxHash: ", tx.Hash()) 142 143 time.Sleep(1 * time.Second) 144 145 err = operations.WaitTxToBeMined(ctx, ethClient, tx, miningTimeout*time.Second) 146 if err != nil { 147 return err 148 } 149 150 query := ethereum.FilterQuery{ 151 FromBlock: currentBlock.Number(), 152 Addresses: []common.Address{poeAddr}, 153 } 154 logs, err := ethClient.FilterLogs(ctx, query) 155 if err != nil { 156 return err 157 } 158 for _, vLog := range logs { 159 fb, err := poe.ParseForceBatch(vLog) 160 if err == nil { 161 log.Debugf("log decoded: %+v", fb) 162 ger := fb.LastGlobalExitRoot 163 log.Info("GlobalExitRoot: ", ger) 164 log.Info("Transactions: ", common.Bytes2Hex(fb.Transactions)) 165 fullBlock, err := ethClient.BlockByHash(ctx, vLog.BlockHash) 166 if err != nil { 167 log.Errorf("error getting hashParent. BlockNumber: %d. Error: %v", vLog.BlockNumber, err) 168 return err 169 } 170 log.Info("MinForcedTimestamp: ", fullBlock.Time()) 171 } 172 } 173 174 return nil 175 }