Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinssgh committed Apr 15, 2024
1 parent 128ac8a commit caabdc2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions zetaclient/bitcoin/bitcoin_rpc_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions zetaclient/common/client_queue.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion zetaclient/evm/evm_rpc_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func NewEthClientFallback(evmCfg config.EVMConfig, logger zerolog.Logger) (*EthC
return &ethClientFallback, 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
Expand Down

0 comments on commit caabdc2

Please sign in to comment.