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.

422 lines
14 KiB

  1. package eth
  2. import (
  3. "math/big"
  4. ethCommon "github.com/ethereum/go-ethereum/common"
  5. "github.com/ethereum/go-ethereum/core/types"
  6. "github.com/ethereum/go-ethereum/ethclient"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. Hermez "github.com/hermeznetwork/hermez-node/eth/contracts/hermez"
  9. "github.com/hermeznetwork/hermez-node/log"
  10. "github.com/hermeznetwork/hermez-node/utils"
  11. "github.com/iden3/go-iden3-crypto/babyjub"
  12. )
  13. const (
  14. // FeeIdxCoordinatorLen is the number of tokens the coordinator can use
  15. // to collect fees (determines the number of tokens that the
  16. // coordinator can collect fees from). This value is determined by the
  17. // circuit.
  18. FeeIdxCoordinatorLen = 64
  19. )
  20. // RollupConstants are the constants of the Rollup Smart Contract
  21. type RollupConstants struct {
  22. // Maxim Deposit allowed
  23. MaxAmountDeposit *big.Int
  24. MaxAmountL2 *big.Int
  25. MaxTokens int64
  26. // maximum L1 transactions allowed to be queued for a batch
  27. MaxL1Tx int
  28. // maximum L1 user transactions allowed to be queued for a batch
  29. MaxL1UserTx int
  30. Rfield *big.Int
  31. L1CoordinatorBytes int
  32. L1UserBytes int
  33. L2Bytes int
  34. MaxTxVerifiers []int
  35. TokenHEZ ethCommon.Address
  36. // Only test
  37. GovernanceAddress ethCommon.Address
  38. // Only test
  39. SafetyBot ethCommon.Address
  40. // Only test
  41. ConsensusContract ethCommon.Address
  42. // Only test
  43. WithdrawalContract ethCommon.Address
  44. ReservedIDx uint32
  45. LastIDx uint32
  46. ExitIDx uint32
  47. NoLimitToken int
  48. NumBuckets int
  49. MaxWDelay int64
  50. }
  51. // RollupVariables are the variables of the Rollup Smart Contract
  52. type RollupVariables struct {
  53. FeeAddToken *big.Int
  54. ForgeL1Timeout int64
  55. }
  56. // QueueStruct is the queue of L1Txs for a batch
  57. //nolint:structcheck
  58. type QueueStruct struct {
  59. L1TxQueue []common.L1Tx
  60. TotalL1TxFee *big.Int
  61. }
  62. // NewQueueStruct creates a new clear QueueStruct.
  63. func NewQueueStruct() *QueueStruct {
  64. return &QueueStruct{
  65. L1TxQueue: make([]common.L1Tx, 0),
  66. TotalL1TxFee: big.NewInt(0),
  67. }
  68. }
  69. // RollupState represents the state of the Rollup in the Smart Contract
  70. //nolint:structcheck,unused
  71. type RollupState struct {
  72. StateRoot *big.Int
  73. ExitRoots []*big.Int
  74. ExitNullifierMap map[[256 / 8]byte]bool
  75. TokenList []ethCommon.Address
  76. TokenMap map[ethCommon.Address]bool
  77. MapL1TxQueue map[int64]*QueueStruct
  78. LastL1L2Batch int64
  79. CurrentToForgeL1TxsNum int64
  80. LastToForgeL1TxsNum int64
  81. CurrentIdx int64
  82. }
  83. // RollupEventL1UserTx is an event of the Rollup Smart Contract
  84. type RollupEventL1UserTx struct {
  85. L1Tx common.L1Tx
  86. QueueIndex *big.Int
  87. TransactionIndex *big.Int
  88. }
  89. // RollupEventAddToken is an event of the Rollup Smart Contract
  90. type RollupEventAddToken struct {
  91. Address ethCommon.Address
  92. TokenID uint32
  93. }
  94. // RollupEventForgeBatch is an event of the Rollup Smart Contract
  95. type RollupEventForgeBatch struct {
  96. BatchNum int64
  97. EthTxHash ethCommon.Hash
  98. }
  99. // RollupEventUpdateForgeL1L2BatchTimeout is an event of the Rollup Smart Contract
  100. type RollupEventUpdateForgeL1L2BatchTimeout struct {
  101. ForgeL1Timeout *big.Int
  102. }
  103. // RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
  104. type RollupEventUpdateFeeAddToken struct {
  105. FeeAddToken *big.Int
  106. }
  107. // RollupEventWithdrawEvent is an event of the Rollup Smart Contract
  108. type RollupEventWithdrawEvent struct {
  109. Idx *big.Int
  110. NumExitRoot *big.Int
  111. InstantWithdraw bool
  112. }
  113. // RollupEvents is the list of events in a block of the Rollup Smart Contract
  114. type RollupEvents struct { //nolint:structcheck
  115. L1UserTx []RollupEventL1UserTx
  116. AddToken []RollupEventAddToken
  117. ForgeBatch []RollupEventForgeBatch
  118. UpdateForgeL1L2BatchTimeout []RollupEventUpdateForgeL1L2BatchTimeout
  119. UpdateFeeAddToken []RollupEventUpdateFeeAddToken
  120. WithdrawEvent []RollupEventWithdrawEvent
  121. }
  122. // NewRollupEvents creates an empty RollupEvents with the slices initialized.
  123. func NewRollupEvents() RollupEvents {
  124. return RollupEvents{
  125. L1UserTx: make([]RollupEventL1UserTx, 0),
  126. AddToken: make([]RollupEventAddToken, 0),
  127. ForgeBatch: make([]RollupEventForgeBatch, 0),
  128. UpdateForgeL1L2BatchTimeout: make([]RollupEventUpdateForgeL1L2BatchTimeout, 0),
  129. UpdateFeeAddToken: make([]RollupEventUpdateFeeAddToken, 0),
  130. WithdrawEvent: make([]RollupEventWithdrawEvent, 0),
  131. }
  132. }
  133. // RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
  134. //nolint:structcheck,unused
  135. type RollupForgeBatchArgs struct {
  136. ProofA [2]*big.Int
  137. ProofB [2][2]*big.Int
  138. ProofC [2]*big.Int
  139. NewLastIdx int64
  140. NewStRoot *big.Int
  141. NewExitRoot *big.Int
  142. L1CoordinatorTxs []*common.L1Tx
  143. L1CoordinatorTxsAuths [][]byte // Authorization for accountCreations for each L1CoordinatorTxs
  144. L2Txs []*common.L2Tx
  145. FeeIdxCoordinator []common.Idx
  146. // Circuit selector
  147. VerifierIdx int64
  148. L1Batch bool
  149. }
  150. // RollupInterface is the inteface to to Rollup Smart Contract
  151. type RollupInterface interface {
  152. //
  153. // Smart Contract Methods
  154. //
  155. // Public Functions
  156. RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
  157. RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error)
  158. RollupWithdraw(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey,
  159. numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error)
  160. RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error)
  161. RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error)
  162. RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
  163. loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
  164. RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16,
  165. tokenID int64, toIdx int64) (*types.Transaction, error)
  166. RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error)
  167. RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
  168. babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error)
  169. RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16,
  170. tokenID int64) (*types.Transaction, error)
  171. RollupGetCurrentTokens() (*big.Int, error)
  172. // RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error)
  173. // RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error)
  174. // RollupGetQueue(queue int64) ([]byte, error)
  175. // Governance Public Functions
  176. RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (*types.Transaction, error)
  177. RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
  178. //
  179. // Smart Contract Status
  180. //
  181. RollupConstants() (*RollupConstants, error)
  182. RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
  183. RollupForgeBatchArgs(ethCommon.Hash) (*RollupForgeBatchArgs, error)
  184. }
  185. //
  186. // Implementation
  187. //
  188. // RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
  189. type RollupClient struct {
  190. client *EthereumClient
  191. address ethCommon.Address
  192. }
  193. // NewRollupClient creates a new RollupClient
  194. func NewRollupClient(client *EthereumClient, address ethCommon.Address) *RollupClient {
  195. return &RollupClient{
  196. client: client,
  197. address: address,
  198. }
  199. }
  200. // RollupForgeBatch is the interface to call the smart contract function
  201. func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
  202. log.Error("TODO")
  203. return nil, errTODO
  204. }
  205. // RollupAddToken is the interface to call the smart contract function
  206. func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
  207. log.Error("TODO")
  208. return nil, errTODO
  209. }
  210. // RollupWithdrawSNARK is the interface to call the smart contract function
  211. // func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  212. // return nil, errTODO
  213. // }
  214. // RollupWithdraw is the interface to call the smart contract function
  215. func (c *RollupClient) RollupWithdraw(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
  216. log.Error("TODO")
  217. return nil, errTODO
  218. }
  219. // RollupForceExit is the interface to call the smart contract function
  220. func (c *RollupClient) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  221. log.Error("TODO")
  222. return nil, errTODO
  223. }
  224. // RollupForceTransfer is the interface to call the smart contract function
  225. func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
  226. log.Error("TODO")
  227. return nil, errTODO
  228. }
  229. // RollupCreateAccountDepositTransfer is the interface to call the smart contract function
  230. func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  231. log.Error("TODO")
  232. return nil, errTODO
  233. }
  234. // RollupDepositTransfer is the interface to call the smart contract function
  235. func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  236. log.Error("TODO")
  237. return nil, errTODO
  238. }
  239. // RollupDeposit is the interface to call the smart contract function
  240. func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  241. log.Error("TODO")
  242. return nil, errTODO
  243. }
  244. // RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
  245. func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
  246. log.Error("TODO")
  247. return nil, errTODO
  248. }
  249. // RollupCreateAccountDeposit is the interface to call the smart contract function
  250. func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  251. log.Error("TODO")
  252. return nil, errTODO
  253. }
  254. // RollupGetTokenAddress is the interface to call the smart contract function
  255. /* func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
  256. return nil, errTODO
  257. } */
  258. // RollupGetCurrentTokens is the interface to call the smart contract function
  259. func (c *RollupClient) RollupGetCurrentTokens() (*big.Int, error) {
  260. log.Error("TODO")
  261. return nil, errTODO
  262. }
  263. // RollupGetL1TxFromQueue is the interface to call the smart contract function
  264. /* func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
  265. return nil, errTODO
  266. } */
  267. // RollupGetQueue is the interface to call the smart contract function
  268. /* func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
  269. return nil, errTODO
  270. }*/
  271. // RollupUpdateForgeL1L2BatchTimeout is the interface to call the smart contract function
  272. func (c *RollupClient) RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (*types.Transaction, error) {
  273. log.Error("TODO")
  274. return nil, errTODO
  275. }
  276. // RollupUpdateFeeAddToken is the interface to call the smart contract function
  277. func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
  278. log.Error("TODO")
  279. return nil, errTODO
  280. }
  281. // RollupConstants returns the Constants of the Rollup Smart Contract
  282. func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
  283. rollupConstants := new(RollupConstants)
  284. if err := c.client.Call(func(ec *ethclient.Client) error {
  285. rollup, err := Hermez.NewHermez(c.address, ec)
  286. if err != nil {
  287. return err
  288. }
  289. // rollupConstants.GovernanceAddress :=
  290. l1CoordinatorBytes, err := rollup.L1COORDINATORBYTES(nil)
  291. if err != nil {
  292. return err
  293. }
  294. rollupConstants.L1CoordinatorBytes = int(l1CoordinatorBytes.Int64())
  295. l1UserBytes, err := rollup.L1USERBYTES(nil)
  296. if err != nil {
  297. return err
  298. }
  299. rollupConstants.L1UserBytes = int(l1UserBytes.Int64())
  300. l2Bytes, err := rollup.L2BYTES(nil)
  301. if err != nil {
  302. return err
  303. }
  304. rollupConstants.L2Bytes = int(l2Bytes.Int64())
  305. rollupConstants.LastIDx, err = rollup.LASTIDX(nil)
  306. if err != nil {
  307. return err
  308. }
  309. rollupConstants.MaxAmountDeposit, err = rollup.MAXLOADAMOUNT(nil)
  310. if err != nil {
  311. return err
  312. }
  313. rollupConstants.MaxAmountL2, err = rollup.MAXAMOUNT(nil)
  314. if err != nil {
  315. return err
  316. }
  317. maxL1Tx, err := rollup.MAXL1TX(nil)
  318. if err != nil {
  319. return err
  320. }
  321. rollupConstants.MaxL1Tx = int(maxL1Tx.Int64())
  322. maxL1UserTx, err := rollup.MAXL1USERTX(nil)
  323. if err != nil {
  324. return err
  325. }
  326. rollupConstants.MaxL1UserTx = int(maxL1UserTx.Int64())
  327. maxTokens, err := rollup.MAXTOKENS(nil)
  328. if err != nil {
  329. return err
  330. }
  331. rollupConstants.MaxTokens = maxTokens.Int64()
  332. maxWDelay, err := rollup.MAXWITHDRAWALDELAY(nil)
  333. if err != nil {
  334. return err
  335. }
  336. rollupConstants.MaxWDelay = maxWDelay.Int64()
  337. noLimitToken, err := rollup.NOLIMIT(nil)
  338. if err != nil {
  339. return err
  340. }
  341. rollupConstants.NoLimitToken = int(noLimitToken.Int64())
  342. numBuckets, err := rollup.NUMBUCKETS(nil)
  343. if err != nil {
  344. return err
  345. }
  346. rollupConstants.NumBuckets = int(numBuckets.Int64())
  347. // rollupConstants.ReservedIDx =
  348. rollupConstants.Rfield, err = rollup.RFIELD(nil)
  349. if err != nil {
  350. return err
  351. }
  352. // rollupConstants.SafetyBot =
  353. // rollupConstants.TokenHEZ =
  354. // rollupConstants.WithdrawalContract =
  355. return nil
  356. }); err != nil {
  357. return nil, err
  358. }
  359. return rollupConstants, nil
  360. }
  361. // RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
  362. func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
  363. log.Error("TODO")
  364. return nil, nil, errTODO
  365. }
  366. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
  367. func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
  368. // tx := client.TransactionByHash(ethTxHash) -> types.Transaction
  369. // txData := types.Transaction -> Data()
  370. // m := abi.MethodById(txData) -> Method
  371. // m.Inputs.Unpack(txData) -> Args
  372. // client.TransactionReceipt()?
  373. log.Error("TODO")
  374. return nil, errTODO
  375. }