interfaces.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2016 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 ethereum defines interfaces for interacting with Ethereum.
  17. package ethereum
  18. import (
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/core/vm"
  23. "golang.org/x/net/context"
  24. )
  25. // TODO: move subscription to package event
  26. // Subscription represents an event subscription where events are
  27. // delivered on a data channel.
  28. type Subscription interface {
  29. // Unsubscribe cancels the sending of events to the data channel
  30. // and closes the error channel.
  31. Unsubscribe()
  32. // Err returns the subscription error channel. The error channel receives
  33. // a value if there is an issue with the subscription (e.g. the network connection
  34. // delivering the events has been closed). Only one value will ever be sent.
  35. // The error channel is closed by Unsubscribe.
  36. Err() <-chan error
  37. }
  38. // ChainReader provides access to the blockchain. The methods in this interface access raw
  39. // data from either the canonical chain (when requesting by block number) or any
  40. // blockchain fork that was previously downloaded and processed by the node. The block
  41. // number argument can be nil to select the latest canonical block. Reading block headers
  42. // should be preferred over full blocks whenever possible.
  43. type ChainReader interface {
  44. BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
  45. BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
  46. HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
  47. HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
  48. TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error)
  49. TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error)
  50. TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, error)
  51. TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
  52. }
  53. // ChainStateReader wraps access to the state trie of the canonical blockchain. Note that
  54. // implementations of the interface may be unable to return state values for old blocks.
  55. // In many cases, using CallContract can be preferable to reading raw contract storage.
  56. type ChainStateReader interface {
  57. BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
  58. StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error)
  59. CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
  60. NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
  61. }
  62. // SyncProgress gives progress indications when the node is synchronising with
  63. // the Ethereum network.
  64. type SyncProgress struct {
  65. StartingBlock uint64 // Block number where sync began
  66. CurrentBlock uint64 // Current block number where sync is at
  67. HighestBlock uint64 // Highest alleged block number in the chain
  68. PulledStates uint64 // Number of state trie entries already downloaded
  69. KnownStates uint64 // Total number os state trie entries known about
  70. }
  71. // ChainSyncReader wraps access to the node's current sync status. If there's no
  72. // sync currently running, it returns nil.
  73. type ChainSyncReader interface {
  74. SyncProgress(ctx context.Context) (*SyncProgress, error)
  75. }
  76. // A ChainHeadEventer returns notifications whenever the canonical head block is updated.
  77. type ChainHeadEventer interface {
  78. SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (Subscription, error)
  79. }
  80. // CallMsg contains parameters for contract calls.
  81. type CallMsg struct {
  82. From common.Address // the sender of the 'transaction'
  83. To *common.Address // the destination contract (nil for contract creation)
  84. Gas *big.Int // if nil, the call executes with near-infinite gas
  85. GasPrice *big.Int // wei <-> gas exchange ratio
  86. Value *big.Int // amount of wei sent along with the call
  87. Data []byte // input data, usually an ABI-encoded contract method invocation
  88. }
  89. // A ContractCaller provides contract calls, essentially transactions that are executed by
  90. // the EVM but not mined into the blockchain. ContractCall is a low-level method to
  91. // execute such calls. For applications which are structured around specific contracts,
  92. // the abigen tool provides a nicer, properly typed way to perform calls.
  93. type ContractCaller interface {
  94. CallContract(ctx context.Context, call CallMsg, blockNumber *big.Int) ([]byte, error)
  95. }
  96. // FilterQuery contains options for contact log filtering.
  97. type FilterQuery struct {
  98. FromBlock *big.Int // beginning of the queried range, nil means genesis block
  99. ToBlock *big.Int // end of the range, nil means latest block
  100. Addresses []common.Address // restricts matches to events created by specific contracts
  101. // The Topic list restricts matches to particular event topics. Each event has a list
  102. // of topics. Topics matches a prefix of that list. An empty element slice matches any
  103. // topic. Non-empty elements represent an alternative that matches any of the
  104. // contained topics.
  105. //
  106. // Examples:
  107. // {} or nil matches any topic list
  108. // {{A}} matches topic A in first position
  109. // {{}, {B}} matches any topic in first position, B in second position
  110. // {{A}}, {B}} matches topic A in first position, B in second position
  111. // {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position
  112. Topics [][]common.Hash
  113. }
  114. // LogFilterer provides access to contract log events using a one-off query or continuous
  115. // event subscription.
  116. type LogFilterer interface {
  117. FilterLogs(ctx context.Context, q FilterQuery) ([]vm.Log, error)
  118. SubscribeFilterLogs(ctx context.Context, q FilterQuery, ch chan<- vm.Log) (Subscription, error)
  119. }
  120. // TransactionSender wraps transaction sending. The SendTransaction method injects a
  121. // signed transaction into the pending transaction pool for execution. If the transaction
  122. // was a contract creation, the TransactionReceipt method can be used to retrieve the
  123. // contract address after the transaction has been mined.
  124. //
  125. // The transaction must be signed and have a valid nonce to be included. Consumers of the
  126. // API can use package accounts to maintain local private keys and need can retrieve the
  127. // next available nonce using PendingNonceAt.
  128. type TransactionSender interface {
  129. SendTransaction(ctx context.Context, tx *types.Transaction) error
  130. }
  131. // GasPricer wraps the gas price oracle, which monitors the blockchain to determine the
  132. // optimal gas price given current fee market conditions.
  133. type GasPricer interface {
  134. SuggestGasPrice(ctx context.Context) (*big.Int, error)
  135. }
  136. // A PendingStateReader provides access to the pending state, which is the result of all
  137. // known executable transactions which have not yet been included in the blockchain. It is
  138. // commonly used to display the result of ’unconfirmed’ actions (e.g. wallet value
  139. // transfers) initiated by the user. The PendingNonceAt operation is a good way to
  140. // retrieve the next available transaction nonce for a specific account.
  141. type PendingStateReader interface {
  142. PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error)
  143. PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error)
  144. PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
  145. PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
  146. PendingTransactionCount(ctx context.Context) (uint, error)
  147. }
  148. // PendingContractCaller can be used to perform calls against the pending state.
  149. type PendingContractCaller interface {
  150. PendingCallContract(ctx context.Context, call CallMsg) ([]byte, error)
  151. }
  152. // GasEstimator wraps EstimateGas, which tries to estimate the gas needed to execute a
  153. // specific transaction based on the pending state. There is no guarantee that this is the
  154. // true gas limit requirement as other transactions may be added or removed by miners, but
  155. // it should provide a basis for setting a reasonable default.
  156. type GasEstimator interface {
  157. EstimateGas(ctx context.Context, call CallMsg) (usedGas *big.Int, err error)
  158. }
  159. // A PendingStateEventer provides access to real time notifications about changes to the
  160. // pending state.
  161. type PendingStateEventer interface {
  162. SubscribePendingTransactions(ctx context.Context, ch chan<- *types.Transaction) (Subscription, error)
  163. }