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.

374 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 *big.Int
  26. // maximum L1 transactions allowed to be queued for a batch
  27. MaxL1Tx *big.Int
  28. // maximum L1 user transactions allowed to be queued for a batch
  29. MaxL1UserTx *big.Int
  30. Rfield *big.Int
  31. L1CoordinatorBytes *big.Int
  32. L1UserBytes *big.Int
  33. L2Bytes *big.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 *big.Int
  48. NumBuckets *big.Int
  49. MaxWDelay *big.Int
  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. // RollupEventL1UserTxEvent is an event of the Rollup Smart Contract
  84. type RollupEventL1UserTxEvent 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 *big.Int
  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. L1UserTxEvent []RollupEventL1UserTxEvent
  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. L1UserTxEvent: make([]RollupEventL1UserTxEvent, 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. rollupConstants.L1CoordinatorBytes, err = rollup.L1COORDINATORBYTES(nil)
  291. rollupConstants.L1UserBytes, err = rollup.L1USERBYTES(nil)
  292. rollupConstants.L2Bytes, err = rollup.L2BYTES(nil)
  293. rollupConstants.LastIDx, err = rollup.LASTIDX(nil)
  294. rollupConstants.MaxAmountDeposit, err = rollup.MAXLOADAMOUNT(nil)
  295. rollupConstants.MaxAmountL2, err = rollup.MAXAMOUNT(nil)
  296. rollupConstants.MaxL1Tx, err = rollup.MAXL1TX(nil)
  297. rollupConstants.MaxL1UserTx, err = rollup.MAXL1USERTX(nil)
  298. rollupConstants.MaxTokens, err = rollup.MAXTOKENS(nil)
  299. rollupConstants.MaxWDelay, err = rollup.MAXWITHDRAWALDELAY(nil)
  300. rollupConstants.NoLimitToken, err = rollup.NOLIMIT(nil)
  301. rollupConstants.NumBuckets, err = rollup.NUMBUCKETS(nil)
  302. // rollupConstants.ReservedIDx =
  303. rollupConstants.Rfield, err = rollup.RFIELD(nil)
  304. // rollupConstants.SafetyBot =
  305. // rollupConstants.TokenHEZ =
  306. // rollupConstants.WithdrawalContract =
  307. return err
  308. }); err != nil {
  309. return nil, err
  310. }
  311. return rollupConstants, nil
  312. }
  313. // RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
  314. func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
  315. log.Error("TODO")
  316. return nil, nil, errTODO
  317. }
  318. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
  319. func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
  320. // tx := client.TransactionByHash(ethTxHash) -> types.Transaction
  321. // txData := types.Transaction -> Data()
  322. // m := abi.MethodById(txData) -> Method
  323. // m.Inputs.Unpack(txData) -> Args
  324. // client.TransactionReceipt()?
  325. log.Error("TODO")
  326. return nil, errTODO
  327. }