client

package
v1.6.0-beta1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2022 License: MIT Imports: 29 Imported by: 2

README

EVM Client

Node FSM

stateDiagram-v2
    [*] --> Started : Start()
    
    state Started {
        [*] --> Undialed
        Undialed --> Unreachable
        Undialed --> Dialed
        
        Unreachable --> Dialed 
        
        Dialed --> Unreachable
        Dialed --> InvalidChainID
        Dialed --> Alive
        
        InvalidChainID --> Unreachable
        InvalidChainID --> Alive
        
        Alive --> Unreachable
        Alive --> OutOfSync
        
        OutOfSync --> Unreachable
        OutOfSync --> InvalidChainID    
        OutOfSync --> Alive    
    }
    
    Started --> Closed : Close()
    Closed --> [*]

Documentation

Index

Constants

View Source
const (
	NonceTooLow = iota
	ReplacementTransactionUnderpriced
	LimitReached
	TransactionAlreadyInMempool
	TerminallyUnderpriced
	InsufficientEth
	TxFeeExceedsCap
	// Note: OptimismFeeTooLow/OptimismFeeTooHigh have a very specific meaning
	// specific to Optimism and clones. Do not implement this for non-L2
	// chains. This is potentially confusing because some RPC nodes e.g.
	// Nethermind implement an error called `FeeTooLow` which has distinct
	// meaning from this one.
	OptimismFeeTooLow
	OptimismFeeTooHigh
	TransactionAlreadyMined
	Fatal
)
View Source
const (
	// NodeStateUndialed is the first state of a virgin node
	NodeStateUndialed = NodeState(iota)
	// NodeStateDialed is after a node has successfully dialed but before it has verified the correct chain ID
	NodeStateDialed
	// NodeStateInvalidChainID is after chain ID verification failed
	NodeStateInvalidChainID
	// NodeStateAlive is a healthy node after chain ID verification succeeded
	NodeStateAlive
	// NodeStateUnreachable is a node that cannot be dialed or has disconnected
	NodeStateUnreachable
	// NodeStateOutOfSync is a node that is accepting connections but exceeded
	// the failure threshold without sending any new heads. It will be
	// disconnected, then put into a revive loop and re-awakened after redial
	// if a new head arrives
	NodeStateOutOfSync
	// NodeStateClosed is after the connection has been closed and the node is at the end of its lifecycle
	NodeStateClosed
)
View Source
const NullClientChainID = 0

NullClientChainID the ChainID that nullclient will return 0 is never used as a real chain ID so makes sense as a dummy value here

Variables

View Source
var (
	// PromEVMPoolRPCNodeStates reports current RPC node state
	PromEVMPoolRPCNodeStates = promauto.NewGaugeVec(prometheus.GaugeOpts{
		Name: "evm_pool_rpc_node_states",
		Help: "The number of RPC nodes currently in the given state for the given chain",
	}, []string{"evmChainID", "state"})
)

Functions

func DefaultQueryCtx

func DefaultQueryCtx(ctxs ...context.Context) (ctx context.Context, cancel context.CancelFunc)

DefaultQueryCtx returns a context with a sensible sanity limit timeout for queries to the eth node This is a sanity limit to try to work around poorly behaved remote WS endpoints that fail to send us data that we requested NO QUERY should ever take longer than this

func ExtractRevertReasonFromRPCError

func ExtractRevertReasonFromRPCError(err error) (string, error)

ExtractRevertReasonFromRPCError attempts to extract the revert reason from the response of an RPC eth_call that reverted by parsing the message from the "data" field ex: kovan (parity) { "error": { "code" : -32015, "data": "Reverted 0xABC123...", "message": "VM execution error." } } // revert reason always omitted rinkeby / ropsten (geth) { "error": { "code": 3, "data": "0x0xABC123...", "message": "execution reverted: hello world" } } // revert reason included in message

func NewClientWithNodes

func NewClientWithNodes(logger logger.Logger, primaryNodes []Node, sendOnlyNodes []SendOnlyNode, chainID *big.Int) (*client, error)

NewClientWithNodes instantiates a client from a list of nodes Currently only supports one primary

func ToBlockNumArg

func ToBlockNumArg(number *big.Int) string

Types

type BatchSender added in v1.4.0

type BatchSender interface {
	BatchCallContext(ctx context.Context, b []rpc.BatchElem) error
}

type CallArgs

type CallArgs struct {
	From common.Address `json:"from"`
	To   common.Address `json:"to"`
	Data hexutil.Bytes  `json:"data"`
}

CallArgs represents the data used to call the balance method of a contract. "To" is the address of the ERC contract. "Data" is the message sent to the contract. "From" is the sender address.

