github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/tools/network/network.go (about) 1 package network 2 3 // import ( 4 // "context" 5 // "flag" 6 // "math/big" 7 // "strings" 8 // "time" 9 10 // "github.com/0xPolygon/supernets2-node/config" 11 // "github.com/0xPolygon/supernets2-node/encoding" 12 // "github.com/0xPolygon/supernets2-node/etherman" 13 // "github.com/0xPolygon/supernets2-node/etherman/smartcontracts/bridge" 14 // "github.com/0xPolygon/supernets2-node/etherman/smartcontracts/matic" 15 // "github.com/0xPolygon/supernets2-node/etherman/smartcontracts/proofofefficiency" 16 // "github.com/0xPolygon/supernets2-node/log" 17 // "github.com/0xPolygon/supernets2-node/test/operations" 18 // "github.com/ethereum/go-ethereum/accounts/abi/bind" 19 // "github.com/ethereum/go-ethereum/common" 20 // "github.com/ethereum/go-ethereum/core/types" 21 // "github.com/ethereum/go-ethereum/crypto" 22 // "github.com/ethereum/go-ethereum/ethclient" 23 // "github.com/urfave/cli/v2" 24 // ) 25 26 // const ( 27 // // The account on which we make the deposit needs to be fixed so that the 28 // // hardcoded proof used in the claim can work. 29 // BridgeDepositReceiverAddress = "0xc949254d682d8c9ad5682521675b8f43b102aec4" 30 // BridgeDepositReceiverPrivateKey = "0xdfd01798f92667dbf91df722434e8fbe96af0211d4d1b82bbbbc8f1def7a814f" 31 // ) 32 33 // type deposit struct { 34 // TokenAddr common.Address 35 // Amount *big.Int 36 // OrigNet uint32 37 // DestNet uint32 38 // DestAddr common.Address 39 // DepositCnt uint32 40 // } 41 42 // type globalExitRoot struct { 43 // BlockID uint64 44 // BlockNumber uint64 45 // GlobalExitRootNum *big.Int 46 // ExitRoots []common.Hash 47 // } 48 49 // type L1Deployer struct { 50 // Address, PrivateKey string 51 // L1ETHAmountToSequencer string 52 // L1MaticAmountToSequencer string 53 // } 54 55 // type InitNetworkConfig struct { 56 // Network string 57 58 // // RPC endpoints 59 // L1NetworkURL, L2NetworkURL string 60 // // Bridge addresses, defined in the deployment description 61 // L1BridgeAddr, L2BridgeAddr string 62 // // Deployer account, needs to have at least 10 L1 ETH for the L2 63 // // deposit 64 // L1Deployer L1Deployer 65 // // Sequencer address, comes from the keystore passed to node 66 // // on config 67 // sequencerAddress, SequencerPrivateKey string 68 // TxTimeout time.Duration 69 // } 70 71 // // InitNetwork initializes the L2 network and moves the L1 funds to L2 72 // func InitNetwork( 73 // ctx context.Context, 74 // nc InitNetworkConfig, 75 // ) error { 76 // app := cli.NewApp() 77 // var n string 78 // if nc.Network == "" { 79 // nc.Network = "local" 80 // } 81 // flag.StringVar(&n, "network", nc.Network, "") 82 // context := cli.NewContext(app, flag.CommandLine, nil) 83 84 // cfg, err := config.Load(context) 85 // if err != nil { 86 // return err 87 // } 88 89 // // Eth client 90 // log.Infof("Connecting to l1") 91 // clientL1, err := ethclient.Dial(nc.L1NetworkURL) 92 // if err != nil { 93 // return err 94 // } 95 96 // // Hermez client 97 // log.Infof("Connecting to l1") 98 // clientL2, err := ethclient.Dial(nc.L2NetworkURL) 99 // if err != nil { 100 // return err 101 // } 102 103 // // Get network chain id 104 // log.Infof("Getting chainID L1") 105 // chainIDL1, err := clientL1.NetworkID(ctx) 106 // if err != nil { 107 // return err 108 // } 109 110 // // Preparing l1 acc info 111 // log.Infof("Creating deployer authorization") 112 // privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(nc.L1Deployer.PrivateKey, "0x")) 113 // if err != nil { 114 // return err 115 // } 116 // authDeployer, err := bind.NewKeyedTransactorWithChainID(privateKey, chainIDL1) 117 // if err != nil { 118 // return err 119 // } 120 121 // // Create sequencer auth 122 // log.Infof("Creating sequencer authorization") 123 // privateKey, err = crypto.HexToECDSA(strings.TrimPrefix(nc.SequencerPrivateKey, "0x")) 124 // if err != nil { 125 // return err 126 // } 127 // authSequencer, err := bind.NewKeyedTransactorWithChainID(privateKey, chainIDL1) 128 // if err != nil { 129 // return err 130 // } 131 132 // // Getting l1 info 133 // log.Infof("Getting L1 info") 134 // gasPrice, err := clientL1.SuggestGasPrice(ctx) 135 // if err != nil { 136 // return err 137 // } 138 139 // sequencerAddress := common.HexToAddress(nc.sequencerAddress) 140 // if nc.L1Deployer.L1ETHAmountToSequencer != "" { 141 // // Send some Ether from L1 deployer to sequencer acc 142 // ethAmount, _ := big.NewInt(0).SetString(nc.L1Deployer.L1ETHAmountToSequencer, encoding.Base10) 143 // log.Infof("Transferring %s L1 ETH to sequencer %q from L1 deployer %q", nc.L1Deployer.L1ETHAmountToSequencer, nc.sequencerAddress, nc.L1Deployer.Address) 144 // fromAddress := common.HexToAddress(nc.L1Deployer.Address) 145 // nonce, err := clientL1.PendingNonceAt(ctx, fromAddress) 146 // if err != nil { 147 // return err 148 // } 149 // const gasLimit = 21000 150 151 // tx := types.NewTransaction(nonce, sequencerAddress, ethAmount, gasLimit, gasPrice, nil) 152 // signedTx, err := authDeployer.Signer(authDeployer.From, tx) 153 // if err != nil { 154 // return err 155 // } 156 // err = clientL1.SendTransaction(ctx, signedTx) 157 // if err != nil { 158 // return err 159 // } 160 // err = operations.WaitTxToBeMined(clientL1, signedTx.Hash(), nc.TxTimeout) 161 // if err != nil { 162 // return err 163 // } 164 // } 165 166 // if nc.L1Deployer.L1MaticAmountToSequencer != "" { 167 // // Create matic maticTokenSC sc instance 168 // log.Infof("Loading Matic token SC instance") 169 // log.Infof("Matic add %s", cfg.NetworkConfig.MaticAddr) 170 // maticTokenSC, err := matic.NewMatic(cfg.NetworkConfig.MaticAddr, clientL1) 171 // if err != nil { 172 // return err 173 // } 174 // // Send matic to sequencer 175 // maticAmount, _ := big.NewInt(0).SetString(nc.L1Deployer.L1MaticAmountToSequencer, encoding.Base10) 176 // log.Infof("Transferring %s L1 MATIC tokens to sequencer %q from L1 deployer %q", nc.L1Deployer.L1MaticAmountToSequencer, nc.sequencerAddress, nc.L1Deployer.Address) 177 // tx, err := maticTokenSC.Transfer(authDeployer, sequencerAddress, maticAmount) 178 // if err != nil { 179 // return err 180 // } 181 182 // // wait matic transfer to be mined 183 // err = operations.WaitTxToBeMined(clientL1, tx.Hash(), nc.TxTimeout) 184 // if err != nil { 185 // return err 186 // } 187 188 // // approve tokens to be used by PoE SC on behalf of the sequencer 189 // log.Infof("Approving %s L1 MATIC tokens to be used by PoE on behalf of the sequencer %q", maticAmount.String(), nc.sequencerAddress) 190 // tx, err = maticTokenSC.Approve(authSequencer, cfg.NetworkConfig.PoEAddr, maticAmount) 191 // if err != nil { 192 // return err 193 // } 194 // err = operations.WaitTxToBeMined(clientL1, tx.Hash(), nc.TxTimeout) 195 // if err != nil { 196 // return err 197 // } 198 // } 199 200 // // Register the sequencer 201 // log.Infof("Registering the sequencer") 202 // ethermanConfig := etherman.Config{ 203 // URL: nc.L1NetworkURL, 204 // } 205 // etherman, err := etherman.NewClient(ethermanConfig, authSequencer, cfg.NetworkConfig.PoEAddr, cfg.NetworkConfig.MaticAddr) 206 // if err != nil { 207 // return err 208 // } 209 // tx, err := etherman.RegisterSequencer(nc.L2NetworkURL) 210 // if err != nil { 211 // return err 212 // } 213 214 // // Wait sequencer to be registered 215 // log.Infof("waiting sequencer to be registered") 216 // err = operations.WaitTxToBeMined(clientL1, tx.Hash(), nc.TxTimeout) 217 // if err != nil { 218 // return err 219 // } 220 // log.Infof("sequencer registered") 221 222 // const intervalToWaitTheSequencerToGetRegistered = 10 * time.Second 223 // time.Sleep(intervalToWaitTheSequencerToGetRegistered) 224 225 // // Deposit funds to L2 via bridge 226 // depositAmount, _ := big.NewInt(0).SetString("10000000000000000000", encoding.Base10) 227 // log.Infof("Depositing funds to L2 via bridge using %s L1 ETH from L1 deployer %q", depositAmount.String(), nc.L1Deployer.Address) 228 // balance, err := clientL1.BalanceAt(ctx, authSequencer.From, nil) 229 // if err != nil { 230 // return err 231 // } 232 // log.Debugf("ETH Balance of %q: %s", nc.L1Deployer.Address, balance.Text(encoding.Base10)) 233 234 // const destNetwork = uint32(1) 235 // ethAddr := common.Address{} 236 // destAddr := common.HexToAddress(BridgeDepositReceiverAddress) 237 // err = sendL1Deposit(ctx, authDeployer, clientL1, ethAddr, depositAmount, destNetwork, &destAddr, nc.L1BridgeAddr, nc.TxTimeout) 238 // if err != nil { 239 // return err 240 // } 241 242 // lastBatchNumber, err := clientL2.BlockNumber(ctx) 243 // if err != nil { 244 // return err 245 // } 246 247 // // Proposing empty batch to trigger the l2 synchronization process 248 // err = forceBatchProposal(ctx, authSequencer, clientL1, cfg.NetworkConfig, nc.L1BridgeAddr, nc.TxTimeout) 249 // if err != nil { 250 // return err 251 // } 252 253 // expectedLastBatchNumber := lastBatchNumber + 1 254 // for { 255 // currentLastBatchNumber, err := clientL2.BlockNumber(ctx) 256 // if err != nil { 257 // return err 258 // } 259 // log.Infof("Waiting synchronizer to sync the forced empty batch. Current: %v Expected: %v", currentLastBatchNumber, expectedLastBatchNumber) 260 // if currentLastBatchNumber == expectedLastBatchNumber { 261 // break 262 // } 263 // time.Sleep(1 * time.Second) 264 // } 265 266 // // Claiming the funds deposited via bridge on L2 267 // deposit := deposit{ 268 // TokenAddr: common.Address{}, 269 // Amount: depositAmount, 270 // OrigNet: 0, 271 // DestNet: destNetwork, 272 // DestAddr: destAddr, 273 // DepositCnt: 0, 274 // } 275 // smtProof := [][32]byte{ 276 // common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), 277 // common.HexToHash("0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5"), 278 // common.HexToHash("0xb4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30"), 279 // common.HexToHash("0x21ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85"), 280 // common.HexToHash("0xe58769b32a1beaf1ea27375a44095a0d1fb664ce2dd358e7fcbfb78c26a19344"), 281 // common.HexToHash("0x0eb01ebfc9ed27500cd4dfc979272d1f0913cc9f66540d7e8005811109e1cf2d"), 282 // common.HexToHash("0x887c22bd8750d34016ac3c66b5ff102dacdd73f6b014e710b51e8022af9a1968"), 283 // common.HexToHash("0xffd70157e48063fc33c97a050f7f640233bf646cc98d9524c6b92bcf3ab56f83"), 284 // common.HexToHash("0x9867cc5f7f196b93bae1e27e6320742445d290f2263827498b54fec539f756af"), 285 // common.HexToHash("0xcefad4e508c098b9a7e1d8feb19955fb02ba9675585078710969d3440f5054e0"), 286 // common.HexToHash("0xf9dc3e7fe016e050eff260334f18a5d4fe391d82092319f5964f2e2eb7c1c3a5"), 287 // common.HexToHash("0xf8b13a49e282f609c317a833fb8d976d11517c571d1221a265d25af778ecf892"), 288 // common.HexToHash("0x3490c6ceeb450aecdc82e28293031d10c7d73bf85e57bf041a97360aa2c5d99c"), 289 // common.HexToHash("0xc1df82d9c4b87413eae2ef048f94b4d3554cea73d92b0f7af96e0271c691e2bb"), 290 // common.HexToHash("0x5c67add7c6caf302256adedf7ab114da0acfe870d449a3a489f781d659e8becc"), 291 // common.HexToHash("0xda7bce9f4e8618b6bd2f4132ce798cdc7a60e7e1460a7299e3c6342a579626d2"), 292 // common.HexToHash("0x2733e50f526ec2fa19a22b31e8ed50f23cd1fdf94c9154ed3a7609a2f1ff981f"), 293 // common.HexToHash("0xe1d3b5c807b281e4683cc6d6315cf95b9ade8641defcb32372f1c126e398ef7a"), 294 // common.HexToHash("0x5a2dce0a8a7f68bb74560f8f71837c2c2ebbcbf7fffb42ae1896f13f7c7479a0"), 295 // common.HexToHash("0xb46a28b6f55540f89444f63de0378e3d121be09e06cc9ded1c20e65876d36aa0"), 296 // common.HexToHash("0xc65e9645644786b620e2dd2ad648ddfcbf4a7e5b1a3a4ecfe7f64667a3f0b7e2"), 297 // common.HexToHash("0xf4418588ed35a2458cffeb39b93d26f18d2ab13bdce6aee58e7b99359ec2dfd9"), 298 // common.HexToHash("0x5a9c16dc00d6ef18b7933a6f8dc65ccb55667138776f7dea101070dc8796e377"), 299 // common.HexToHash("0x4df84f40ae0c8229d0d6069e5c8f39a7c299677a09d367fc7b05e3bc380ee652"), 300 // common.HexToHash("0xcdc72595f74c7b1043d0e1ffbab734648c838dfb0527d971b602bc216c9619ef"), 301 // common.HexToHash("0x0abf5ac974a1ed57f4050aa510dd9c74f508277b39d7973bb2dfccc5eeb0618d"), 302 // common.HexToHash("0xb8cd74046ff337f0a7bf2c8e03e10f642c1886798d71806ab1e888d9e5ee87d0"), 303 // common.HexToHash("0x838c5655cb21c6cb83313b5a631175dff4963772cce9108188b34ac87c81c41e"), 304 // common.HexToHash("0x662ee4dd2dd7b2bc707961b1e646c4047669dcb6584f0d8d770daf5d7e7deb2e"), 305 // common.HexToHash("0x388ab20e2573d171a88108e79d820e98f26c0b84aa8b2f4aa4968dbb818ea322"), 306 // common.HexToHash("0x93237c50ba75ee485f4c22adf2f741400bdf8d6a9cc7df7ecae576221665d735"), 307 // common.HexToHash("0x8448818bb4ae4562849e949e17ac16e0be16688e156b5cf15e098c627c0056a9"), 308 // } 309 // globalExitRoot := &globalExitRoot{ 310 // GlobalExitRootNum: big.NewInt(1), 311 // ExitRoots: []common.Hash{ 312 // common.HexToHash("0x843cb84814162b93794ad9087a037a1948f9aff051838ba3a93db0ac92b9f719"), 313 // common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), 314 // }, 315 // } 316 317 // log.Infof("Getting chainID L2") 318 // chainIDL2, err := clientL2.NetworkID(ctx) 319 // if err != nil { 320 // return err 321 // } 322 323 // // Preparing bridge receiver acc info 324 // log.Infof("Creating bridge receiver authorization") 325 // privateKey, err = crypto.HexToECDSA(strings.TrimPrefix(BridgeDepositReceiverPrivateKey, "0x")) 326 // if err != nil { 327 // return err 328 // } 329 // authBridgeReceiver, err := bind.NewKeyedTransactorWithChainID(privateKey, chainIDL2) 330 // if err != nil { 331 // return err 332 // } 333 334 // err = sendL2Claim(ctx, authBridgeReceiver, clientL2, deposit, smtProof, globalExitRoot, nc.L2BridgeAddr, nc.TxTimeout) 335 // if err != nil { 336 // return err 337 // } 338 339 // log.Infof("Network initialized properly") 340 // return nil 341 // } 342 343 // // sendL1Deposit sends a deposit from l1 to l2 344 // func sendL1Deposit(ctx context.Context, auth *bind.TransactOpts, client *ethclient.Client, tokenAddr common.Address, amount *big.Int, 345 // destNetwork uint32, destAddr *common.Address, l1BridgeAddr string, txTimeout time.Duration, 346 // ) error { 347 // emptyAddr := common.Address{} 348 // if tokenAddr == emptyAddr { 349 // auth.Value = amount 350 // } 351 // if destAddr == nil { 352 // destAddr = &auth.From 353 // } 354 // br, err := bridge.NewBridge(common.HexToAddress(l1BridgeAddr), client) 355 // if err != nil { 356 // return err 357 // } 358 359 // tx, err := br.Bridge(auth, tokenAddr, amount, destNetwork, *destAddr) 360 // if err != nil { 361 // return err 362 // } 363 364 // log.Infof("Waiting L1Deposit to be mined") 365 // err = operations.WaitTxToBeMined(client, tx.Hash(), txTimeout) 366 // if err != nil { 367 // return err 368 // } 369 // log.Infof("L1Deposit mined: %v", tx.Hash()) 370 // return nil 371 // } 372 373 // func forceBatchProposal(ctx context.Context, auth *bind.TransactOpts, client *ethclient.Client, cfg config.NetworkConfig, l1BridgeAddr string, txTimeout time.Duration) error { 374 // log.Infof("Forcing batch proposal") 375 376 // poe, err := proofofefficiency.NewProofofefficiency(cfg.PoEAddr, client) 377 // if err != nil { 378 // return err 379 // } 380 // maticAmount, err := poe.CalculateSequencerCollateral(&bind.CallOpts{Pending: false}) 381 // if err != nil { 382 // return err 383 // } 384 // log.Infof("Collateral: %v", maticAmount.Text(encoding.Base10)) 385 386 // tx, err := poe.SendBatch(auth, []byte{}, maticAmount) 387 // if err != nil { 388 // return err 389 // } 390 391 // log.Infof("Waiting force batch proposal to be mined") 392 // err = operations.WaitTxToBeMined(client, tx.Hash(), txTimeout) 393 394 // return err 395 // } 396 397 // func sendL2Claim(ctx context.Context, auth *bind.TransactOpts, client *ethclient.Client, dep deposit, smtProof [][32]byte, globalExitRoot *globalExitRoot, l2BridgeAddr string, txTimeout time.Duration) error { 398 // auth.GasPrice = big.NewInt(0) 399 // br, err := bridge.NewBridge(common.HexToAddress(l2BridgeAddr), client) 400 // if err != nil { 401 // return err 402 // } 403 404 // tx, err := br.Claim(auth, dep.TokenAddr, dep.Amount, dep.OrigNet, dep.DestNet, 405 // dep.DestAddr, smtProof, dep.DepositCnt, globalExitRoot.GlobalExitRootNum, 406 // globalExitRoot.ExitRoots[0], globalExitRoot.ExitRoots[1]) 407 // if err != nil { 408 // return err 409 // } 410 411 // log.Infof("waiting L2 Claim tx to be mined") 412 // err = operations.WaitTxToBeMined(client, tx.Hash(), txTimeout) 413 414 // return err 415 // }