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.

332 lines
12 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/common"
  7. "github.com/hermeznetwork/hermez-node/utils"
  8. "github.com/iden3/go-iden3-crypto/babyjub"
  9. )
  10. const (
  11. // FeeIdxCoordinatorLen is the number of tokens the coordinator can use
  12. // to collect fees (determines the number of tokens that the
  13. // coordinator can collect fees from). This value is determined by the
  14. // circuit.
  15. FeeIdxCoordinatorLen = 64
  16. )
  17. // RollupConstants are the constants of the Rollup Smart Contract
  18. type RollupConstants struct {
  19. // Maxim Deposit allowed
  20. MaxAmountDeposit *big.Int
  21. MaxAmountL2 *big.Int
  22. MaxTokens uint32
  23. // maximum L1 transactions allowed to be queued for a batch
  24. MaxL1Tx int
  25. // maximum L1 user transactions allowed to be queued for a batch
  26. MaxL1UserTx int
  27. Rfield *big.Int
  28. L1CoordinatorBytes int
  29. L1UserBytes int
  30. L2Bytes int
  31. }
  32. // RollupVariables are the variables of the Rollup Smart Contract
  33. type RollupVariables struct {
  34. MaxTxVerifiers []int
  35. TokenHEZ ethCommon.Address
  36. GovernanceAddress ethCommon.Address
  37. SafetyBot ethCommon.Address
  38. ConsensusContract ethCommon.Address
  39. WithdrawalContract ethCommon.Address
  40. FeeAddToken *big.Int
  41. ForgeL1Timeout int64
  42. FeeL1UserTx *big.Int
  43. }
  44. // QueueStruct is the queue of L1Txs for a batch
  45. //nolint:structcheck
  46. type QueueStruct struct {
  47. L1TxQueue []common.L1Tx
  48. TotalL1TxFee *big.Int
  49. }
  50. // NewQueueStruct creates a new clear QueueStruct.
  51. func NewQueueStruct() *QueueStruct {
  52. return &QueueStruct{
  53. L1TxQueue: make([]common.L1Tx, 0),
  54. TotalL1TxFee: big.NewInt(0),
  55. }
  56. }
  57. // RollupState represents the state of the Rollup in the Smart Contract
  58. //nolint:structcheck,unused
  59. type RollupState struct {
  60. StateRoot *big.Int
  61. ExitRoots []*big.Int
  62. ExitNullifierMap map[[256 / 8]byte]bool
  63. TokenList []ethCommon.Address
  64. TokenMap map[ethCommon.Address]bool
  65. MapL1TxQueue map[int64]*QueueStruct
  66. LastL1L2Batch int64
  67. CurrentToForgeL1TxsNum int64
  68. LastToForgeL1TxsNum int64
  69. CurrentIdx int64
  70. }
  71. // RollupEventL1UserTx is an event of the Rollup Smart Contract
  72. type RollupEventL1UserTx struct {
  73. L1Tx common.L1Tx
  74. }
  75. // RollupEventAddToken is an event of the Rollup Smart Contract
  76. type RollupEventAddToken struct {
  77. Address ethCommon.Address
  78. TokenID uint32
  79. }
  80. // RollupEventForgeBatch is an event of the Rollup Smart Contract
  81. type RollupEventForgeBatch struct {
  82. BatchNum int64
  83. EthTxHash ethCommon.Hash
  84. }
  85. // RollupEventUpdateForgeL1Timeout is an event of the Rollup Smart Contract
  86. type RollupEventUpdateForgeL1Timeout struct {
  87. ForgeL1Timeout int64
  88. }
  89. // RollupEventUpdateFeeL1UserTx is an event of the Rollup Smart Contract
  90. type RollupEventUpdateFeeL1UserTx struct {
  91. FeeL1UserTx *big.Int
  92. }
  93. // RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
  94. type RollupEventUpdateFeeAddToken struct {
  95. FeeAddToken *big.Int
  96. }
  97. // RollupEventUpdateTokenHez is an event of the Rollup Smart Contract
  98. type RollupEventUpdateTokenHez struct {
  99. TokenHEZ ethCommon.Address
  100. }
  101. // RollupEventWithdraw is an event of the Rollup Smart Contract
  102. type RollupEventWithdraw struct {
  103. Idx int64
  104. NumExitRoot int
  105. }
  106. // RollupEvents is the list of events in a block of the Rollup Smart Contract
  107. type RollupEvents struct { //nolint:structcheck
  108. L1UserTx []RollupEventL1UserTx
  109. AddToken []RollupEventAddToken
  110. ForgeBatch []RollupEventForgeBatch
  111. UpdateForgeL1Timeout []RollupEventUpdateForgeL1Timeout
  112. UpdateFeeL1UserTx []RollupEventUpdateFeeL1UserTx
  113. UpdateFeeAddToken []RollupEventUpdateFeeAddToken
  114. UpdateTokenHez []RollupEventUpdateTokenHez
  115. Withdraw []RollupEventWithdraw
  116. }
  117. // NewRollupEvents creates an empty RollupEvents with the slices initialized.
  118. func NewRollupEvents() RollupEvents {
  119. return RollupEvents{
  120. L1UserTx: make([]RollupEventL1UserTx, 0),
  121. AddToken: make([]RollupEventAddToken, 0),
  122. ForgeBatch: make([]RollupEventForgeBatch, 0),
  123. UpdateForgeL1Timeout: make([]RollupEventUpdateForgeL1Timeout, 0),
  124. UpdateFeeL1UserTx: make([]RollupEventUpdateFeeL1UserTx, 0),
  125. UpdateFeeAddToken: make([]RollupEventUpdateFeeAddToken, 0),
  126. UpdateTokenHez: make([]RollupEventUpdateTokenHez, 0),
  127. Withdraw: make([]RollupEventWithdraw, 0),
  128. }
  129. }
  130. // RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
  131. //nolint:structcheck,unused
  132. type RollupForgeBatchArgs struct {
  133. ProofA [2]*big.Int
  134. ProofB [2][2]*big.Int
  135. ProofC [2]*big.Int
  136. NewLastIdx int64
  137. NewStRoot *big.Int
  138. NewExitRoot *big.Int
  139. L1CoordinatorTxs []*common.L1Tx
  140. L2Txs []*common.L2Tx
  141. FeeIdxCoordinator []common.Idx
  142. // Circuit selector
  143. VerifierIdx int64
  144. L1Batch bool
  145. }
  146. // RollupInterface is the inteface to to Rollup Smart Contract
  147. type RollupInterface interface {
  148. //
  149. // Smart Contract Methods
  150. //
  151. // Public Functions
  152. RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
  153. RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error)
  154. // RollupWithdrawSNARK() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
  155. RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey,
  156. numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error)
  157. RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error)
  158. RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error)
  159. RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
  160. loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
  161. RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16,
  162. tokenID int64, toIdx int64) (*types.Transaction, error)
  163. RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error)
  164. RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
  165. babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error)
  166. RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16,
  167. tokenID int64) (*types.Transaction, error)
  168. RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error)
  169. RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error)
  170. RollupGetQueue(queue int64) ([]byte, error)
  171. // Governance Public Functions
  172. RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error)
  173. RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error)
  174. RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
  175. RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error)
  176. // RollupUpdateGovernance() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
  177. //
  178. // Smart Contract Status
  179. //
  180. RollupConstants() (*RollupConstants, error)
  181. RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
  182. RollupForgeBatchArgs(ethCommon.Hash) (*RollupForgeBatchArgs, error)
  183. }
  184. //
  185. // Implementation
  186. //
  187. // RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
  188. type RollupClient struct {
  189. }
  190. // RollupForgeBatch is the interface to call the smart contract function
  191. func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
  192. return nil, errTODO
  193. }
  194. // RollupAddToken is the interface to call the smart contract function
  195. func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
  196. return nil, errTODO
  197. }
  198. // RollupWithdrawSNARK is the interface to call the smart contract function
  199. // func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  200. // return nil, errTODO
  201. // }
  202. // RollupWithdrawMerkleProof is the interface to call the smart contract function
  203. func (c *RollupClient) RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
  204. return nil, errTODO
  205. }
  206. // RollupForceExit is the interface to call the smart contract function
  207. func (c *RollupClient) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  208. return nil, errTODO
  209. }
  210. // RollupForceTransfer is the interface to call the smart contract function
  211. func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
  212. return nil, errTODO
  213. }
  214. // RollupCreateAccountDepositTransfer is the interface to call the smart contract function
  215. func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  216. return nil, errTODO
  217. }
  218. // RollupDepositTransfer is the interface to call the smart contract function
  219. func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  220. return nil, errTODO
  221. }
  222. // RollupDeposit is the interface to call the smart contract function
  223. func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  224. return nil, errTODO
  225. }
  226. // RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
  227. func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
  228. return nil, errTODO
  229. }
  230. // RollupCreateAccountDeposit is the interface to call the smart contract function
  231. func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  232. return nil, errTODO
  233. }
  234. // RollupGetTokenAddress is the interface to call the smart contract function
  235. func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
  236. return nil, errTODO
  237. }
  238. // RollupGetL1TxFromQueue is the interface to call the smart contract function
  239. func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
  240. return nil, errTODO
  241. }
  242. // RollupGetQueue is the interface to call the smart contract function
  243. func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
  244. return nil, errTODO
  245. }
  246. // RollupUpdateForgeL1Timeout is the interface to call the smart contract function
  247. func (c *RollupClient) RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error) {
  248. return nil, errTODO
  249. }
  250. // RollupUpdateFeeL1UserTx is the interface to call the smart contract function
  251. func (c *RollupClient) RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error) {
  252. return nil, errTODO
  253. }
  254. // RollupUpdateFeeAddToken is the interface to call the smart contract function
  255. func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
  256. return nil, errTODO
  257. }
  258. // RollupUpdateTokensHEZ is the interface to call the smart contract function
  259. func (c *RollupClient) RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error) {
  260. return nil, errTODO
  261. }
  262. // RollupUpdateGovernance is the interface to call the smart contract function
  263. // func (c *RollupClient) RollupUpdateGovernance() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  264. // return nil, errTODO
  265. // }
  266. // RollupConstants returns the Constants of the Rollup Smart Contract
  267. func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
  268. return nil, errTODO
  269. }
  270. // RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
  271. func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
  272. return nil, nil, errTODO
  273. }
  274. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
  275. func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
  276. // tx := client.TransactionByHash(ethTxHash) -> types.Transaction
  277. // txData := types.Transaction -> Data()
  278. // m := abi.MethodById(txData) -> Method
  279. // m.Inputs.Unpack(txData) -> Args
  280. // client.TransactionReceipt()?
  281. return nil, errTODO
  282. }