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.

1642 lines
49 KiB

  1. package test
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "encoding/json"
  6. "fmt"
  7. "math/big"
  8. "reflect"
  9. "sync"
  10. "time"
  11. "github.com/ethereum/go-ethereum"
  12. ethCommon "github.com/ethereum/go-ethereum/common"
  13. "github.com/ethereum/go-ethereum/core/types"
  14. "github.com/hermeznetwork/hermez-node/common"
  15. "github.com/hermeznetwork/hermez-node/eth"
  16. "github.com/hermeznetwork/hermez-node/log"
  17. "github.com/iden3/go-iden3-crypto/babyjub"
  18. "github.com/mitchellh/copystructure"
  19. )
  20. func init() {
  21. copystructure.Copiers[reflect.TypeOf(big.Int{})] =
  22. func(raw interface{}) (interface{}, error) {
  23. in := raw.(big.Int)
  24. out := new(big.Int).Set(&in)
  25. return *out, nil
  26. }
  27. }
  28. // WDelayerBlock stores all the data related to the WDelayer SC from an ethereum block
  29. type WDelayerBlock struct {
  30. // State eth.WDelayerState // TODO
  31. Vars common.WDelayerVariables
  32. Events eth.WDelayerEvents
  33. Txs map[ethCommon.Hash]*types.Transaction
  34. Constants *common.WDelayerConstants
  35. Eth *EthereumBlock
  36. }
  37. func (w *WDelayerBlock) addTransaction(tx *types.Transaction) *types.Transaction {
  38. txHash := tx.Hash()
  39. w.Txs[txHash] = tx
  40. return tx
  41. }
  42. // RollupBlock stores all the data related to the Rollup SC from an ethereum block
  43. type RollupBlock struct {
  44. State eth.RollupState
  45. Vars common.RollupVariables
  46. Events eth.RollupEvents
  47. Txs map[ethCommon.Hash]*types.Transaction
  48. Constants *common.RollupConstants
  49. Eth *EthereumBlock
  50. }
  51. func (r *RollupBlock) addTransaction(tx *types.Transaction) *types.Transaction {
  52. txHash := tx.Hash()
  53. r.Txs[txHash] = tx
  54. return tx
  55. }
  56. var (
  57. errBidClosed = fmt.Errorf("Bid has already been closed")
  58. errBidNotOpen = fmt.Errorf("Bid has not been opened yet")
  59. errBidBelowMin = fmt.Errorf("Bid below minimum")
  60. errCoordNotReg = fmt.Errorf("Coordinator not registered")
  61. )
  62. // AuctionBlock stores all the data related to the Auction SC from an ethereum block
  63. type AuctionBlock struct {
  64. State eth.AuctionState
  65. Vars common.AuctionVariables
  66. Events eth.AuctionEvents
  67. Txs map[ethCommon.Hash]*types.Transaction
  68. Constants *common.AuctionConstants
  69. Eth *EthereumBlock
  70. }
  71. func (a *AuctionBlock) addTransaction(tx *types.Transaction) *types.Transaction {
  72. txHash := tx.Hash()
  73. a.Txs[txHash] = tx
  74. return tx
  75. }
  76. func (a *AuctionBlock) getSlotNumber(blockNumber int64) int64 {
  77. if a.Eth.BlockNum >= a.Constants.GenesisBlockNum {
  78. return (blockNumber - a.Constants.GenesisBlockNum) / int64(a.Constants.BlocksPerSlot)
  79. }
  80. return 0
  81. }
  82. func (a *AuctionBlock) getCurrentSlotNumber() int64 {
  83. return a.getSlotNumber(a.Eth.BlockNum)
  84. }
  85. func (a *AuctionBlock) getSlotSet(slot int64) int64 {
  86. return slot % int64(len(a.Vars.DefaultSlotSetBid))
  87. }
  88. func (a *AuctionBlock) getMinBidBySlot(slot int64) (*big.Int, error) {
  89. if slot < a.getCurrentSlotNumber()+int64(a.Vars.ClosedAuctionSlots) {
  90. return nil, errBidClosed
  91. }
  92. slotSet := a.getSlotSet(slot)
  93. // fmt.Println("slot:", slot, "slotSet:", slotSet)
  94. var prevBid *big.Int
  95. slotState, ok := a.State.Slots[slot]
  96. if !ok {
  97. slotState = eth.NewSlotState()
  98. a.State.Slots[slot] = slotState
  99. }
  100. // If the bidAmount for a slot is 0 it means that it has not yet been bid, so the midBid will be the minimum
  101. // bid for the slot time plus the outbidding set, otherwise it will be the bidAmount plus the outbidding
  102. if slotState.BidAmount.Cmp(big.NewInt(0)) == 0 {
  103. prevBid = a.Vars.DefaultSlotSetBid[slotSet]
  104. } else {
  105. prevBid = slotState.BidAmount
  106. }
  107. outBid := new(big.Int).Set(prevBid)
  108. // fmt.Println("outBid:", outBid)
  109. outBid.Mul(outBid, big.NewInt(int64(a.Vars.Outbidding)))
  110. outBid.Div(outBid, big.NewInt(10000)) //nolint:gomnd
  111. outBid.Add(prevBid, outBid)
  112. // fmt.Println("minBid:", outBid)
  113. return outBid, nil
  114. }
  115. func (a *AuctionBlock) forge(forger ethCommon.Address) error {
  116. if ok, err := a.canForge(forger, a.Eth.BlockNum); err != nil {
  117. return err
  118. } else if !ok {
  119. return fmt.Errorf("Can't forge")
  120. }
  121. slotToForge := a.getSlotNumber(a.Eth.BlockNum)
  122. slotState, ok := a.State.Slots[slotToForge]
  123. if !ok {
  124. slotState = eth.NewSlotState()
  125. a.State.Slots[slotToForge] = slotState
  126. }
  127. slotState.Fulfilled = true
  128. a.Events.NewForge = append(a.Events.NewForge, eth.AuctionEventNewForge{
  129. Forger: forger,
  130. SlotToForge: slotToForge,
  131. })
  132. return nil
  133. }
  134. func (a *AuctionBlock) canForge(forger ethCommon.Address, blockNum int64) (bool, error) {
  135. if blockNum < a.Constants.GenesisBlockNum {
  136. return false, fmt.Errorf("Auction has not started yet")
  137. }
  138. slotToForge := a.getSlotNumber(blockNum)
  139. // Get the relativeBlock to check if the slotDeadline has been exceeded
  140. relativeBlock := blockNum - (a.Constants.GenesisBlockNum + (slotToForge * int64(a.Constants.BlocksPerSlot)))
  141. // If the closedMinBid is 0 it means that we have to take as minBid the one that is set for this slot set,
  142. // otherwise the one that has been saved will be used
  143. var minBid *big.Int
  144. slotState, ok := a.State.Slots[slotToForge]
  145. if !ok {
  146. slotState = eth.NewSlotState()
  147. a.State.Slots[slotToForge] = slotState
  148. }
  149. if slotState.ClosedMinBid.Cmp(big.NewInt(0)) == 0 {
  150. minBid = a.Vars.DefaultSlotSetBid[a.getSlotSet(slotToForge)]
  151. } else {
  152. minBid = slotState.ClosedMinBid
  153. }
  154. if !slotState.Fulfilled && (relativeBlock >= int64(a.Vars.SlotDeadline)) {
  155. // if the relative block has exceeded the slotDeadline and no batch has been forged, anyone can forge
  156. return true, nil
  157. // TODO, find the forger set by the Bidder
  158. } else if coord, ok := a.State.Coordinators[slotState.Bidder]; ok &&
  159. coord.Forger == forger && slotState.BidAmount.Cmp(minBid) >= 0 {
  160. // if forger bidAmount has exceeded the minBid it can forge
  161. return true, nil
  162. } else if a.Vars.BootCoordinator == forger && slotState.BidAmount.Cmp(minBid) == -1 {
  163. // if it's the boot coordinator and it has not been bid or the bid is below the minimum it can forge
  164. return true, nil
  165. } else {
  166. return false, nil
  167. }
  168. }
  169. // EthereumBlock stores all the generic data related to the an ethereum block
  170. type EthereumBlock struct {
  171. BlockNum int64
  172. Time int64
  173. Hash ethCommon.Hash
  174. ParentHash ethCommon.Hash
  175. Tokens map[ethCommon.Address]eth.ERC20Consts
  176. // state ethState
  177. }
  178. // Block represents a ethereum block
  179. type Block struct {
  180. Rollup *RollupBlock
  181. Auction *AuctionBlock
  182. WDelayer *WDelayerBlock
  183. Eth *EthereumBlock
  184. }
  185. func (b *Block) copy() *Block {
  186. bCopyRaw, err := copystructure.Copy(b)
  187. if err != nil {
  188. panic(err)
  189. }
  190. bCopy := bCopyRaw.(*Block)
  191. return bCopy
  192. }
  193. // Next prepares the successive block.
  194. func (b *Block) Next() *Block {
  195. blockNext := b.copy()
  196. blockNext.Rollup.Events = eth.NewRollupEvents()
  197. blockNext.Auction.Events = eth.NewAuctionEvents()
  198. blockNext.Eth.BlockNum = b.Eth.BlockNum + 1
  199. blockNext.Eth.ParentHash = b.Eth.Hash
  200. blockNext.Rollup.Constants = b.Rollup.Constants
  201. blockNext.Auction.Constants = b.Auction.Constants
  202. blockNext.WDelayer.Constants = b.WDelayer.Constants
  203. blockNext.Rollup.Eth = blockNext.Eth
  204. blockNext.Auction.Eth = blockNext.Eth
  205. blockNext.WDelayer.Eth = blockNext.Eth
  206. return blockNext
  207. }
  208. // ClientSetup is used to initialize the constants of the Smart Contracts and
  209. // other details of the test Client
  210. type ClientSetup struct {
  211. RollupConstants *common.RollupConstants
  212. RollupVariables *common.RollupVariables
  213. AuctionConstants *common.AuctionConstants
  214. AuctionVariables *common.AuctionVariables
  215. WDelayerConstants *common.WDelayerConstants
  216. WDelayerVariables *common.WDelayerVariables
  217. VerifyProof bool
  218. }
  219. // NewClientSetupExample returns a ClientSetup example with hardcoded realistic
  220. // values. With this setup, the rollup genesis will be block 1, and block 0
  221. // and 1 will be premined.
  222. //nolint:gomnd
  223. func NewClientSetupExample() *ClientSetup {
  224. // rfield, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  225. // if !ok {
  226. // panic("bad rfield")
  227. // }
  228. initialMinimalBidding, ok := new(big.Int).SetString("10000000000000000000", 10) // 10 * (1e18)
  229. if !ok {
  230. panic("bad initialMinimalBidding")
  231. }
  232. tokenHEZ := ethCommon.HexToAddress("0x51D243D62852Bba334DD5cc33f242BAc8c698074")
  233. governanceAddress := ethCommon.HexToAddress("0x688EfD95BA4391f93717CF02A9aED9DBD2855cDd")
  234. rollupConstants := &common.RollupConstants{
  235. Verifiers: []common.RollupVerifierStruct{
  236. {
  237. MaxTx: 2048,
  238. NLevels: 32,
  239. },
  240. },
  241. TokenHEZ: tokenHEZ,
  242. HermezGovernanceDAOAddress: governanceAddress,
  243. SafetyAddress: ethCommon.HexToAddress("0x84d8B79E84fe87B14ad61A554e740f6736bF4c20"),
  244. HermezAuctionContract: ethCommon.HexToAddress("0x8E442975805fb1908f43050c9C1A522cB0e28D7b"),
  245. WithdrawDelayerContract: ethCommon.HexToAddress("0x5CB7979cBdbf65719BEE92e4D15b7b7Ed3D79114"),
  246. }
  247. rollupVariables := &common.RollupVariables{
  248. FeeAddToken: big.NewInt(11),
  249. ForgeL1L2BatchTimeout: 9,
  250. WithdrawalDelay: 80,
  251. }
  252. auctionConstants := &common.AuctionConstants{
  253. BlocksPerSlot: 40,
  254. InitialMinimalBidding: initialMinimalBidding,
  255. GenesisBlockNum: 1,
  256. GovernanceAddress: governanceAddress,
  257. TokenHEZ: tokenHEZ,
  258. HermezRollup: ethCommon.HexToAddress("0x474B6e29852257491cf283EfB1A9C61eBFe48369"),
  259. }
  260. auctionVariables := &common.AuctionVariables{
  261. DonationAddress: ethCommon.HexToAddress("0x61Ed87CF0A1496b49A420DA6D84B58196b98f2e7"),
  262. BootCoordinator: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
  263. DefaultSlotSetBid: [6]*big.Int{
  264. big.NewInt(1000), big.NewInt(1100), big.NewInt(1200),
  265. big.NewInt(1300), big.NewInt(1400), big.NewInt(1500)},
  266. ClosedAuctionSlots: 2,
  267. OpenAuctionSlots: 4320,
  268. AllocationRatio: [3]uint16{4000, 4000, 2000},
  269. Outbidding: 1000,
  270. SlotDeadline: 20,
  271. }
  272. wDelayerConstants := &common.WDelayerConstants{
  273. MaxWithdrawalDelay: 60 * 60 * 24 * 7 * 2, // 2 weeks
  274. MaxEmergencyModeTime: 60 * 60 * 24 * 7 * 26, // 26 weeks
  275. HermezRollup: auctionConstants.HermezRollup,
  276. }
  277. wDelayerVariables := &common.WDelayerVariables{
  278. HermezGovernanceDAOAddress: ethCommon.HexToAddress("0xcfD0d163AE6432a72682323E2C3A5a69e6B37D12"),
  279. WhiteHackGroupAddress: ethCommon.HexToAddress("0x2730700932a4FDB97B9268A3Ca29f97Ea5fd7EA0"),
  280. HermezKeeperAddress: ethCommon.HexToAddress("0x92aAD86176dC0f0046FE85Ed5dA008a828bE3868"),
  281. WithdrawalDelay: 60,
  282. EmergencyModeStartingTime: 0,
  283. EmergencyMode: false,
  284. }
  285. return &ClientSetup{
  286. RollupConstants: rollupConstants,
  287. RollupVariables: rollupVariables,
  288. AuctionConstants: auctionConstants,
  289. AuctionVariables: auctionVariables,
  290. WDelayerConstants: wDelayerConstants,
  291. WDelayerVariables: wDelayerVariables,
  292. }
  293. }
  294. // Timer is an interface to simulate a source of time, useful to advance time
  295. // virtually.
  296. type Timer interface {
  297. Time() int64
  298. }
  299. // type forgeBatchArgs struct {
  300. // ethTx *types.Transaction
  301. // blockNum int64
  302. // blockHash ethCommon.Hash
  303. // }
  304. type batch struct {
  305. ForgeBatchArgs eth.RollupForgeBatchArgs
  306. Sender ethCommon.Address
  307. }
  308. // Client implements the eth.ClientInterface interface, allowing to manipulate the
  309. // values for testing, working with deterministic results.
  310. type Client struct {
  311. rw *sync.RWMutex
  312. log bool
  313. addr *ethCommon.Address
  314. rollupConstants *common.RollupConstants
  315. auctionConstants *common.AuctionConstants
  316. wDelayerConstants *common.WDelayerConstants
  317. blocks map[int64]*Block
  318. // state state
  319. blockNum int64 // last mined block num
  320. maxBlockNum int64 // highest block num calculated
  321. timer Timer
  322. hasher hasher
  323. forgeBatchArgsPending map[ethCommon.Hash]*batch
  324. forgeBatchArgs map[ethCommon.Hash]*batch
  325. }
  326. // NewClient returns a new test Client that implements the eth.IClient
  327. // interface, at the given initialBlockNumber.
  328. func NewClient(l bool, timer Timer, addr *ethCommon.Address, setup *ClientSetup) *Client {
  329. blocks := make(map[int64]*Block)
  330. blockNum := int64(0)
  331. hasher := hasher{}
  332. // Add ethereum genesis block
  333. mapL1TxQueue := make(map[int64]*eth.QueueStruct)
  334. mapL1TxQueue[0] = eth.NewQueueStruct()
  335. mapL1TxQueue[1] = eth.NewQueueStruct()
  336. blockCurrent := &Block{
  337. Rollup: &RollupBlock{
  338. State: eth.RollupState{
  339. StateRoot: big.NewInt(0),
  340. ExitRoots: make([]*big.Int, 1),
  341. ExitNullifierMap: make(map[int64]map[int64]bool),
  342. // TokenID = 0 is ETH. Set first entry in TokenList with 0x0 address for ETH.
  343. TokenList: []ethCommon.Address{{}},
  344. TokenMap: make(map[ethCommon.Address]bool),
  345. MapL1TxQueue: mapL1TxQueue,
  346. LastL1L2Batch: 0,
  347. CurrentToForgeL1TxsNum: 0,
  348. LastToForgeL1TxsNum: 1,
  349. CurrentIdx: 0,
  350. },
  351. Vars: *setup.RollupVariables,
  352. Txs: make(map[ethCommon.Hash]*types.Transaction),
  353. Events: eth.NewRollupEvents(),
  354. Constants: setup.RollupConstants,
  355. },
  356. Auction: &AuctionBlock{
  357. State: eth.AuctionState{
  358. Slots: make(map[int64]*eth.SlotState),
  359. PendingBalances: make(map[ethCommon.Address]*big.Int),
  360. Coordinators: make(map[ethCommon.Address]*eth.Coordinator),
  361. },
  362. Vars: *setup.AuctionVariables,
  363. Txs: make(map[ethCommon.Hash]*types.Transaction),
  364. Events: eth.NewAuctionEvents(),
  365. Constants: setup.AuctionConstants,
  366. },
  367. WDelayer: &WDelayerBlock{
  368. // State: TODO
  369. Vars: *setup.WDelayerVariables,
  370. Txs: make(map[ethCommon.Hash]*types.Transaction),
  371. Events: eth.NewWDelayerEvents(),
  372. Constants: setup.WDelayerConstants,
  373. },
  374. Eth: &EthereumBlock{
  375. BlockNum: blockNum,
  376. Time: timer.Time(),
  377. Hash: hasher.Next(),
  378. ParentHash: ethCommon.Hash{},
  379. Tokens: make(map[ethCommon.Address]eth.ERC20Consts),
  380. },
  381. }
  382. blockCurrent.Rollup.Eth = blockCurrent.Eth
  383. blockCurrent.Auction.Eth = blockCurrent.Eth
  384. blocks[blockNum] = blockCurrent
  385. blockNext := blockCurrent.Next()
  386. blocks[blockNum+1] = blockNext
  387. c := Client{
  388. rw: &sync.RWMutex{},
  389. log: l,
  390. addr: addr,
  391. rollupConstants: setup.RollupConstants,
  392. auctionConstants: setup.AuctionConstants,
  393. wDelayerConstants: setup.WDelayerConstants,
  394. blocks: blocks,
  395. timer: timer,
  396. hasher: hasher,
  397. forgeBatchArgsPending: make(map[ethCommon.Hash]*batch),
  398. forgeBatchArgs: make(map[ethCommon.Hash]*batch),
  399. blockNum: blockNum,
  400. maxBlockNum: blockNum,
  401. }
  402. for i := int64(1); i < setup.AuctionConstants.GenesisBlockNum+1; i++ {
  403. c.CtlMineBlock()
  404. }
  405. return &c
  406. }
  407. //
  408. // Mock Control
  409. //
  410. func (c *Client) setNextBlock(block *Block) {
  411. c.blocks[c.blockNum+1] = block
  412. }
  413. func (c *Client) revertIfErr(err error, block *Block) {
  414. if err != nil {
  415. log.Infow("TestClient revert", "block", block.Eth.BlockNum, "err", err)
  416. c.setNextBlock(block)
  417. }
  418. }
  419. // Debugf calls log.Debugf if c.log is true
  420. func (c *Client) Debugf(template string, args ...interface{}) {
  421. if c.log {
  422. log.Debugf(template, args...)
  423. }
  424. }
  425. // Debugw calls log.Debugw if c.log is true
  426. func (c *Client) Debugw(template string, kv ...interface{}) {
  427. if c.log {
  428. log.Debugw(template, kv...)
  429. }
  430. }
  431. type hasher struct {
  432. counter uint64
  433. }
  434. // Next returns the next hash
  435. func (h *hasher) Next() ethCommon.Hash {
  436. var hash ethCommon.Hash
  437. binary.LittleEndian.PutUint64(hash[:], h.counter)
  438. h.counter++
  439. return hash
  440. }
  441. func (c *Client) nextBlock() *Block {
  442. return c.blocks[c.blockNum+1]
  443. }
  444. func (c *Client) currentBlock() *Block {
  445. return c.blocks[c.blockNum]
  446. }
  447. // CtlSetAddr sets the address of the client
  448. func (c *Client) CtlSetAddr(addr ethCommon.Address) {
  449. c.addr = &addr
  450. }
  451. // CtlMineBlock moves one block forward
  452. func (c *Client) CtlMineBlock() {
  453. c.rw.Lock()
  454. defer c.rw.Unlock()
  455. blockCurrent := c.nextBlock()
  456. c.blockNum++
  457. c.maxBlockNum = c.blockNum
  458. blockCurrent.Eth.Time = c.timer.Time()
  459. blockCurrent.Eth.Hash = c.hasher.Next()
  460. for ethTxHash, forgeBatchArgs := range c.forgeBatchArgsPending {
  461. c.forgeBatchArgs[ethTxHash] = forgeBatchArgs
  462. }
  463. c.forgeBatchArgsPending = make(map[ethCommon.Hash]*batch)
  464. blockNext := blockCurrent.Next()
  465. c.blocks[c.blockNum+1] = blockNext
  466. c.Debugw("TestClient mined block", "blockNum", c.blockNum)
  467. }
  468. // CtlRollback discards the last mined block. Use this to replace a mined
  469. // block to simulate reorgs.
  470. func (c *Client) CtlRollback() {
  471. c.rw.Lock()
  472. defer c.rw.Unlock()
  473. if c.blockNum == 0 {
  474. panic("Can't rollback at blockNum = 0")
  475. }
  476. delete(c.blocks, c.blockNum+1) // delete next block
  477. delete(c.blocks, c.blockNum) // delete current block
  478. c.blockNum--
  479. blockCurrent := c.blocks[c.blockNum]
  480. blockNext := blockCurrent.Next()
  481. c.blocks[c.blockNum+1] = blockNext
  482. }
  483. //
  484. // Ethereum
  485. //
  486. // EthCurrentBlock returns the current blockNum
  487. func (c *Client) EthCurrentBlock() (int64, error) {
  488. c.rw.RLock()
  489. defer c.rw.RUnlock()
  490. if c.blockNum < c.maxBlockNum {
  491. panic("blockNum has decreased. " +
  492. "After a rollback you must mine to reach the same or higher blockNum")
  493. }
  494. return c.blockNum, nil
  495. }
  496. // EthTransactionReceipt returns the transaction receipt of the given txHash
  497. func (c *Client) EthTransactionReceipt(ctx context.Context, txHash ethCommon.Hash) (*types.Receipt, error) {
  498. c.rw.RLock()
  499. defer c.rw.RUnlock()
  500. for i := int64(0); i < c.blockNum; i++ {
  501. b := c.blocks[i]
  502. _, ok := b.Rollup.Txs[txHash]
  503. if !ok {
  504. _, ok = b.Auction.Txs[txHash]
  505. }
  506. if ok {
  507. return &types.Receipt{
  508. TxHash: txHash,
  509. Status: types.ReceiptStatusSuccessful,
  510. BlockHash: b.Eth.Hash,
  511. BlockNumber: big.NewInt(b.Eth.BlockNum),
  512. }, nil
  513. }
  514. }
  515. return nil, nil
  516. }
  517. // CtlAddERC20 adds an ERC20 token to the blockchain.
  518. func (c *Client) CtlAddERC20(tokenAddr ethCommon.Address, constants eth.ERC20Consts) {
  519. nextBlock := c.nextBlock()
  520. e := nextBlock.Eth
  521. e.Tokens[tokenAddr] = constants
  522. }
  523. // EthERC20Consts returns the constants defined for a particular ERC20 Token instance.
  524. func (c *Client) EthERC20Consts(tokenAddr ethCommon.Address) (*eth.ERC20Consts, error) {
  525. currentBlock := c.currentBlock()
  526. e := currentBlock.Eth
  527. if constants, ok := e.Tokens[tokenAddr]; ok {
  528. return &constants, nil
  529. }
  530. return nil, fmt.Errorf("tokenAddr not found")
  531. }
  532. // func newHeader(number *big.Int) *types.Header {
  533. // return &types.Header{
  534. // Number: number,
  535. // Time: uint64(number.Int64()),
  536. // }
  537. // }
  538. // EthHeaderByNumber returns the *types.Header for the given block number in a
  539. // deterministic way.
  540. // func (c *Client) EthHeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
  541. // return newHeader(number), nil
  542. // }
  543. // EthBlockByNumber returns the *common.Block for the given block number in a
  544. // deterministic way.
  545. func (c *Client) EthBlockByNumber(ctx context.Context, blockNum int64) (*common.Block, error) {
  546. c.rw.RLock()
  547. defer c.rw.RUnlock()
  548. if blockNum > c.blockNum {
  549. return nil, ethereum.NotFound
  550. }
  551. block := c.blocks[blockNum]
  552. return &common.Block{
  553. EthBlockNum: blockNum,
  554. Timestamp: time.Unix(block.Eth.Time, 0),
  555. Hash: block.Eth.Hash,
  556. ParentHash: block.Eth.ParentHash,
  557. }, nil
  558. }
  559. // EthAddress returns the ethereum address of the account loaded into the Client
  560. func (c *Client) EthAddress() (*ethCommon.Address, error) {
  561. if c.addr == nil {
  562. return nil, eth.ErrAccountNil
  563. }
  564. return c.addr, nil
  565. }
  566. var errTODO = fmt.Errorf("TODO: Not implemented yet")
  567. //
  568. // Rollup
  569. //
  570. // CtlAddL1TxUser adds an L1TxUser to the L1UserTxs queue of the Rollup
  571. // func (c *Client) CtlAddL1TxUser(l1Tx *common.L1Tx) {
  572. // c.rw.Lock()
  573. // defer c.rw.Unlock()
  574. //
  575. // nextBlock := c.nextBlock()
  576. // r := nextBlock.Rollup
  577. // queue := r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum]
  578. // if len(queue.L1TxQueue) >= eth.RollupConstMaxL1UserTx {
  579. // r.State.LastToForgeL1TxsNum++
  580. // r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum] = eth.NewQueueStruct()
  581. // queue = r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum]
  582. // }
  583. // if int64(l1Tx.FromIdx) > r.State.CurrentIdx {
  584. // panic("l1Tx.FromIdx > r.State.CurrentIdx")
  585. // }
  586. // if int(l1Tx.TokenID)+1 > len(r.State.TokenList) {
  587. // panic("l1Tx.TokenID + 1 > len(r.State.TokenList)")
  588. // }
  589. // queue.L1TxQueue = append(queue.L1TxQueue, *l1Tx)
  590. // r.Events.L1UserTx = append(r.Events.L1UserTx, eth.RollupEventL1UserTx{
  591. // L1Tx: *l1Tx,
  592. // ToForgeL1TxsNum: r.State.LastToForgeL1TxsNum,
  593. // Position: len(queue.L1TxQueue) - 1,
  594. // })
  595. // }
  596. // RollupL1UserTxERC20Permit is the interface to call the smart contract function
  597. func (c *Client) RollupL1UserTxERC20Permit(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64, deadline *big.Int) (tx *types.Transaction, err error) {
  598. log.Error("TODO")
  599. return nil, errTODO
  600. }
  601. // RollupL1UserTxERC20ETH sends an L1UserTx to the Rollup.
  602. func (c *Client) RollupL1UserTxERC20ETH(
  603. fromBJJ *babyjub.PublicKey,
  604. fromIdx int64,
  605. loadAmount *big.Int,
  606. amount *big.Int,
  607. tokenID uint32,
  608. toIdx int64,
  609. ) (tx *types.Transaction, err error) {
  610. c.rw.Lock()
  611. defer c.rw.Unlock()
  612. cpy := c.nextBlock().copy()
  613. defer func() { c.revertIfErr(err, cpy) }()
  614. _, err = common.NewFloat16(amount)
  615. if err != nil {
  616. return nil, err
  617. }
  618. _, err = common.NewFloat16(loadAmount)
  619. if err != nil {
  620. return nil, err
  621. }
  622. nextBlock := c.nextBlock()
  623. r := nextBlock.Rollup
  624. queue := r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum]
  625. if len(queue.L1TxQueue) >= common.RollupConstMaxL1UserTx {
  626. r.State.LastToForgeL1TxsNum++
  627. r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum] = eth.NewQueueStruct()
  628. queue = r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum]
  629. }
  630. if fromIdx > r.State.CurrentIdx {
  631. panic("l1Tx.FromIdx > r.State.CurrentIdx")
  632. }
  633. if int(tokenID)+1 > len(r.State.TokenList) {
  634. panic("l1Tx.TokenID + 1 > len(r.State.TokenList)")
  635. }
  636. toForgeL1TxsNum := r.State.LastToForgeL1TxsNum
  637. l1Tx, err := common.NewL1Tx(&common.L1Tx{
  638. FromIdx: common.Idx(fromIdx),
  639. FromEthAddr: *c.addr,
  640. FromBJJ: fromBJJ,
  641. Amount: amount,
  642. LoadAmount: loadAmount,
  643. TokenID: common.TokenID(tokenID),
  644. ToIdx: common.Idx(toIdx),
  645. ToForgeL1TxsNum: &toForgeL1TxsNum,
  646. Position: len(queue.L1TxQueue),
  647. UserOrigin: true,
  648. })
  649. if err != nil {
  650. return nil, err
  651. }
  652. queue.L1TxQueue = append(queue.L1TxQueue, *l1Tx)
  653. r.Events.L1UserTx = append(r.Events.L1UserTx, eth.RollupEventL1UserTx{
  654. L1UserTx: *l1Tx,
  655. })
  656. return r.addTransaction(newTransaction("l1UserTxERC20ETH", l1Tx)), nil
  657. }
  658. // RollupL1UserTxERC777 is the interface to call the smart contract function
  659. // func (c *Client) RollupL1UserTxERC777(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error) {
  660. // log.Error("TODO")
  661. // return nil, errTODO
  662. // }
  663. // RollupRegisterTokensCount is the interface to call the smart contract function
  664. func (c *Client) RollupRegisterTokensCount() (*big.Int, error) {
  665. log.Error("TODO")
  666. return nil, errTODO
  667. }
  668. // RollupWithdrawCircuit is the interface to call the smart contract function
  669. func (c *Client) RollupWithdrawCircuit(proofA, proofC [2]*big.Int, proofB [2][2]*big.Int, tokenID uint32, numExitRoot, idx int64, amount *big.Int, instantWithdraw bool) (*types.Transaction, error) {
  670. log.Error("TODO")
  671. return nil, errTODO
  672. }
  673. // RollupWithdrawMerkleProof is the interface to call the smart contract function
  674. func (c *Client) RollupWithdrawMerkleProof(babyPubKey *babyjub.PublicKey, tokenID uint32, numExitRoot, idx int64, amount *big.Int, siblings []*big.Int, instantWithdraw bool) (tx *types.Transaction, err error) {
  675. c.rw.Lock()
  676. defer c.rw.Unlock()
  677. cpy := c.nextBlock().copy()
  678. defer func() { c.revertIfErr(err, cpy) }()
  679. nextBlock := c.nextBlock()
  680. r := nextBlock.Rollup
  681. if int(numExitRoot) >= len(r.State.ExitRoots) {
  682. return nil, fmt.Errorf("numExitRoot >= len(r.State.ExitRoots)")
  683. }
  684. if _, ok := r.State.ExitNullifierMap[numExitRoot][idx]; ok {
  685. return nil, fmt.Errorf("exit already withdrawn")
  686. }
  687. r.State.ExitNullifierMap[numExitRoot][idx] = true
  688. r.Events.Withdraw = append(r.Events.Withdraw, eth.RollupEventWithdraw{
  689. Idx: uint64(idx),
  690. NumExitRoot: uint64(numExitRoot),
  691. InstantWithdraw: instantWithdraw,
  692. })
  693. type data struct {
  694. babyPubKey *babyjub.PublicKey
  695. tokenID uint32
  696. numExitRoot int64
  697. idx int64
  698. amount *big.Int
  699. siblings []*big.Int
  700. instantWithdraw bool
  701. }
  702. return r.addTransaction(newTransaction("withdrawMerkleProof", data{
  703. babyPubKey: babyPubKey,
  704. tokenID: tokenID,
  705. numExitRoot: numExitRoot,
  706. idx: idx,
  707. amount: amount,
  708. siblings: siblings,
  709. instantWithdraw: instantWithdraw,
  710. })), nil
  711. }
  712. type transactionData struct {
  713. Name string
  714. Value interface{}
  715. }
  716. func newTransaction(name string, value interface{}) *types.Transaction {
  717. data, err := json.Marshal(transactionData{name, value})
  718. if err != nil {
  719. panic(err)
  720. }
  721. return types.NewTransaction(0, ethCommon.Address{}, nil, 0, nil,
  722. data)
  723. }
  724. // RollupForgeBatch is the interface to call the smart contract function
  725. func (c *Client) RollupForgeBatch(args *eth.RollupForgeBatchArgs) (tx *types.Transaction, err error) {
  726. c.rw.Lock()
  727. defer c.rw.Unlock()
  728. cpy := c.nextBlock().copy()
  729. defer func() { c.revertIfErr(err, cpy) }()
  730. if c.addr == nil {
  731. return nil, eth.ErrAccountNil
  732. }
  733. a := c.nextBlock().Auction
  734. ok, err := a.canForge(*c.addr, a.Eth.BlockNum)
  735. if err != nil {
  736. return nil, err
  737. }
  738. if !ok {
  739. return nil, fmt.Errorf("incorrect slot")
  740. }
  741. // TODO: Verify proof
  742. // Auction
  743. err = a.forge(*c.addr)
  744. if err != nil {
  745. return nil, err
  746. }
  747. // TODO: If successful, store the tx in a successful array.
  748. // TODO: If failed, store the tx in a failed array.
  749. // TODO: Add method to move the tx to another block, reapply it there, and possibly go from successful to failed.
  750. return c.addBatch(args)
  751. }
  752. // CtlAddBatch adds forged batch to the Rollup, without checking any ZKProof
  753. func (c *Client) CtlAddBatch(args *eth.RollupForgeBatchArgs) {
  754. c.rw.Lock()
  755. defer c.rw.Unlock()
  756. if _, err := c.addBatch(args); err != nil {
  757. panic(err)
  758. }
  759. }
  760. func (c *Client) addBatch(args *eth.RollupForgeBatchArgs) (*types.Transaction, error) {
  761. nextBlock := c.nextBlock()
  762. r := nextBlock.Rollup
  763. r.State.StateRoot = args.NewStRoot
  764. if args.NewLastIdx < r.State.CurrentIdx {
  765. return nil, fmt.Errorf("args.NewLastIdx < r.State.CurrentIdx")
  766. }
  767. r.State.CurrentIdx = args.NewLastIdx
  768. r.State.ExitNullifierMap[int64(len(r.State.ExitRoots))] = make(map[int64]bool)
  769. r.State.ExitRoots = append(r.State.ExitRoots, args.NewExitRoot)
  770. if args.L1Batch {
  771. r.State.CurrentToForgeL1TxsNum++
  772. if r.State.CurrentToForgeL1TxsNum == r.State.LastToForgeL1TxsNum {
  773. r.State.LastToForgeL1TxsNum++
  774. r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum] = eth.NewQueueStruct()
  775. }
  776. }
  777. ethTx := r.addTransaction(newTransaction("forgebatch", args))
  778. c.forgeBatchArgsPending[ethTx.Hash()] = &batch{*args, *c.addr}
  779. r.Events.ForgeBatch = append(r.Events.ForgeBatch, eth.RollupEventForgeBatch{
  780. BatchNum: int64(len(r.State.ExitRoots)) - 1,
  781. EthTxHash: ethTx.Hash(),
  782. })
  783. return ethTx, nil
  784. }
  785. // RollupAddTokenSimple is a wrapper around RollupAddToken that automatically
  786. // sets `deadlie`.
  787. func (c *Client) RollupAddTokenSimple(tokenAddress ethCommon.Address, feeAddToken *big.Int) (tx *types.Transaction, err error) {
  788. return c.RollupAddToken(tokenAddress, feeAddToken, big.NewInt(9999)) //nolint:gomnd
  789. }
  790. // RollupAddToken is the interface to call the smart contract function
  791. func (c *Client) RollupAddToken(tokenAddress ethCommon.Address, feeAddToken *big.Int,
  792. deadline *big.Int) (tx *types.Transaction, err error) {
  793. c.rw.Lock()
  794. defer c.rw.Unlock()
  795. cpy := c.nextBlock().copy()
  796. defer func() { c.revertIfErr(err, cpy) }()
  797. if c.addr == nil {
  798. return nil, eth.ErrAccountNil
  799. }
  800. nextBlock := c.nextBlock()
  801. r := nextBlock.Rollup
  802. if _, ok := r.State.TokenMap[tokenAddress]; ok {
  803. return nil, fmt.Errorf("Token %v already registered", tokenAddress)
  804. }
  805. if feeAddToken.Cmp(r.Vars.FeeAddToken) != 0 {
  806. return nil, fmt.Errorf("Expected fee: %v but got: %v", r.Vars.FeeAddToken, feeAddToken)
  807. }
  808. r.State.TokenMap[tokenAddress] = true
  809. r.State.TokenList = append(r.State.TokenList, tokenAddress)
  810. r.Events.AddToken = append(r.Events.AddToken, eth.RollupEventAddToken{TokenAddress: tokenAddress,
  811. TokenID: uint32(len(r.State.TokenList) - 1)})
  812. return r.addTransaction(newTransaction("addtoken", tokenAddress)), nil
  813. }
  814. // RollupGetCurrentTokens is the interface to call the smart contract function
  815. func (c *Client) RollupGetCurrentTokens() (*big.Int, error) {
  816. c.rw.RLock()
  817. defer c.rw.RUnlock()
  818. log.Error("TODO")
  819. return nil, errTODO
  820. }
  821. // RollupUpdateForgeL1L2BatchTimeout is the interface to call the smart contract function
  822. func (c *Client) RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (tx *types.Transaction, err error) {
  823. c.rw.Lock()
  824. defer c.rw.Unlock()
  825. cpy := c.nextBlock().copy()
  826. defer func() { c.revertIfErr(err, cpy) }()
  827. if c.addr == nil {
  828. return nil, eth.ErrAccountNil
  829. }
  830. nextBlock := c.nextBlock()
  831. r := nextBlock.Rollup
  832. r.Vars.ForgeL1L2BatchTimeout = newForgeL1Timeout
  833. r.Events.UpdateForgeL1L2BatchTimeout = append(r.Events.UpdateForgeL1L2BatchTimeout,
  834. eth.RollupEventUpdateForgeL1L2BatchTimeout{NewForgeL1L2BatchTimeout: newForgeL1Timeout})
  835. return r.addTransaction(newTransaction("updateForgeL1L2BatchTimeout", newForgeL1Timeout)), nil
  836. }
  837. // RollupUpdateFeeAddToken is the interface to call the smart contract function
  838. func (c *Client) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (tx *types.Transaction, err error) {
  839. c.rw.Lock()
  840. defer c.rw.Unlock()
  841. cpy := c.nextBlock().copy()
  842. defer func() { c.revertIfErr(err, cpy) }()
  843. if c.addr == nil {
  844. return nil, eth.ErrAccountNil
  845. }
  846. log.Error("TODO")
  847. return nil, errTODO
  848. }
  849. // RollupUpdateTokensHEZ is the interface to call the smart contract function
  850. // func (c *Client) RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (tx *types.Transaction, err error) {
  851. // c.rw.Lock()
  852. // defer c.rw.Unlock()
  853. // cpy := c.nextBlock().copy()
  854. // defer func() { c.revertIfErr(err, cpy) }()
  855. //
  856. // log.Error("TODO")
  857. // return nil, errTODO
  858. // }
  859. // RollupUpdateGovernance is the interface to call the smart contract function
  860. // func (c *Client) RollupUpdateGovernance() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  861. // return nil, errTODO
  862. // }
  863. // RollupConstants returns the Constants of the Rollup Smart Contract
  864. func (c *Client) RollupConstants() (*common.RollupConstants, error) {
  865. c.rw.RLock()
  866. defer c.rw.RUnlock()
  867. return c.rollupConstants, nil
  868. }
  869. // RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
  870. func (c *Client) RollupEventsByBlock(blockNum int64) (*eth.RollupEvents, *ethCommon.Hash, error) {
  871. c.rw.RLock()
  872. defer c.rw.RUnlock()
  873. block, ok := c.blocks[blockNum]
  874. if !ok {
  875. return nil, nil, fmt.Errorf("Block %v doesn't exist", blockNum)
  876. }
  877. return &block.Rollup.Events, &block.Eth.Hash, nil
  878. }
  879. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
  880. func (c *Client) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*eth.RollupForgeBatchArgs, *ethCommon.Address, error) {
  881. c.rw.RLock()
  882. defer c.rw.RUnlock()
  883. batch, ok := c.forgeBatchArgs[ethTxHash]
  884. if !ok {
  885. return nil, nil, fmt.Errorf("transaction not found")
  886. }
  887. return &batch.ForgeBatchArgs, &batch.Sender, nil
  888. }
  889. //
  890. // Auction
  891. //
  892. // AuctionSetSlotDeadline is the interface to call the smart contract function
  893. func (c *Client) AuctionSetSlotDeadline(newDeadline uint8) (tx *types.Transaction, err error) {
  894. c.rw.Lock()
  895. defer c.rw.Unlock()
  896. cpy := c.nextBlock().copy()
  897. defer func() { c.revertIfErr(err, cpy) }()
  898. if c.addr == nil {
  899. return nil, eth.ErrAccountNil
  900. }
  901. log.Error("TODO")
  902. return nil, errTODO
  903. }
  904. // AuctionGetSlotDeadline is the interface to call the smart contract function
  905. func (c *Client) AuctionGetSlotDeadline() (uint8, error) {
  906. c.rw.RLock()
  907. defer c.rw.RUnlock()
  908. log.Error("TODO")
  909. return 0, errTODO
  910. }
  911. // AuctionSetOpenAuctionSlots is the interface to call the smart contract function
  912. func (c *Client) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (tx *types.Transaction, err error) {
  913. c.rw.Lock()
  914. defer c.rw.Unlock()
  915. cpy := c.nextBlock().copy()
  916. defer func() { c.revertIfErr(err, cpy) }()
  917. if c.addr == nil {
  918. return nil, eth.ErrAccountNil
  919. }
  920. nextBlock := c.nextBlock()
  921. a := nextBlock.Auction
  922. a.Vars.OpenAuctionSlots = newOpenAuctionSlots
  923. a.Events.NewOpenAuctionSlots = append(a.Events.NewOpenAuctionSlots,
  924. eth.AuctionEventNewOpenAuctionSlots{NewOpenAuctionSlots: newOpenAuctionSlots})
  925. return a.addTransaction(newTransaction("setOpenAuctionSlots", newOpenAuctionSlots)), nil
  926. }
  927. // AuctionGetOpenAuctionSlots is the interface to call the smart contract function
  928. func (c *Client) AuctionGetOpenAuctionSlots() (uint16, error) {
  929. c.rw.RLock()
  930. defer c.rw.RUnlock()
  931. log.Error("TODO")
  932. return 0, errTODO
  933. }
  934. // AuctionSetClosedAuctionSlots is the interface to call the smart contract function
  935. func (c *Client) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (tx *types.Transaction, err error) {
  936. c.rw.Lock()
  937. defer c.rw.Unlock()
  938. cpy := c.nextBlock().copy()
  939. defer func() { c.revertIfErr(err, cpy) }()
  940. if c.addr == nil {
  941. return nil, eth.ErrAccountNil
  942. }
  943. log.Error("TODO")
  944. return nil, errTODO
  945. }
  946. // AuctionGetClosedAuctionSlots is the interface to call the smart contract function
  947. func (c *Client) AuctionGetClosedAuctionSlots() (uint16, error) {
  948. c.rw.RLock()
  949. defer c.rw.RUnlock()
  950. log.Error("TODO")
  951. return 0, errTODO
  952. }
  953. // AuctionSetOutbidding is the interface to call the smart contract function
  954. func (c *Client) AuctionSetOutbidding(newOutbidding uint16) (tx *types.Transaction, err error) {
  955. c.rw.Lock()
  956. defer c.rw.Unlock()
  957. cpy := c.nextBlock().copy()
  958. defer func() { c.revertIfErr(err, cpy) }()
  959. if c.addr == nil {
  960. return nil, eth.ErrAccountNil
  961. }
  962. log.Error("TODO")
  963. return nil, errTODO
  964. }
  965. // AuctionGetOutbidding is the interface to call the smart contract function
  966. func (c *Client) AuctionGetOutbidding() (uint16, error) {
  967. c.rw.RLock()
  968. defer c.rw.RUnlock()
  969. log.Error("TODO")
  970. return 0, errTODO
  971. }
  972. // AuctionSetAllocationRatio is the interface to call the smart contract function
  973. func (c *Client) AuctionSetAllocationRatio(newAllocationRatio [3]uint16) (tx *types.Transaction, err error) {
  974. c.rw.Lock()
  975. defer c.rw.Unlock()
  976. cpy := c.nextBlock().copy()
  977. defer func() { c.revertIfErr(err, cpy) }()
  978. if c.addr == nil {
  979. return nil, eth.ErrAccountNil
  980. }
  981. log.Error("TODO")
  982. return nil, errTODO
  983. }
  984. // AuctionGetAllocationRatio is the interface to call the smart contract function
  985. func (c *Client) AuctionGetAllocationRatio() ([3]uint16, error) {
  986. c.rw.RLock()
  987. defer c.rw.RUnlock()
  988. log.Error("TODO")
  989. return [3]uint16{}, errTODO
  990. }
  991. // AuctionSetDonationAddress is the interface to call the smart contract function
  992. func (c *Client) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (tx *types.Transaction, err error) {
  993. c.rw.Lock()
  994. defer c.rw.Unlock()
  995. cpy := c.nextBlock().copy()
  996. defer func() { c.revertIfErr(err, cpy) }()
  997. if c.addr == nil {
  998. return nil, eth.ErrAccountNil
  999. }
  1000. log.Error("TODO")
  1001. return nil, errTODO
  1002. }
  1003. // AuctionGetDonationAddress is the interface to call the smart contract function
  1004. func (c *Client) AuctionGetDonationAddress() (*ethCommon.Address, error) {
  1005. c.rw.RLock()
  1006. defer c.rw.RUnlock()
  1007. log.Error("TODO")
  1008. return nil, errTODO
  1009. }
  1010. // AuctionSetBootCoordinator is the interface to call the smart contract function
  1011. func (c *Client) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (tx *types.Transaction, err error) {
  1012. c.rw.Lock()
  1013. defer c.rw.Unlock()
  1014. cpy := c.nextBlock().copy()
  1015. defer func() { c.revertIfErr(err, cpy) }()
  1016. if c.addr == nil {
  1017. return nil, eth.ErrAccountNil
  1018. }
  1019. log.Error("TODO")
  1020. return nil, errTODO
  1021. }
  1022. // AuctionGetBootCoordinator is the interface to call the smart contract function
  1023. func (c *Client) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
  1024. c.rw.RLock()
  1025. defer c.rw.RUnlock()
  1026. currentBlock := c.currentBlock()
  1027. a := currentBlock.Auction
  1028. return &a.Vars.BootCoordinator, nil
  1029. }
  1030. // AuctionChangeDefaultSlotSetBid is the interface to call the smart contract function
  1031. func (c *Client) AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (tx *types.Transaction, err error) {
  1032. c.rw.Lock()
  1033. defer c.rw.Unlock()
  1034. cpy := c.nextBlock().copy()
  1035. defer func() { c.revertIfErr(err, cpy) }()
  1036. if c.addr == nil {
  1037. return nil, eth.ErrAccountNil
  1038. }
  1039. log.Error("TODO")
  1040. return nil, errTODO
  1041. }
  1042. // AuctionSetCoordinator is the interface to call the smart contract function
  1043. func (c *Client) AuctionSetCoordinator(forger ethCommon.Address, URL string) (tx *types.Transaction, err error) {
  1044. c.rw.Lock()
  1045. defer c.rw.Unlock()
  1046. cpy := c.nextBlock().copy()
  1047. defer func() { c.revertIfErr(err, cpy) }()
  1048. if c.addr == nil {
  1049. return nil, eth.ErrAccountNil
  1050. }
  1051. nextBlock := c.nextBlock()
  1052. a := nextBlock.Auction
  1053. a.State.Coordinators[*c.addr] = &eth.Coordinator{
  1054. Forger: forger,
  1055. URL: URL,
  1056. }
  1057. a.Events.SetCoordinator = append(a.Events.SetCoordinator,
  1058. eth.AuctionEventSetCoordinator{
  1059. BidderAddress: *c.addr,
  1060. ForgerAddress: forger,
  1061. CoordinatorURL: URL,
  1062. })
  1063. type data struct {
  1064. BidderAddress ethCommon.Address
  1065. ForgerAddress ethCommon.Address
  1066. URL string
  1067. }
  1068. return a.addTransaction(newTransaction("registercoordinator", data{*c.addr, forger, URL})), nil
  1069. }
  1070. // AuctionIsRegisteredCoordinator is the interface to call the smart contract function
  1071. func (c *Client) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
  1072. c.rw.RLock()
  1073. defer c.rw.RUnlock()
  1074. log.Error("TODO")
  1075. return false, errTODO
  1076. }
  1077. // AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
  1078. func (c *Client) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (tx *types.Transaction, err error) {
  1079. c.rw.Lock()
  1080. defer c.rw.Unlock()
  1081. cpy := c.nextBlock().copy()
  1082. defer func() { c.revertIfErr(err, cpy) }()
  1083. if c.addr == nil {
  1084. return nil, eth.ErrAccountNil
  1085. }
  1086. log.Error("TODO")
  1087. return nil, errTODO
  1088. }
  1089. // AuctionGetSlotNumber is the interface to call the smart contract function
  1090. func (c *Client) AuctionGetSlotNumber(blockNum int64) (int64, error) {
  1091. c.rw.RLock()
  1092. defer c.rw.RUnlock()
  1093. currentBlock := c.currentBlock()
  1094. a := currentBlock.Auction
  1095. return a.getSlotNumber(blockNum), nil
  1096. }
  1097. // AuctionGetCurrentSlotNumber is the interface to call the smart contract function
  1098. func (c *Client) AuctionGetCurrentSlotNumber() (int64, error) {
  1099. c.rw.RLock()
  1100. defer c.rw.RUnlock()
  1101. log.Error("TODO")
  1102. return 0, errTODO
  1103. }
  1104. // AuctionGetMinBidBySlot is the interface to call the smart contract function
  1105. func (c *Client) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
  1106. c.rw.RLock()
  1107. defer c.rw.RUnlock()
  1108. log.Error("TODO")
  1109. return nil, errTODO
  1110. }
  1111. // AuctionGetDefaultSlotSetBid is the interface to call the smart contract function
  1112. func (c *Client) AuctionGetDefaultSlotSetBid(slotSet uint8) (*big.Int, error) {
  1113. c.rw.RLock()
  1114. defer c.rw.RUnlock()
  1115. log.Error("TODO")
  1116. return nil, errTODO
  1117. }
  1118. // AuctionGetSlotSet is the interface to call the smart contract function
  1119. func (c *Client) AuctionGetSlotSet(slot int64) (*big.Int, error) {
  1120. c.rw.RLock()
  1121. defer c.rw.RUnlock()
  1122. log.Error("TODO")
  1123. return nil, errTODO
  1124. }
  1125. // AuctionTokensReceived is the interface to call the smart contract function
  1126. // func (c *Client) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
  1127. // return errTODO
  1128. // }
  1129. // AuctionBidSimple is a wrapper around AuctionBid that automatically sets `amount` and `deadline`.
  1130. func (c *Client) AuctionBidSimple(slot int64, bidAmount *big.Int) (tx *types.Transaction, err error) {
  1131. return c.AuctionBid(bidAmount, slot, bidAmount, big.NewInt(99999)) //nolint:gomnd
  1132. }
  1133. // AuctionBid is the interface to call the smart contract function. This
  1134. // implementation behaves as if any address has infinite tokens.
  1135. func (c *Client) AuctionBid(amount *big.Int, slot int64, bidAmount *big.Int,
  1136. deadline *big.Int) (tx *types.Transaction, err error) {
  1137. c.rw.Lock()
  1138. defer c.rw.Unlock()
  1139. cpy := c.nextBlock().copy()
  1140. defer func() { func() { c.revertIfErr(err, cpy) }() }()
  1141. if c.addr == nil {
  1142. return nil, eth.ErrAccountNil
  1143. }
  1144. nextBlock := c.nextBlock()
  1145. a := nextBlock.Auction
  1146. if slot < a.getCurrentSlotNumber()+int64(a.Vars.ClosedAuctionSlots) {
  1147. return nil, errBidClosed
  1148. }
  1149. if slot >= a.getCurrentSlotNumber()+int64(a.Vars.ClosedAuctionSlots)+int64(a.Vars.OpenAuctionSlots) {
  1150. return nil, errBidNotOpen
  1151. }
  1152. minBid, err := a.getMinBidBySlot(slot)
  1153. if err != nil {
  1154. return nil, err
  1155. }
  1156. if bidAmount.Cmp(minBid) == -1 {
  1157. return nil, errBidBelowMin
  1158. }
  1159. if _, ok := a.State.Coordinators[*c.addr]; !ok {
  1160. return nil, errCoordNotReg
  1161. }
  1162. slotState, ok := a.State.Slots[slot]
  1163. if !ok {
  1164. slotState = eth.NewSlotState()
  1165. a.State.Slots[slot] = slotState
  1166. }
  1167. slotState.Bidder = *c.addr
  1168. slotState.BidAmount = bidAmount
  1169. a.Events.NewBid = append(a.Events.NewBid,
  1170. eth.AuctionEventNewBid{Slot: slot, BidAmount: bidAmount, Bidder: *c.addr})
  1171. type data struct {
  1172. Slot int64
  1173. BidAmount *big.Int
  1174. Bidder ethCommon.Address
  1175. }
  1176. return a.addTransaction(newTransaction("bid", data{slot, bidAmount, *c.addr})), nil
  1177. }
  1178. // AuctionMultiBid is the interface to call the smart contract function. This
  1179. // implementation behaves as if any address has infinite tokens.
  1180. func (c *Client) AuctionMultiBid(amount *big.Int, startingSlot int64, endingSlot int64, slotSet [6]bool,
  1181. maxBid, closedMinBid, deadline *big.Int) (tx *types.Transaction, err error) {
  1182. c.rw.Lock()
  1183. defer c.rw.Unlock()
  1184. cpy := c.nextBlock().copy()
  1185. defer func() { c.revertIfErr(err, cpy) }()
  1186. if c.addr == nil {
  1187. return nil, eth.ErrAccountNil
  1188. }
  1189. log.Error("TODO")
  1190. return nil, errTODO
  1191. }
  1192. // AuctionCanForge is the interface to call the smart contract function
  1193. func (c *Client) AuctionCanForge(forger ethCommon.Address, blockNum int64) (bool, error) {
  1194. c.rw.RLock()
  1195. defer c.rw.RUnlock()
  1196. currentBlock := c.currentBlock()
  1197. a := currentBlock.Auction
  1198. return a.canForge(forger, blockNum)
  1199. }
  1200. // AuctionForge is the interface to call the smart contract function
  1201. func (c *Client) AuctionForge(forger ethCommon.Address) (tx *types.Transaction, err error) {
  1202. c.rw.Lock()
  1203. defer c.rw.Unlock()
  1204. cpy := c.nextBlock().copy()
  1205. defer func() { c.revertIfErr(err, cpy) }()
  1206. if c.addr == nil {
  1207. return nil, eth.ErrAccountNil
  1208. }
  1209. log.Error("TODO")
  1210. return nil, errTODO
  1211. }
  1212. // AuctionClaimHEZ is the interface to call the smart contract function
  1213. func (c *Client) AuctionClaimHEZ() (tx *types.Transaction, err error) {
  1214. c.rw.Lock()
  1215. defer c.rw.Unlock()
  1216. cpy := c.nextBlock().copy()
  1217. defer func() { c.revertIfErr(err, cpy) }()
  1218. if c.addr == nil {
  1219. return nil, eth.ErrAccountNil
  1220. }
  1221. log.Error("TODO")
  1222. return nil, errTODO
  1223. }
  1224. // AuctionGetClaimableHEZ is the interface to call the smart contract function
  1225. func (c *Client) AuctionGetClaimableHEZ(bidder ethCommon.Address) (*big.Int, error) {
  1226. c.rw.RLock()
  1227. defer c.rw.RUnlock()
  1228. log.Error("TODO")
  1229. return nil, errTODO
  1230. }
  1231. // AuctionConstants returns the Constants of the Auction Smart Contract
  1232. func (c *Client) AuctionConstants() (*common.AuctionConstants, error) {
  1233. c.rw.RLock()
  1234. defer c.rw.RUnlock()
  1235. return c.auctionConstants, nil
  1236. }
  1237. // AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
  1238. func (c *Client) AuctionEventsByBlock(blockNum int64) (*eth.AuctionEvents, *ethCommon.Hash, error) {
  1239. c.rw.RLock()
  1240. defer c.rw.RUnlock()
  1241. block, ok := c.blocks[blockNum]
  1242. if !ok {
  1243. return nil, nil, fmt.Errorf("Block %v doesn't exist", blockNum)
  1244. }
  1245. return &block.Auction.Events, &block.Eth.Hash, nil
  1246. }
  1247. //
  1248. // WDelayer
  1249. //
  1250. // WDelayerGetHermezGovernanceDAOAddress is the interface to call the smart contract function
  1251. func (c *Client) WDelayerGetHermezGovernanceDAOAddress() (*ethCommon.Address, error) {
  1252. c.rw.RLock()
  1253. defer c.rw.RUnlock()
  1254. log.Error("TODO")
  1255. return nil, errTODO
  1256. }
  1257. // WDelayerSetHermezGovernanceDAOAddress is the interface to call the smart contract function
  1258. func (c *Client) WDelayerSetHermezGovernanceDAOAddress(newAddress ethCommon.Address) (tx *types.Transaction, err error) {
  1259. c.rw.Lock()
  1260. defer c.rw.Unlock()
  1261. cpy := c.nextBlock().copy()
  1262. defer func() { c.revertIfErr(err, cpy) }()
  1263. if c.addr == nil {
  1264. return nil, eth.ErrAccountNil
  1265. }
  1266. log.Error("TODO")
  1267. return nil, errTODO
  1268. }
  1269. // WDelayerGetHermezKeeperAddress is the interface to call the smart contract function
  1270. func (c *Client) WDelayerGetHermezKeeperAddress() (*ethCommon.Address, error) {
  1271. c.rw.RLock()
  1272. defer c.rw.RUnlock()
  1273. log.Error("TODO")
  1274. return nil, errTODO
  1275. }
  1276. // WDelayerSetHermezKeeperAddress is the interface to call the smart contract function
  1277. func (c *Client) WDelayerSetHermezKeeperAddress(newAddress ethCommon.Address) (tx *types.Transaction, err error) {
  1278. c.rw.Lock()
  1279. defer c.rw.Unlock()
  1280. cpy := c.nextBlock().copy()
  1281. defer func() { c.revertIfErr(err, cpy) }()
  1282. if c.addr == nil {
  1283. return nil, eth.ErrAccountNil
  1284. }
  1285. log.Error("TODO")
  1286. return nil, errTODO
  1287. }
  1288. // WDelayerGetWhiteHackGroupAddress is the interface to call the smart contract function
  1289. func (c *Client) WDelayerGetWhiteHackGroupAddress() (*ethCommon.Address, error) {
  1290. c.rw.RLock()
  1291. defer c.rw.RUnlock()
  1292. log.Error("TODO")
  1293. return nil, errTODO
  1294. }
  1295. // WDelayerSetWhiteHackGroupAddress is the interface to call the smart contract function
  1296. func (c *Client) WDelayerSetWhiteHackGroupAddress(newAddress ethCommon.Address) (tx *types.Transaction, err error) {
  1297. c.rw.Lock()
  1298. defer c.rw.Unlock()
  1299. cpy := c.nextBlock().copy()
  1300. defer func() { c.revertIfErr(err, cpy) }()
  1301. if c.addr == nil {
  1302. return nil, eth.ErrAccountNil
  1303. }
  1304. log.Error("TODO")
  1305. return nil, errTODO
  1306. }
  1307. // WDelayerIsEmergencyMode is the interface to call the smart contract function
  1308. func (c *Client) WDelayerIsEmergencyMode() (bool, error) {
  1309. c.rw.RLock()
  1310. defer c.rw.RUnlock()
  1311. log.Error("TODO")
  1312. return false, errTODO
  1313. }
  1314. // WDelayerGetWithdrawalDelay is the interface to call the smart contract function
  1315. func (c *Client) WDelayerGetWithdrawalDelay() (*big.Int, error) {
  1316. c.rw.RLock()
  1317. defer c.rw.RUnlock()
  1318. log.Error("TODO")
  1319. return nil, errTODO
  1320. }
  1321. // WDelayerGetEmergencyModeStartingTime is the interface to call the smart contract function
  1322. func (c *Client) WDelayerGetEmergencyModeStartingTime() (*big.Int, error) {
  1323. c.rw.RLock()
  1324. defer c.rw.RUnlock()
  1325. log.Error("TODO")
  1326. return nil, errTODO
  1327. }
  1328. // WDelayerEnableEmergencyMode is the interface to call the smart contract function
  1329. func (c *Client) WDelayerEnableEmergencyMode() (tx *types.Transaction, err error) {
  1330. c.rw.Lock()
  1331. defer c.rw.Unlock()
  1332. cpy := c.nextBlock().copy()
  1333. defer func() { c.revertIfErr(err, cpy) }()
  1334. if c.addr == nil {
  1335. return nil, eth.ErrAccountNil
  1336. }
  1337. log.Error("TODO")
  1338. return nil, errTODO
  1339. }
  1340. // WDelayerChangeWithdrawalDelay is the interface to call the smart contract function
  1341. func (c *Client) WDelayerChangeWithdrawalDelay(newWithdrawalDelay uint64) (tx *types.Transaction, err error) {
  1342. c.rw.Lock()
  1343. defer c.rw.Unlock()
  1344. cpy := c.nextBlock().copy()
  1345. defer func() { c.revertIfErr(err, cpy) }()
  1346. if c.addr == nil {
  1347. return nil, eth.ErrAccountNil
  1348. }
  1349. nextBlock := c.nextBlock()
  1350. w := nextBlock.WDelayer
  1351. w.Vars.WithdrawalDelay = newWithdrawalDelay
  1352. w.Events.NewWithdrawalDelay = append(w.Events.NewWithdrawalDelay,
  1353. eth.WDelayerEventNewWithdrawalDelay{WithdrawalDelay: newWithdrawalDelay})
  1354. return w.addTransaction(newTransaction("changeWithdrawalDelay", newWithdrawalDelay)), nil
  1355. }
  1356. // WDelayerDepositInfo is the interface to call the smart contract function
  1357. func (c *Client) WDelayerDepositInfo(owner, token ethCommon.Address) (eth.DepositState, error) {
  1358. c.rw.RLock()
  1359. defer c.rw.RUnlock()
  1360. log.Error("TODO")
  1361. return eth.DepositState{}, errTODO
  1362. }
  1363. // WDelayerDeposit is the interface to call the smart contract function
  1364. func (c *Client) WDelayerDeposit(onwer, token ethCommon.Address, amount *big.Int) (tx *types.Transaction, err error) {
  1365. c.rw.Lock()
  1366. defer c.rw.Unlock()
  1367. cpy := c.nextBlock().copy()
  1368. defer func() { c.revertIfErr(err, cpy) }()
  1369. if c.addr == nil {
  1370. return nil, eth.ErrAccountNil
  1371. }
  1372. log.Error("TODO")
  1373. return nil, errTODO
  1374. }
  1375. // WDelayerWithdrawal is the interface to call the smart contract function
  1376. func (c *Client) WDelayerWithdrawal(owner, token ethCommon.Address) (tx *types.Transaction, err error) {
  1377. c.rw.Lock()
  1378. defer c.rw.Unlock()
  1379. cpy := c.nextBlock().copy()
  1380. defer func() { c.revertIfErr(err, cpy) }()
  1381. if c.addr == nil {
  1382. return nil, eth.ErrAccountNil
  1383. }
  1384. log.Error("TODO")
  1385. return nil, errTODO
  1386. }
  1387. // WDelayerEscapeHatchWithdrawal is the interface to call the smart contract function
  1388. func (c *Client) WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address, amount *big.Int) (tx *types.Transaction, err error) {
  1389. c.rw.Lock()
  1390. defer c.rw.Unlock()
  1391. cpy := c.nextBlock().copy()
  1392. defer func() { c.revertIfErr(err, cpy) }()
  1393. if c.addr == nil {
  1394. return nil, eth.ErrAccountNil
  1395. }
  1396. log.Error("TODO")
  1397. return nil, errTODO
  1398. }
  1399. // WDelayerEventsByBlock returns the events in a block that happened in the WDelayer Contract
  1400. func (c *Client) WDelayerEventsByBlock(blockNum int64) (*eth.WDelayerEvents, *ethCommon.Hash, error) {
  1401. c.rw.RLock()
  1402. defer c.rw.RUnlock()
  1403. block, ok := c.blocks[blockNum]
  1404. if !ok {
  1405. return nil, nil, fmt.Errorf("Block %v doesn't exist", blockNum)
  1406. }
  1407. return &block.WDelayer.Events, &block.Eth.Hash, nil
  1408. }
  1409. // WDelayerConstants returns the Constants of the WDelayer Contract
  1410. func (c *Client) WDelayerConstants() (*common.WDelayerConstants, error) {
  1411. c.rw.RLock()
  1412. defer c.rw.RUnlock()
  1413. return c.wDelayerConstants, nil
  1414. }