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.

79 lines
2.1 KiB

  1. package test
  2. import (
  3. "context"
  4. "math/big"
  5. "time"
  6. "github.com/ethereum/go-ethereum/core/types"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/hermeznetwork/hermez-node/log"
  9. )
  10. // Client implements the eth.IClient interface, allowing to manipulate the
  11. // values for testing, working with deterministic results.
  12. type Client struct {
  13. log bool
  14. blockNum *big.Int
  15. }
  16. // NewTestEthClient returns a new test Client that implements the eth.IClient
  17. // interface, at the given initialBlockNumber.
  18. func NewTestEthClient(l bool, initialBlockNumber int64) *Client {
  19. return &Client{
  20. log: l,
  21. blockNum: big.NewInt(initialBlockNumber),
  22. }
  23. }
  24. // Advance moves one block forward
  25. func (c *Client) Advance() {
  26. c.blockNum = c.blockNum.Add(c.blockNum, big.NewInt(1))
  27. if c.log {
  28. log.Debugf("TestEthClient blockNum advanced: %d", c.blockNum)
  29. }
  30. }
  31. // SetBlockNum sets the Client.blockNum to the given blockNum
  32. func (c *Client) SetBlockNum(blockNum *big.Int) {
  33. c.blockNum = blockNum
  34. if c.log {
  35. log.Debugf("TestEthClient blockNum set to: %d", c.blockNum)
  36. }
  37. }
  38. // CurrentBlock returns the current blockNum
  39. func (c *Client) CurrentBlock() (*big.Int, error) {
  40. return c.blockNum, nil
  41. }
  42. func newHeader(number *big.Int) *types.Header {
  43. return &types.Header{
  44. Number: number,
  45. Time: uint64(number.Int64()),
  46. }
  47. }
  48. // HeaderByNumber returns the *types.Header for the given block number in a
  49. // deterministic way.
  50. func (c *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
  51. return newHeader(number), nil
  52. }
  53. // BlockByNumber returns the *common.Block for the given block number in a
  54. // deterministic way.
  55. func (c *Client) BlockByNumber(ctx context.Context, number *big.Int) (*common.Block, error) {
  56. header := newHeader(number)
  57. return &common.Block{
  58. EthBlockNum: uint64(number.Int64()),
  59. Timestamp: time.Unix(number.Int64(), 0),
  60. Hash: header.Hash(),
  61. }, nil
  62. }
  63. // ForgeCall send the *common.CallDataForge to the Forge method of the mock
  64. // smart contract
  65. func (c *Client) ForgeCall(*common.CallDataForge) ([]byte, error) {
  66. return nil, nil
  67. }