Skip to content

Commit

Permalink
servers/bitwindow: dont crash if enforcer not connected
Browse files Browse the repository at this point in the history
  • Loading branch information
octobocto committed Nov 14, 2024
1 parent 198042f commit c330bca
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
9 changes: 9 additions & 0 deletions servers/bitwindow/api/drivechain/drivechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 23 additions & 3 deletions servers/bitwindow/api/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 4 additions & 17 deletions servers/bitwindow/dial/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
2 changes: 0 additions & 2 deletions servers/bitwindow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion servers/bitwindow/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c330bca

Please sign in to comment.