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.

341 lines
12 KiB

  1. package test
  2. import (
  3. "context"
  4. "fmt"
  5. "math/big"
  6. "time"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/core/types"
  9. "github.com/hermeznetwork/hermez-node/common"
  10. "github.com/hermeznetwork/hermez-node/eth"
  11. "github.com/hermeznetwork/hermez-node/log"
  12. "github.com/hermeznetwork/hermez-node/utils"
  13. "github.com/iden3/go-iden3-crypto/babyjub"
  14. )
  15. // Client implements the eth.ClientInterface interface, allowing to manipulate the
  16. // values for testing, working with deterministic results.
  17. type Client struct {
  18. log bool
  19. blockNum *big.Int
  20. }
  21. // NewClient returns a new test Client that implements the eth.IClient
  22. // interface, at the given initialBlockNumber.
  23. func NewClient(l bool, initialBlockNumber int64) *Client {
  24. return &Client{
  25. log: l,
  26. blockNum: big.NewInt(initialBlockNumber),
  27. }
  28. }
  29. // Advance moves one block forward
  30. func (c *Client) Advance() {
  31. c.blockNum = c.blockNum.Add(c.blockNum, big.NewInt(1))
  32. if c.log {
  33. log.Debugf("TestEthClient blockNum advanced: %d", c.blockNum)
  34. }
  35. }
  36. // SetBlockNum sets the Client.blockNum to the given blockNum
  37. func (c *Client) SetBlockNum(blockNum *big.Int) {
  38. c.blockNum = blockNum
  39. if c.log {
  40. log.Debugf("TestEthClient blockNum set to: %d", c.blockNum)
  41. }
  42. }
  43. // EthCurrentBlock returns the current blockNum
  44. func (c *Client) EthCurrentBlock() (*big.Int, error) {
  45. return c.blockNum, nil
  46. }
  47. func newHeader(number *big.Int) *types.Header {
  48. return &types.Header{
  49. Number: number,
  50. Time: uint64(number.Int64()),
  51. }
  52. }
  53. // EthHeaderByNumber returns the *types.Header for the given block number in a
  54. // deterministic way.
  55. func (c *Client) EthHeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
  56. return newHeader(number), nil
  57. }
  58. // EthBlockByNumber returns the *common.Block for the given block number in a
  59. // deterministic way.
  60. func (c *Client) EthBlockByNumber(ctx context.Context, number *big.Int) (*common.Block, error) {
  61. header := newHeader(number)
  62. return &common.Block{
  63. EthBlockNum: uint64(number.Int64()),
  64. Timestamp: time.Unix(number.Int64(), 0),
  65. Hash: header.Hash(),
  66. }, nil
  67. }
  68. var errTODO = fmt.Errorf("TODO: Not implemented yet")
  69. //
  70. // Rollup
  71. //
  72. // RollupForgeBatch is the interface to call the smart contract function
  73. func (c *Client) RollupForgeBatch(*eth.RollupForgeBatchArgs) (*types.Transaction, error) {
  74. return nil, errTODO
  75. }
  76. // RollupAddToken is the interface to call the smart contract function
  77. func (c *Client) RollupAddToken(tokenAddress ethCommon.Address) (*types.Transaction, error) {
  78. return nil, errTODO
  79. }
  80. // RollupWithdrawSNARK is the interface to call the smart contract function
  81. // func (c *Client) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  82. // return nil, errTODO
  83. // }
  84. // RollupWithdrawMerkleProof is the interface to call the smart contract function
  85. func (c *Client) RollupWithdrawMerkleProof(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
  86. return nil, errTODO
  87. }
  88. // RollupForceExit is the interface to call the smart contract function
  89. func (c *Client) RollupForceExit(fromIdx int64, amountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  90. return nil, errTODO
  91. }
  92. // RollupForceTransfer is the interface to call the smart contract function
  93. func (c *Client) RollupForceTransfer(fromIdx int64, amountF utils.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
  94. return nil, errTODO
  95. }
  96. // RollupCreateAccountDepositTransfer is the interface to call the smart contract function
  97. func (c *Client) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  98. return nil, errTODO
  99. }
  100. // RollupDepositTransfer is the interface to call the smart contract function
  101. func (c *Client) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF utils.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
  102. return nil, errTODO
  103. }
  104. // RollupDeposit is the interface to call the smart contract function
  105. func (c *Client) RollupDeposit(fromIdx int64, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  106. return nil, errTODO
  107. }
  108. // RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
  109. func (c *Client) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF utils.Float16) (*types.Transaction, error) {
  110. return nil, errTODO
  111. }
  112. // RollupCreateAccountDeposit is the interface to call the smart contract function
  113. func (c *Client) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF utils.Float16, tokenID int64) (*types.Transaction, error) {
  114. return nil, errTODO
  115. }
  116. // RollupGetTokenAddress is the interface to call the smart contract function
  117. func (c *Client) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
  118. return nil, errTODO
  119. }
  120. // RollupGetL1TxFromQueue is the interface to call the smart contract function
  121. func (c *Client) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
  122. return nil, errTODO
  123. }
  124. // RollupGetQueue is the interface to call the smart contract function
  125. func (c *Client) RollupGetQueue(queue int64) ([]byte, error) {
  126. return nil, errTODO
  127. }
  128. // RollupUpdateForgeL1Timeout is the interface to call the smart contract function
  129. func (c *Client) RollupUpdateForgeL1Timeout(newForgeL1Timeout int64) (*types.Transaction, error) {
  130. return nil, errTODO
  131. }
  132. // RollupUpdateFeeL1UserTx is the interface to call the smart contract function
  133. func (c *Client) RollupUpdateFeeL1UserTx(newFeeL1UserTx *big.Int) (*types.Transaction, error) {
  134. return nil, errTODO
  135. }
  136. // RollupUpdateFeeAddToken is the interface to call the smart contract function
  137. func (c *Client) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
  138. return nil, errTODO
  139. }
  140. // RollupUpdateTokensHEZ is the interface to call the smart contract function
  141. func (c *Client) RollupUpdateTokensHEZ(newTokenHEZ ethCommon.Address) (*types.Transaction, error) {
  142. return nil, errTODO
  143. }
  144. // RollupUpdateGovernance is the interface to call the smart contract function
  145. // func (c *Client) RollupUpdateGovernance() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
  146. // return nil, errTODO
  147. // }
  148. // RollupConstants returns the Constants of the Rollup Smart Contract
  149. func (c *Client) RollupConstants() (*eth.RollupConstants, error) {
  150. return nil, errTODO
  151. }
  152. // RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
  153. func (c *Client) RollupEventsByBlock(blockNum int64) (*eth.RollupEvents, *ethCommon.Hash, error) {
  154. return nil, nil, errTODO
  155. }
  156. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
  157. func (c *Client) RollupForgeBatchArgs(transaction *types.Transaction) (*eth.RollupForgeBatchArgs, error) {
  158. return nil, errTODO
  159. }
  160. //
  161. // Auction
  162. //
  163. // AuctionSetSlotDeadline is the interface to call the smart contract function
  164. func (c *Client) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
  165. return nil, errTODO
  166. }
  167. // AuctionGetSlotDeadline is the interface to call the smart contract function
  168. func (c *Client) AuctionGetSlotDeadline() (uint8, error) {
  169. return 0, errTODO
  170. }
  171. // AuctionSetOpenAuctionSlots is the interface to call the smart contract function
  172. func (c *Client) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
  173. return nil, errTODO
  174. }
  175. // AuctionGetOpenAuctionSlots is the interface to call the smart contract function
  176. func (c *Client) AuctionGetOpenAuctionSlots() (uint16, error) { return 0, errTODO }
  177. // AuctionSetClosedAuctionSlots is the interface to call the smart contract function
  178. func (c *Client) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
  179. return nil, errTODO
  180. }
  181. // AuctionGetClosedAuctionSlots is the interface to call the smart contract function
  182. func (c *Client) AuctionGetClosedAuctionSlots() (uint16, error) {
  183. return 0, errTODO
  184. }
  185. // AuctionSetOutbidding is the interface to call the smart contract function
  186. func (c *Client) AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error) {
  187. return nil, errTODO
  188. }
  189. // AuctionGetOutbidding is the interface to call the smart contract function
  190. func (c *Client) AuctionGetOutbidding() (uint8, error) {
  191. return 0, errTODO
  192. }
  193. // AuctionSetAllocationRatio is the interface to call the smart contract function
  194. func (c *Client) AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error) {
  195. return nil, errTODO
  196. }
  197. // AuctionGetAllocationRatio is the interface to call the smart contract function
  198. func (c *Client) AuctionGetAllocationRatio() ([3]uint8, error) {
  199. return [3]uint8{}, errTODO
  200. }
  201. // AuctionSetDonationAddress is the interface to call the smart contract function
  202. func (c *Client) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
  203. return nil, errTODO
  204. }
  205. // AuctionGetDonationAddress is the interface to call the smart contract function
  206. func (c *Client) AuctionGetDonationAddress() (*ethCommon.Address, error) {
  207. return nil, errTODO
  208. }
  209. // AuctionSetBootCoordinator is the interface to call the smart contract function
  210. func (c *Client) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
  211. return nil, errTODO
  212. }
  213. // AuctionGetBootCoordinator is the interface to call the smart contract function
  214. func (c *Client) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
  215. return nil, errTODO
  216. }
  217. // AuctionChangeEpochMinBid is the interface to call the smart contract function
  218. func (c *Client) AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
  219. return nil, errTODO
  220. }
  221. // AuctionRegisterCoordinator is the interface to call the smart contract function
  222. func (c *Client) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) {
  223. return nil, errTODO
  224. }
  225. // AuctionIsRegisteredCoordinator is the interface to call the smart contract function
  226. func (c *Client) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
  227. return false, errTODO
  228. }
  229. // AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
  230. func (c *Client) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
  231. return nil, errTODO
  232. }
  233. // AuctionGetCurrentSlotNumber is the interface to call the smart contract function
  234. func (c *Client) AuctionGetCurrentSlotNumber() (int64, error) {
  235. return 0, errTODO
  236. }
  237. // AuctionGetMinBidBySlot is the interface to call the smart contract function
  238. func (c *Client) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
  239. return nil, errTODO
  240. }
  241. // AuctionGetMinBidEpoch is the interface to call the smart contract function
  242. func (c *Client) AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error) {
  243. return nil, errTODO
  244. }
  245. // AuctionTokensReceived is the interface to call the smart contract function
  246. // func (c *Client) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
  247. // return errTODO
  248. // }
  249. // AuctionBid is the interface to call the smart contract function
  250. func (c *Client) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
  251. return nil, errTODO
  252. }
  253. // AuctionMultiBid is the interface to call the smart contract function
  254. func (c *Client) AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
  255. return nil, errTODO
  256. }
  257. // AuctionCanForge is the interface to call the smart contract function
  258. func (c *Client) AuctionCanForge(forger ethCommon.Address) (bool, error) {
  259. return false, errTODO
  260. }
  261. // AuctionForge is the interface to call the smart contract function
  262. // func (c *Client) AuctionForge(forger ethCommon.Address) (bool, error) {
  263. // return false, errTODO
  264. // }
  265. // AuctionClaimHEZ is the interface to call the smart contract function
  266. func (c *Client) AuctionClaimHEZ() (*types.Transaction, error) {
  267. return nil, errTODO
  268. }
  269. // AuctionConstants returns the Constants of the Auction Smart Contract
  270. func (c *Client) AuctionConstants() (*eth.AuctionConstants, error) {
  271. return nil, errTODO
  272. }
  273. // AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
  274. func (c *Client) AuctionEventsByBlock(blockNum int64) (*eth.AuctionEvents, *ethCommon.Hash, error) {
  275. return nil, nil, errTODO
  276. }