diff --git a/zetaclient/bitcoin/bitcoin_rpc_fallback.go b/zetaclient/bitcoin/bitcoin_rpc_fallback.go index e9961a51f9..42619d85e7 100644 --- a/zetaclient/bitcoin/bitcoin_rpc_fallback.go +++ b/zetaclient/bitcoin/bitcoin_rpc_fallback.go @@ -17,12 +17,14 @@ import ( var _ interfaces.BTCRPCClient = &RPCClientFallback{} +// RPCClientFallback - a decorator type for adding fallback capability to bitcoin rpc client type RPCClientFallback struct { btcConfig config.BTCConfig rpcClients *common.ClientQueue logger zerolog.Logger } +// NewRPCClientFallback - Constructor, reads config and connects to each client in the endpoint list func NewRPCClientFallback(cfg config.BTCConfig, logger zerolog.Logger) (*RPCClientFallback, error) { if len(cfg.Endpoints) == 0 { return nil, errors.New("invalid endpoints") @@ -56,6 +58,10 @@ func NewRPCClientFallback(cfg config.BTCConfig, logger zerolog.Logger) (*RPCClie return &rpcClientFallback, nil } +// Below is an implementation of the BTCRPCClient interface. The logic is similar for all functions, the first client +// in the queue is used to attempt the rpc call. If this fails then it will attempt to call the next client in the list +// until it has tried them all. + func (R *RPCClientFallback) GetNetworkInfo() (*btcjson.GetNetworkInfoResult, error) { var res *btcjson.GetNetworkInfoResult var err error diff --git a/zetaclient/common/client_queue.go b/zetaclient/common/client_queue.go index ceceb86ce5..d37298bd02 100644 --- a/zetaclient/common/client_queue.go +++ b/zetaclient/common/client_queue.go @@ -1,23 +1,28 @@ package common +// ClientQueue - Generic data structure to keep track of fallback clients used to connect to EVM and Bitcoin nodes type ClientQueue struct { clientList []any } +// NewClientQueue - constructor func NewClientQueue() *ClientQueue { return &ClientQueue{ clientList: []any{}, } } +// Append - adds another client to the end of the queue func (c *ClientQueue) Append(item any) { c.clientList = append(c.clientList, item) } +// Length - returns total length of queue or number of clients func (c *ClientQueue) Length() int { return len(c.clientList) } +// Next - rotates list of clients by moving the first to the last func (c *ClientQueue) Next() { if len(c.clientList) < 1 { return @@ -26,6 +31,7 @@ func (c *ClientQueue) Next() { c.clientList = append(c.clientList[1:], c.clientList[:1]...) } +// First - returns the first client in the list or highest priority func (c *ClientQueue) First() any { if len(c.clientList) < 1 { return nil diff --git a/zetaclient/evm/evm_rpc_fallback.go b/zetaclient/evm/evm_rpc_fallback.go index a5b0f444cc..9daafa4e75 100644 --- a/zetaclient/evm/evm_rpc_fallback.go +++ b/zetaclient/evm/evm_rpc_fallback.go @@ -57,7 +57,9 @@ func NewEthClientFallback(evmCfg config.EVMConfig, logger zerolog.Logger) (*EthC return ðClientFallback, nil } -// The following functions are wrappers for EVMRPCClient interface +// The following functions are wrappers for EVMRPCClient interface. The logic is similar for all functions, the first client +// in the queue is used to attempt the rpc call. If this fails then it will attempt to call the next client in the list +// until it is successful or returns an error when the list of clients have been exhausted. func (e *EthClientFallback) CodeAt(ctx context.Context, contract ethcommon.Address, blockNumber *big.Int) ([]byte, error) { var res []byte