gitlab.com/flarenetwork/coreth@v0.1.1/interfaces/interfaces.go (about)

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