github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/interfaces.go (about) 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 17 // Package ethereum defines interfaces for interacting with Ethereum. 18 package ethereum 19 20 import ( 21 "context" 22 "errors" 23 "math/big" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/core/types" 27 ) 28 29 // NotFound is returned by API methods if the requested item does not exist. 30 var NotFound = errors.New("not found") 31 32 // Subscription represents an event subscription where events are 33 // delivered on a data channel. 34 type Subscription interface { 35 // Unsubscribe cancels the sending of events to the data channel 36 // and closes the error channel. 37 Unsubscribe() 38 // Err returns the subscription error channel. The error channel receives 39 // a value if there is an issue with the subscription (e.g. the network connection 40 // delivering the events has been closed). Only one value will ever be sent. 41 // The error channel is closed by Unsubscribe. 42 Err() <-chan error 43 } 44 45 // ChainReader provides access to the blockchain. The methods in this interface access raw 46 // data from either the canonical chain (when requesting by block number) or any 47 // blockchain fork that was previously downloaded and processed by the node. The block 48 // number argument can be nil to select the latest canonical block. Reading block headers 49 // should be preferred over full blocks whenever possible. 50 // 51 // The returned error is NotFound if the requested item does not exist. 52 type ChainReader interface { 53 BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) 54 BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) 55 HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) 56 HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) 57 TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) 58 TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) 59 60 // This method subscribes to notifications about changes of the head block of 61 // the canonical chain. 62 SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (Subscription, error) 63 } 64 65 // TransactionReader provides access to past transactions and their receipts. 66 // Implementations may impose arbitrary restrictions on the transactions and receipts that 67 // can be retrieved. Historic transactions may not be available. 68 // 69 // Avoid relying on this interface if possible. Contract logs (through the LogFilterer 70 // interface) are more reliable and usually safer in the presence of chain 71 // reorganisations. 72 // 73 // The returned error is NotFound if the requested item does not exist. 74 type TransactionReader interface { 75 // TransactionByHash checks the pool of pending transactions in addition to the 76 // blockchain. The isPending return value indicates whether the transaction has been 77 // mined yet. Note that the transaction may not be part of the canonical chain even if 78 // it's not pending. 79 TransactionByHash(ctx context.Context, txHash common.Hash) (tx *types.Transaction, isPending bool, err error) 80 // TransactionReceipt returns the receipt of a mined transaction. Note that the 81 // transaction may not be included in the current canonical chain even if a receipt 82 // exists. 83 TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) 84 } 85 86 // ChainStateReader wraps access to the state trie of the canonical blockchain. Note that 87 // implementations of the interface may be unable to return state values for old blocks. 88 // In many cases, using CallContract can be preferable to reading raw contract storage. 89 type ChainStateReader interface { 90 BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) 91 StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) 92 CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) 93 NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) 94 } 95 96 // SyncProgress gives progress indications when the node is synchronising with 97 // the Ethereum network. 98 type SyncProgress struct { 99 StartingBlock uint64 // Block number where sync began 100 CurrentBlock uint64 // Current block number where sync is at 101 HighestBlock uint64 // Highest alleged block number in the chain 102 103 // "fast sync" fields. These used to be sent by geth, but are no longer used 104 // since version v1.10. 105 PulledStates uint64 // Number of state trie entries already downloaded 106 KnownStates uint64 // Total number of state trie entries known about 107 108 // "snap sync" fields. 109 SyncedAccounts uint64 // Number of accounts downloaded 110 SyncedAccountBytes uint64 // Number of account trie bytes persisted to disk 111 SyncedBytecodes uint64 // Number of bytecodes downloaded 112 SyncedBytecodeBytes uint64 // Number of bytecode bytes downloaded 113 SyncedStorage uint64 // Number of storage slots downloaded 114 SyncedStorageBytes uint64 // Number of storage trie bytes persisted to disk 115 116 HealedTrienodes uint64 // Number of state trie nodes downloaded 117 HealedTrienodeBytes uint64 // Number of state trie bytes persisted to disk 118 HealedBytecodes uint64 // Number of bytecodes downloaded 119 HealedBytecodeBytes uint64 // Number of bytecodes persisted to disk 120 121 HealingTrienodes uint64 // Number of state trie nodes pending 122 HealingBytecode uint64 // Number of bytecodes pending 123 124 // "transaction indexing" fields 125 TxIndexFinishedBlocks uint64 // Number of blocks whose transactions are already indexed 126 TxIndexRemainingBlocks uint64 // Number of blocks whose transactions are not indexed yet 127 } 128 129 // Done returns the indicator if the initial sync is finished or not. 130 func (prog SyncProgress) Done() bool { 131 if prog.CurrentBlock < prog.HighestBlock { 132 return false 133 } 134 return prog.TxIndexRemainingBlocks == 0 135 } 136 137 // ChainSyncReader wraps access to the node's current sync status. If there's no 138 // sync currently running, it returns nil. 139 type ChainSyncReader interface { 140 SyncProgress(ctx context.Context) (*SyncProgress, error) 141 } 142 143 // CallMsg contains parameters for contract calls. 144 type CallMsg struct { 145 From common.Address // the sender of the 'transaction' 146 To *common.Address // the destination contract (nil for contract creation) 147 Gas uint64 // if 0, the call executes with near-infinite gas 148 GasPrice *big.Int // wei <-> gas exchange ratio 149 GasFeeCap *big.Int // EIP-1559 fee cap per gas. 150 GasTipCap *big.Int // EIP-1559 tip per gas. 151 Value *big.Int // amount of wei sent along with the call 152 Data []byte // input data, usually an ABI-encoded contract method invocation 153 154 AccessList types.AccessList // EIP-2930 access list. 155 156 // For BlobTxType 157 BlobGasFeeCap *big.Int 158 BlobHashes []common.Hash 159 } 160 161 // A ContractCaller provides contract calls, essentially transactions that are executed by 162 // the EVM but not mined into the blockchain. ContractCall is a low-level method to 163 // execute such calls. For applications which are structured around specific contracts, 164 // the abigen tool provides a nicer, properly typed way to perform calls. 165 type ContractCaller interface { 166 CallContract(ctx context.Context, call CallMsg, blockNumber *big.Int) ([]byte, error) 167 } 168 169 // FilterQuery contains options for contract log filtering. 170 type FilterQuery struct { 171 BlockHash *common.Hash // used by eth_getLogs, return logs only from block with this hash 172 FromBlock *big.Int // beginning of the queried range, nil means genesis block 173 ToBlock *big.Int // end of the range, nil means latest block 174 Addresses []common.Address // restricts matches to events created by specific contracts 175 176 // The Topic list restricts matches to particular event topics. Each event has a list 177 // of topics. Topics matches a prefix of that list. An empty element slice matches any 178 // topic. Non-empty elements represent an alternative that matches any of the 179 // contained topics. 180 // 181 // Examples: 182 // {} or nil matches any topic list 183 // {{A}} matches topic A in first position 184 // {{}, {B}} matches any topic in first position AND B in second position 185 // {{A}, {B}} matches topic A in first position AND B in second position 186 // {{A, B}, {C, D}} matches topic (A OR B) in first position AND (C OR D) in second position 187 Topics [][]common.Hash 188 } 189 190 // LogFilterer provides access to contract log events using a one-off query or continuous 191 // event subscription. 192 // 193 // Logs received through a streaming query subscription may have Removed set to true, 194 // indicating that the log was reverted due to a chain reorganisation. 195 type LogFilterer interface { 196 FilterLogs(ctx context.Context, q FilterQuery) ([]types.Log, error) 197 SubscribeFilterLogs(ctx context.Context, q FilterQuery, ch chan<- types.Log) (Subscription, error) 198 } 199 200 // TransactionSender wraps transaction sending. The SendTransaction method injects a 201 // signed transaction into the pending transaction pool for execution. If the transaction 202 // was a contract creation, the TransactionReceipt method can be used to retrieve the 203 // contract address after the transaction has been mined. 204 // 205 // The transaction must be signed and have a valid nonce to be included. Consumers of the 206 // API can use package accounts to maintain local private keys and need can retrieve the 207 // next available nonce using PendingNonceAt. 208 type TransactionSender interface { 209 SendTransaction(ctx context.Context, tx *types.Transaction) error 210 } 211 212 // GasPricer wraps the gas price oracle, which monitors the blockchain to determine the 213 // optimal gas price given current fee market conditions. 214 type GasPricer interface { 215 SuggestGasPrice(ctx context.Context) (*big.Int, error) 216 } 217 218 // GasPricer1559 provides access to the EIP-1559 gas price oracle. 219 type GasPricer1559 interface { 220 SuggestGasTipCap(ctx context.Context) (*big.Int, error) 221 } 222 223 // FeeHistoryReader provides access to the fee history oracle. 224 type FeeHistoryReader interface { 225 FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*FeeHistory, error) 226 } 227 228 // FeeHistory provides recent fee market data that consumers can use to determine 229 // a reasonable maxPriorityFeePerGas value. 230 type FeeHistory struct { 231 OldestBlock *big.Int // block corresponding to first response value 232 Reward [][]*big.Int // list every txs priority fee per block 233 BaseFee []*big.Int // list of each block's base fee 234 GasUsedRatio []float64 // ratio of gas used out of the total available limit 235 } 236 237 // A PendingStateReader provides access to the pending state, which is the result of all 238 // known executable transactions which have not yet been included in the blockchain. It is 239 // commonly used to display the result of ’unconfirmed’ actions (e.g. wallet value 240 // transfers) initiated by the user. The PendingNonceAt operation is a good way to 241 // retrieve the next available transaction nonce for a specific account. 242 type PendingStateReader interface { 243 PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) 244 PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) 245 PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) 246 PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) 247 PendingTransactionCount(ctx context.Context) (uint, error) 248 } 249 250 // PendingContractCaller can be used to perform calls against the pending state. 251 type PendingContractCaller interface { 252 PendingCallContract(ctx context.Context, call CallMsg) ([]byte, error) 253 } 254 255 // GasEstimator wraps EstimateGas, which tries to estimate the gas needed to execute a 256 // specific transaction based on the pending state. There is no guarantee that this is the 257 // true gas limit requirement as other transactions may be added or removed by miners, but 258 // it should provide a basis for setting a reasonable default. 259 type GasEstimator interface { 260 EstimateGas(ctx context.Context, call CallMsg) (uint64, error) 261 } 262 263 // A PendingStateEventer provides access to real time notifications about changes to the 264 // pending state. 265 type PendingStateEventer interface { 266 SubscribePendingTransactions(ctx context.Context, ch chan<- *types.Transaction) (Subscription, error) 267 } 268 269 // BlockNumberReader provides access to the current block number. 270 type BlockNumberReader interface { 271 BlockNumber(ctx context.Context) (uint64, error) 272 } 273 274 // ChainIDReader provides access to the chain ID. 275 type ChainIDReader interface { 276 ChainID(ctx context.Context) (*big.Int, error) 277 }