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.

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