From c330bca88dfa5fd8ebf3e511ed8e4e63ac33f47f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Olav?= Date: Thu, 14 Nov 2024 07:35:57 +0100 Subject: [PATCH] servers/bitwindow: dont crash if enforcer not connected --- .../bitwindow/api/drivechain/drivechain.go | 9 +++++++ servers/bitwindow/api/wallet/wallet.go | 26 ++++++++++++++++--- servers/bitwindow/dial/dial.go | 21 +++------------ servers/bitwindow/main.go | 2 -- servers/bitwindow/server/server.go | 2 +- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/servers/bitwindow/api/drivechain/drivechain.go b/servers/bitwindow/api/drivechain/drivechain.go index fb307547..e92b1be7 100644 --- a/servers/bitwindow/api/drivechain/drivechain.go +++ b/servers/bitwindow/api/drivechain/drivechain.go @@ -2,6 +2,7 @@ package api_drivechain import ( "context" + "errors" "connectrpc.com/connect" validatorpb "github.com/LayerTwo-Labs/sidesail/servers/bitwindow/gen/cusf/mainchain/v1" @@ -35,6 +36,10 @@ type Server struct { // ListSidechainProposals implements drivechainv1connect.DrivechainServiceHandler. func (s *Server) ListSidechainProposals(ctx context.Context, c *connect.Request[pb.ListSidechainProposalsRequest]) (*connect.Response[pb.ListSidechainProposalsResponse], error) { + if s.enforcer == nil { + return nil, errors.New("enforcer not connected") + } + sidechainProposals, err := s.enforcer.GetSidechainProposals(ctx, connect.NewRequest(&validatorpb.GetSidechainProposalsRequest{})) if err != nil { return nil, err @@ -57,6 +62,10 @@ func (s *Server) ListSidechainProposals(ctx context.Context, c *connect.Request[ // ListSidechains implements drivechainv1connect.DrivechainServiceHandler. func (s *Server) ListSidechains(ctx context.Context, _ *connect.Request[pb.ListSidechainsRequest]) (*connect.Response[pb.ListSidechainsResponse], error) { + if s.enforcer == nil { + return nil, errors.New("enforcer not connected") + } + sidechains, err := s.enforcer.GetSidechains(ctx, connect.NewRequest(&validatorpb.GetSidechainsRequest{})) if err != nil { return nil, err diff --git a/servers/bitwindow/api/wallet/wallet.go b/servers/bitwindow/api/wallet/wallet.go index 2706b5f7..a99fca9c 100644 --- a/servers/bitwindow/api/wallet/wallet.go +++ b/servers/bitwindow/api/wallet/wallet.go @@ -28,13 +28,12 @@ var _ rpc.WalletServiceHandler = new(Server) // New creates a new Server and starts the balance update loop func New( ctx context.Context, bitcoind *coreproxy.Bitcoind, wallet validatorrpc.WalletServiceClient, - enforcer validatorrpc.ValidatorServiceClient, drivechain drivechainrpc.DrivechainServiceClient, + drivechain drivechainrpc.DrivechainServiceClient, ) *Server { s := &Server{ bitcoind: bitcoind, wallet: wallet, - enforcer: enforcer, drivechain: drivechain, } return s @@ -43,12 +42,15 @@ func New( type Server struct { bitcoind *coreproxy.Bitcoind wallet validatorrpc.WalletServiceClient - enforcer validatorrpc.ValidatorServiceClient drivechain drivechainrpc.DrivechainServiceClient } // SendTransaction implements drivechainv1connect.DrivechainServiceHandler. func (s *Server) SendTransaction(ctx context.Context, c *connect.Request[pb.SendTransactionRequest]) (*connect.Response[pb.SendTransactionResponse], error) { + if s.wallet == nil { + return nil, errors.New("wallet not connected") + } + if len(c.Msg.Destinations) == 0 { err := errors.New("must provide at least one destination") return nil, connect.NewError(connect.CodeInvalidArgument, err) @@ -109,6 +111,10 @@ func (s *Server) SendTransaction(ctx context.Context, c *connect.Request[pb.Send // GetNewAddress implements drivechainv1connect.DrivechainServiceHandler. func (s *Server) GetNewAddress(ctx context.Context, c *connect.Request[emptypb.Empty]) (*connect.Response[pb.GetNewAddressResponse], error) { + if s.wallet == nil { + return nil, errors.New("wallet not connected") + } + address, err := s.wallet.CreateNewAddress(ctx, connect.NewRequest(&validatorpb.CreateNewAddressRequest{})) if err != nil { return nil, err @@ -121,6 +127,9 @@ func (s *Server) GetNewAddress(ctx context.Context, c *connect.Request[emptypb.E // GetBalance implements drivechainv1connect.DrivechainServiceHandler. func (s *Server) GetBalance(ctx context.Context, c *connect.Request[emptypb.Empty]) (*connect.Response[pb.GetBalanceResponse], error) { + if s.wallet == nil { + return nil, errors.New("wallet not connected") + } balance, err := s.wallet.GetBalance(ctx, connect.NewRequest(&validatorpb.GetBalanceRequest{})) if err != nil { @@ -135,6 +144,10 @@ func (s *Server) GetBalance(ctx context.Context, c *connect.Request[emptypb.Empt // ListTransactions implements drivechainv1connect.DrivechainServiceHandler. func (s *Server) ListTransactions(ctx context.Context, c *connect.Request[emptypb.Empty]) (*connect.Response[pb.ListTransactionsResponse], error) { + if s.wallet == nil { + return nil, errors.New("wallet not connected") + } + txs, err := s.wallet.ListTransactions(ctx, connect.NewRequest(&validatorpb.ListTransactionsRequest{})) if err != nil { return nil, err @@ -164,6 +177,9 @@ func (s *Server) ListTransactions(ctx context.Context, c *connect.Request[emptyp // ListSidechainDeposits implements walletv1connect.WalletServiceHandler. func (s *Server) ListSidechainDeposits(ctx context.Context, c *connect.Request[pb.ListSidechainDepositsRequest]) (*connect.Response[pb.ListSidechainDepositsResponse], error) { + if s.wallet == nil { + return nil, errors.New("wallet not connected") + } // TODO: Call ListSidechainDeposits with the CUSF-wallet here type Deposit struct { @@ -213,6 +229,10 @@ func (s *Server) ListSidechainDeposits(ctx context.Context, c *connect.Request[p // CreateSidechainDeposit implements walletv1connect.WalletServiceHandler. func (s *Server) CreateSidechainDeposit(ctx context.Context, c *connect.Request[pb.CreateSidechainDepositRequest]) (*connect.Response[pb.CreateSidechainDepositResponse], error) { + if s.wallet == nil { + return nil, errors.New("wallet not connected") + } + slot, depositAddress, _, err := drivechain.DecodeDepositAddress(c.Msg.Destination) if err != nil { return nil, fmt.Errorf("invalid deposit address: %w", err) diff --git a/servers/bitwindow/dial/dial.go b/servers/bitwindow/dial/dial.go index 1f617940..0a91fb42 100644 --- a/servers/bitwindow/dial/dial.go +++ b/servers/bitwindow/dial/dial.go @@ -32,25 +32,12 @@ func Enforcer(ctx context.Context, url string) ( fmt.Sprintf("http://%s", url), connect.WithGRPC(), ) - var tip *connect.Response[pb.GetChainInfoResponse] - var err error - for attempt := range 3 { - tip, err = client.GetChainInfo(ctx, connect.NewRequest(&pb.GetChainInfoRequest{})) - if err == nil { - break - } - - sleepDuration := time.Second * time.Duration(attempt+1) + tip, err := client.GetChainInfo(ctx, connect.NewRequest(&pb.GetChainInfoRequest{})) + if err != nil { zerolog.Ctx(ctx).Info(). - Int("attempt", attempt+1). Err(err). - Msgf("failed to get chain info. trying again in %s", sleepDuration) - if attempt < 4 { - time.Sleep(sleepDuration) - } - } - if err != nil { - return nil, nil, fmt.Errorf("get chain info after 3 attempts: %w", err) + Msg("failed to get chain info") + return nil, nil, fmt.Errorf("get chain info: %w", err) } zerolog.Ctx(ctx).Info(). diff --git a/servers/bitwindow/main.go b/servers/bitwindow/main.go index a5298526..b5f1ba10 100644 --- a/servers/bitwindow/main.go +++ b/servers/bitwindow/main.go @@ -99,8 +99,6 @@ func realMain(ctx context.Context) error { enforcer, wallet, err := dial.Enforcer(ctx, conf.EnforcerHost) if err != nil { log.Error().Err(err).Msg("connect to enforcer") - // enforcer does not work right now, but we still want to test - // the rest of the server } log.Info().Msgf("blockchain info: %s", info.Msg.String()) diff --git a/servers/bitwindow/server/server.go b/servers/bitwindow/server/server.go index a0c8214d..eb58e53e 100644 --- a/servers/bitwindow/server/server.go +++ b/servers/bitwindow/server/server.go @@ -47,7 +47,7 @@ func New( )) Register(srv, drivechainv1connect.NewDrivechainServiceHandler, drivechainClient) Register(srv, walletv1connect.NewWalletServiceHandler, walletv1connect.WalletServiceHandler(api_wallet.New( - ctx, bitcoind, wallet, enforcer, drivechainClient, + ctx, bitcoind, wallet, drivechainClient, ))) Register(srv, miscv1connect.NewMiscServiceHandler, miscv1connect.MiscServiceHandler(api_misc.New( database, bitcoind, wallet,