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.

737 lines
28 KiB

  1. package eth
  2. import (
  3. "context"
  4. "fmt"
  5. "math/big"
  6. "strings"
  7. "github.com/ethereum/go-ethereum"
  8. "github.com/ethereum/go-ethereum/accounts/abi"
  9. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. "github.com/ethereum/go-ethereum/core/types"
  12. "github.com/ethereum/go-ethereum/crypto"
  13. "github.com/ethereum/go-ethereum/ethclient"
  14. "github.com/hermeznetwork/hermez-node/common"
  15. ERC777 "github.com/hermeznetwork/hermez-node/eth/contracts/erc777"
  16. Hermez "github.com/hermeznetwork/hermez-node/eth/contracts/hermez"
  17. "github.com/hermeznetwork/hermez-node/log"
  18. "github.com/iden3/go-iden3-crypto/babyjub"
  19. "golang.org/x/crypto/sha3"
  20. )
  21. const (
  22. // RollupConstFeeIdxCoordinatorLen is the number of tokens the coordinator can use
  23. // to collect fees (determines the number of tokens that the
  24. // coordinator can collect fees from). This value is determined by the
  25. // circuit.
  26. RollupConstFeeIdxCoordinatorLen = 64
  27. // RollupConstReservedIDx First 256 indexes reserved, first user index will be the 256
  28. RollupConstReservedIDx = 255
  29. // RollupConstExitIDx IDX 1 is reserved for exits
  30. RollupConstExitIDx = 1
  31. // RollupConstLimitLoadAmount Max load amount allowed (loadAmount: L1 --> L2)
  32. RollupConstLimitLoadAmount = (1 << 128)
  33. // RollupConstLimitL2TransferAmount Max amount allowed (amount L2 --> L2)
  34. RollupConstLimitL2TransferAmount = (1 << 192)
  35. // RollupConstLimitTokens Max number of tokens allowed to be registered inside the rollup
  36. RollupConstLimitTokens = (1 << 32)
  37. // RollupConstL1CoordinatorTotalBytes [4 bytes] token + [32 bytes] babyjub + [65 bytes] compressedSignature
  38. RollupConstL1CoordinatorTotalBytes = 101
  39. // RollupConstL1UserTotalBytes [20 bytes] fromEthAddr + [32 bytes] fromBjj-compressed + [6 bytes] fromIdx +
  40. // [2 bytes] loadAmountFloat16 + [2 bytes] amountFloat16 + [4 bytes] tokenId + [6 bytes] toIdx
  41. RollupConstL1UserTotalBytes = 72
  42. // RollupConstMaxL1UserTx Maximum L1-user transactions allowed to be queued in a batch
  43. RollupConstMaxL1UserTx = 128
  44. // RollupConstMaxL1Tx Maximum L1 transactions allowed to be queued in a batch
  45. RollupConstMaxL1Tx = 256
  46. // RollupConstInputSHAConstantBytes [6 bytes] lastIdx + [6 bytes] newLastIdx + [32 bytes] stateRoot + [32 bytes] newStRoot + [32 bytes] newExitRoot +
  47. // [_MAX_L1_TX * _L1_USER_TOTALBYTES bytes] l1TxsData + totalL2TxsDataLength + feeIdxCoordinatorLength + [2 bytes] chainID =
  48. // 18542 bytes + totalL2TxsDataLength + feeIdxCoordinatorLength
  49. RollupConstInputSHAConstantBytes = 18542
  50. // RollupConstNumBuckets Number of buckets
  51. RollupConstNumBuckets = 5
  52. // RollupConstMaxWithdrawalDelay max withdrawal delay in seconds
  53. RollupConstMaxWithdrawalDelay = 2 * 7 * 24 * 60 * 60
  54. // RollupConstExchangeMultiplier exchange multiplier
  55. RollupConstExchangeMultiplier = 1e14
  56. // LenVerifiers number of Rollup Smart Contract Verifiers
  57. LenVerifiers = 1
  58. )
  59. var (
  60. // RollupConstEthAddressInternalOnly This ethereum address is used internally for rollup accounts that don't have ethereum address, only Babyjubjub
  61. // This non-ethereum accounts can be created by the coordinator and allow users to have a rollup
  62. // account without needing an ethereum address
  63. RollupConstEthAddressInternalOnly = ethCommon.HexToAddress("0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF")
  64. // RollupConstRfield Modulus zkSNARK
  65. RollupConstRfield, _ = new(big.Int).SetString(
  66. "21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  67. // RollupConstERC1820 ERC1820Registry address
  68. RollupConstERC1820 = ethCommon.HexToAddress("0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24")
  69. // ERC777 tokens signatures
  70. // RollupConstRecipientInterfaceHash ERC777 recipient interface hash
  71. RollupConstRecipientInterfaceHash = crypto.Keccak256([]byte("ERC777TokensRecipient"))
  72. // RollupConstPerformL1UserTxSignature the signature of the function that can be called thru an ERC777 `send`
  73. RollupConstPerformL1UserTxSignature = crypto.Keccak256([]byte("addL1Transaction(uint256,uint48,uint16,uint16,uint32,uint48)"))
  74. // RollupConstAddTokenSignature the signature of the function that can be called thru an ERC777 `send`
  75. RollupConstAddTokenSignature = crypto.Keccak256([]byte("addToken(address)"))
  76. // RollupConstSendSignature ERC777 Signature
  77. RollupConstSendSignature = crypto.Keccak256([]byte("send(address,uint256,bytes)"))
  78. // RollupConstERC777Granularity ERC777 Signature
  79. RollupConstERC777Granularity = crypto.Keccak256([]byte("granularity()"))
  80. // RollupConstWithdrawalDelayerDeposit This constant are used to deposit tokens from ERC77 tokens into withdrawal delayer
  81. RollupConstWithdrawalDelayerDeposit = crypto.Keccak256([]byte("deposit(address,address,uint192)"))
  82. // ERC20 signature
  83. // RollupConstTransferSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
  84. RollupConstTransferSignature = crypto.Keccak256([]byte("transfer(address,uint256)"))
  85. // RollupConstTransferFromSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
  86. RollupConstTransferFromSignature = crypto.Keccak256([]byte("transferFrom(address,address,uint256)"))
  87. // RollupConstApproveSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
  88. RollupConstApproveSignature = crypto.Keccak256([]byte("approve(address,uint256)"))
  89. // RollupConstERC20Signature ERC20 decimals signature
  90. RollupConstERC20Signature = crypto.Keccak256([]byte("decimals()"))
  91. )
  92. // RollupConstants are the constants of the Rollup Smart Contract
  93. /* type RollupConstants struct {
  94. // Maxim Deposit allowed
  95. MaxAmountDeposit *big.Int
  96. MaxAmountL2 *big.Int
  97. MaxTokens int64
  98. // maximum L1 transactions allowed to be queued for a batch
  99. MaxL1Tx int
  100. // maximum L1 user transactions allowed to be queued for a batch
  101. MaxL1UserTx int
  102. Rfield *big.Int
  103. L1CoordinatorBytes int
  104. L1UserBytes int
  105. L2Bytes int
  106. MaxTxVerifiers []int
  107. TokenHEZ ethCommon.Address
  108. // Only test
  109. GovernanceAddress ethCommon.Address
  110. // Only test
  111. SafetyBot ethCommon.Address
  112. // Only test
  113. ConsensusContract ethCommon.Address
  114. // Only test
  115. WithdrawalContract ethCommon.Address
  116. ReservedIDx uint32
  117. LastIDx uint32
  118. ExitIDx uint32
  119. NoLimitToken int
  120. NumBuckets int
  121. MaxWDelay int64
  122. }*/
  123. // RollupPublicConstants are the constants of the Rollup Smart Contract
  124. type RollupPublicConstants struct {
  125. AbsoluteMaxL1L2BatchTimeout int64
  126. TokenHEZ ethCommon.Address
  127. Verifiers []RollupVerifierStruct
  128. HermezAuctionContract ethCommon.Address
  129. HermezGovernanceDAOAddress ethCommon.Address
  130. SafetyAddress ethCommon.Address
  131. WithdrawDelayerContract ethCommon.Address
  132. }
  133. // RollupVariables are the variables of the Rollup Smart Contract
  134. type RollupVariables struct {
  135. FeeAddToken *big.Int
  136. ForgeL1L2BatchTimeout int64
  137. WithdrawalDelay uint64
  138. }
  139. // QueueStruct is the queue of L1Txs for a batch
  140. //nolint:structcheck
  141. type QueueStruct struct {
  142. L1TxQueue []common.L1Tx
  143. TotalL1TxFee *big.Int
  144. }
  145. // NewQueueStruct creates a new clear QueueStruct.
  146. func NewQueueStruct() *QueueStruct {
  147. return &QueueStruct{
  148. L1TxQueue: make([]common.L1Tx, 0),
  149. TotalL1TxFee: big.NewInt(0),
  150. }
  151. }
  152. // RollupVerifierStruct is the information about verifiers of the Rollup Smart Contract
  153. type RollupVerifierStruct struct {
  154. MaxTx int64
  155. NLevels int64
  156. }
  157. // RollupState represents the state of the Rollup in the Smart Contract
  158. //nolint:structcheck,unused
  159. type RollupState struct {
  160. StateRoot *big.Int
  161. ExitRoots []*big.Int
  162. ExitNullifierMap map[[256 / 8]byte]bool
  163. TokenList []ethCommon.Address
  164. TokenMap map[ethCommon.Address]bool
  165. MapL1TxQueue map[int64]*QueueStruct
  166. LastL1L2Batch int64
  167. CurrentToForgeL1TxsNum int64
  168. LastToForgeL1TxsNum int64
  169. CurrentIdx int64
  170. }
  171. // RollupEventL1UserTx is an event of the Rollup Smart Contract
  172. type RollupEventL1UserTx struct {
  173. ToForgeL1TxsNum int64 // QueueIndex *big.Int
  174. Position int // TransactionIndex *big.Int
  175. L1Tx common.L1Tx
  176. }
  177. // RollupEventL1UserTxAux is an event of the Rollup Smart Contract
  178. type RollupEventL1UserTxAux struct {
  179. ToForgeL1TxsNum uint64 // QueueIndex *big.Int
  180. Position uint8 // TransactionIndex *big.Int
  181. L1Tx []byte
  182. }
  183. // RollupEventAddToken is an event of the Rollup Smart Contract
  184. type RollupEventAddToken struct {
  185. Address ethCommon.Address
  186. TokenID uint32
  187. }
  188. // RollupEventForgeBatch is an event of the Rollup Smart Contract
  189. type RollupEventForgeBatch struct {
  190. BatchNum int64
  191. EthTxHash ethCommon.Hash
  192. }
  193. // RollupEventUpdateForgeL1L2BatchTimeout is an event of the Rollup Smart Contract
  194. type RollupEventUpdateForgeL1L2BatchTimeout struct {
  195. ForgeL1L2BatchTimeout uint8
  196. }
  197. // RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
  198. type RollupEventUpdateFeeAddToken struct {
  199. FeeAddToken *big.Int
  200. }
  201. // RollupEventWithdrawEvent is an event of the Rollup Smart Contract
  202. type RollupEventWithdrawEvent struct {
  203. Idx uint64
  204. NumExitRoot uint64
  205. InstantWithdraw bool
  206. }
  207. // RollupEvents is the list of events in a block of the Rollup Smart Contract
  208. type RollupEvents struct { //nolint:structcheck
  209. L1UserTx []RollupEventL1UserTx
  210. AddToken []RollupEventAddToken
  211. ForgeBatch []RollupEventForgeBatch
  212. UpdateForgeL1L2BatchTimeout []RollupEventUpdateForgeL1L2BatchTimeout
  213. UpdateFeeAddToken []RollupEventUpdateFeeAddToken
  214. WithdrawEvent []RollupEventWithdrawEvent
  215. }
  216. // NewRollupEvents creates an empty RollupEvents with the slices initialized.
  217. func NewRollupEvents() RollupEvents {
  218. return RollupEvents{
  219. L1UserTx: make([]RollupEventL1UserTx, 0),
  220. AddToken: make([]RollupEventAddToken, 0),
  221. ForgeBatch: make([]RollupEventForgeBatch, 0),
  222. UpdateForgeL1L2BatchTimeout: make([]RollupEventUpdateForgeL1L2BatchTimeout, 0),
  223. UpdateFeeAddToken: make([]RollupEventUpdateFeeAddToken, 0),
  224. WithdrawEvent: make([]RollupEventWithdrawEvent, 0),
  225. }
  226. }
  227. // RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
  228. //nolint:structcheck,unused
  229. type RollupForgeBatchArgs struct {
  230. NewLastIdx int64
  231. NewStRoot *big.Int
  232. NewExitRoot *big.Int
  233. L1CoordinatorTxs []*common.L1Tx
  234. L1CoordinatorTxsAuths [][]byte // Authorization for accountCreations for each L1CoordinatorTx
  235. L2TxsData []*common.L2Tx
  236. FeeIdxCoordinator []common.Idx
  237. // Circuit selector
  238. VerifierIdx uint8
  239. L1Batch bool
  240. ProofA [2]*big.Int
  241. ProofB [2][2]*big.Int
  242. ProofC [2]*big.Int
  243. }
  244. // RollupForgeBatchArgsAux are the arguments to the ForgeBatch function in the Rollup Smart Contract
  245. //nolint:structcheck,unused
  246. type RollupForgeBatchArgsAux struct {
  247. NewLastIdx *big.Int
  248. NewStRoot *big.Int
  249. NewExitRoot *big.Int
  250. EncodedL1CoordinatorTx []byte
  251. L2TxsData []byte
  252. FeeIdxCoordinator []byte
  253. // Circuit selector
  254. VerifierIdx uint8
  255. L1Batch bool
  256. ProofA [2]*big.Int
  257. ProofB [2][2]*big.Int
  258. ProofC [2]*big.Int
  259. }
  260. // RollupInterface is the inteface to to Rollup Smart Contract
  261. type RollupInterface interface {
  262. //
  263. // Smart Contract Methods
  264. //
  265. // Public Functions
  266. RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
  267. RollupAddToken(tokenAddress ethCommon.Address, feeAddToken *big.Int) (*types.Transaction, error)
  268. RollupWithdraw(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey,
  269. numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error)
  270. RollupForceExit(fromIdx int64, amountF common.Float16, tokenID int64) (*types.Transaction, error)
  271. RollupForceTransfer(fromIdx int64, amountF common.Float16, tokenID, toIdx int64) (*types.Transaction, error)
  272. RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
  273. loadAmountF, amountF common.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
  274. RollupDepositTransfer(fromIdx int64, loadAmountF, amountF common.Float16,
  275. tokenID int64, toIdx int64) (*types.Transaction, error)
  276. RollupDeposit(fromIdx int64, loadAmountF common.Float16, tokenID int64) (*types.Transaction, error)
  277. RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
  278. babyPubKey babyjub.PublicKey, loadAmountF common.Float16) (*types.Transaction, error)
  279. RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF common.Float16,
  280. tokenID int64) (*types.Transaction, error)
  281. RollupGetCurrentTokens() (*big.Int, error)
  282. // RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error)
  283. // RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error)
  284. // RollupGetQueue(queue int64) ([]byte, error)
  285. // Governance Public Functions
  286. RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (*types.Transaction, error)
  287. RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
  288. //
  289. // Smart Contract Status
  290. //
  291. RollupConstants() (*RollupPublicConstants, error)
  292. RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
  293. RollupForgeBatchArgs(ethCommon.Hash) (*RollupForgeBatchArgs, error)
  294. }
  295. //
  296. // Implementation
  297. //
  298. // RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
  299. type RollupClient struct {
  300. client *EthereumClient
  301. address ethCommon.Address
  302. tokenAddress ethCommon.Address
  303. gasLimit uint64
  304. contractAbi abi.ABI
  305. }
  306. // NewRollupClient creates a new RollupClient
  307. func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenAddress ethCommon.Address) (*RollupClient, error) {
  308. contractAbi, err := abi.JSON(strings.NewReader(string(Hermez.HermezABI)))
  309. if err != nil {
  310. return nil, err
  311. }
  312. return &RollupClient{
  313. client: client,
  314. address: address,
  315. tokenAddress: tokenAddress,
  316. gasLimit: 1000000, //nolint:gomnd
  317. contractAbi: contractAbi,
  318. }, nil
  319. }
  320. // RollupForgeBatch is the interface to call the smart contract function
  321. func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
  322. var tx *types.Transaction
  323. var err error
  324. if tx, err = c.client.CallAuth(
  325. c.gasLimit,
  326. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  327. hermez, err := Hermez.NewHermez(c.address, ec)
  328. if err != nil {
  329. return nil, err
  330. }
  331. rollupConst, err := c.RollupConstants()
  332. if err != nil {
  333. return nil, err
  334. }
  335. nLevels := rollupConst.Verifiers[args.VerifierIdx].NLevels
  336. lenBytes := nLevels / 8
  337. newLastIdx := big.NewInt(int64(args.NewLastIdx))
  338. var l1CoordinatorBytes []byte
  339. for i := 0; i < len(args.L1CoordinatorTxs); i++ {
  340. l1 := args.L1CoordinatorTxs[i]
  341. bytesl1, err := l1.BytesCoordinatorTx(args.L1CoordinatorTxsAuths[i])
  342. if err != nil {
  343. return nil, err
  344. }
  345. l1CoordinatorBytes = append(l1CoordinatorBytes, bytesl1[:]...)
  346. }
  347. var l2DataBytes []byte
  348. for i := 0; i < len(args.L2TxsData); i++ {
  349. l2 := args.L2TxsData[i]
  350. bytesl2, err := l2.Bytes(int(nLevels))
  351. if err != nil {
  352. return nil, err
  353. }
  354. l2DataBytes = append(l2DataBytes, bytesl2[:]...)
  355. }
  356. var feeIdxCoordinator []byte
  357. for i := 0; i < len(args.FeeIdxCoordinator); i++ {
  358. feeIdx := args.FeeIdxCoordinator[i]
  359. bytesFeeIdx, err := feeIdx.Bytes()
  360. if err != nil {
  361. return nil, err
  362. }
  363. feeIdxCoordinator = append(feeIdxCoordinator, bytesFeeIdx[len(bytesFeeIdx)-int(lenBytes):]...)
  364. }
  365. return hermez.ForgeBatch(auth, newLastIdx, args.NewStRoot, args.NewExitRoot, l1CoordinatorBytes, l2DataBytes, feeIdxCoordinator, args.VerifierIdx, args.L1Batch, args.ProofA, args.ProofB, args.ProofC)
  366. },
  367. ); err != nil {
  368. return nil, fmt.Errorf("Failed forge batch: %w", err)
  369. }
  370. return tx, nil
  371. }
  372. // RollupAddToken is the interface to call the smart contract function
  373. func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address, feeAddToken *big.Int) (*types.Transaction, error) {
  374. var tx *types.Transaction
  375. var err error
  376. if tx, err = c.client.CallAuth(
  377. c.gasLimit,
  378. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  379. tokens, err := ERC777.NewERC777(c.tokenAddress, ec)
  380. if err != nil {
  381. return nil, err
  382. }
  383. addTokenFnSignature := []byte("addToken(address)")
  384. hash := sha3.NewLegacyKeccak256()
  385. _, err = hash.Write(addTokenFnSignature)
  386. if err != nil {
  387. return nil, err
  388. }
  389. methodID := hash.Sum(nil)[:4]
  390. var data []byte
  391. data = append(data, methodID...)
  392. paddedAddress := ethCommon.LeftPadBytes(tokenAddress.Bytes(), 32)
  393. data = append(data, paddedAddress[:]...)
  394. return tokens.Send(auth, c.address, feeAddToken, data)
  395. },
  396. ); err != nil {
  397. return nil, fmt.Errorf("Failed add Token %w", err)
  398. }
  399. return tx, nil
  400. }
  401. // RollupWithdrawSNARK is the interface to call the smart contract function
  402. // func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  403. // return nil, errTODO
  404. // }
  405. // RollupWithdraw is the interface to call the smart contract function
  406. func (c *RollupClient) RollupWithdraw(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
  407. log.Error("TODO")
  408. return nil, errTODO
  409. }
  410. // RollupForceExit is the interface to call the smart contract function
  411. func (c *RollupClient) RollupForceExit(fromIdx int64, amountF common.Float16, tokenID int64) (*types.Transaction, error) {
  412. log.Error("TODO")
  413. return nil, errTODO
  414. }
  415. // RollupForceTransfer is the interface to call the smart contract function
  416. func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF common.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
  417. log.Error("TODO")
  418. return nil, errTODO
  419. }
  420. // RollupCreateAccountDepositTransfer is the interface to call the smart contract function
  421. func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF common.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  422. log.Error("TODO")
  423. return nil, errTODO
  424. }
  425. // RollupDepositTransfer is the interface to call the smart contract function
  426. func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF common.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  427. log.Error("TODO")
  428. return nil, errTODO
  429. }
  430. // RollupDeposit is the interface to call the smart contract function
  431. func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF common.Float16, tokenID int64) (*types.Transaction, error) {
  432. log.Error("TODO")
  433. return nil, errTODO
  434. }
  435. // RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
  436. func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF common.Float16) (*types.Transaction, error) {
  437. log.Error("TODO")
  438. return nil, errTODO
  439. }
  440. // RollupCreateAccountDeposit is the interface to call the smart contract function
  441. func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF common.Float16, tokenID int64) (*types.Transaction, error) {
  442. log.Error("TODO")
  443. return nil, errTODO
  444. }
  445. // RollupGetTokenAddress is the interface to call the smart contract function
  446. /* func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
  447. return nil, errTODO
  448. } */
  449. // RollupGetCurrentTokens is the interface to call the smart contract function
  450. func (c *RollupClient) RollupGetCurrentTokens() (*big.Int, error) {
  451. log.Error("TODO")
  452. return nil, errTODO
  453. }
  454. // RollupGetL1TxFromQueue is the interface to call the smart contract function
  455. /* func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
  456. return nil, errTODO
  457. } */
  458. // RollupGetQueue is the interface to call the smart contract function
  459. /* func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
  460. return nil, errTODO
  461. }*/
  462. // RollupUpdateForgeL1L2BatchTimeout is the interface to call the smart contract function
  463. func (c *RollupClient) RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (*types.Transaction, error) {
  464. log.Error("TODO")
  465. return nil, errTODO
  466. }
  467. // RollupUpdateFeeAddToken is the interface to call the smart contract function
  468. func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
  469. log.Error("TODO")
  470. return nil, errTODO
  471. }
  472. // RollupConstants returns the Constants of the Rollup Smart Contract
  473. func (c *RollupClient) RollupConstants() (*RollupPublicConstants, error) {
  474. rollupConstants := new(RollupPublicConstants)
  475. if err := c.client.Call(func(ec *ethclient.Client) error {
  476. hermez, err := Hermez.NewHermez(c.address, ec)
  477. if err != nil {
  478. return err
  479. }
  480. absoluteMaxL1L2BatchTimeout, err := hermez.ABSOLUTEMAXL1L2BATCHTIMEOUT(nil)
  481. if err != nil {
  482. return err
  483. }
  484. rollupConstants.AbsoluteMaxL1L2BatchTimeout = int64(absoluteMaxL1L2BatchTimeout)
  485. rollupConstants.TokenHEZ, err = hermez.TokenHEZ(nil)
  486. if err != nil {
  487. return err
  488. }
  489. for i := int64(0); i < int64(LenVerifiers); i++ {
  490. var newRollupVerifier RollupVerifierStruct
  491. rollupVerifier, err := hermez.RollupVerifiers(nil, big.NewInt(i))
  492. if err != nil {
  493. return err
  494. }
  495. newRollupVerifier.MaxTx = rollupVerifier.MaxTx.Int64()
  496. newRollupVerifier.NLevels = rollupVerifier.NLevels.Int64()
  497. rollupConstants.Verifiers = append(rollupConstants.Verifiers, newRollupVerifier)
  498. }
  499. rollupConstants.HermezAuctionContract, err = hermez.HermezAuctionContract(nil)
  500. if err != nil {
  501. return err
  502. }
  503. rollupConstants.HermezGovernanceDAOAddress, err = hermez.HermezGovernanceDAOAddress(nil)
  504. if err != nil {
  505. return err
  506. }
  507. rollupConstants.SafetyAddress, err = hermez.SafetyAddress(nil)
  508. if err != nil {
  509. return err
  510. }
  511. rollupConstants.WithdrawDelayerContract, err = hermez.WithdrawDelayerContract(nil)
  512. return err
  513. }); err != nil {
  514. return nil, err
  515. }
  516. return rollupConstants, nil
  517. }
  518. var (
  519. logHermezL1UserTxEvent = crypto.Keccak256Hash([]byte("L1UserTxEvent(uint64,uint8,bytes)"))
  520. logHermezAddToken = crypto.Keccak256Hash([]byte("AddToken(address,uint32)"))
  521. logHermezForgeBatch = crypto.Keccak256Hash([]byte("ForgeBatch(uint64)"))
  522. logHermezUpdateForgeL1L2BatchTimeout = crypto.Keccak256Hash([]byte("UpdateForgeL1L2BatchTimeout(uint8)"))
  523. logHermezUpdateFeeAddToken = crypto.Keccak256Hash([]byte("UpdateFeeAddToken(uint256)"))
  524. logHermezWithdrawEvent = crypto.Keccak256Hash([]byte("WithdrawEvent(uint48,uint48,bool)"))
  525. )
  526. // RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
  527. func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
  528. var rollupEvents RollupEvents
  529. var blockHash ethCommon.Hash
  530. query := ethereum.FilterQuery{
  531. FromBlock: big.NewInt(blockNum),
  532. ToBlock: big.NewInt(blockNum),
  533. Addresses: []ethCommon.Address{
  534. c.address,
  535. },
  536. BlockHash: nil,
  537. Topics: [][]ethCommon.Hash{},
  538. }
  539. logs, err := c.client.client.FilterLogs(context.Background(), query)
  540. if err != nil {
  541. return nil, nil, err
  542. }
  543. if len(logs) > 0 {
  544. blockHash = logs[0].BlockHash
  545. }
  546. for _, vLog := range logs {
  547. if vLog.BlockHash != blockHash {
  548. return nil, nil, ErrBlockHashMismatchEvent
  549. }
  550. switch vLog.Topics[0] {
  551. case logHermezL1UserTxEvent:
  552. var L1UserTxAux RollupEventL1UserTxAux
  553. var L1UserTx RollupEventL1UserTx
  554. err := c.contractAbi.Unpack(&L1UserTxAux, "L1UserTxEvent", vLog.Data)
  555. if err != nil {
  556. return nil, nil, err
  557. }
  558. L1Tx, err := common.L1TxFromBytes(L1UserTxAux.L1Tx)
  559. if err != nil {
  560. return nil, nil, err
  561. }
  562. L1UserTx.ToForgeL1TxsNum = new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
  563. L1UserTx.Position = int(new(big.Int).SetBytes(vLog.Topics[2][:]).Int64())
  564. L1UserTx.L1Tx = *L1Tx
  565. rollupEvents.L1UserTx = append(rollupEvents.L1UserTx, L1UserTx)
  566. case logHermezAddToken:
  567. var addToken RollupEventAddToken
  568. err := c.contractAbi.Unpack(&addToken, "AddToken", vLog.Data)
  569. if err != nil {
  570. return nil, nil, err
  571. }
  572. addToken.Address = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  573. rollupEvents.AddToken = append(rollupEvents.AddToken, addToken)
  574. case logHermezForgeBatch:
  575. var forgeBatch RollupEventForgeBatch
  576. forgeBatch.BatchNum = new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
  577. forgeBatch.EthTxHash = vLog.TxHash
  578. rollupEvents.ForgeBatch = append(rollupEvents.ForgeBatch, forgeBatch)
  579. case logHermezUpdateForgeL1L2BatchTimeout:
  580. var updateForgeL1L2BatchTimeout RollupEventUpdateForgeL1L2BatchTimeout
  581. err := c.contractAbi.Unpack(&updateForgeL1L2BatchTimeout, "UpdateForgeL1L2BatchTimeout", vLog.Data)
  582. if err != nil {
  583. return nil, nil, err
  584. }
  585. rollupEvents.UpdateForgeL1L2BatchTimeout = append(rollupEvents.UpdateForgeL1L2BatchTimeout, updateForgeL1L2BatchTimeout)
  586. case logHermezUpdateFeeAddToken:
  587. var updateFeeAddToken RollupEventUpdateFeeAddToken
  588. err := c.contractAbi.Unpack(&updateFeeAddToken, "UpdateFeeAddToken", vLog.Data)
  589. if err != nil {
  590. return nil, nil, err
  591. }
  592. rollupEvents.UpdateFeeAddToken = append(rollupEvents.UpdateFeeAddToken, updateFeeAddToken)
  593. case logHermezWithdrawEvent:
  594. var withdraw RollupEventWithdrawEvent
  595. err := c.contractAbi.Unpack(&withdraw, "WithdrawEvent", vLog.Data)
  596. if err != nil {
  597. return nil, nil, err
  598. }
  599. withdraw.Idx = new(big.Int).SetBytes(vLog.Topics[1][:]).Uint64()
  600. withdraw.NumExitRoot = new(big.Int).SetBytes(vLog.Topics[2][:]).Uint64()
  601. rollupEvents.WithdrawEvent = append(rollupEvents.WithdrawEvent, withdraw)
  602. }
  603. }
  604. return &rollupEvents, &blockHash, nil
  605. }
  606. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
  607. func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
  608. tx, _, err := c.client.client.TransactionByHash(context.Background(), ethTxHash)
  609. if err != nil {
  610. return nil, err
  611. }
  612. txData := tx.Data()
  613. method, err := c.contractAbi.MethodById(txData[:4])
  614. if err != nil {
  615. return nil, err
  616. }
  617. var aux RollupForgeBatchArgsAux
  618. if err := method.Inputs.Unpack(&aux, txData[4:]); err != nil {
  619. return nil, err
  620. }
  621. var rollupForgeBatchArgs RollupForgeBatchArgs
  622. rollupForgeBatchArgs.L1Batch = aux.L1Batch
  623. rollupForgeBatchArgs.NewExitRoot = aux.NewExitRoot
  624. rollupForgeBatchArgs.NewLastIdx = aux.NewLastIdx.Int64()
  625. rollupForgeBatchArgs.NewStRoot = aux.NewStRoot
  626. rollupForgeBatchArgs.ProofA = aux.ProofA
  627. rollupForgeBatchArgs.ProofB = aux.ProofB
  628. rollupForgeBatchArgs.ProofC = aux.ProofC
  629. rollupForgeBatchArgs.VerifierIdx = aux.VerifierIdx
  630. numTxsL1 := len(aux.EncodedL1CoordinatorTx) / common.L1CoordinatorTxBytesLen
  631. for i := 0; i < numTxsL1; i++ {
  632. bytesL1Coordinator := aux.EncodedL1CoordinatorTx[i*common.L1CoordinatorTxBytesLen : (i+1)*common.L1CoordinatorTxBytesLen]
  633. var signature []byte
  634. v := bytesL1Coordinator[0]
  635. s := bytesL1Coordinator[1:33]
  636. r := bytesL1Coordinator[33:65]
  637. signature = append(signature, r[:]...)
  638. signature = append(signature, s[:]...)
  639. signature = append(signature, v)
  640. l1Tx, err := common.L1TxFromCoordinatorBytes(bytesL1Coordinator)
  641. if err != nil {
  642. return nil, err
  643. }
  644. rollupForgeBatchArgs.L1CoordinatorTxs = append(rollupForgeBatchArgs.L1CoordinatorTxs, l1Tx)
  645. rollupForgeBatchArgs.L1CoordinatorTxsAuths = append(rollupForgeBatchArgs.L1CoordinatorTxsAuths, signature)
  646. }
  647. rollupConsts, err := c.RollupConstants()
  648. if err != nil {
  649. return nil, err
  650. }
  651. nLevels := rollupConsts.Verifiers[rollupForgeBatchArgs.VerifierIdx].NLevels
  652. lenL2TxsBytes := int((nLevels/8)*2 + 2 + 1)
  653. numTxsL2 := len(aux.L2TxsData) / lenL2TxsBytes
  654. for i := 0; i < numTxsL2; i++ {
  655. l2Tx, err := common.L2TxFromBytes(aux.L2TxsData[i*lenL2TxsBytes:(i+1)*lenL2TxsBytes], int(nLevels))
  656. if err != nil {
  657. return nil, err
  658. }
  659. rollupForgeBatchArgs.L2TxsData = append(rollupForgeBatchArgs.L2TxsData, l2Tx)
  660. }
  661. lenFeeIdxCoordinatorBytes := int(nLevels / 8) //nolint:gomnd
  662. numFeeIdxCoordinator := len(aux.FeeIdxCoordinator) / lenFeeIdxCoordinatorBytes
  663. for i := 0; i < numFeeIdxCoordinator; i++ {
  664. var paddedFeeIdx [6]byte
  665. // TODO: This check is not necessary: the first case will always work. Test it before removing the if.
  666. if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
  667. copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], aux.FeeIdxCoordinator[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  668. } else {
  669. copy(paddedFeeIdx[:], aux.FeeIdxCoordinator[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  670. }
  671. FeeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
  672. if err != nil {
  673. return nil, err
  674. }
  675. rollupForgeBatchArgs.FeeIdxCoordinator = append(rollupForgeBatchArgs.FeeIdxCoordinator, FeeIdxCoordinator)
  676. }
  677. return &rollupForgeBatchArgs, nil
  678. }