Skip to content

Commit

Permalink
add bitcoin support and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinssgh committed Apr 15, 2024
1 parent dd5cb48 commit 161c6b2
Show file tree
Hide file tree
Showing 13 changed files with 759 additions and 96 deletions.
12 changes: 1 addition & 11 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"sync"

"github.com/btcsuite/btcd/rpcclient"
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -177,16 +176,7 @@ func DebugCmd() *cobra.Command {
obBtc.WithZetaClient(bridge)
obBtc.WithLogger(chainLogger)
obBtc.WithChain(*chains.GetChainFromChainID(chainID))
connCfg := &rpcclient.ConnConfig{
Host: cfg.BitcoinConfig.RPCHost,
User: cfg.BitcoinConfig.RPCUsername,
Pass: cfg.BitcoinConfig.RPCPassword,
HTTPPostMode: true,
DisableTLS: true,
Params: cfg.BitcoinConfig.RPCParams,
}

btcClient, err := rpcclient.New(connCfg, nil)
btcClient, err := bitcoin.NewRPCClientFallback(cfg.BitcoinConfig, chainLogger)
if err != nil {
return err
}
Expand Down
14 changes: 8 additions & 6 deletions cmd/zetaclientd/start_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ func maskCfg(cfg config.Config) string {
HsmHotKey: cfg.HsmHotKey,
}

endpoints := make([]config.BTCEndpoint, len(cfg.BitcoinConfig.Endpoints))
copy(endpoints, cfg.BitcoinConfig.Endpoints)
maskedCfg.BitcoinConfig = config.BTCConfig{
RPCUsername: cfg.BitcoinConfig.RPCUsername,
RPCPassword: cfg.BitcoinConfig.RPCPassword,
RPCHost: cfg.BitcoinConfig.RPCHost,
RPCParams: cfg.BitcoinConfig.RPCParams,
Endpoints: endpoints,
}

restrictedAddresses := make([]string, len(cfg.ComplianceConfig.RestrictedAddresses))
Expand Down Expand Up @@ -116,8 +115,11 @@ func maskCfg(cfg config.Config) string {
chain.Endpoints[i] = endpointURL.Hostname()
}
}
maskedCfg.BitcoinConfig.RPCUsername = ""
maskedCfg.BitcoinConfig.RPCPassword = ""

for i := range maskedCfg.BitcoinConfig.Endpoints {
maskedCfg.BitcoinConfig.Endpoints[i].RPCUsername = ""
maskedCfg.BitcoinConfig.Endpoints[i].RPCPassword = ""
}

return maskedCfg.String()
}
22 changes: 4 additions & 18 deletions zetaclient/bitcoin/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
ethcommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -108,7 +107,7 @@ func (ob *BTCChainClient) WithLogger(logger zerolog.Logger) {
}
}

func (ob *BTCChainClient) WithBtcClient(client *rpcclient.Client) {
func (ob *BTCChainClient) WithBtcClient(client interfaces.BTCRPCClient) {
ob.Mu.Lock()
defer ob.Mu.Unlock()
ob.rpcClient = client
Expand Down Expand Up @@ -175,24 +174,11 @@ func NewBitcoinClient(
return nil, fmt.Errorf("btc chains params not initialized")
}
ob.params = *chainParams

// initialize the Client
ob.logger.Chain.Info().Msgf("Chain %s endpoint %s", ob.chain.String(), btcCfg.RPCHost)
connCfg := &rpcclient.ConnConfig{
Host: btcCfg.RPCHost,
User: btcCfg.RPCUsername,
Pass: btcCfg.RPCPassword,
HTTPPostMode: true,
DisableTLS: true,
Params: btcCfg.RPCParams,
}
client, err := rpcclient.New(connCfg, nil)
ob.rpcClient, err = NewRPCClientFallback(btcCfg, ob.logger.Chain)
if err != nil {
return nil, fmt.Errorf("error creating rpc client: %s", err)
}
ob.rpcClient = client
err = client.Ping()
if err != nil {
return nil, fmt.Errorf("error ping the bitcoin server: %s", err)
return nil, err
}

ob.BlockCache, err = lru.New(btcBlocksPerDay)
Expand Down
Loading

0 comments on commit 161c6b2

Please sign in to comment.