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.

421 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/iden3/go-iden3-crypto/babyjub"
  11. )
  12. const (
  13. // FeeIdxCoordinatorLen is the number of tokens the coordinator can use
  14. // to collect fees (determines the number of tokens that the
  15. // coordinator can collect fees from). This value is determined by the
  16. // circuit.
  17. FeeIdxCoordinatorLen = 64
  18. )
  19. // RollupConstants are the constants of the Rollup Smart Contract
  20. type RollupConstants struct {
  21. // Maxim Deposit allowed
  22. MaxAmountDeposit *big.Int
  23. MaxAmountL2 *big.Int
  24. MaxTokens int64
  25. // maximum L1 transactions allowed to be queued for a batch
  26. MaxL1Tx int
  27. // maximum L1 user transactions allowed to be queued for a batch
  28. MaxL1UserTx int
  29. Rfield *big.Int
  30. L1CoordinatorBytes int
  31. L1UserBytes int
  32. L2Bytes int
  33. MaxTxVerifiers []int
  34. TokenHEZ ethCommon.Address
  35. // Only test
  36. GovernanceAddress ethCommon.Address
  37. // Only test
  38. SafetyBot ethCommon.Address
  39. // Only test
  40. ConsensusContract ethCommon.Address
  41. // Only test
  42. WithdrawalContract ethCommon.Address
  43. ReservedIDx uint32
  44. LastIDx uint32
  45. ExitIDx uint32
  46. NoLimitToken int
  47. NumBuckets int
  48. MaxWDelay int64
  49. }
  50. // RollupVariables are the variables of the Rollup Smart Contract
  51. type RollupVariables struct {
  52. FeeAddToken *big.Int
  53. ForgeL1Timeout int64
  54. }
  55. // QueueStruct is the queue of L1Txs for a batch
  56. //nolint:structcheck
  57. type QueueStruct struct {
  58. L1TxQueue []common.L1Tx
  59. TotalL1TxFee *big.Int
  60. }
  61. // NewQueueStruct creates a new clear QueueStruct.
  62. func NewQueueStruct() *QueueStruct {
  63. return &QueueStruct{
  64. L1TxQueue: make([]common.L1Tx, 0),
  65. TotalL1TxFee: big.NewInt(0),
  66. }
  67. }
  68. // RollupState represents the state of the Rollup in the Smart Contract
  69. //nolint:structcheck,unused
  70. type RollupState struct {
  71. StateRoot *big.Int
  72. ExitRoots []*big.Int
  73. ExitNullifierMap map[[256 / 8]byte]bool
  74. TokenList []ethCommon.Address
  75. TokenMap map[ethCommon.Address]bool
  76. MapL1TxQueue map[int64]*QueueStruct
  77. LastL1L2Batch int64
  78. CurrentToForgeL1TxsNum int64
  79. LastToForgeL1TxsNum int64
  80. CurrentIdx int64
  81. }
  82. // RollupEventL1UserTx is an event of the Rollup Smart Contract
  83. type RollupEventL1UserTx struct {
  84. L1Tx common.L1Tx
  85. ToForgeL1TxsNum int64 // QueueIndex *big.Int
  86. Position int // TransactionIndex *big.Int
  87. }
  88. // RollupEventAddToken is an event of the Rollup Smart Contract
  89. type RollupEventAddToken struct {
  90. Address ethCommon.Address
  91. TokenID uint32
  92. }
  93. // RollupEventForgeBatch is an event of the Rollup Smart Contract
  94. type RollupEventForgeBatch struct {
  95. BatchNum int64
  96. EthTxHash ethCommon.Hash
  97. }
  98. // RollupEventUpdateForgeL1L2BatchTimeout is an event of the Rollup Smart Contract
  99. type RollupEventUpdateForgeL1L2BatchTimeout struct {
  100. ForgeL1Timeout *big.Int
  101. }
  102. // RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
  103. type RollupEventUpdateFeeAddToken struct {
  104. FeeAddToken *big.Int
  105. }
  106. // RollupEventWithdrawEvent is an event of the Rollup Smart Contract
  107. type RollupEventWithdrawEvent struct {
  108. Idx *big.Int
  109. NumExitRoot *big.Int
  110. InstantWithdraw bool
  111. }
  112. // RollupEvents is the list of events in a block of the Rollup Smart Contract
  113. type RollupEvents struct { //nolint:structcheck
  114. L1UserTx []RollupEventL1UserTx
  115. AddToken []RollupEventAddToken
  116. ForgeBatch []RollupEventForgeBatch
  117. UpdateForgeL1L2BatchTimeout []RollupEventUpdateForgeL1L2BatchTimeout
  118. UpdateFeeAddToken []RollupEventUpdateFeeAddToken
  119. WithdrawEvent []RollupEventWithdrawEvent
  120. }
  121. // NewRollupEvents creates an empty RollupEvents with the slices initialized.
  122. func NewRollupEvents() RollupEvents {
  123. return RollupEvents{
  124. L1UserTx: make([]RollupEventL1UserTx, 0),
  125. AddToken: make([]RollupEventAddToken, 0),
  126. ForgeBatch: make([]RollupEventForgeBatch, 0),
  127. UpdateForgeL1L2BatchTimeout: make([]RollupEventUpdateForgeL1L2BatchTimeout, 0),
  128. UpdateFeeAddToken: make([]RollupEventUpdateFeeAddToken, 0),
  129. WithdrawEvent: make([]RollupEventWithdrawEvent, 0),
  130. }
  131. }
  132. // RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
  133. //nolint:structcheck,unused
  134. type RollupForgeBatchArgs struct {
  135. ProofA [2]*big.Int
  136. ProofB [2][2]*big.Int
  137. ProofC [2]*big.Int
  138. NewLastIdx int64
  139. NewStRoot *big.Int
  140. NewExitRoot *big.Int
  141. L1CoordinatorTxs []*common.L1Tx
  142. L1CoordinatorTxsAuths [][]byte // Authorization for accountCreations for each L1CoordinatorTxs
  143. L2Txs []*common.L2Tx
  144. FeeIdxCoordinator []common.Idx
  145. // Circuit selector
  146. VerifierIdx int64
  147. L1Batch bool
  148. }
  149. // RollupInterface is the inteface to to Rollup Smart Contract
  150. type RollupInterface interface {
  151. //
  152. // Smart Contract Methods
  153. //
  154. // Public Functions
  155. RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
  156. RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error)
  157. RollupWithdraw(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 common.Float16, tokenID int64) (*types.Transaction, error)
  160. RollupForceTransfer(fromIdx int64, amountF common.Float16, tokenID, toIdx int64) (*types.Transaction, error)
  161. RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
  162. loadAmountF, amountF common.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
  163. RollupDepositTransfer(fromIdx int64, loadAmountF, amountF common.Float16,
  164. tokenID int64, toIdx int64) (*types.Transaction, error)
  165. RollupDeposit(fromIdx int64, loadAmountF common.Float16, tokenID int64) (*types.Transaction, error)
  166. RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
  167. babyPubKey babyjub.PublicKey, loadAmountF common.Float16) (*types.Transaction, error)
  168. RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF common.Float16,
  169. tokenID int64) (*types.Transaction, error)
  170. RollupGetCurrentTokens() (*big.Int, error)
  171. // RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error)
  172. // RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error)
  173. // RollupGetQueue(queue int64) ([]byte, error)
  174. // Governance Public Functions
  175. RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (*types.Transaction, error)
  176. RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
  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. client *EthereumClient
  190. address ethCommon.Address
  191. }
  192. // NewRollupClient creates a new RollupClient
  193. func NewRollupClient(client *EthereumClient, address ethCommon.Address) *RollupClient {
  194. return &RollupClient{
  195. client: client,
  196. address: address,
  197. }
  198. }
  199. // RollupForgeBatch is the interface to call the smart contract function
  200. func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Transaction, error) {
  201. log.Error("TODO")
  202. return nil, errTODO
  203. }
  204. // RollupAddToken is the interface to call the smart contract function
  205. func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
  206. log.Error("TODO")
  207. return nil, errTODO
  208. }
  209. // RollupWithdrawSNARK is the interface to call the smart contract function
  210. // func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  211. // return nil, errTODO
  212. // }
  213. // RollupWithdraw is the interface to call the smart contract function
  214. func (c *RollupClient) RollupWithdraw(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
  215. log.Error("TODO")
  216. return nil, errTODO
  217. }
  218. // RollupForceExit is the interface to call the smart contract function
  219. func (c *RollupClient) RollupForceExit(fromIdx int64, amountF common.Float16, tokenID int64) (*types.Transaction, error) {
  220. log.Error("TODO")
  221. return nil, errTODO
  222. }
  223. // RollupForceTransfer is the interface to call the smart contract function
  224. func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF common.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
  225. log.Error("TODO")
  226. return nil, errTODO
  227. }
  228. // RollupCreateAccountDepositTransfer is the interface to call the smart contract function
  229. func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF common.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  230. log.Error("TODO")
  231. return nil, errTODO
  232. }
  233. // RollupDepositTransfer is the interface to call the smart contract function
  234. func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF common.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  235. log.Error("TODO")
  236. return nil, errTODO
  237. }
  238. // RollupDeposit is the interface to call the smart contract function
  239. func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF common.Float16, tokenID int64) (*types.Transaction, error) {
  240. log.Error("TODO")
  241. return nil, errTODO
  242. }
  243. // RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
  244. func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF common.Float16) (*types.Transaction, error) {
  245. log.Error("TODO")
  246. return nil, errTODO
  247. }
  248. // RollupCreateAccountDeposit is the interface to call the smart contract function
  249. func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF common.Float16, tokenID int64) (*types.Transaction, error) {
  250. log.Error("TODO")
  251. return nil, errTODO
  252. }
  253. // RollupGetTokenAddress is the interface to call the smart contract function
  254. /* func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
  255. return nil, errTODO
  256. } */
  257. // RollupGetCurrentTokens is the interface to call the smart contract function
  258. func (c *RollupClient) RollupGetCurrentTokens() (*big.Int, error) {
  259. log.Error("TODO")
  260. return nil, errTODO
  261. }
  262. // RollupGetL1TxFromQueue is the interface to call the smart contract function
  263. /* func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
  264. return nil, errTODO
  265. } */
  266. // RollupGetQueue is the interface to call the smart contract function
  267. /* func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
  268. return nil, errTODO
  269. }*/
  270. // RollupUpdateForgeL1L2BatchTimeout is the interface to call the smart contract function
  271. func (c *RollupClient) RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (*types.Transaction, error) {
  272. log.Error("TODO")
  273. return nil, errTODO
  274. }
  275. // RollupUpdateFeeAddToken is the interface to call the smart contract function
  276. func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
  277. log.Error("TODO")
  278. return nil, errTODO
  279. }
  280. // RollupConstants returns the Constants of the Rollup Smart Contract
  281. func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
  282. rollupConstants := new(RollupConstants)
  283. if err := c.client.Call(func(ec *ethclient.Client) error {
  284. rollup, err := Hermez.NewHermez(c.address, ec)
  285. if err != nil {
  286. return err
  287. }
  288. // rollupConstants.GovernanceAddress :=
  289. l1CoordinatorBytes, err := rollup.L1COORDINATORBYTES(nil)
  290. if err != nil {
  291. return err
  292. }
  293. rollupConstants.L1CoordinatorBytes = int(l1CoordinatorBytes.Int64())
  294. l1UserBytes, err := rollup.L1USERBYTES(nil)
  295. if err != nil {
  296. return err
  297. }
  298. rollupConstants.L1UserBytes = int(l1UserBytes.Int64())
  299. l2Bytes, err := rollup.L2BYTES(nil)
  300. if err != nil {
  301. return err
  302. }
  303. rollupConstants.L2Bytes = int(l2Bytes.Int64())
  304. rollupConstants.LastIDx, err = rollup.LASTIDX(nil)
  305. if err != nil {
  306. return err
  307. }
  308. rollupConstants.MaxAmountDeposit, err = rollup.MAXLOADAMOUNT(nil)
  309. if err != nil {
  310. return err
  311. }
  312. rollupConstants.MaxAmountL2, err = rollup.MAXAMOUNT(nil)
  313. if err != nil {
  314. return err
  315. }
  316. maxL1Tx, err := rollup.MAXL1TX(nil)
  317. if err != nil {
  318. return err
  319. }
  320. rollupConstants.MaxL1Tx = int(maxL1Tx.Int64())
  321. maxL1UserTx, err := rollup.MAXL1USERTX(nil)
  322. if err != nil {
  323. return err
  324. }
  325. rollupConstants.MaxL1UserTx = int(maxL1UserTx.Int64())
  326. maxTokens, err := rollup.MAXTOKENS(nil)
  327. if err != nil {
  328. return err
  329. }
  330. rollupConstants.MaxTokens = maxTokens.Int64()
  331. maxWDelay, err := rollup.MAXWITHDRAWALDELAY(nil)
  332. if err != nil {
  333. return err
  334. }
  335. rollupConstants.MaxWDelay = maxWDelay.Int64()
  336. noLimitToken, err := rollup.NOLIMIT(nil)
  337. if err != nil {
  338. return err
  339. }
  340. rollupConstants.NoLimitToken = int(noLimitToken.Int64())
  341. numBuckets, err := rollup.NUMBUCKETS(nil)
  342. if err != nil {
  343. return err
  344. }
  345. rollupConstants.NumBuckets = int(numBuckets.Int64())
  346. // rollupConstants.ReservedIDx =
  347. rollupConstants.Rfield, err = rollup.RFIELD(nil)
  348. if err != nil {
  349. return err
  350. }
  351. // rollupConstants.SafetyBot =
  352. // rollupConstants.TokenHEZ =
  353. // rollupConstants.WithdrawalContract =
  354. return nil
  355. }); err != nil {
  356. return nil, err
  357. }
  358. return rollupConstants, nil
  359. }
  360. // RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
  361. func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
  362. log.Error("TODO")
  363. return nil, nil, errTODO
  364. }
  365. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
  366. func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
  367. // tx := client.TransactionByHash(ethTxHash) -> types.Transaction
  368. // txData := types.Transaction -> Data()
  369. // m := abi.MethodById(txData) -> Method
  370. // m.Inputs.Unpack(txData) -> Args
  371. // client.TransactionReceipt()?
  372. log.Error("TODO")
  373. return nil, errTODO
  374. }