backend.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bind
  17. import (
  18. "context"
  19. "errors"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. )
  25. var (
  26. // ErrNoCode is returned by call and transact operations for which the requested
  27. // recipient contract to operate on does not exist in the state db or does not
  28. // have any code associated with it (i.e. suicided).
  29. ErrNoCode = errors.New("no contract code at given address")
  30. // This error is raised when attempting to perform a pending state action
  31. // on a backend that doesn't implement PendingContractCaller.
  32. ErrNoPendingState = errors.New("backend does not support pending state")
  33. // This error is returned by WaitDeployed if contract creation leaves an
  34. // empty contract behind.
  35. ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
  36. )
  37. // ContractCaller defines the methods needed to allow operating with a contract on a read
  38. // only basis.
  39. type ContractCaller interface {
  40. // CodeAt returns the code of the given account. This is needed to differentiate
  41. // between contract internal errors and the local chain being out of sync.
  42. CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
  43. // ContractCall executes an Ethereum contract call with the specified data as the
  44. // input.
  45. CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
  46. }
  47. // PendingContractCaller defines methods to perform contract calls on the pending state.
  48. // Call will try to discover this interface when access to the pending state is requested.
  49. // If the backend does not support the pending state, Call returns ErrNoPendingState.
  50. type PendingContractCaller interface {
  51. // PendingCodeAt returns the code of the given account in the pending state.
  52. PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error)
  53. // PendingCallContract executes an Ethereum contract call against the pending state.
  54. PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
  55. }
  56. // ContractTransactor defines the methods needed to allow operating with a contract
  57. // on a write only basis. Besides the transacting method, the remainder are helpers
  58. // used when the user does not provide some needed values, but rather leaves it up
  59. // to the transactor to decide.
  60. type ContractTransactor interface {
  61. // PendingCodeAt returns the code of the given account in the pending state.
  62. PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
  63. // PendingNonceAt retrieves the current pending nonce associated with an account.
  64. PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
  65. // SuggestGasPrice retrieves the currently suggested gas price to allow a timely
  66. // execution of a transaction.
  67. SuggestGasPrice(ctx context.Context) (*big.Int, error)
  68. // EstimateGas tries to estimate the gas needed to execute a specific
  69. // transaction based on the current pending state of the backend blockchain.
  70. // There is no guarantee that this is the true gas limit requirement as other
  71. // transactions may be added or removed by miners, but it should provide a basis
  72. // for setting a reasonable default.
  73. EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)
  74. // SendTransaction injects the transaction into the pending pool for execution.
  75. SendTransaction(ctx context.Context, tx *types.Transaction, args PrivateTxArgs) error
  76. // PreparePrivateTransaction send the private transaction to Tessera/Constellation's /storeraw API using HTTP
  77. PreparePrivateTransaction(data []byte, privateFrom string) (common.EncryptedPayloadHash, error)
  78. // DistributeTransaction distributes the private transaction payload to its private recipients, and sends the
  79. // private transaction to the nodes PTM, returning a PTM hash for the Privacy Marker Transaction
  80. DistributeTransaction(ctx context.Context, tx *types.Transaction, args PrivateTxArgs) (string, error)
  81. }
  82. // ContractFilterer defines the methods needed to access log events using one-off
  83. // queries or continuous event subscriptions.
  84. type ContractFilterer interface {
  85. // FilterLogs executes a log filter operation, blocking during execution and
  86. // returning all the results in one batch.
  87. //
  88. // TODO(karalabe): Deprecate when the subscription one can return past data too.
  89. FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error)
  90. // SubscribeFilterLogs creates a background log filtering operation, returning
  91. // a subscription immediately, which can be used to stream the found events.
  92. SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
  93. }
  94. // DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
  95. type DeployBackend interface {
  96. TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
  97. CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
  98. }
  99. // ContractBackend defines the methods needed to work with contracts on a read-write basis.
  100. type ContractBackend interface {
  101. ContractCaller
  102. ContractTransactor
  103. ContractFilterer
  104. }