type Client

type Client interface {
	Dial(ctx context.Context) error
	Close()
	ChainID() *big.Int
	// NodeStates returns a map of node ID->node state
	// It might be nil or empty, e.g. for mock clients etc
	NodeStates() map[int32]string

	GetERC20Balance(address common.Address, contractAddress common.Address) (*big.Int, error)
	GetLINKBalance(linkAddress common.Address, address common.Address) (*assets.Link, error)
	GetEthBalance(ctx context.Context, account common.Address, blockNumber *big.Int) (*assets.Eth, error)

	// Wrapped RPC methods
	Call(result interface{}, method string, args ...interface{}) error
	CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
	BatchCallContext(ctx context.Context, b []rpc.BatchElem) error
	// BatchCallContextAll calls BatchCallContext for every single node including
	// sendonlys.
	// CAUTION: This should only be used for mass re-transmitting transactions, it
	// might have unexpected effects to use it for anything else.
	BatchCallContextAll(ctx context.Context, b []rpc.BatchElem) error

	// HeadByNumber is a reimplemented version of HeaderByNumber due to a
	// difference in how block header hashes are calculated by Parity nodes
	// running on Kovan. We have to return our own wrapper type to capture the
	// correct hash from the RPC response.
	HeadByNumber(ctx context.Context, n *big.Int) (*evmtypes.Head, error)
	SubscribeNewHead(ctx context.Context, ch chan<- *evmtypes.Head) (ethereum.Subscription, error)

	// Wrapped Geth client methods
	SendTransaction(ctx context.Context, tx *types.Transaction) error
	PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
	PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
	NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
	TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
	BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
	BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
	BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
	FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
	SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
	EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error)
	SuggestGasPrice(ctx context.Context) (*big.Int, error)
	CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
	CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)

	// bind.ContractBackend methods
	HeaderByNumber(context.Context, *big.Int) (*types.Header, error)
	SuggestGasTipCap(ctx context.Context) (*big.Int, error)
}

Client is the interface used to interact with an ethereum node.

type ClientErrors

type ClientErrors = map[int]*regexp.Regexp

type JsonError

type JsonError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

go-ethereum@v1.10.0/rpc/json.go

func ExtractRPCError

func ExtractRPCError(err error) *JsonError

func (*JsonError) Error

func (err *JsonError) Error() string

func (*JsonError) String

func (err *JsonError) String() string

type Node

type Node interface {
	Start(ctx context.Context) error
	Close()

	State() NodeState
	// Unique identifier for node
	ID() int32
	ChainID() *big.Int

	CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
	BatchCallContext(ctx context.Context, b []rpc.BatchElem) error
	SendTransaction(ctx context.Context, tx *types.Transaction) error
	PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
	PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
	NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
	TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
	BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
	BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
	BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
	FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
	SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
	EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error)
	SuggestGasPrice(ctx context.Context) (*big.Int, error)
	CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
	CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
	HeaderByNumber(context.Context, *big.Int) (*types.Header, error)
	SuggestGasTipCap(ctx context.Context) (*big.Int, error)
	EthSubscribe(ctx context.Context, channel chan<- *evmtypes.Head, args ...interface{}) (ethereum.Subscription, error)

	String() string
}

Node represents a client that connects to an ethereum-compatible RPC node

func NewNode

func NewNode(nodeCfg NodeConfig, lggr logger.Logger, wsuri url.URL, httpuri *url.URL, name string, id int32, chainID *big.Int) Node

NewNode returns a new *node as Node

type NodeConfig added in v1.3.0

type NodeConfig interface {
	NodeNoNewHeadsThreshold() time.Duration
	NodePollFailureThreshold() uint32
	NodePollInterval() time.Duration
}

NodeConfig allows configuration of the node

type NodeState

type NodeState int

NodeState represents the current state of the node Node is a FSM (finite state machine)

func (NodeState) GoString added in v1.3.0

func (n NodeState) GoString() string

GoString prints a prettier state

func (NodeState) String added in v1.3.0

func (n NodeState) String() string

type NullClient

type NullClient struct {
	// contains filtered or unexported fields
}

NullClient satisfies the Client but has no side effects

func NewNullClient

func NewNullClient(cid *big.Int, lggr logger.Logger) *NullClient

func (*NullClient) BalanceAt

func (nc *NullClient) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)

func (*NullClient) BatchCallContext

func (nc *NullClient) BatchCallContext(ctx context.Context, b []rpc.BatchElem) error

func (*NullClient) BatchCallContextAll added in v1.3.0

func (nc *NullClient) BatchCallContextAll(ctx context.Context, b []rpc.BatchElem) error

