Skip to content

Commit

Permalink
Merge pull request #6 from catalogfi/release-0.0.6
Browse files Browse the repository at this point in the history
release 0.0.6
  • Loading branch information
tok-kkk authored Dec 19, 2023
2 parents df460b0 + 27bdd07 commit 36f3ec0
Showing 1 changed file with 73 additions and 24 deletions.
97 changes: 73 additions & 24 deletions btc/btctest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// MockClient is a mock implementation of the `btc.Client` for testing purposes. All the function can be customised to
// test different scenarios.
type MockClient struct {
Client btc.Client
FuncNet func() *chaincfg.Params
FuncGetUTXOs func(context.Context, btcutil.Address) (btc.UTXOs, error)
FuncLatestBlock func(context.Context) (int64, string, error)
Expand All @@ -24,47 +25,71 @@ type MockClient struct {
FuncGetTxOut func(context.Context, *chainhash.Hash, uint32) (*btcjson.GetTxOutResult, error)
}

// Make sure the MockClient implements the Client interface in case
// Make sure the MockClient implements the Client interface
var _ btc.Client = NewMockClient()

func NewMockClient() *MockClient {
return &MockClient{}
}

func (m *MockClient) Net() *chaincfg.Params {
return m.FuncNet()
func NewMockClientWrapper(client btc.Client) *MockClient {
return &MockClient{
Client: client,
}
}

func (m *MockClient) GetUTXOs(ctx context.Context, address btcutil.Address) (btc.UTXOs, error) {
return m.FuncGetUTXOs(ctx, address)
func (m *MockClient) Net() *chaincfg.Params {
if m.FuncNet != nil {
return m.FuncNet()
}
return m.Client.Net()
}

func (m *MockClient) LatestBlock(ctx context.Context) (int64, string, error) {
return m.FuncLatestBlock(ctx)
if m.FuncLatestBlock != nil {
return m.FuncLatestBlock(ctx)
}
return m.Client.LatestBlock(ctx)
}

func (m *MockClient) SubmitTx(ctx context.Context, tx *wire.MsgTx) error {
return m.FuncSubmitTx(ctx, tx)
if m.FuncSubmitTx != nil {
return m.FuncSubmitTx(ctx, tx)
}
return m.SubmitTx(ctx, tx)
}

func (m *MockClient) GetRawTransaction(ctx context.Context, txhash *chainhash.Hash) (*btcjson.TxRawResult, error) {
return m.FuncGetRawTransaction(ctx, txhash)
if m.FuncGetRawTransaction != nil {
return m.FuncGetRawTransaction(ctx, txhash)
}
return m.Client.GetRawTransaction(ctx, txhash)
}

func (m *MockClient) GetBlockByHeight(ctx context.Context, height int64) (*btcjson.GetBlockVerboseResult, error) {
return m.FuncGetBlockByHeight(ctx, height)
if m.FuncGetBlockByHeight != nil {
return m.FuncGetBlockByHeight(ctx, height)
}
return m.Client.GetBlockByHeight(ctx, height)
}

func (m *MockClient) GetBlockByHash(ctx context.Context, hash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error) {
return m.FuncGetBlockByHash(ctx, hash)
if m.FuncGetBlockByHash != nil {
return m.FuncGetBlockByHash(ctx, hash)
}
return m.Client.GetBlockByHash(ctx, hash)
}

func (m *MockClient) GetTxOut(ctx context.Context, hash *chainhash.Hash, vout uint32) (*btcjson.GetTxOutResult, error) {
return m.FuncGetTxOut(ctx, hash, vout)
if m.FuncGetTxOut != nil {
return m.FuncGetTxOut(ctx, hash, vout)
}
return m.Client.GetTxOut(ctx, hash, vout)
}

// MockIndexerClient for testing purpose
type MockIndexerClient struct {
Indexer btc.IndexerClient
FuncGetAddressTxs func(context.Context, btcutil.Address) ([]btc.Transaction, error)
FuncGetUTXOs func(context.Context, btcutil.Address) (btc.UTXOs, error)
FuncGetTipBlockHeight func(ctx context.Context) (uint64, error)
Expand All @@ -73,33 +98,57 @@ type MockIndexerClient struct {
FuncFeeEstimate func(ctx context.Context) (btc.FeeSuggestion, error)
}

// Make sure the MockIndexerClient implements the Client interface
var _ btc.IndexerClient = NewMockIndexerClient()

func NewMockIndexerClient() *MockIndexerClient {
return &MockIndexerClient{}
}

func NewMockIndexerClientWrapper(indexer btc.IndexerClient) *MockIndexerClient {
return &MockIndexerClient{
Indexer: indexer,
}
}

func (client MockIndexerClient) GetAddressTxs(ctx context.Context, address btcutil.Address) ([]btc.Transaction, error) {
return client.FuncGetAddressTxs(ctx, address)
if client.FuncGetAddressTxs != nil {
return client.FuncGetAddressTxs(ctx, address)
}
return client.Indexer.GetAddressTxs(ctx, address)
}

func (client MockIndexerClient) GetUTXOs(ctx context.Context, address btcutil.Address) (btc.UTXOs, error) {
return client.FuncGetUTXOs(ctx, address)
if client.FuncGetUTXOs != nil {
return client.FuncGetUTXOs(ctx, address)
}
return client.Indexer.GetUTXOs(ctx, address)
}

func (client MockIndexerClient) GetTipBlockHeight(ctx context.Context) (uint64, error) {
return client.FuncGetTipBlockHeight(ctx)
if client.FuncGetTipBlockHeight != nil {
return client.Indexer.GetTipBlockHeight(ctx)
}
return client.Indexer.GetTipBlockHeight(ctx)
}

func (client MockIndexerClient) GetTx(ctx context.Context, txid string) (btc.Transaction, error) {
return client.FuncGetTx(ctx, txid)
if client.FuncGetTx != nil {
return client.FuncGetTx(ctx, txid)
}
return client.Indexer.GetTx(ctx, txid)
}

func (client MockIndexerClient) SubmitTx(ctx context.Context, tx *wire.MsgTx) error {
return client.FuncSubmitTx(ctx, tx)
if client.FuncSubmitTx != nil {
return client.FuncSubmitTx(ctx, tx)
}
return client.Indexer.SubmitTx(ctx, tx)
}

func (client MockIndexerClient) FeeEstimate(ctx context.Context) (btc.FeeSuggestion, error) {
return client.FeeEstimate(ctx)
}

// Make sure the MockIndexerClient implements the Client interface
var _ *MockIndexerClient = NewMockIndexerClient()

func NewMockIndexerClient() *MockIndexerClient {
return &MockIndexerClient{}
if client.FuncFeeEstimate != nil {
return client.FuncFeeEstimate(ctx)
}
return client.Indexer.FeeEstimate(ctx)
}

0 comments on commit 36f3ec0

Please sign in to comment.