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.

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