Skip to content

Commit 0232f80

Browse files
feat: FLY-2237 bitcoin/rootstock peer watchers
1 parent 826afc3 commit 0232f80

35 files changed

+1034
-5
lines changed

.mockery.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ packages:
115115
interfaces:
116116
EclipseCheckUseCase:
117117
UpdateBtcReleaseUseCase:
118+
NodePeerAlertUseCase:
118119
github.com/rsksmart/liquidity-provider-server/internal/adapters/entrypoints/watcher/monitoring:
119120
interfaces:
120121
GetAssetReportUseCase:

cmd/application/lps/application.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ func (app *Application) prepareWatchers() ([]watcher.Watcher, error) {
172172
app.watcherRegistry.PenalizationAlertWatcher,
173173
app.watcherRegistry.PegoutBridgeWatcher,
174174
app.watcherRegistry.BtcReleaseWatcher,
175+
app.watcherRegistry.BitcoinPeerWatcher,
176+
app.watcherRegistry.RootstockPeerWatcher,
175177
app.watcherRegistry.QuoteMetricsWatcher,
176178
app.watcherRegistry.AssetReportWatcher,
177179
}

docker-compose/local/docker-compose.lps.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ services:
8787
- BTC_RELEASE_WATCHER_START_BLOCK
8888
- BTC_RELEASE_WATCHER_PAGE_SIZE
8989
- BTC_RELEASE_CHECK_TIMEOUT
90+
- RSK_MIN_PEERS
91+
- BITCOIN_MIN_PEERS
9092
ports:
9193
- "8080:8080"
9294
volumes:

internal/adapters/dataproviders/bitcoin/btcclient/binding.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type RpcClient interface {
5050
CreateWallet(name string, opts ...rpcclient.CreateWalletOpt) (*btcjson.CreateWalletResult, error)
5151
LoadWallet(walletName string) (*btcjson.LoadWalletResult, error)
5252
EstimateSmartFee(confTarget int64, mode *btcjson.EstimateSmartFeeMode) (*btcjson.EstimateSmartFeeResult, error)
53+
GetConnectionCount() (int64, error)
5354
}
5455

5556
type ClientAdapter interface {

internal/adapters/dataproviders/bitcoin/mempool_space/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ func (api *MempoolSpaceApi) GetZeroAddress(addressType blockchain.BtcAddressType
470470
return "", errors.New("not supported in MempoolSpace API")
471471
}
472472

473+
func (api *MempoolSpaceApi) GetConnectionCount() (int64, error) {
474+
return 0, errors.New("GetConnectionCount is not supported in MempoolSpace API")
475+
}
476+
473477
func (api *MempoolSpaceApi) getBlock(blockHash string) (*btcutil.Block, error) {
474478
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, fmt.Sprintf("%s/block/%s/raw", api.url, blockHash), nil)
475479
if err != nil {

internal/adapters/dataproviders/bitcoin/rpc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ func (rpc *bitcoindRpc) GetZeroAddress(addressType blockchain.BtcAddressType) (s
261261
return addresses.Address(addressType)
262262
}
263263

264+
func (rpc *bitcoindRpc) GetConnectionCount() (int64, error) {
265+
return rpc.conn.client.GetConnectionCount()
266+
}
267+
264268
func (rpc *bitcoindRpc) getTxBlock(txHash string) (*wire.MsgBlock, *chainhash.Hash, error) {
265269
parsedTxHash, err := chainhash.NewHashFromStr(txHash)
266270
if err != nil {

internal/adapters/dataproviders/bitcoin/rpc_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,3 +707,23 @@ func TestBitcoindRpc_GetZeroAddress(t *testing.T) {
707707
assert.Empty(t, result)
708708
})
709709
}
710+
711+
func TestBitcoindRpc_GetConnectionCount(t *testing.T) {
712+
t.Run("Should return connection count", func(t *testing.T) {
713+
client := &mocks.ClientAdapterMock{}
714+
client.EXPECT().GetConnectionCount().Return(int64(8), nil).Once()
715+
rpc := bitcoin.NewBitcoindRpc(bitcoin.NewConnection(&chaincfg.MainNetParams, client))
716+
count, err := rpc.GetConnectionCount()
717+
require.NoError(t, err)
718+
assert.Equal(t, int64(8), count)
719+
client.AssertExpectations(t)
720+
})
721+
t.Run("Should return error when client fails", func(t *testing.T) {
722+
client := &mocks.ClientAdapterMock{}
723+
client.EXPECT().GetConnectionCount().Return(int64(0), assert.AnError).Once()
724+
rpc := bitcoin.NewBitcoindRpc(bitcoin.NewConnection(&chaincfg.MainNetParams, client))
725+
_, err := rpc.GetConnectionCount()
726+
require.Error(t, err)
727+
client.AssertExpectations(t)
728+
})
729+
}

internal/adapters/dataproviders/rootstock/bindings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type RpcClientBinding interface {
2929
BlockNumber(ctx context.Context) (uint64, error)
3030
BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
3131
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
32+
PeerCount(ctx context.Context) (uint64, error)
3233
}
3334

3435
type RskBridgeBinding interface {

internal/adapters/dataproviders/rootstock/rpc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ func (rpc *rskjRpcServer) GetBlockByNumber(ctx context.Context, blockNumber *big
193193
}, nil
194194
}
195195

196+
func (rpc *rskjRpcServer) PeerCount(ctx context.Context) (uint64, error) {
197+
return rskRetry(rpc.retryParams.Retries, rpc.retryParams.Sleep,
198+
func() (uint64, error) {
199+
return rpc.client.PeerCount(ctx)
200+
})
201+
}
202+
196203
func (rpc *rskjRpcServer) ChainId(ctx context.Context) (uint64, error) {
197204
result, err := rskRetry(rpc.retryParams.Retries, rpc.retryParams.Sleep,
198205
func() (uint64, error) {

internal/adapters/dataproviders/rootstock/rpc_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,24 @@ func TestRskjRpcServer_GetBlockByNumber(t *testing.T) {
469469
client.AssertExpectations(t)
470470
})
471471
}
472+
473+
func TestRskjRpcServer_PeerCount(t *testing.T) {
474+
t.Run("should return peer count", func(t *testing.T) {
475+
client := &mocks.RpcClientBindingMock{}
476+
client.EXPECT().PeerCount(mock.Anything).Return(uint64(5), nil)
477+
rpc := rootstock.NewRskjRpcServer(rootstock.NewRskClient(client), rootstock.RetryParams{})
478+
count, err := rpc.PeerCount(context.Background())
479+
require.NoError(t, err)
480+
assert.Equal(t, uint64(5), count)
481+
client.AssertExpectations(t)
482+
})
483+
t.Run("should handle errors", func(t *testing.T) {
484+
client := &mocks.RpcClientBindingMock{}
485+
client.EXPECT().PeerCount(mock.Anything).Return(uint64(0), assert.AnError)
486+
rpc := rootstock.NewRskjRpcServer(rootstock.NewRskClient(client), rootstock.RetryParams{})
487+
count, err := rpc.PeerCount(context.Background())
488+
require.Error(t, err)
489+
assert.Equal(t, uint64(0), count)
490+
client.AssertExpectations(t)
491+
})
492+
}

0 commit comments

Comments
 (0)