Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinssgh committed Apr 8, 2024
1 parent bc3a88c commit ba95d2d
Show file tree
Hide file tree
Showing 5 changed files with 430 additions and 24 deletions.
9 changes: 2 additions & 7 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/btcsuite/btcd/rpcclient"
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/zeta-chain/zetacore/pkg/chains"
Expand Down Expand Up @@ -102,18 +100,15 @@ func DebugCmd() *cobra.Command {
}
ob.WithZetaClient(bridge)
ob.WithLogger(chainLogger)
var ethRPC *ethrpc.EthRPC
var client *ethclient.Client
var client *evm.EthClientFallback
coinType := coin.CoinType_Cmd
for chain, evmConfig := range cfg.GetAllEVMConfigs() {
if chainID == chain {
ethRPC = ethrpc.NewEthRPC(evmConfig.Endpoint)
client, err = ethclient.Dial(evmConfig.Endpoint)
client, err = evm.NewEthClientFallback(&evmConfig, chainLogger)
if err != nil {
return err
}
ob.WithEvmClient(client)
ob.WithEvmJSONRPC(ethRPC)
ob.WithChain(*chains.GetChainFromChainID(chainID))
}
}
Expand Down
34 changes: 34 additions & 0 deletions zetaclient/common/client_queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package common

type ClientQueue struct {
clientList []any
}

func NewClientQueue() *ClientQueue {
return &ClientQueue{
clientList: []any{},
}
}

func (c *ClientQueue) Append(item any) {
c.clientList = append(c.clientList, item)
}

func (c *ClientQueue) Length() int {
return len(c.clientList)
}

func (c *ClientQueue) Next() {
if len(c.clientList) < 1 {
return
}
// Rotate client order by 1
c.clientList = append(c.clientList[1:], c.clientList[:1]...)
}

func (c *ClientQueue) First() any {
if len(c.clientList) < 1 {
return nil
}
return c.clientList[0]
}
2 changes: 1 addition & 1 deletion zetaclient/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type ClientConfiguration struct {

type EVMConfig struct {
Chain chains.Chain
Endpoint string
Endpoint []string
}

type BTCConfig struct {
Expand Down
22 changes: 6 additions & 16 deletions zetaclient/evm/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rlp"
lru "github.com/hashicorp/golang-lru"
"github.com/onrik/ethrpc"
Expand Down Expand Up @@ -70,8 +69,7 @@ var _ interfaces.ChainClient = &ChainClient{}
// Filled with above constants depending on chain
type ChainClient struct {
chain chains.Chain
evmClient interfaces.EVMRPCClient
evmJSONRPC interfaces.EVMJSONRPCClient
evmClient EthClientFallbackInterface
zetaClient interfaces.ZetaCoreBridger
Tss interfaces.TSSSigner
lastBlockScanned uint64
Expand Down Expand Up @@ -130,14 +128,12 @@ func NewEVMChainClient(
ob.outTXConfirmedReceipts = make(map[string]*ethtypes.Receipt)
ob.outTXConfirmedTransactions = make(map[string]*ethtypes.Transaction)

ob.logger.Chain.Info().Msgf("Chain %s endpoint %s", ob.chain.ChainName.String(), evmCfg.Endpoint)
client, err := ethclient.Dial(evmCfg.Endpoint)
// Initialize evm client
client, err := NewEthClientFallback(&evmCfg, chainLogger)
if err != nil {
ob.logger.Chain.Error().Err(err).Msg("eth Client Dial")
return nil, err
}
ob.evmClient = client
ob.evmJSONRPC = ethrpc.NewEthRPC(evmCfg.Endpoint)

// create block header and block caches
ob.blockCache, err = lru.New(1000)
Expand Down Expand Up @@ -176,18 +172,12 @@ func (ob *ChainClient) WithLogger(logger zerolog.Logger) {
}
}

func (ob *ChainClient) WithEvmClient(client interfaces.EVMRPCClient) {
func (ob *ChainClient) WithEvmClient(client *EthClientFallback) {
ob.Mu.Lock()
defer ob.Mu.Unlock()
ob.evmClient = client
}

func (ob *ChainClient) WithEvmJSONRPC(client interfaces.EVMJSONRPCClient) {
ob.Mu.Lock()
defer ob.Mu.Unlock()
ob.evmJSONRPC = client
}

func (ob *ChainClient) WithZetaClient(bridge interfaces.ZetaCoreBridger) {
ob.Mu.Lock()
defer ob.Mu.Unlock()
Expand Down Expand Up @@ -1329,7 +1319,7 @@ func (ob *ChainClient) GetTxID(nonce uint64) string {

// BlockByNumber query block by number via JSON-RPC
func (ob *ChainClient) BlockByNumber(blockNumber int) (*ethrpc.Block, error) {
block, err := ob.evmJSONRPC.EthGetBlockByNumber(blockNumber, true)
block, err := ob.evmClient.EthGetBlockByNumber(blockNumber, true)
if err != nil {
return nil, err
}
Expand All @@ -1344,7 +1334,7 @@ func (ob *ChainClient) BlockByNumber(blockNumber int) (*ethrpc.Block, error) {

// TransactionByHash query transaction by hash via JSON-RPC
func (ob *ChainClient) TransactionByHash(txHash string) (*ethrpc.Transaction, bool, error) {
tx, err := ob.evmJSONRPC.EthGetTransactionByHash(txHash)
tx, err := ob.evmClient.EthGetTransactionByHash(txHash)
if err != nil {
return nil, false, err
}
Expand Down
Loading

0 comments on commit ba95d2d

Please sign in to comment.