Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error out if RPC credentials aren't valid #44

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/config/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions app/config/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading