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.

333 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. L1CoordinatorTxsAuths [][]byte // Authorization for accountCreations for each L1CoordinatorTxs
  141. L2Txs []*common.L2Tx
  142. FeeIdxCoordinator []common.Idx
  143. // Circuit selector
  144. VerifierIdx int64
  145. L1Batch bool
  146. }
  147. // RollupInterface is the inteface to to Rollup Smart Contract
  148. type RollupInterface interface {
  149. //
  150. // Smart Contract Methods
  151. //
  152. // Public Functions
  153. RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
  154. RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error)
  155. // RollupWithdrawSNARK() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
  156. RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey,
  157. numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error)
  158. RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error)
  159. RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error)
  160. RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
  161. loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
  162. RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16,
  163. tokenID int64, toIdx int64) (*types.Transaction, error)
  164. RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error)
  165. RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
  166. babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error)
  167. RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16,
  168. tokenID int64) (*types.Transaction, error)
  169. RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error)
  170. RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error)
  171. RollupGetQueue(queue int64) ([]byte, error)
  172. // Governance Public Functions
  173. RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error)
  174. RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error)
  175. RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
  176. RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error)
  177. // RollupUpdateGovernance() (*types.Transaction, error) // TODO (Not defined in Hermez.sol)
  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. }
  191. // RollupForgeBatch is the interface to call the smart contract function
  192. func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
  193. return nil, errTODO
  194. }
  195. // RollupAddToken is the interface to call the smart contract function
  196. func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
  197. return nil, errTODO
  198. }
  199. // RollupWithdrawSNARK is the interface to call the smart contract function
  200. // func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  201. // return nil, errTODO
  202. // }
  203. // RollupWithdrawMerkleProof is the interface to call the smart contract function
  204. func (c *RollupClient) RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
  205. return nil, errTODO
  206. }
  207. // RollupForceExit is the interface to call the smart contract function
  208. func (c *RollupClient) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  209. return nil, errTODO
  210. }
  211. // RollupForceTransfer is the interface to call the smart contract function
  212. func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
  213. return nil, errTODO
  214. }
  215. // RollupCreateAccountDepositTransfer is the interface to call the smart contract function
  216. func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  217. return nil, errTODO
  218. }
  219. // RollupDepositTransfer is the interface to call the smart contract function
  220. func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  221. return nil, errTODO
  222. }
  223. // RollupDeposit is the interface to call the smart contract function
  224. func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  225. return nil, errTODO
  226. }
  227. // RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
  228. func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
  229. return nil, errTODO
  230. }
  231. // RollupCreateAccountDeposit is the interface to call the smart contract function
  232. func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  233. return nil, errTODO
  234. }
  235. // RollupGetTokenAddress is the interface to call the smart contract function
  236. func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
  237. return nil, errTODO
  238. }
  239. // RollupGetL1TxFromQueue is the interface to call the smart contract function
  240. func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
  241. return nil, errTODO
  242. }
  243. // RollupGetQueue is the interface to call the smart contract function
  244. func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
  245. return nil, errTODO
  246. }
  247. // RollupUpdateForgeL1Timeout is the interface to call the smart contract function
  248. func (c *RollupClient) RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error) {
  249. return nil, errTODO
  250. }
  251. // RollupUpdateFeeL1UserTx is the interface to call the smart contract function
  252. func (c *RollupClient) RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error) {
  253. return nil, errTODO
  254. }
  255. // RollupUpdateFeeAddToken is the interface to call the smart contract function
  256. func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
  257. return nil, errTODO
  258. }
  259. // RollupUpdateTokensHEZ is the interface to call the smart contract function
  260. func (c *RollupClient) RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error) {
  261. return nil, errTODO
  262. }
  263. // RollupUpdateGovernance is the interface to call the smart contract function
  264. // func (c *RollupClient) RollupUpdateGovernance() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  265. // return nil, errTODO
  266. // }
  267. // RollupConstants returns the Constants of the Rollup Smart Contract
  268. func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
  269. return nil, errTODO
  270. }
  271. // RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
  272. func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
  273. return nil, nil, errTODO
  274. }
  275. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
  276. func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
  277. // tx := client.TransactionByHash(ethTxHash) -> types.Transaction
  278. // txData := types.Transaction -> Data()
  279. // m := abi.MethodById(txData) -> Method
  280. // m.Inputs.Unpack(txData) -> Args
  281. // client.TransactionReceipt()?
  282. return nil, errTODO
  283. }