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.

298 lines
11 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/hermeznetwork/hermez-node/utils"
  7. "github.com/iden3/go-iden3-crypto/babyjub"
  8. )
  9. // RollupConstants are the constants of the Rollup Smart Contract
  10. type RollupConstants struct {
  11. // Maxim Deposit allowed
  12. MaxAmountDeposit *big.Int
  13. MaxAmountL2 *big.Int
  14. MaxTokens uint32
  15. // maximum L1 transactions allowed to be queued for a batch
  16. MaxL1Tx int
  17. // maximum L1 user transactions allowed to be queued for a batch
  18. MaxL1UserTx int
  19. Rfield *big.Int
  20. L1CoordinatorBytes int
  21. L1UserBytes int
  22. L2Bytes int
  23. }
  24. // RollupVariables are the variables of the Rollup Smart Contract
  25. type RollupVariables struct {
  26. MaxTxVerifiers []int
  27. TokenHEZ ethCommon.Address
  28. GovernanceAddress ethCommon.Address
  29. SafetyBot ethCommon.Address
  30. ConsensusContract ethCommon.Address
  31. WithdrawalContract ethCommon.Address
  32. FeeAddToken *big.Int
  33. ForgeL1Timeout int64
  34. FeeL1UserTx *big.Int
  35. }
  36. // QueueStruct is the queue of L1Txs for a batch
  37. //nolint:structcheck
  38. type QueueStruct struct {
  39. L1TxQueue [][]byte
  40. CurrentIndex int64
  41. TotalL1TxFee *big.Int
  42. }
  43. // RollupState represents the state of the Rollup in the Smart Contract
  44. //nolint:structcheck,unused
  45. type RollupState struct {
  46. StateRoot *big.Int
  47. ExitRoots []*big.Int
  48. ExiNullifierMap map[[256 / 8]byte]bool
  49. TokenList []ethCommon.Address
  50. TokenMap map[ethCommon.Address]bool
  51. mapL1TxQueue map[int64]QueueStruct
  52. LastLTxBatch int64
  53. CurrentToForgeL1TxsNum int64
  54. LastToForgeL1TxsNum int64
  55. CurrentIdx int64
  56. }
  57. // RollupEventL1UserTx is an event of the Rollup Smart Contract
  58. type RollupEventL1UserTx struct {
  59. L1UserTx []byte
  60. ToForgeL1TxsNum int64
  61. Position int
  62. }
  63. // RollupEventAddToken is an event of the Rollup Smart Contract
  64. type RollupEventAddToken struct {
  65. Address ethCommon.Address
  66. TokenID uint32
  67. }
  68. // RollupEventForgeBatch is an event of the Rollup Smart Contract
  69. type RollupEventForgeBatch struct {
  70. BatchNum int64
  71. }
  72. // RollupEventUpdateForgeL1Timeout is an event of the Rollup Smart Contract
  73. type RollupEventUpdateForgeL1Timeout struct {
  74. ForgeL1Timeout int64
  75. }
  76. // RollupEventUpdateFeeL1UserTx is an event of the Rollup Smart Contract
  77. type RollupEventUpdateFeeL1UserTx struct {
  78. FeeL1UserTx *big.Int
  79. }
  80. // RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
  81. type RollupEventUpdateFeeAddToken struct {
  82. FeeAddToken *big.Int
  83. }
  84. // RollupEventUpdateTokenHez is an event of the Rollup Smart Contract
  85. type RollupEventUpdateTokenHez struct {
  86. TokenHEZ ethCommon.Address
  87. }
  88. // RollupEventWithdraw is an event of the Rollup Smart Contract
  89. type RollupEventWithdraw struct {
  90. Idx int64
  91. NumExitRoot int
  92. }
  93. // RollupEvents is the list of events in a block of the Rollup Smart Contract
  94. type RollupEvents struct { //nolint:structcheck
  95. L1UserTx []RollupEventL1UserTx
  96. AddToken []RollupEventAddToken
  97. ForgeBatch []RollupEventForgeBatch
  98. UpdateForgeL1Timeout []RollupEventUpdateForgeL1Timeout
  99. UpdateFeeL1UserTx []RollupEventUpdateFeeL1UserTx
  100. UpdateFeeAddToken []RollupEventUpdateFeeAddToken
  101. UpdateTokenHez []RollupEventUpdateTokenHez
  102. Withdraw []RollupEventWithdraw
  103. }
  104. // RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
  105. //nolint:structcheck,unused
  106. type RollupForgeBatchArgs struct {
  107. proofA [2]*big.Int
  108. proofB [2][2]*big.Int
  109. proofC [2]*big.Int
  110. newLastIdx int64
  111. newStRoot *big.Int
  112. newExitRoot *big.Int
  113. // TODO: Replace compressedL1CoordinatorTx, l2TxsData, feeIdxCoordinator for vectors
  114. compressedL1CoordinatorTx []byte
  115. l2TxsData []byte
  116. feeIdxCoordinator []byte
  117. verifierIdx int64
  118. l1Batch bool
  119. }
  120. // RollupInterface is the inteface to to Rollup Smart Contract
  121. type RollupInterface interface {
  122. //
  123. // Smart Contract Methods
  124. //
  125. // Public Functions
  126. RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
  127. RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error)
  128. // RollupWithdrawSNARK() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
  129. RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey,
  130. numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error)
  131. RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error)
  132. RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error)
  133. RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
  134. loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
  135. RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16,
  136. tokenID int64, toIdx int64) (*types.Transaction, error)
  137. RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error)
  138. RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
  139. babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error)
  140. RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16,
  141. tokenID int64) (*types.Transaction, error)
  142. RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error)
  143. RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error)
  144. RollupGetQueue(queue int64) ([]byte, error)
  145. // Governance Public Functions
  146. RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error)
  147. RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error)
  148. RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
  149. RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error)
  150. // RollupUpdateGovernance() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
  151. //
  152. // Smart Contract Status
  153. //
  154. RollupConstants() (*RollupConstants, error)
  155. RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
  156. RollupForgeBatchArgs(*types.Transaction) (*RollupForgeBatchArgs, error)
  157. }
  158. //
  159. // Implementation
  160. //
  161. // RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
  162. type RollupClient struct {
  163. }
  164. // RollupForgeBatch is the interface to call the smart contract function
  165. func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
  166. return nil, errTODO
  167. }
  168. // RollupAddToken is the interface to call the smart contract function
  169. func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
  170. return nil, errTODO
  171. }
  172. // RollupWithdrawSNARK is the interface to call the smart contract function
  173. // func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  174. // return nil, errTODO
  175. // }
  176. // RollupWithdrawMerkleProof is the interface to call the smart contract function
  177. func (c *RollupClient) RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
  178. return nil, errTODO
  179. }
  180. // RollupForceExit is the interface to call the smart contract function
  181. func (c *RollupClient) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  182. return nil, errTODO
  183. }
  184. // RollupForceTransfer is the interface to call the smart contract function
  185. func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
  186. return nil, errTODO
  187. }
  188. // RollupCreateAccountDepositTransfer is the interface to call the smart contract function
  189. func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  190. return nil, errTODO
  191. }
  192. // RollupDepositTransfer is the interface to call the smart contract function
  193. func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  194. return nil, errTODO
  195. }
  196. // RollupDeposit is the interface to call the smart contract function
  197. func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  198. return nil, errTODO
  199. }
  200. // RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
  201. func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
  202. return nil, errTODO
  203. }
  204. // RollupCreateAccountDeposit is the interface to call the smart contract function
  205. func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  206. return nil, errTODO
  207. }
  208. // RollupGetTokenAddress is the interface to call the smart contract function
  209. func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
  210. return nil, errTODO
  211. }
  212. // RollupGetL1TxFromQueue is the interface to call the smart contract function
  213. func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
  214. return nil, errTODO
  215. }
  216. // RollupGetQueue is the interface to call the smart contract function
  217. func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
  218. return nil, errTODO
  219. }
  220. // RollupUpdateForgeL1Timeout is the interface to call the smart contract function
  221. func (c *RollupClient) RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error) {
  222. return nil, errTODO
  223. }
  224. // RollupUpdateFeeL1UserTx is the interface to call the smart contract function
  225. func (c *RollupClient) RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error) {
  226. return nil, errTODO
  227. }
  228. // RollupUpdateFeeAddToken is the interface to call the smart contract function
  229. func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
  230. return nil, errTODO
  231. }
  232. // RollupUpdateTokensHEZ is the interface to call the smart contract function
  233. func (c *RollupClient) RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error) {
  234. return nil, errTODO
  235. }
  236. // RollupUpdateGovernance is the interface to call the smart contract function
  237. // func (c *RollupClient) RollupUpdateGovernance() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  238. // return nil, errTODO
  239. // }
  240. // RollupConstants returns the Constants of the Rollup Smart Contract
  241. func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
  242. return nil, errTODO
  243. }
  244. // RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
  245. func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
  246. return nil, nil, errTODO
  247. }
  248. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
  249. func (c *RollupClient) RollupForgeBatchArgs(transaction *types.Transaction) (*RollupForgeBatchArgs, error) {
  250. return nil, errTODO
  251. }