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.

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