BatchCallContextAll implements evmclient.Client interface

func (*NullClient) BlockByHash added in v1.4.0

func (nc *NullClient) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)

func (*NullClient) BlockByNumber

func (nc *NullClient) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)

func (*NullClient) Call

func (nc *NullClient) Call(result interface{}, method string, args ...interface{}) error

func (*NullClient) CallContext

func (nc *NullClient) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error

func (*NullClient) CallContract

func (nc *NullClient) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)

func (*NullClient) ChainID

func (nc *NullClient) ChainID() *big.Int

func (*NullClient) Close

func (nc *NullClient) Close()

func (*NullClient) CodeAt

func (nc *NullClient) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)

func (*NullClient) Dial

func (nc *NullClient) Dial(context.Context) error

func (*NullClient) EstimateGas

func (nc *NullClient) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error)

func (*NullClient) FilterLogs

func (nc *NullClient) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)

func (*NullClient) GetERC20Balance

func (nc *NullClient) GetERC20Balance(address common.Address, contractAddress common.Address) (*big.Int, error)

func (*NullClient) GetEthBalance

func (nc *NullClient) GetEthBalance(context.Context, common.Address, *big.Int) (*assets.Eth, error)

func (*NullClient) GetLINKBalance

func (nc *NullClient) GetLINKBalance(linkAddress common.Address, address common.Address) (*assets.Link, error)

func (*NullClient) HeadByNumber

func (nc *NullClient) HeadByNumber(ctx context.Context, n *big.Int) (*evmtypes.Head, error)

func (*NullClient) HeaderByNumber

func (nc *NullClient) HeaderByNumber(ctx context.Context, n *big.Int) (*types.Header, error)

func (*NullClient) NodeStates added in v1.3.0

func (nc *NullClient) NodeStates() map[int32]string

NodeStates implements evmclient.Client

func (*NullClient) NonceAt

func (nc *NullClient) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)

func (*NullClient) PendingCodeAt

func (nc *NullClient) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)

func (*NullClient) PendingNonceAt

func (nc *NullClient) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)

func (*NullClient) SendTransaction

func (nc *NullClient) SendTransaction(ctx context.Context, tx *types.Transaction) error

func (*NullClient) SubscribeFilterLogs

func (nc *NullClient) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)

func (*NullClient) SubscribeNewHead

func (nc *NullClient) SubscribeNewHead(ctx context.Context, ch chan<- *evmtypes.Head) (ethereum.Subscription, error)

func (*NullClient) SuggestGasPrice

func (nc *NullClient) SuggestGasPrice(ctx context.Context) (*big.Int, error)

func (*NullClient) SuggestGasTipCap

func (nc *NullClient) SuggestGasTipCap(ctx context.Context) (tipCap *big.Int, err error)

func (*NullClient) TransactionReceipt

func (nc *NullClient) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)

type Pool

type Pool struct {
	utils.StartStopOnce
	// contains filtered or unexported fields
}

Pool represents an abstraction over one or more primary nodes It is responsible for liveness checking and balancing queries across live nodes

func NewPool

func NewPool(logger logger.Logger, nodes []Node, sendonlys []SendOnlyNode, chainID *big.Int) *Pool

func (*Pool) BalanceAt

func (p *Pool) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)

func (*Pool) BatchCallContext

func (p *Pool) BatchCallContext(ctx context.Context, b []rpc.BatchElem) error

func (*Pool) BatchCallContextAll added in v1.3.0

func (p *Pool) BatchCallContextAll(ctx context.Context, b []rpc.BatchElem) error

BatchCallContextAll calls BatchCallContext for every single node including sendonlys. CAUTION: This should only be used for mass re-transmitting transactions, it might have unexpected effects to use it for anything else.

func (*Pool) BlockByHash added in v1.4.0

func (p *Pool) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)

func (*Pool) BlockByNumber

func (p *Pool) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)

func (*Pool) CallContext

func (p *Pool) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error

func (*Pool) CallContract

func (p *Pool) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)

func (*Pool) ChainID

func (p *Pool) ChainID() *big.Int

func (*Pool) Close

func (p *Pool) Close()

Close tears down the pool and closes all nodes

func (*Pool) CodeAt

func (p *Pool) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)

func (*Pool) Dial

func (p *Pool) Dial(ctx context.Context) error

Dial starts every node in the pool

func (*Pool) EstimateGas

func (p *Pool) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error)

func (*Pool) EthSubscribe

func (p *Pool) EthSubscribe(ctx context.Context, channel chan<- *evmtypes.Head, args ...interface{}) (ethereum.Subscription, error)

EthSubscribe implements evmclient.Client

func (*Pool) FilterLogs

