Skip to content

Commit

Permalink
BTC Client mocks. Drop old interface/mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Jan 15, 2025
1 parent 26e231c commit 4e79625
Show file tree
Hide file tree
Showing 6 changed files with 841 additions and 585 deletions.
10 changes: 6 additions & 4 deletions zetaclient/chains/bitcoin/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import (
"strings"
"time"

"cosmossdk.io/errors"
types "github.com/btcsuite/btcd/btcjson"
chains "github.com/btcsuite/btcd/chaincfg"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/tendermint/btcd/btcjson"
"github.com/tendermint/btcd/chaincfg"

"github.com/zeta-chain/node/zetaclient/config"
"github.com/zeta-chain/node/zetaclient/logs"
"github.com/zeta-chain/node/zetaclient/metrics"
)

// Client Bitcoin RPC client
type Client struct {
hostURL string
client *http.Client
Expand All @@ -35,8 +35,8 @@ type Client struct {
type Opt func(c *Client)

type rawResponse struct {
Result json.RawMessage `json:"result"`
Error *btcjson.RPCError `json:"error"`
Result json.RawMessage `json:"result"`
Error *types.RPCError `json:"error"`
}

const (
Expand All @@ -47,6 +47,8 @@ const (
commandID = uint64(1)
)

var _ client = (*Client)(nil)

func WithHTTP(httpClient *http.Client) Opt {
return func(c *Client) { c.client = httpClient }
}
Expand Down
14 changes: 9 additions & 5 deletions zetaclient/chains/bitcoin/client/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/pkg/errors"
"github.com/tendermint/btcd/btcjson"
)

// GetBlockVerboseByStr alias for GetBlockVerbose
Expand Down Expand Up @@ -38,13 +37,18 @@ func (c *Client) GetBlockHeightByStr(ctx context.Context, blockHash string) (int
}

// GetTransactionByStr alias for GetTransaction
func (c *Client) GetTransactionByStr(ctx context.Context, hash string) (*types.GetTransactionResult, error) {
func (c *Client) GetTransactionByStr(
ctx context.Context,
hash string,
) (*chainhash.Hash, *types.GetTransactionResult, error) {
h, err := strToHash(hash)
if err != nil {
return nil, err
return nil, nil, err
}

return c.GetTransaction(ctx, h)
tx, err := c.GetTransaction(ctx, h)

return h, tx, err
}

// GetRawTransactionByStr alias for GetRawTransaction
Expand All @@ -60,7 +64,7 @@ func (c *Client) GetRawTransactionByStr(ctx context.Context, hash string) (*btcu
// GetRawTransactionResult gets the raw tx result
func (c *Client) GetRawTransactionResult(ctx context.Context,
hash *chainhash.Hash,
res *btcjson.GetTransactionResult,
res *types.GetTransactionResult,
) (types.TxRawResult, error) {
switch {
case res.Confirmations == 0:
Expand Down
68 changes: 68 additions & 0 deletions zetaclient/chains/bitcoin/client/mockgen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package client

import (
"context"
"time"

types "github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
hash "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
)

// client represents interface version of Client.
// It's unexported on purpose ONLY for mock generation.
//
//go:generate mockery --name client --structname BitcoinClient --filename bitcoin_client.go --output ../../../testutils/mocks
type client interface {
Ping(ctx context.Context) error
Healthcheck(ctx context.Context, tssAddress btcutil.Address) (time.Time, error)
GetNetworkInfo(ctx context.Context) (*types.GetNetworkInfoResult, error)

GetBlockCount(ctx context.Context) (int64, error)
GetBlockHash(ctx context.Context, blockHeight int64) (*hash.Hash, error)
GetBlockHeader(ctx context.Context, hash *hash.Hash) (*wire.BlockHeader, error)
GetBlockVerbose(ctx context.Context, hash *hash.Hash) (*types.GetBlockVerboseTxResult, error)

GetTransaction(ctx context.Context, hash *hash.Hash) (*types.GetTransactionResult, error)
GetRawTransaction(ctx context.Context, hash *hash.Hash) (*btcutil.Tx, error)
GetRawTransactionVerbose(ctx context.Context, hash *hash.Hash) (*types.TxRawResult, error)
GetRawTransactionResult(
ctx context.Context,
hash *hash.Hash,
res *types.GetTransactionResult,
) (types.TxRawResult, error)

GetTransactionFeeAndRate(ctx context.Context, tx *types.TxRawResult) (int64, int64, error)

SendRawTransaction(ctx context.Context, tx *wire.MsgTx, allowHighFees bool) (*hash.Hash, error)

EstimateSmartFee(
ctx context.Context,
confTarget int64,
mode *types.EstimateSmartFeeMode,
) (*types.EstimateSmartFeeResult, error)

ListUnspent(ctx context.Context) ([]types.ListUnspentResult, error)
ListUnspentMinMaxAddresses(
ctx context.Context,
minConf, maxConf int,
addresses []btcutil.Address,
) ([]types.ListUnspentResult, error)

CreateWallet(ctx context.Context, name string, opts ...rpcclient.CreateWalletOpt) (*types.CreateWalletResult, error)
GetBalance(ctx context.Context, account string) (btcutil.Amount, error)
GetNewAddress(ctx context.Context, account string) (btcutil.Address, error)
GenerateToAddress(
ctx context.Context,
numBlocks int64,
address btcutil.Address,
maxTries *int64,
) ([]*hash.Hash, error)

GetBlockVerboseByStr(ctx context.Context, blockHash string) (*types.GetBlockVerboseTxResult, error)
GetBlockHeightByStr(ctx context.Context, blockHash string) (int64, error)
GetTransactionByStr(ctx context.Context, hash string) (*hash.Hash, *types.GetTransactionResult, error)
GetRawTransactionByStr(ctx context.Context, hash string) (*btcutil.Tx, error)
}
28 changes: 0 additions & 28 deletions zetaclient/chains/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import (
"math/big"

sdkmath "cosmossdk.io/math"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
cometbfttypes "github.com/cometbft/cometbft/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -148,29 +143,6 @@ type ZetacoreClient interface {
NewBlockSubscriber(ctx context.Context) (chan cometbfttypes.EventDataNewBlock, error)
}

// BTCRPCClient is the interface for BTC RPC client
//
// WARN: you must add any RPCs used on mainnet/testnet to the whitelist in https://github.com/zeta-chain/bitcoin-core-docker
type BTCRPCClient interface {
GetNetworkInfo() (*btcjson.GetNetworkInfoResult, error)
CreateWallet(name string, opts ...rpcclient.CreateWalletOpt) (*btcjson.CreateWalletResult, error)
GetNewAddress(account string) (btcutil.Address, error)
GenerateToAddress(numBlocks int64, address btcutil.Address, maxTries *int64) ([]*chainhash.Hash, error)
GetBalance(account string) (btcutil.Amount, error)
SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (*chainhash.Hash, error)
ListUnspent() ([]btcjson.ListUnspentResult, error)
ListUnspentMinMaxAddresses(minConf int, maxConf int, addrs []btcutil.Address) ([]btcjson.ListUnspentResult, error)
EstimateSmartFee(confTarget int64, mode *btcjson.EstimateSmartFeeMode) (*btcjson.EstimateSmartFeeResult, error)
GetTransaction(txHash *chainhash.Hash) (*btcjson.GetTransactionResult, error)
GetRawTransaction(txHash *chainhash.Hash) (*btcutil.Tx, error)
GetRawTransactionVerbose(txHash *chainhash.Hash) (*btcjson.TxRawResult, error)
GetBlockCount() (int64, error)
GetBlockHash(blockHeight int64) (*chainhash.Hash, error)
GetBlockVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error)
GetBlockVerboseTx(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseTxResult, error)
GetBlockHeader(blockHash *chainhash.Hash) (*wire.BlockHeader, error)
}

// EVMRPCClient is the interface for EVM RPC client
type EVMRPCClient interface {
bind.ContractBackend
Expand Down
Loading

0 comments on commit 4e79625

Please sign in to comment.