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