func (p *Pool) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)

func (*Pool) HeaderByNumber

func (p *Pool) HeaderByNumber(ctx context.Context, n *big.Int) (*types.Header, error)

bind.ContractBackend methods

func (*Pool) NonceAt

func (p *Pool) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)

func (*Pool) PendingCodeAt

func (p *Pool) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)

func (*Pool) PendingNonceAt

func (p *Pool) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)

func (*Pool) SendTransaction

func (p *Pool) SendTransaction(ctx context.Context, tx *types.Transaction) error

Wrapped Geth client methods

func (*Pool) SubscribeFilterLogs

func (p *Pool) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)

func (*Pool) SuggestGasPrice

func (p *Pool) SuggestGasPrice(ctx context.Context) (*big.Int, error)

func (*Pool) SuggestGasTipCap

func (p *Pool) SuggestGasTipCap(ctx context.Context) (*big.Int, error)

func (*Pool) TransactionReceipt

func (p *Pool) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)

type SendError

type SendError struct {
	// contains filtered or unexported fields
}

fatal means this transaction can never be accepted even with a different nonce or higher gas price

func NewFatalSendError

func NewFatalSendError(e error) *SendError

func NewSendError

func NewSendError(e error) *SendError

func NewSendErrorS

func NewSendErrorS(s string) *SendError

func (*SendError) CauseStr

func (s *SendError) CauseStr() string

CauseStr returns the string of the original error

func (*SendError) Error

func (s *SendError) Error() string

func (*SendError) Fatal

func (s *SendError) Fatal() bool

Fatal indicates whether the error should be considered fatal or not Fatal errors mean that no matter how many times the send is retried, no node will ever accept it

func (*SendError) IsInsufficientEth

func (s *SendError) IsInsufficientEth() bool

func (*SendError) IsNonceTooLowError

func (s *SendError) IsNonceTooLowError() bool

func (*SendError) IsOptimismFeeTooHigh added in v1.5.0

func (s *SendError) IsOptimismFeeTooHigh() bool

IsOptimismFeeTooHigh is an optimism-specific error returned when total fee is too high

func (*SendError) IsOptimismFeeTooLow added in v1.5.0

func (s *SendError) IsOptimismFeeTooLow() bool

IsOptimismFeeTooLow is an optimism-specific error returned when total fee is too low

func (*SendError) IsReplacementUnderpriced

func (s *SendError) IsReplacementUnderpriced() bool

IsReplacementUnderpriced indicates that a transaction already exists in the mempool with this nonce but a different gas price or payload

func (*SendError) IsTemporarilyUnderpriced

func (s *SendError) IsTemporarilyUnderpriced() bool

func (*SendError) IsTerminallyUnderpriced

func (s *SendError) IsTerminallyUnderpriced() bool

IsTerminallyUnderpriced indicates that this transaction is so far underpriced the node won't even accept it in the first place

func (*SendError) IsTransactionAlreadyInMempool

func (s *SendError) IsTransactionAlreadyInMempool() bool

Geth/parity returns this error if the transaction is already in the node's mempool

func (*SendError) IsTransactionAlreadyMined added in v1.3.0

func (s *SendError) IsTransactionAlreadyMined() bool

IsTransactionAlreadyMined - Harmony returns this error if the transaction has already been mined

func (*SendError) IsTxFeeExceedsCap added in v1.6.0

func (s *SendError) IsTxFeeExceedsCap() bool

IsTxFeeExceedsCap returns true if the transaction and gas price are combined in some way that makes the total transaction too expensive for the eth node to accept at all. No amount of retrying at this or higher gas prices can ever succeed.

type SendOnlyNode

type SendOnlyNode interface {
	// Start may attempt to connect to the node, but should only return error for misconfiguration - never for temporary errors.
	Start(context.Context) error
	Close()

	ChainID() (chainID *big.Int)

	SendTransaction(ctx context.Context, tx *types.Transaction) error
	BatchCallContext(ctx context.Context, b []rpc.BatchElem) error

	String() string
}

SendOnlyNode represents one ethereum node used as a sendonly

func NewSendOnlyNode

func NewSendOnlyNode(lggr logger.Logger, httpuri url.URL, name string, chainID *big.Int) SendOnlyNode

NewSendOnlyNode returns a new sendonly node

type Subscription

type Subscription interface {
	Err() <-chan error
	Unsubscribe()
}

This interface only exists so that we can generate a mock for it. It is identical to `ethereum.Subscription`.

type TxSender added in v1.4.0

type TxSender interface {
	SendTransaction(ctx context.Context, tx *types.Transaction) error
	ChainID(context.Context) (*big.Int, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL