github.com/klaytn/klaytn@v1.10.2/interfaces.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from interfaces.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 // Package klaytn defines interfaces for interacting with Klaytn. 22 package klaytn 23 24 import ( 25 "context" 26 "errors" 27 "math/big" 28 29 "github.com/klaytn/klaytn/blockchain/types" 30 "github.com/klaytn/klaytn/common" 31 ) 32 33 // NotFound is returned by API methods if the requested item does not exist. 34 var NotFound = errors.New("not found") 35 36 // Subscription represents an event subscription where events are 37 // delivered on a data channel. 38 type Subscription interface { 39 // Unsubscribe cancels the sending of events to the data channel 40 // and closes the error channel. 41 Unsubscribe() 42 // Err returns the subscription error channel. The error channel receives 43 // a value if there is an issue with the subscription (e.g. the network connection 44 // delivering the events has been closed). Only one value will ever be sent. 45 // The error channel is closed by Unsubscribe. 46 Err() <-chan error 47 } 48 49 // ChainReader provides access to the blockchain. The methods in this interface access raw 50 // data from either the canonical chain (when requesting by block number) or any 51 // blockchain fork that was previously downloaded and processed by the node. The block 52 // number argument can be nil to select the latest canonical block. Reading block headers 53 // should be preferred over full blocks whenever possible. 54 // 55 // The returned error is NotFound if the requested item does not exist. 56 type ChainReader interface { 57 BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) 58 BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) 59 HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) 60 HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) 61 TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) 62 TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) 63 64 // This method subscribes to notifications about changes of the head block of 65 // the canonical chain. 66 SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (Subscription, error) 67 } 68 69 // TransactionReader provides access to past transactions and their receipts. 70 // Implementations may impose arbitrary restrictions on the transactions and receipts that 71 // can be retrieved. Historic transactions may not be available. 72 // 73 // Avoid relying on this interface if possible. Contract logs (through the LogFilterer 74 // interface) are more reliable and usually safer in the presence of chain 75 // reorganisations. 76 // 77 // The returned error is NotFound if the requested item does not exist. 78 type TransactionReader interface { 79 // TransactionByHash checks the pool of pending transactions in addition to the 80 // blockchain. The isPending return value indicates whether the transaction has been 81 // mined yet. Note that the transaction may not be part of the canonical chain even if 82 // it's not pending. 83 TransactionByHash(ctx context.Context, txHash common.Hash) (tx *types.Transaction, isPending bool, err error) 84 // TransactionReceipt returns the receipt of a mined transaction. Note that the 85 // transaction may not be included in the current canonical chain even if a receipt 86 // exists. 87 TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) 88 } 89 90 // ChainStateReader wraps access to the state trie of the canonical blockchain. Note that 91 // implementations of the interface may be unable to return state values for old blocks. 92 // In many cases, using CallContract can be preferable to reading raw contract storage. 93 type ChainStateReader interface { 94 BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) 95 StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) 96 CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) 97 NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) 98 } 99 100 // SyncProgress gives progress indications when the node is synchronising with 101 // the Klaytn network. 102 type SyncProgress struct { 103 StartingBlock uint64 // Block number where sync began 104 CurrentBlock uint64 // Current block number where sync is at 105 HighestBlock uint64 // Highest alleged block number in the chain 106 PulledStates uint64 // Number of state trie entries already downloaded 107 KnownStates uint64 // Total number of state trie entries known about 108 } 109 110 // ChainSyncReader wraps access to the node's current sync status. If there's no 111 // sync currently running, it returns nil. 112 type ChainSyncReader interface { 113 SyncProgress(ctx context.Context) (*SyncProgress, error) 114 } 115 116 // CallMsg contains parameters for contract calls. 117 type CallMsg struct { 118 From common.Address // the sender of the 'transaction' 119 To *common.Address // the destination contract (nil for contract creation) 120 Gas uint64 // if 0, the call executes with near-infinite gas 121 GasPrice *big.Int // peb <-> gas exchange ratio 122 Value *big.Int // amount of peb sent along with the call 123 Data []byte // input data, usually an ABI-encoded contract method invocation 124 } 125 126 // A ContractCaller provides contract calls, essentially transactions that are executed by 127 // the EVM but not mined into the blockchain. ContractCall is a low-level method to 128 // execute such calls. For applications which are structured around specific contracts, 129 // the abigen tool provides a nicer, properly typed way to perform calls. 130 type ContractCaller interface { 131 CallContract(ctx context.Context, call CallMsg, blockNumber *big.Int) ([]byte, error) 132 } 133 134 // FilterQuery contains options for contract log filtering. 135 type FilterQuery struct { 136 BlockHash *common.Hash // used by klay_getLogs and eth_getLogs, return logs only from block with this hash 137 FromBlock *big.Int // beginning of the queried range, nil means genesis block 138 ToBlock *big.Int // end of the range, nil means latest block 139 Addresses []common.Address // restricts matches to events created by specific contracts 140 141 // The Topic list restricts matches to particular event topics. Each event has a list 142 // of topics. Topics matches a prefix of that list. An empty element slice matches any 143 // topic. Non-empty elements represent an alternative that matches any of the 144 // contained topics. 145 // 146 // Examples: 147 // {} or nil matches any topic list 148 // {{A}} matches topic A in first position 149 // {{}, {B}} matches any topic in first position, B in second position 150 // {{A}}, {B}} matches topic A in first position, B in second position 151 // {{A, B}}, {C, D}} matches topic (A OR B) in first position, (C OR D) in second position 152 Topics [][]common.Hash 153 } 154 155 // LogFilterer provides access to contract log events using a one-off query or continuous 156 // event subscription. 157 // 158 // Logs received through a streaming query subscription may have Removed set to true, 159 // indicating that the log was reverted due to a chain reorganisation. 160 type LogFilterer interface { 161 FilterLogs(ctx context.Context, q FilterQuery) ([]types.Log, error) 162 SubscribeFilterLogs(ctx context.Context, q FilterQuery, ch chan<- types.Log) (Subscription, error) 163 } 164 165 // TransactionSender wraps transaction sending. The SendTransaction method injects a 166 // signed transaction into the pending transaction pool for execution. If the transaction 167 // was a contract creation, the TransactionReceipt method can be used to retrieve the 168 // contract address after the transaction has been mined. 169 // 170 // The transaction must be signed and have a valid nonce to be included. Consumers of the 171 // API can use package accounts to maintain local private keys and need can retrieve the 172 // next available nonce using PendingNonceAt. 173 type TransactionSender interface { 174 SendTransaction(ctx context.Context, tx *types.Transaction) error 175 } 176 177 // GasPricer wraps the gas price oracle, which monitors the blockchain to determine the 178 // optimal gas price given current fee market conditions. 179 type GasPricer interface { 180 SuggestGasPrice(ctx context.Context) (*big.Int, error) 181 } 182 183 // A PendingStateReader provides access to the pending state, which is the result of all 184 // known executable transactions which have not yet been included in the blockchain. It is 185 // commonly used to display the result of ’unconfirmed’ actions (e.g. wallet value 186 // transfers) initiated by the user. The PendingNonceAt operation is a good way to 187 // retrieve the next available transaction nonce for a specific account. 188 type PendingStateReader interface { 189 PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) 190 PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) 191 PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) 192 PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) 193 PendingTransactionCount(ctx context.Context) (uint, error) 194 } 195 196 // PendingContractCaller can be used to perform calls against the pending state. 197 type PendingContractCaller interface { 198 PendingCallContract(ctx context.Context, call CallMsg) ([]byte, error) 199 } 200 201 // GasEstimator wraps EstimateGas, which tries to estimate the gas needed to execute a 202 // specific transaction based on the latest state. There is no guarantee that this is the 203 // true gas limit requirement as other transactions may be added or removed by miners, but 204 // it should provide a basis for setting a reasonable default. 205 type GasEstimator interface { 206 EstimateGas(ctx context.Context, call CallMsg) (uint64, error) 207 } 208 209 // A PendingStateEventer provides access to real time notifications about changes to the 210 // pending state. 211 type PendingStateEventer interface { 212 SubscribePendingTransactions(ctx context.Context, ch chan<- *types.Transaction) (Subscription, error) 213 }