You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

989 lines
38 KiB

4 years ago
  1. package eth
  2. import (
  3. "context"
  4. "fmt"
  5. "math/big"
  6. "strconv"
  7. "strings"
  8. "github.com/ethereum/go-ethereum"
  9. "github.com/ethereum/go-ethereum/accounts/abi"
  10. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  11. ethCommon "github.com/ethereum/go-ethereum/common"
  12. "github.com/ethereum/go-ethereum/core/types"
  13. "github.com/ethereum/go-ethereum/crypto"
  14. "github.com/ethereum/go-ethereum/ethclient"
  15. "github.com/hermeznetwork/hermez-node/common"
  16. Hermez "github.com/hermeznetwork/hermez-node/eth/contracts/hermez"
  17. HEZ "github.com/hermeznetwork/hermez-node/eth/contracts/tokenHEZ"
  18. "github.com/hermeznetwork/hermez-node/log"
  19. "github.com/hermeznetwork/tracerr"
  20. "github.com/iden3/go-iden3-crypto/babyjub"
  21. )
  22. // QueueStruct is the queue of L1Txs for a batch
  23. type QueueStruct struct {
  24. L1TxQueue []common.L1Tx
  25. TotalL1TxFee *big.Int
  26. }
  27. // NewQueueStruct creates a new clear QueueStruct.
  28. func NewQueueStruct() *QueueStruct {
  29. return &QueueStruct{
  30. L1TxQueue: make([]common.L1Tx, 0),
  31. TotalL1TxFee: big.NewInt(0),
  32. }
  33. }
  34. // RollupState represents the state of the Rollup in the Smart Contract
  35. type RollupState struct {
  36. StateRoot *big.Int
  37. ExitRoots []*big.Int
  38. // ExitNullifierMap map[[256 / 8]byte]bool
  39. ExitNullifierMap map[int64]map[int64]bool // batchNum -> idx -> bool
  40. TokenList []ethCommon.Address
  41. TokenMap map[ethCommon.Address]bool
  42. MapL1TxQueue map[int64]*QueueStruct
  43. LastL1L2Batch int64
  44. CurrentToForgeL1TxsNum int64
  45. LastToForgeL1TxsNum int64
  46. CurrentIdx int64
  47. }
  48. // RollupEventInitialize is the InitializeHermezEvent event of the
  49. // Smart Contract
  50. type RollupEventInitialize struct {
  51. ForgeL1L2BatchTimeout uint8
  52. FeeAddToken *big.Int
  53. WithdrawalDelay uint64
  54. }
  55. // RollupVariables returns the RollupVariables from the initialize event
  56. func (ei *RollupEventInitialize) RollupVariables() *common.RollupVariables {
  57. var buckets [common.RollupConstNumBuckets]common.BucketParams
  58. for i := range buckets {
  59. buckets[i] = common.BucketParams{
  60. CeilUSD: big.NewInt(0),
  61. Withdrawals: big.NewInt(0),
  62. BlockWithdrawalRate: big.NewInt(0),
  63. MaxWithdrawals: big.NewInt(0),
  64. }
  65. }
  66. return &common.RollupVariables{
  67. EthBlockNum: 0,
  68. FeeAddToken: ei.FeeAddToken,
  69. ForgeL1L2BatchTimeout: int64(ei.ForgeL1L2BatchTimeout),
  70. WithdrawalDelay: ei.WithdrawalDelay,
  71. Buckets: buckets,
  72. SafeMode: false,
  73. }
  74. }
  75. // RollupEventL1UserTx is an event of the Rollup Smart Contract
  76. type RollupEventL1UserTx struct {
  77. // ToForgeL1TxsNum int64 // QueueIndex *big.Int
  78. // Position int // TransactionIndex *big.Int
  79. L1UserTx common.L1Tx
  80. }
  81. // RollupEventL1UserTxAux is an event of the Rollup Smart Contract
  82. type rollupEventL1UserTxAux struct {
  83. ToForgeL1TxsNum uint64 // QueueIndex *big.Int
  84. Position uint8 // TransactionIndex *big.Int
  85. L1UserTx []byte
  86. }
  87. // RollupEventAddToken is an event of the Rollup Smart Contract
  88. type RollupEventAddToken struct {
  89. TokenAddress ethCommon.Address
  90. TokenID uint32
  91. }
  92. // RollupEventForgeBatch is an event of the Rollup Smart Contract
  93. type RollupEventForgeBatch struct {
  94. BatchNum int64
  95. // Sender ethCommon.Address
  96. EthTxHash ethCommon.Hash
  97. L1UserTxsLen uint16
  98. }
  99. // RollupEventUpdateForgeL1L2BatchTimeout is an event of the Rollup Smart Contract
  100. type RollupEventUpdateForgeL1L2BatchTimeout struct {
  101. NewForgeL1L2BatchTimeout int64
  102. }
  103. // RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
  104. type RollupEventUpdateFeeAddToken struct {
  105. NewFeeAddToken *big.Int
  106. }
  107. // RollupEventWithdraw is an event of the Rollup Smart Contract
  108. type RollupEventWithdraw struct {
  109. Idx uint64
  110. NumExitRoot uint64
  111. InstantWithdraw bool
  112. TxHash ethCommon.Hash // Hash of the transaction that generated this event
  113. }
  114. type rollupEventUpdateBucketWithdrawAux struct {
  115. NumBucket uint8
  116. BlockStamp *big.Int
  117. Withdrawals *big.Int
  118. }
  119. // RollupEventUpdateBucketWithdraw is an event of the Rollup Smart Contract
  120. type RollupEventUpdateBucketWithdraw struct {
  121. NumBucket int
  122. BlockStamp int64 // blockNum
  123. Withdrawals *big.Int
  124. }
  125. // RollupEventUpdateWithdrawalDelay is an event of the Rollup Smart Contract
  126. type RollupEventUpdateWithdrawalDelay struct {
  127. NewWithdrawalDelay uint64
  128. }
  129. // RollupUpdateBucketsParameters are the bucket parameters used in an update
  130. type RollupUpdateBucketsParameters struct {
  131. CeilUSD *big.Int
  132. Withdrawals *big.Int
  133. BlockWithdrawalRate *big.Int
  134. MaxWithdrawals *big.Int
  135. }
  136. type rollupEventUpdateBucketsParametersAux struct {
  137. ArrayBuckets [common.RollupConstNumBuckets][4]*big.Int
  138. }
  139. // RollupEventUpdateBucketsParameters is an event of the Rollup Smart Contract
  140. type RollupEventUpdateBucketsParameters struct {
  141. // ArrayBuckets [common.RollupConstNumBuckets][4]*big.Int
  142. ArrayBuckets [common.RollupConstNumBuckets]RollupUpdateBucketsParameters
  143. SafeMode bool
  144. }
  145. // RollupEventUpdateTokenExchange is an event of the Rollup Smart Contract
  146. type RollupEventUpdateTokenExchange struct {
  147. AddressArray []ethCommon.Address
  148. ValueArray []uint64
  149. }
  150. // RollupEventSafeMode is an event of the Rollup Smart Contract
  151. type RollupEventSafeMode struct {
  152. }
  153. // RollupEvents is the list of events in a block of the Rollup Smart Contract
  154. type RollupEvents struct {
  155. L1UserTx []RollupEventL1UserTx
  156. AddToken []RollupEventAddToken
  157. ForgeBatch []RollupEventForgeBatch
  158. UpdateForgeL1L2BatchTimeout []RollupEventUpdateForgeL1L2BatchTimeout
  159. UpdateFeeAddToken []RollupEventUpdateFeeAddToken
  160. Withdraw []RollupEventWithdraw
  161. UpdateWithdrawalDelay []RollupEventUpdateWithdrawalDelay
  162. UpdateBucketWithdraw []RollupEventUpdateBucketWithdraw
  163. UpdateBucketsParameters []RollupEventUpdateBucketsParameters
  164. UpdateTokenExchange []RollupEventUpdateTokenExchange
  165. SafeMode []RollupEventSafeMode
  166. }
  167. // NewRollupEvents creates an empty RollupEvents with the slices initialized.
  168. func NewRollupEvents() RollupEvents {
  169. return RollupEvents{
  170. L1UserTx: make([]RollupEventL1UserTx, 0),
  171. AddToken: make([]RollupEventAddToken, 0),
  172. ForgeBatch: make([]RollupEventForgeBatch, 0),
  173. UpdateForgeL1L2BatchTimeout: make([]RollupEventUpdateForgeL1L2BatchTimeout, 0),
  174. UpdateFeeAddToken: make([]RollupEventUpdateFeeAddToken, 0),
  175. Withdraw: make([]RollupEventWithdraw, 0),
  176. }
  177. }
  178. // RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
  179. type RollupForgeBatchArgs struct {
  180. NewLastIdx int64
  181. NewStRoot *big.Int
  182. NewExitRoot *big.Int
  183. L1UserTxs []common.L1Tx
  184. L1CoordinatorTxs []common.L1Tx
  185. L1CoordinatorTxsAuths [][]byte // Authorization for accountCreations for each L1CoordinatorTx
  186. L2TxsData []common.L2Tx
  187. FeeIdxCoordinator []common.Idx
  188. // Circuit selector
  189. VerifierIdx uint8
  190. L1Batch bool
  191. ProofA [2]*big.Int
  192. ProofB [2][2]*big.Int
  193. ProofC [2]*big.Int
  194. }
  195. // RollupForgeBatchArgsAux are the arguments to the ForgeBatch function in the Rollup Smart Contract
  196. type rollupForgeBatchArgsAux struct {
  197. NewLastIdx *big.Int
  198. NewStRoot *big.Int
  199. NewExitRoot *big.Int
  200. EncodedL1CoordinatorTx []byte
  201. L1L2TxsData []byte
  202. FeeIdxCoordinator []byte
  203. // Circuit selector
  204. VerifierIdx uint8
  205. L1Batch bool
  206. ProofA [2]*big.Int
  207. ProofB [2][2]*big.Int
  208. ProofC [2]*big.Int
  209. }
  210. // RollupInterface is the inteface to to Rollup Smart Contract
  211. type RollupInterface interface {
  212. //
  213. // Smart Contract Methods
  214. //
  215. // Public Functions
  216. RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
  217. RollupAddToken(tokenAddress ethCommon.Address, feeAddToken, deadline *big.Int) (*types.Transaction, error)
  218. RollupWithdrawMerkleProof(babyPubKey *babyjub.PublicKey, tokenID uint32, numExitRoot, idx int64, amount *big.Int, siblings []*big.Int, instantWithdraw bool) (*types.Transaction, error)
  219. RollupWithdrawCircuit(proofA, proofC [2]*big.Int, proofB [2][2]*big.Int, tokenID uint32, numExitRoot, idx int64, amount *big.Int, instantWithdraw bool) (*types.Transaction, error)
  220. RollupL1UserTxERC20ETH(fromBJJ *babyjub.PublicKey, fromIdx int64, depositAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error)
  221. RollupL1UserTxERC20Permit(fromBJJ *babyjub.PublicKey, fromIdx int64, depositAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64, deadline *big.Int) (tx *types.Transaction, err error)
  222. // Governance Public Functions
  223. RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout int64) (*types.Transaction, error)
  224. RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
  225. // Viewers
  226. RollupRegisterTokensCount() (*big.Int, error)
  227. RollupLastForgedBatch() (int64, error)
  228. //
  229. // Smart Contract Status
  230. //
  231. RollupConstants() (*common.RollupConstants, error)
  232. RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
  233. RollupForgeBatchArgs(ethCommon.Hash, uint16) (*RollupForgeBatchArgs, *ethCommon.Address, error)
  234. RollupEventInit() (*RollupEventInitialize, int64, error)
  235. }
  236. //
  237. // Implementation
  238. //
  239. // RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
  240. type RollupClient struct {
  241. client *EthereumClient
  242. chainID *big.Int
  243. address ethCommon.Address
  244. tokenHEZCfg TokenConfig
  245. hermez *Hermez.Hermez
  246. tokenHEZ *HEZ.HEZ
  247. contractAbi abi.ABI
  248. opts *bind.CallOpts
  249. }
  250. // NewRollupClient creates a new RollupClient
  251. func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZCfg TokenConfig) (*RollupClient, error) {
  252. contractAbi, err := abi.JSON(strings.NewReader(string(Hermez.HermezABI)))
  253. if err != nil {
  254. return nil, tracerr.Wrap(err)
  255. }
  256. hermez, err := Hermez.NewHermez(address, client.Client())
  257. if err != nil {
  258. return nil, tracerr.Wrap(err)
  259. }
  260. tokenHEZ, err := HEZ.NewHEZ(tokenHEZCfg.Address, client.Client())
  261. if err != nil {
  262. return nil, tracerr.Wrap(err)
  263. }
  264. chainID, err := client.client.ChainID(context.Background())
  265. if err != nil {
  266. return nil, tracerr.Wrap(err)
  267. }
  268. return &RollupClient{
  269. client: client,
  270. chainID: chainID,
  271. address: address,
  272. tokenHEZCfg: tokenHEZCfg,
  273. hermez: hermez,
  274. tokenHEZ: tokenHEZ,
  275. contractAbi: contractAbi,
  276. opts: newCallOpts(),
  277. }, nil
  278. }
  279. // RollupForgeBatch is the interface to call the smart contract function
  280. func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (tx *types.Transaction, err error) {
  281. if tx, err = c.client.CallAuth(
  282. 1000000, //nolint:gomnd
  283. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  284. rollupConst, err := c.RollupConstants()
  285. if err != nil {
  286. return nil, tracerr.Wrap(err)
  287. }
  288. nLevels := rollupConst.Verifiers[args.VerifierIdx].NLevels
  289. lenBytes := nLevels / 8 //nolint:gomnd
  290. newLastIdx := big.NewInt(int64(args.NewLastIdx))
  291. // L1CoordinatorBytes
  292. var l1CoordinatorBytes []byte
  293. for i := 0; i < len(args.L1CoordinatorTxs); i++ {
  294. l1 := args.L1CoordinatorTxs[i]
  295. bytesl1, err := l1.BytesCoordinatorTx(args.L1CoordinatorTxsAuths[i])
  296. if err != nil {
  297. return nil, tracerr.Wrap(err)
  298. }
  299. l1CoordinatorBytes = append(l1CoordinatorBytes, bytesl1[:]...)
  300. }
  301. // L1L2TxData
  302. var l1l2TxData []byte
  303. for i := 0; i < len(args.L1UserTxs); i++ {
  304. l1User := args.L1UserTxs[i]
  305. bytesl1User, err := l1User.BytesDataAvailability(uint32(nLevels))
  306. if err != nil {
  307. return nil, tracerr.Wrap(err)
  308. }
  309. l1l2TxData = append(l1l2TxData, bytesl1User[:]...)
  310. }
  311. for i := 0; i < len(args.L1CoordinatorTxs); i++ {
  312. l1Coord := args.L1CoordinatorTxs[i]
  313. bytesl1Coord, err := l1Coord.BytesDataAvailability(uint32(nLevels))
  314. if err != nil {
  315. return nil, tracerr.Wrap(err)
  316. }
  317. l1l2TxData = append(l1l2TxData, bytesl1Coord[:]...)
  318. }
  319. for i := 0; i < len(args.L2TxsData); i++ {
  320. l2 := args.L2TxsData[i]
  321. bytesl2, err := l2.BytesDataAvailability(uint32(nLevels))
  322. if err != nil {
  323. return nil, tracerr.Wrap(err)
  324. }
  325. l1l2TxData = append(l1l2TxData, bytesl2[:]...)
  326. }
  327. // FeeIdxCoordinator
  328. var feeIdxCoordinator []byte
  329. if len(args.FeeIdxCoordinator) > common.RollupConstMaxFeeIdxCoordinator {
  330. return nil, tracerr.Wrap(fmt.Errorf("len(args.FeeIdxCoordinator) > %v",
  331. common.RollupConstMaxFeeIdxCoordinator))
  332. }
  333. for i := 0; i < common.RollupConstMaxFeeIdxCoordinator; i++ {
  334. feeIdx := common.Idx(0)
  335. if i < len(args.FeeIdxCoordinator) {
  336. feeIdx = args.FeeIdxCoordinator[i]
  337. }
  338. bytesFeeIdx, err := feeIdx.Bytes()
  339. if err != nil {
  340. return nil, tracerr.Wrap(err)
  341. }
  342. feeIdxCoordinator = append(feeIdxCoordinator, bytesFeeIdx[len(bytesFeeIdx)-int(lenBytes):]...)
  343. }
  344. return c.hermez.ForgeBatch(auth, newLastIdx, args.NewStRoot, args.NewExitRoot, l1CoordinatorBytes, l1l2TxData, feeIdxCoordinator, args.VerifierIdx, args.L1Batch, args.ProofA, args.ProofB, args.ProofC)
  345. },
  346. ); err != nil {
  347. return nil, tracerr.Wrap(fmt.Errorf("Failed forge batch: %w", err))
  348. }
  349. return tx, nil
  350. }
  351. // RollupAddToken is the interface to call the smart contract function.
  352. // `feeAddToken` is the amount of HEZ tokens that will be paid to add the
  353. // token. `feeAddToken` must match the public value of the smart contract.
  354. func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address, feeAddToken, deadline *big.Int) (tx *types.Transaction, err error) {
  355. if tx, err = c.client.CallAuth(
  356. 0,
  357. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  358. owner := c.client.account.Address
  359. spender := c.address
  360. nonce, err := c.tokenHEZ.Nonces(c.opts, owner)
  361. if err != nil {
  362. return nil, tracerr.Wrap(err)
  363. }
  364. tokenName := c.tokenHEZCfg.Name
  365. tokenAddr := c.tokenHEZCfg.Address
  366. digest, _ := createPermitDigest(tokenAddr, owner, spender, c.chainID, feeAddToken, nonce, deadline, tokenName)
  367. signature, _ := c.client.ks.SignHash(*c.client.account, digest)
  368. permit := createPermit(owner, spender, feeAddToken, deadline, digest, signature)
  369. return c.hermez.AddToken(auth, tokenAddress, permit)
  370. },
  371. ); err != nil {
  372. return nil, tracerr.Wrap(fmt.Errorf("Failed add Token %w", err))
  373. }
  374. return tx, nil
  375. }
  376. // RollupWithdrawMerkleProof is the interface to call the smart contract function
  377. func (c *RollupClient) RollupWithdrawMerkleProof(fromBJJ *babyjub.PublicKey, tokenID uint32, numExitRoot, idx int64, amount *big.Int, siblings []*big.Int, instantWithdraw bool) (tx *types.Transaction, err error) {
  378. if tx, err = c.client.CallAuth(
  379. 0,
  380. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  381. pkCompL := fromBJJ.Compress()
  382. pkCompB := common.SwapEndianness(pkCompL[:])
  383. babyPubKey := new(big.Int).SetBytes(pkCompB)
  384. numExitRootB := uint32(numExitRoot)
  385. idxBig := big.NewInt(idx)
  386. return c.hermez.WithdrawMerkleProof(auth, tokenID, amount, babyPubKey, numExitRootB, siblings, idxBig, instantWithdraw)
  387. },
  388. ); err != nil {
  389. return nil, tracerr.Wrap(fmt.Errorf("Failed update WithdrawMerkleProof: %w", err))
  390. }
  391. return tx, nil
  392. }
  393. // RollupWithdrawCircuit is the interface to call the smart contract function
  394. func (c *RollupClient) RollupWithdrawCircuit(proofA, proofC [2]*big.Int, proofB [2][2]*big.Int, tokenID uint32, numExitRoot, idx int64, amount *big.Int, instantWithdraw bool) (*types.Transaction, error) {
  395. log.Error("TODO")
  396. return nil, tracerr.Wrap(errTODO)
  397. }
  398. // RollupL1UserTxERC20ETH is the interface to call the smart contract function
  399. func (c *RollupClient) RollupL1UserTxERC20ETH(fromBJJ *babyjub.PublicKey, fromIdx int64, depositAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (tx *types.Transaction, err error) {
  400. if tx, err = c.client.CallAuth(
  401. 0,
  402. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  403. var babyPubKey *big.Int
  404. if fromBJJ != nil {
  405. pkCompL := fromBJJ.Compress()
  406. pkCompB := common.SwapEndianness(pkCompL[:])
  407. babyPubKey = new(big.Int).SetBytes(pkCompB)
  408. } else {
  409. babyPubKey = big.NewInt(0)
  410. }
  411. fromIdxBig := big.NewInt(fromIdx)
  412. toIdxBig := big.NewInt(toIdx)
  413. depositAmountF, err := common.NewFloat16(depositAmount)
  414. if err != nil {
  415. return nil, tracerr.Wrap(err)
  416. }
  417. amountF, err := common.NewFloat16(amount)
  418. if err != nil {
  419. return nil, tracerr.Wrap(err)
  420. }
  421. if tokenID == 0 {
  422. auth.Value = depositAmount
  423. }
  424. var permit []byte
  425. return c.hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(depositAmountF),
  426. uint16(amountF), tokenID, toIdxBig, permit)
  427. },
  428. ); err != nil {
  429. return nil, tracerr.Wrap(fmt.Errorf("Failed add L1 Tx ERC20/ETH: %w", err))
  430. }
  431. return tx, nil
  432. }
  433. // RollupL1UserTxERC20Permit is the interface to call the smart contract function
  434. func (c *RollupClient) RollupL1UserTxERC20Permit(fromBJJ *babyjub.PublicKey, fromIdx int64, depositAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64, deadline *big.Int) (tx *types.Transaction, err error) {
  435. if tx, err = c.client.CallAuth(
  436. 0,
  437. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  438. var babyPubKey *big.Int
  439. if fromBJJ != nil {
  440. pkCompL := fromBJJ.Compress()
  441. pkCompB := common.SwapEndianness(pkCompL[:])
  442. babyPubKey = new(big.Int).SetBytes(pkCompB)
  443. } else {
  444. babyPubKey = big.NewInt(0)
  445. }
  446. fromIdxBig := big.NewInt(fromIdx)
  447. toIdxBig := big.NewInt(toIdx)
  448. depositAmountF, err := common.NewFloat16(depositAmount)
  449. if err != nil {
  450. return nil, tracerr.Wrap(err)
  451. }
  452. amountF, err := common.NewFloat16(amount)
  453. if err != nil {
  454. return nil, tracerr.Wrap(err)
  455. }
  456. if tokenID == 0 {
  457. auth.Value = depositAmount
  458. }
  459. owner := c.client.account.Address
  460. spender := c.address
  461. nonce, err := c.tokenHEZ.Nonces(c.opts, owner)
  462. if err != nil {
  463. return nil, tracerr.Wrap(err)
  464. }
  465. tokenName := c.tokenHEZCfg.Name
  466. tokenAddr := c.tokenHEZCfg.Address
  467. digest, _ := createPermitDigest(tokenAddr, owner, spender, c.chainID, amount, nonce, deadline, tokenName)
  468. signature, _ := c.client.ks.SignHash(*c.client.account, digest)
  469. permit := createPermit(owner, spender, amount, deadline, digest, signature)
  470. return c.hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(depositAmountF),
  471. uint16(amountF), tokenID, toIdxBig, permit)
  472. },
  473. ); err != nil {
  474. return nil, tracerr.Wrap(fmt.Errorf("Failed add L1 Tx ERC20Permit: %w", err))
  475. }
  476. return tx, nil
  477. }
  478. // RollupRegisterTokensCount is the interface to call the smart contract function
  479. func (c *RollupClient) RollupRegisterTokensCount() (registerTokensCount *big.Int, err error) {
  480. if err := c.client.Call(func(ec *ethclient.Client) error {
  481. registerTokensCount, err = c.hermez.RegisterTokensCount(c.opts)
  482. return tracerr.Wrap(err)
  483. }); err != nil {
  484. return nil, tracerr.Wrap(err)
  485. }
  486. return registerTokensCount, nil
  487. }
  488. // RollupLastForgedBatch is the interface to call the smart contract function
  489. func (c *RollupClient) RollupLastForgedBatch() (lastForgedBatch int64, err error) {
  490. if err := c.client.Call(func(ec *ethclient.Client) error {
  491. _lastForgedBatch, err := c.hermez.LastForgedBatch(c.opts)
  492. lastForgedBatch = int64(_lastForgedBatch)
  493. return tracerr.Wrap(err)
  494. }); err != nil {
  495. return 0, tracerr.Wrap(err)
  496. }
  497. return lastForgedBatch, nil
  498. }
  499. // RollupUpdateForgeL1L2BatchTimeout is the interface to call the smart contract function
  500. func (c *RollupClient) RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout int64) (tx *types.Transaction, err error) {
  501. if tx, err = c.client.CallAuth(
  502. 0,
  503. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  504. return c.hermez.UpdateForgeL1L2BatchTimeout(auth, uint8(newForgeL1L2BatchTimeout))
  505. },
  506. ); err != nil {
  507. return nil, tracerr.Wrap(fmt.Errorf("Failed update ForgeL1L2BatchTimeout: %w", err))
  508. }
  509. return tx, nil
  510. }
  511. // RollupUpdateFeeAddToken is the interface to call the smart contract function
  512. func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (tx *types.Transaction, err error) {
  513. if tx, err = c.client.CallAuth(
  514. 0,
  515. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  516. return c.hermez.UpdateFeeAddToken(auth, newFeeAddToken)
  517. },
  518. ); err != nil {
  519. return nil, tracerr.Wrap(fmt.Errorf("Failed update FeeAddToken: %w", err))
  520. }
  521. return tx, nil
  522. }
  523. // RollupUpdateBucketsParameters is the interface to call the smart contract function
  524. func (c *RollupClient) RollupUpdateBucketsParameters(
  525. arrayBuckets [common.RollupConstNumBuckets]RollupUpdateBucketsParameters,
  526. ) (tx *types.Transaction, err error) {
  527. params := [common.RollupConstNumBuckets][4]*big.Int{}
  528. for i, bucket := range arrayBuckets {
  529. params[i][0] = bucket.CeilUSD
  530. params[i][1] = bucket.Withdrawals
  531. params[i][2] = bucket.BlockWithdrawalRate
  532. params[i][3] = bucket.MaxWithdrawals
  533. }
  534. if tx, err = c.client.CallAuth(
  535. 12500000, //nolint:gomnd
  536. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  537. return c.hermez.UpdateBucketsParameters(auth, params)
  538. },
  539. ); err != nil {
  540. return nil, tracerr.Wrap(fmt.Errorf("Failed update Buckets Parameters: %w", err))
  541. }
  542. return tx, nil
  543. }
  544. // RollupUpdateTokenExchange is the interface to call the smart contract function
  545. func (c *RollupClient) RollupUpdateTokenExchange(addressArray []ethCommon.Address, valueArray []uint64) (tx *types.Transaction, err error) {
  546. if tx, err = c.client.CallAuth(
  547. 0,
  548. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  549. return c.hermez.UpdateTokenExchange(auth, addressArray, valueArray)
  550. },
  551. ); err != nil {
  552. return nil, tracerr.Wrap(fmt.Errorf("Failed update Token Exchange: %w", err))
  553. }
  554. return tx, nil
  555. }
  556. // RollupUpdateWithdrawalDelay is the interface to call the smart contract function
  557. func (c *RollupClient) RollupUpdateWithdrawalDelay(newWithdrawalDelay int64) (tx *types.Transaction, err error) {
  558. if tx, err = c.client.CallAuth(
  559. 0,
  560. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  561. return c.hermez.UpdateWithdrawalDelay(auth, uint64(newWithdrawalDelay))
  562. },
  563. ); err != nil {
  564. return nil, tracerr.Wrap(fmt.Errorf("Failed update WithdrawalDelay: %w", err))
  565. }
  566. return tx, nil
  567. }
  568. // RollupSafeMode is the interface to call the smart contract function
  569. func (c *RollupClient) RollupSafeMode() (tx *types.Transaction, err error) {
  570. if tx, err = c.client.CallAuth(
  571. 0,
  572. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  573. return c.hermez.SafeMode(auth)
  574. },
  575. ); err != nil {
  576. return nil, tracerr.Wrap(fmt.Errorf("Failed update Safe Mode: %w", err))
  577. }
  578. return tx, nil
  579. }
  580. // RollupInstantWithdrawalViewer is the interface to call the smart contract function
  581. func (c *RollupClient) RollupInstantWithdrawalViewer(tokenAddress ethCommon.Address, amount *big.Int) (instantAllowed bool, err error) {
  582. if err := c.client.Call(func(ec *ethclient.Client) error {
  583. instantAllowed, err = c.hermez.InstantWithdrawalViewer(c.opts, tokenAddress, amount)
  584. return tracerr.Wrap(err)
  585. }); err != nil {
  586. return false, tracerr.Wrap(err)
  587. }
  588. return instantAllowed, nil
  589. }
  590. // RollupConstants returns the Constants of the Rollup Smart Contract
  591. func (c *RollupClient) RollupConstants() (rollupConstants *common.RollupConstants, err error) {
  592. rollupConstants = new(common.RollupConstants)
  593. if err := c.client.Call(func(ec *ethclient.Client) error {
  594. absoluteMaxL1L2BatchTimeout, err := c.hermez.ABSOLUTEMAXL1L2BATCHTIMEOUT(c.opts)
  595. if err != nil {
  596. return tracerr.Wrap(err)
  597. }
  598. rollupConstants.AbsoluteMaxL1L2BatchTimeout = int64(absoluteMaxL1L2BatchTimeout)
  599. rollupConstants.TokenHEZ, err = c.hermez.TokenHEZ(c.opts)
  600. if err != nil {
  601. return tracerr.Wrap(err)
  602. }
  603. for i := int64(0); i < int64(common.LenVerifiers); i++ {
  604. var newRollupVerifier common.RollupVerifierStruct
  605. rollupVerifier, err := c.hermez.RollupVerifiers(c.opts, big.NewInt(i))
  606. if err != nil {
  607. return tracerr.Wrap(err)
  608. }
  609. newRollupVerifier.MaxTx = rollupVerifier.MaxTx.Int64()
  610. newRollupVerifier.NLevels = rollupVerifier.NLevels.Int64()
  611. rollupConstants.Verifiers = append(rollupConstants.Verifiers, newRollupVerifier)
  612. }
  613. rollupConstants.HermezAuctionContract, err = c.hermez.HermezAuctionContract(c.opts)
  614. if err != nil {
  615. return tracerr.Wrap(err)
  616. }
  617. rollupConstants.HermezGovernanceAddress, err = c.hermez.HermezGovernanceAddress(c.opts)
  618. if err != nil {
  619. return tracerr.Wrap(err)
  620. }
  621. rollupConstants.WithdrawDelayerContract, err = c.hermez.WithdrawDelayerContract(c.opts)
  622. return tracerr.Wrap(err)
  623. }); err != nil {
  624. return nil, tracerr.Wrap(err)
  625. }
  626. return rollupConstants, nil
  627. }
  628. var (
  629. logHermezL1UserTxEvent = crypto.Keccak256Hash([]byte("L1UserTxEvent(uint32,uint8,bytes)"))
  630. logHermezAddToken = crypto.Keccak256Hash([]byte("AddToken(address,uint32)"))
  631. logHermezForgeBatch = crypto.Keccak256Hash([]byte("ForgeBatch(uint32,uint16)"))
  632. logHermezUpdateForgeL1L2BatchTimeout = crypto.Keccak256Hash([]byte("UpdateForgeL1L2BatchTimeout(uint8)"))
  633. logHermezUpdateFeeAddToken = crypto.Keccak256Hash([]byte("UpdateFeeAddToken(uint256)"))
  634. logHermezWithdrawEvent = crypto.Keccak256Hash([]byte("WithdrawEvent(uint48,uint32,bool)"))
  635. logHermezUpdateBucketWithdraw = crypto.Keccak256Hash([]byte("UpdateBucketWithdraw(uint8,uint256,uint256)"))
  636. logHermezUpdateWithdrawalDelay = crypto.Keccak256Hash([]byte("UpdateWithdrawalDelay(uint64)"))
  637. logHermezUpdateBucketsParameters = crypto.Keccak256Hash([]byte("UpdateBucketsParameters(uint256[4][" +
  638. strconv.Itoa(common.RollupConstNumBuckets) + "])"))
  639. logHermezUpdateTokenExchange = crypto.Keccak256Hash([]byte("UpdateTokenExchange(address[],uint64[])"))
  640. logHermezSafeMode = crypto.Keccak256Hash([]byte("SafeMode()"))
  641. logHermezInitialize = crypto.Keccak256Hash([]byte("InitializeHermezEvent(uint8,uint256,uint64)"))
  642. )
  643. // RollupEventInit returns the initialize event with its corresponding block number
  644. func (c *RollupClient) RollupEventInit() (*RollupEventInitialize, int64, error) {
  645. query := ethereum.FilterQuery{
  646. Addresses: []ethCommon.Address{
  647. c.address,
  648. },
  649. Topics: [][]ethCommon.Hash{{logHermezInitialize}},
  650. }
  651. logs, err := c.client.client.FilterLogs(context.Background(), query)
  652. if err != nil {
  653. return nil, 0, tracerr.Wrap(err)
  654. }
  655. if len(logs) != 1 {
  656. return nil, 0, tracerr.Wrap(fmt.Errorf("no event of type InitializeHermezEvent found"))
  657. }
  658. vLog := logs[0]
  659. if vLog.Topics[0] != logHermezInitialize {
  660. return nil, 0, tracerr.Wrap(fmt.Errorf("event is not InitializeHermezEvent"))
  661. }
  662. var rollupInit RollupEventInitialize
  663. if err := c.contractAbi.UnpackIntoInterface(&rollupInit, "InitializeHermezEvent", vLog.Data); err != nil {
  664. return nil, 0, tracerr.Wrap(err)
  665. }
  666. return &rollupInit, int64(vLog.BlockNumber), tracerr.Wrap(err)
  667. }
  668. // RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
  669. func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
  670. var rollupEvents RollupEvents
  671. var blockHash *ethCommon.Hash
  672. query := ethereum.FilterQuery{
  673. FromBlock: big.NewInt(blockNum),
  674. ToBlock: big.NewInt(blockNum),
  675. Addresses: []ethCommon.Address{
  676. c.address,
  677. },
  678. BlockHash: nil,
  679. Topics: [][]ethCommon.Hash{},
  680. }
  681. logs, err := c.client.client.FilterLogs(context.Background(), query)
  682. if err != nil {
  683. return nil, nil, tracerr.Wrap(err)
  684. }
  685. if len(logs) > 0 {
  686. blockHash = &logs[0].BlockHash
  687. }
  688. for _, vLog := range logs {
  689. if vLog.BlockHash != *blockHash {
  690. log.Errorw("Block hash mismatch", "expected", blockHash.String(), "got", vLog.BlockHash.String())
  691. return nil, nil, tracerr.Wrap(ErrBlockHashMismatchEvent)
  692. }
  693. switch vLog.Topics[0] {
  694. case logHermezL1UserTxEvent:
  695. var L1UserTxAux rollupEventL1UserTxAux
  696. var L1UserTx RollupEventL1UserTx
  697. err := c.contractAbi.UnpackIntoInterface(&L1UserTxAux, "L1UserTxEvent", vLog.Data)
  698. if err != nil {
  699. return nil, nil, tracerr.Wrap(err)
  700. }
  701. L1Tx, err := common.L1UserTxFromBytes(L1UserTxAux.L1UserTx)
  702. if err != nil {
  703. return nil, nil, tracerr.Wrap(err)
  704. }
  705. toForgeL1TxsNum := new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
  706. L1Tx.ToForgeL1TxsNum = &toForgeL1TxsNum
  707. L1Tx.Position = int(new(big.Int).SetBytes(vLog.Topics[2][:]).Int64())
  708. L1Tx.UserOrigin = true
  709. L1UserTx.L1UserTx = *L1Tx
  710. rollupEvents.L1UserTx = append(rollupEvents.L1UserTx, L1UserTx)
  711. case logHermezAddToken:
  712. var addToken RollupEventAddToken
  713. err := c.contractAbi.UnpackIntoInterface(&addToken, "AddToken", vLog.Data)
  714. if err != nil {
  715. return nil, nil, tracerr.Wrap(err)
  716. }
  717. addToken.TokenAddress = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  718. rollupEvents.AddToken = append(rollupEvents.AddToken, addToken)
  719. case logHermezForgeBatch:
  720. var forgeBatch RollupEventForgeBatch
  721. err := c.contractAbi.UnpackIntoInterface(&forgeBatch, "ForgeBatch", vLog.Data)
  722. if err != nil {
  723. return nil, nil, tracerr.Wrap(err)
  724. }
  725. forgeBatch.BatchNum = new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
  726. forgeBatch.EthTxHash = vLog.TxHash
  727. // forgeBatch.Sender = vLog.Address
  728. rollupEvents.ForgeBatch = append(rollupEvents.ForgeBatch, forgeBatch)
  729. case logHermezUpdateForgeL1L2BatchTimeout:
  730. var updateForgeL1L2BatchTimeout struct {
  731. NewForgeL1L2BatchTimeout uint8
  732. }
  733. err := c.contractAbi.UnpackIntoInterface(&updateForgeL1L2BatchTimeout, "UpdateForgeL1L2BatchTimeout", vLog.Data)
  734. if err != nil {
  735. return nil, nil, tracerr.Wrap(err)
  736. }
  737. rollupEvents.UpdateForgeL1L2BatchTimeout = append(rollupEvents.UpdateForgeL1L2BatchTimeout,
  738. RollupEventUpdateForgeL1L2BatchTimeout{
  739. NewForgeL1L2BatchTimeout: int64(updateForgeL1L2BatchTimeout.NewForgeL1L2BatchTimeout),
  740. })
  741. case logHermezUpdateFeeAddToken:
  742. var updateFeeAddToken RollupEventUpdateFeeAddToken
  743. err := c.contractAbi.UnpackIntoInterface(&updateFeeAddToken, "UpdateFeeAddToken", vLog.Data)
  744. if err != nil {
  745. return nil, nil, tracerr.Wrap(err)
  746. }
  747. rollupEvents.UpdateFeeAddToken = append(rollupEvents.UpdateFeeAddToken, updateFeeAddToken)
  748. case logHermezWithdrawEvent:
  749. var withdraw RollupEventWithdraw
  750. withdraw.Idx = new(big.Int).SetBytes(vLog.Topics[1][:]).Uint64()
  751. withdraw.NumExitRoot = new(big.Int).SetBytes(vLog.Topics[2][:]).Uint64()
  752. instantWithdraw := new(big.Int).SetBytes(vLog.Topics[3][:]).Uint64()
  753. if instantWithdraw == 1 {
  754. withdraw.InstantWithdraw = true
  755. }
  756. withdraw.TxHash = vLog.TxHash
  757. rollupEvents.Withdraw = append(rollupEvents.Withdraw, withdraw)
  758. case logHermezUpdateBucketWithdraw:
  759. var updateBucketWithdrawAux rollupEventUpdateBucketWithdrawAux
  760. var updateBucketWithdraw RollupEventUpdateBucketWithdraw
  761. err := c.contractAbi.UnpackIntoInterface(&updateBucketWithdrawAux, "UpdateBucketWithdraw", vLog.Data)
  762. if err != nil {
  763. return nil, nil, tracerr.Wrap(err)
  764. }
  765. updateBucketWithdraw.Withdrawals = updateBucketWithdrawAux.Withdrawals
  766. updateBucketWithdraw.NumBucket = int(new(big.Int).SetBytes(vLog.Topics[1][:]).Int64())
  767. updateBucketWithdraw.BlockStamp = new(big.Int).SetBytes(vLog.Topics[2][:]).Int64()
  768. rollupEvents.UpdateBucketWithdraw = append(rollupEvents.UpdateBucketWithdraw, updateBucketWithdraw)
  769. case logHermezUpdateWithdrawalDelay:
  770. var withdrawalDelay RollupEventUpdateWithdrawalDelay
  771. err := c.contractAbi.UnpackIntoInterface(&withdrawalDelay, "UpdateWithdrawalDelay", vLog.Data)
  772. if err != nil {
  773. return nil, nil, tracerr.Wrap(err)
  774. }
  775. rollupEvents.UpdateWithdrawalDelay = append(rollupEvents.UpdateWithdrawalDelay, withdrawalDelay)
  776. case logHermezUpdateBucketsParameters:
  777. var bucketsParametersAux rollupEventUpdateBucketsParametersAux
  778. var bucketsParameters RollupEventUpdateBucketsParameters
  779. err := c.contractAbi.UnpackIntoInterface(&bucketsParametersAux, "UpdateBucketsParameters", vLog.Data)
  780. if err != nil {
  781. return nil, nil, tracerr.Wrap(err)
  782. }
  783. for i, bucket := range bucketsParametersAux.ArrayBuckets {
  784. bucketsParameters.ArrayBuckets[i].CeilUSD = bucket[0]
  785. bucketsParameters.ArrayBuckets[i].Withdrawals = bucket[1]
  786. bucketsParameters.ArrayBuckets[i].BlockWithdrawalRate = bucket[2]
  787. bucketsParameters.ArrayBuckets[i].MaxWithdrawals = bucket[3]
  788. }
  789. rollupEvents.UpdateBucketsParameters = append(rollupEvents.UpdateBucketsParameters, bucketsParameters)
  790. case logHermezUpdateTokenExchange:
  791. var tokensExchange RollupEventUpdateTokenExchange
  792. err := c.contractAbi.UnpackIntoInterface(&tokensExchange, "UpdateTokenExchange", vLog.Data)
  793. if err != nil {
  794. return nil, nil, tracerr.Wrap(err)
  795. }
  796. rollupEvents.UpdateTokenExchange = append(rollupEvents.UpdateTokenExchange, tokensExchange)
  797. case logHermezSafeMode:
  798. var safeMode RollupEventSafeMode
  799. rollupEvents.SafeMode = append(rollupEvents.SafeMode, safeMode)
  800. // Also add an UpdateBucketsParameter with
  801. // SafeMode=true to keep the order between `safeMode`
  802. // and `UpdateBucketsParameters`
  803. bucketsParameters := RollupEventUpdateBucketsParameters{
  804. SafeMode: true,
  805. }
  806. for i := range bucketsParameters.ArrayBuckets {
  807. bucketsParameters.ArrayBuckets[i].CeilUSD = big.NewInt(0)
  808. bucketsParameters.ArrayBuckets[i].Withdrawals = big.NewInt(0)
  809. bucketsParameters.ArrayBuckets[i].BlockWithdrawalRate = big.NewInt(0)
  810. bucketsParameters.ArrayBuckets[i].MaxWithdrawals = big.NewInt(0)
  811. }
  812. rollupEvents.UpdateBucketsParameters = append(rollupEvents.UpdateBucketsParameters,
  813. bucketsParameters)
  814. }
  815. }
  816. return &rollupEvents, blockHash, nil
  817. }
  818. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the
  819. // Rollup Smart Contract in the given transaction, and the sender address.
  820. func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash, l1UserTxsLen uint16) (*RollupForgeBatchArgs, *ethCommon.Address, error) {
  821. tx, _, err := c.client.client.TransactionByHash(context.Background(), ethTxHash)
  822. if err != nil {
  823. return nil, nil, tracerr.Wrap(err)
  824. }
  825. txData := tx.Data()
  826. method, err := c.contractAbi.MethodById(txData[:4])
  827. if err != nil {
  828. return nil, nil, tracerr.Wrap(err)
  829. }
  830. receipt, err := c.client.client.TransactionReceipt(context.Background(), ethTxHash)
  831. if err != nil {
  832. return nil, nil, tracerr.Wrap(err)
  833. }
  834. sender, err := c.client.client.TransactionSender(context.Background(), tx, receipt.Logs[0].BlockHash, receipt.Logs[0].Index)
  835. if err != nil {
  836. return nil, nil, tracerr.Wrap(err)
  837. }
  838. var aux rollupForgeBatchArgsAux
  839. if values, err := method.Inputs.Unpack(txData[4:]); err != nil {
  840. return nil, nil, tracerr.Wrap(err)
  841. } else if err := method.Inputs.Copy(&aux, values); err != nil {
  842. return nil, nil, tracerr.Wrap(err)
  843. }
  844. rollupForgeBatchArgs := RollupForgeBatchArgs{
  845. L1Batch: aux.L1Batch,
  846. NewExitRoot: aux.NewExitRoot,
  847. NewLastIdx: aux.NewLastIdx.Int64(),
  848. NewStRoot: aux.NewStRoot,
  849. ProofA: aux.ProofA,
  850. ProofB: aux.ProofB,
  851. ProofC: aux.ProofC,
  852. VerifierIdx: aux.VerifierIdx,
  853. L1CoordinatorTxs: []common.L1Tx{},
  854. L1CoordinatorTxsAuths: [][]byte{},
  855. L2TxsData: []common.L2Tx{},
  856. FeeIdxCoordinator: []common.Idx{},
  857. }
  858. rollupConsts, err := c.RollupConstants()
  859. if err != nil {
  860. return nil, nil, tracerr.Wrap(err)
  861. }
  862. nLevels := rollupConsts.Verifiers[rollupForgeBatchArgs.VerifierIdx].NLevels
  863. lenL1L2TxsBytes := int((nLevels/8)*2 + 2 + 1)
  864. numBytesL1TxUser := int(l1UserTxsLen) * lenL1L2TxsBytes
  865. numTxsL1Coord := len(aux.EncodedL1CoordinatorTx) / common.L1CoordinatorTxBytesLen
  866. numBytesL1TxCoord := numTxsL1Coord * lenL1L2TxsBytes
  867. numBeginL2Tx := numBytesL1TxCoord + numBytesL1TxUser
  868. l1UserTxsData := []byte{}
  869. if l1UserTxsLen > 0 {
  870. l1UserTxsData = aux.L1L2TxsData[:numBytesL1TxUser]
  871. }
  872. for i := 0; i < int(l1UserTxsLen); i++ {
  873. l1Tx, err := common.L1TxFromDataAvailability(l1UserTxsData[i*lenL1L2TxsBytes:(i+1)*lenL1L2TxsBytes], uint32(nLevels))
  874. if err != nil {
  875. return nil, nil, tracerr.Wrap(err)
  876. }
  877. rollupForgeBatchArgs.L1UserTxs = append(rollupForgeBatchArgs.L1UserTxs, *l1Tx)
  878. }
  879. l2TxsData := []byte{}
  880. if numBeginL2Tx < len(aux.L1L2TxsData) {
  881. l2TxsData = aux.L1L2TxsData[numBeginL2Tx:]
  882. }
  883. numTxsL2 := len(l2TxsData) / lenL1L2TxsBytes
  884. for i := 0; i < numTxsL2; i++ {
  885. l2Tx, err := common.L2TxFromBytesDataAvailability(l2TxsData[i*lenL1L2TxsBytes:(i+1)*lenL1L2TxsBytes], int(nLevels))
  886. if err != nil {
  887. return nil, nil, tracerr.Wrap(err)
  888. }
  889. rollupForgeBatchArgs.L2TxsData = append(rollupForgeBatchArgs.L2TxsData, *l2Tx)
  890. }
  891. for i := 0; i < numTxsL1Coord; i++ {
  892. bytesL1Coordinator := aux.EncodedL1CoordinatorTx[i*common.L1CoordinatorTxBytesLen : (i+1)*common.L1CoordinatorTxBytesLen]
  893. var signature []byte
  894. v := bytesL1Coordinator[0]
  895. s := bytesL1Coordinator[1:33]
  896. r := bytesL1Coordinator[33:65]
  897. signature = append(signature, r[:]...)
  898. signature = append(signature, s[:]...)
  899. signature = append(signature, v)
  900. l1Tx, err := common.L1CoordinatorTxFromBytes(bytesL1Coordinator, c.chainID, c.address)
  901. if err != nil {
  902. return nil, nil, tracerr.Wrap(err)
  903. }
  904. rollupForgeBatchArgs.L1CoordinatorTxs = append(rollupForgeBatchArgs.L1CoordinatorTxs, *l1Tx)
  905. rollupForgeBatchArgs.L1CoordinatorTxsAuths = append(rollupForgeBatchArgs.L1CoordinatorTxsAuths, signature)
  906. }
  907. lenFeeIdxCoordinatorBytes := int(nLevels / 8) //nolint:gomnd
  908. numFeeIdxCoordinator := len(aux.FeeIdxCoordinator) / lenFeeIdxCoordinatorBytes
  909. for i := 0; i < numFeeIdxCoordinator; i++ {
  910. var paddedFeeIdx [6]byte
  911. // TODO: This check is not necessary: the first case will always work. Test it before removing the if.
  912. if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
  913. copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], aux.FeeIdxCoordinator[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  914. } else {
  915. copy(paddedFeeIdx[:], aux.FeeIdxCoordinator[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  916. }
  917. feeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
  918. if err != nil {
  919. return nil, nil, tracerr.Wrap(err)
  920. }
  921. if feeIdxCoordinator != common.Idx(0) {
  922. rollupForgeBatchArgs.FeeIdxCoordinator = append(rollupForgeBatchArgs.FeeIdxCoordinator, feeIdxCoordinator)
  923. }
  924. }
  925. return &rollupForgeBatchArgs, &sender, nil
  926. }