From 8c5409c4709b081063062e1a973b50941457c1d5 Mon Sep 17 00:00:00 2001 From: Dylan Murray Date: Thu, 8 Feb 2024 12:59:40 -0500 Subject: [PATCH] Error out if RPC credentials aren't valid --- app/config/mocks/mocks.go | 9 +++++++++ app/config/node.go | 7 +++++++ cmd/main.go | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/app/config/mocks/mocks.go b/app/config/mocks/mocks.go index e1a865e..5e008d0 100644 --- a/app/config/mocks/mocks.go +++ b/app/config/mocks/mocks.go @@ -16,6 +16,7 @@ type Node struct { // Functions BanPeerFunc func(ctx context.Context, peer string) error + BestBlockHashFunc func(ctx context.Context) (string, error) InvalidateBlockFunc func(ctx context.Context, hash string) error UnbanPeerFunc func(ctx context.Context, peer string) error AddToConsensusBlacklistFunc func(ctx context.Context, funds []models.Fund) (*models.AddToConsensusBlacklistResponse, error) @@ -47,6 +48,14 @@ func (n *Node) BanPeer(ctx context.Context, peer string) error { return nil } +// BestBlockHash will call the BestBlockHashFunc +func (n *Node) BestBlockHash(ctx context.Context) (string, error) { + if n.BestBlockHashFunc != nil { + return n.BestBlockHashFunc(ctx) + } + return "", nil +} + // InvalidateBlock will call the InvalidateBlockFunc if not nil, otherwise return nil func (n *Node) InvalidateBlock(ctx context.Context, hash string) error { if n.InvalidateBlockFunc != nil { diff --git a/app/config/node.go b/app/config/node.go index c10bf3e..df5d992 100644 --- a/app/config/node.go +++ b/app/config/node.go @@ -12,6 +12,7 @@ import ( // NodeInterface is the interface for a node type NodeInterface interface { BanPeer(ctx context.Context, peer string) error + BestBlockHash(ctx context.Context) (string, error) GetRPCHost() string GetRPCPassword() string GetRPCUser() string @@ -66,6 +67,12 @@ func (n *Node) BanPeer(ctx context.Context, peer string) error { return c.SetBan(ctx, peer, bn.BanActionAdd, nil) } +// BestBlockHash gets the best block hash +func (n *Node) BestBlockHash(ctx context.Context) (string, error) { + c := bn.NewNodeClient(bn.WithCreds(n.RPCUser, n.RPCPassword), bn.WithHost(n.RPCHost)) + return c.BestBlockHash(ctx) +} + // UnbanPeer unbans a peer func (n *Node) UnbanPeer(ctx context.Context, peer string) error { c := bn.NewNodeClient(bn.WithCreds(n.RPCUser, n.RPCPassword), bn.WithHost(n.RPCHost)) diff --git a/cmd/main.go b/cmd/main.go index 8ab4801..ec5a963 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -33,6 +33,12 @@ func main() { _appConfig.Services.Log.Fatalf("error creating genesis alert: %s", err.Error()) } + // Ensure that RPC connection is valid + if _, err = _appConfig.Services.Node.BestBlockHash(context.Background()); err != nil { + _appConfig.Services.Log.Errorf("error talking to Bitcoin node with supplied RPC credentials: %s", err.Error()) + return + } + // Create the p2p server var p2pServer *p2p.Server if p2pServer, err = p2p.NewServer(p2p.ServerOptions{