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.

243 lines
7.8 KiB

  1. package eth
  2. import (
  3. "context"
  4. "fmt"
  5. "math/big"
  6. "time"
  7. "github.com/ethereum/go-ethereum/accounts"
  8. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  9. ethKeystore "github.com/ethereum/go-ethereum/accounts/keystore"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. "github.com/ethereum/go-ethereum/core/types"
  12. "github.com/ethereum/go-ethereum/ethclient"
  13. "github.com/hermeznetwork/hermez-node/common"
  14. "github.com/hermeznetwork/hermez-node/log"
  15. )
  16. var (
  17. // ErrAccountNil is used when the calls can not be made because the account is nil
  18. ErrAccountNil = fmt.Errorf("Authorized calls can't be made when the account is nil")
  19. // ErrReceiptStatusFailed is used when receiving a failed transaction
  20. ErrReceiptStatusFailed = fmt.Errorf("receipt status is failed")
  21. // ErrReceiptNotReceived is used when unable to retrieve a transaction
  22. ErrReceiptNotReceived = fmt.Errorf("receipt not available")
  23. )
  24. const (
  25. errStrDeploy = "deployment of %s failed: %w"
  26. errStrWaitReceipt = "wait receipt of %s deploy failed: %w"
  27. // default values
  28. defaultCallGasLimit = 300000
  29. defaultDeployGasLimit = 1000000
  30. defaultGasPriceDiv = 100
  31. defaultReceiptTimeout = 60
  32. defaultIntervalReceiptLoop = 200
  33. )
  34. // Config defines the configuration parameters of the Client
  35. type Config struct {
  36. CallGasLimit uint64
  37. DeployGasLimit uint64
  38. GasPriceDiv uint64
  39. ReceiptTimeout time.Duration // in seconds
  40. IntervalReceiptLoop time.Duration // in milliseconds
  41. }
  42. // Client is an ethereum client to call Smart Contract methods.
  43. type Client struct {
  44. client *ethclient.Client
  45. account *accounts.Account
  46. ks *ethKeystore.KeyStore
  47. ReceiptTimeout time.Duration
  48. config *Config
  49. }
  50. // NewClient creates a Client instance. The account is not mandatory (it can
  51. // be nil). If the account is nil, CallAuth will fail with ErrAccountNil.
  52. func NewClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore, config *Config) *Client {
  53. if config == nil {
  54. config = &Config{
  55. CallGasLimit: defaultCallGasLimit,
  56. DeployGasLimit: defaultDeployGasLimit,
  57. GasPriceDiv: defaultGasPriceDiv,
  58. ReceiptTimeout: defaultReceiptTimeout,
  59. IntervalReceiptLoop: defaultIntervalReceiptLoop,
  60. }
  61. }
  62. return &Client{client: client, account: account, ks: ks, ReceiptTimeout: config.ReceiptTimeout * time.Second, config: config}
  63. }
  64. // BalanceAt retieves information about the default account
  65. func (c *Client) BalanceAt(addr ethCommon.Address) (*big.Int, error) {
  66. return c.client.BalanceAt(context.TODO(), addr, nil)
  67. }
  68. // Account returns the underlying ethereum account
  69. func (c *Client) Account() *accounts.Account {
  70. return c.account
  71. }
  72. // CallAuth performs a Smart Contract method call that requires authorization.
  73. // This call requires a valid account with Ether that can be spend during the
  74. // call.
  75. func (c *Client) CallAuth(gasLimit uint64,
  76. fn func(*ethclient.Client, *bind.TransactOpts) (*types.Transaction, error)) (*types.Transaction, error) {
  77. if c.account == nil {
  78. return nil, ErrAccountNil
  79. }
  80. gasPrice, err := c.client.SuggestGasPrice(context.Background())
  81. if err != nil {
  82. return nil, err
  83. }
  84. inc := new(big.Int).Set(gasPrice)
  85. inc.Div(inc, new(big.Int).SetUint64(c.config.GasPriceDiv))
  86. gasPrice.Add(gasPrice, inc)
  87. log.Debug("Transaction metadata", "gasPrice", gasPrice)
  88. auth, err := bind.NewKeyStoreTransactor(c.ks, *c.account)
  89. if err != nil {
  90. return nil, err
  91. }
  92. auth.Value = big.NewInt(0) // in wei
  93. if gasLimit == 0 {
  94. auth.GasLimit = c.config.CallGasLimit // in units
  95. } else {
  96. auth.GasLimit = gasLimit // in units
  97. }
  98. auth.GasPrice = gasPrice
  99. tx, err := fn(c.client, auth)
  100. if tx != nil {
  101. log.Debug("Transaction", "tx", tx.Hash().Hex(), "nonce", tx.Nonce())
  102. }
  103. return tx, err
  104. }
  105. // ContractData contains the contract data
  106. type ContractData struct {
  107. Address ethCommon.Address
  108. Tx *types.Transaction
  109. Receipt *types.Receipt
  110. }
  111. // Deploy a smart contract. `name` is used to log deployment information. fn
  112. // is a wrapper to the deploy function generated by abigen. In case of error,
  113. // the returned `ContractData` may have some parameters filled depending on the
  114. // kind of error that occurred.
  115. func (c *Client) Deploy(name string,
  116. fn func(c *ethclient.Client, auth *bind.TransactOpts) (ethCommon.Address, *types.Transaction, interface{}, error)) (ContractData, error) {
  117. var contractData ContractData
  118. log.Info("Deploying", "contract", name)
  119. tx, err := c.CallAuth(
  120. c.config.DeployGasLimit,
  121. func(client *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  122. addr, tx, _, err := fn(client, auth)
  123. if err != nil {
  124. return nil, err
  125. }
  126. contractData.Address = addr
  127. return tx, nil
  128. },
  129. )
  130. if err != nil {
  131. return contractData, fmt.Errorf(errStrDeploy, name, err)
  132. }
  133. log.Info("Waiting receipt", "tx", tx.Hash().Hex(), "contract", name)
  134. contractData.Tx = tx
  135. receipt, err := c.WaitReceipt(tx)
  136. if err != nil {
  137. return contractData, fmt.Errorf(errStrWaitReceipt, name, err)
  138. }
  139. contractData.Receipt = receipt
  140. return contractData, nil
  141. }
  142. // Call performs a read only Smart Contract method call.
  143. func (c *Client) Call(fn func(*ethclient.Client) error) error {
  144. return fn(c.client)
  145. }
  146. // WaitReceipt will block until a transaction is confirmed. Internally it
  147. // polls the state every 200 milliseconds.
  148. func (c *Client) WaitReceipt(tx *types.Transaction) (*types.Receipt, error) {
  149. return c.waitReceipt(context.TODO(), tx, c.ReceiptTimeout)
  150. }
  151. // GetReceipt will check if a transaction is confirmed and return
  152. // immediately, waiting at most 1 second and returning error if the transaction
  153. // is still pending.
  154. func (c *Client) GetReceipt(tx *types.Transaction) (*types.Receipt, error) {
  155. ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
  156. defer cancel()
  157. return c.waitReceipt(ctx, tx, 0)
  158. }
  159. func (c *Client) waitReceipt(ctx context.Context, tx *types.Transaction, timeout time.Duration) (*types.Receipt, error) {
  160. var err error
  161. var receipt *types.Receipt
  162. txid := tx.Hash()
  163. log.Debug("Waiting for receipt", "tx", txid.Hex())
  164. start := time.Now()
  165. for {
  166. receipt, err = c.client.TransactionReceipt(ctx, txid)
  167. if receipt != nil || time.Since(start) >= timeout {
  168. break
  169. }
  170. time.Sleep(c.config.IntervalReceiptLoop * time.Millisecond)
  171. }
  172. if receipt != nil && receipt.Status == types.ReceiptStatusFailed {
  173. log.Error("Failed transaction", "tx", txid.Hex())
  174. return receipt, ErrReceiptStatusFailed
  175. }
  176. if receipt == nil {
  177. log.Debug("Pendingtransaction / Wait receipt timeout", "tx", txid.Hex(), "lasterr", err)
  178. return receipt, ErrReceiptNotReceived
  179. }
  180. log.Debug("Successful transaction", "tx", txid.Hex())
  181. return receipt, err
  182. }
  183. // CurrentBlock returns the current block number in the blockchain
  184. func (c *Client) CurrentBlock() (*big.Int, error) {
  185. ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
  186. defer cancel()
  187. header, err := c.client.HeaderByNumber(ctx, nil)
  188. if err != nil {
  189. return nil, err
  190. }
  191. return header.Number, nil
  192. }
  193. // HeaderByNumber internally calls ethclient.Client HeaderByNumber
  194. func (c *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
  195. return c.client.HeaderByNumber(ctx, number)
  196. }
  197. // BlockByNumber internally calls ethclient.Client BlockByNumber and returns *common.Block
  198. func (c *Client) BlockByNumber(ctx context.Context, number *big.Int) (*common.Block, error) {
  199. block, err := c.client.BlockByNumber(ctx, number)
  200. if err != nil {
  201. return nil, err
  202. }
  203. b := &common.Block{
  204. EthBlockNum: block.Number().Uint64(),
  205. Timestamp: time.Unix(int64(block.Time()), 0),
  206. Hash: block.Hash(),
  207. }
  208. return b, nil
  209. }
  210. // ForgeCall send the *common.CallDataForge to the Forge method of the smart contract
  211. func (c *Client) ForgeCall(callData *common.CallDataForge) ([]byte, error) {
  212. // TODO this depends on the smart contracts, once are ready this will be updated
  213. return nil, nil
  214. }