Skip to content

Commit

Permalink
Correct handling of empty assets list in gRPC accounts API GetBalance…
Browse files Browse the repository at this point in the history
…s method added. (#388)
  • Loading branch information
alexeykiselev authored Nov 25, 2020
1 parent b0ff654 commit 7b05e8f
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions pkg/grpc/server/accounts_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,27 @@ func (s *Server) GetBalances(req *g.BalancesRequest, srv g.AccountsApi_GetBalanc
return status.Errorf(codes.InvalidArgument, err.Error())
}
rcp := proto.NewRecipientFromAddress(addr)
if len(req.Assets) == 0 {
if err := s.sendWavesBalance(rcp, srv); err != nil {
return status.Errorf(codes.Internal, err.Error())
}
}
for _, asset := range req.Assets {
var res g.BalanceResponse
if len(asset) == 0 {
// Waves.
balanceInfo, err := s.state.FullWavesBalance(rcp)
if err != nil {
res.Balance = &g.BalanceResponse_Waves{Waves: &g.BalanceResponse_WavesBalances{}}
} else {
res.Balance = &g.BalanceResponse_Waves{Waves: balanceInfo.ToProtobuf()}
if err := s.sendWavesBalance(rcp, srv); err != nil {
return status.Errorf(codes.Internal, err.Error())
}
} else {
// Asset.
balance, err := s.state.AccountBalance(rcp, asset)
if err != nil {
return status.Errorf(codes.NotFound, err.Error())
}
var res g.BalanceResponse
res.Balance = &g.BalanceResponse_Asset{Asset: &pb.Amount{AssetId: asset, Amount: int64(balance)}}
}
if err := srv.Send(&res); err != nil {
return status.Errorf(codes.Internal, err.Error())
if err := srv.Send(&res); err != nil {
return status.Errorf(codes.Internal, err.Error())
}
}
}
return nil
Expand All @@ -55,23 +56,6 @@ func (s *Server) GetScript(ctx context.Context, req *g.AccountRequest) (*g.Scrip
return scriptInfo.ToProtobuf(), nil
}

type getActiveLeasesHandler struct {
srv g.AccountsApi_GetActiveLeasesServer
s *Server
}

func (h *getActiveLeasesHandler) handle(tx proto.Transaction, failed bool) error {
res, err := h.s.transactionToTransactionResponse(tx, true, failed)
if err != nil {
return errors.Wrap(err, "failed to form transaction response")
}
err = h.srv.Send(res)
if err != nil {
return errors.Wrap(err, "failed to send")
}
return nil
}

func (s *Server) GetActiveLeases(req *g.AccountRequest, srv g.AccountsApi_GetActiveLeasesServer) error {
extendedApi, err := s.state.ProvidesExtendedApi()
if err != nil {
Expand Down Expand Up @@ -165,3 +149,31 @@ func (s *Server) ResolveAlias(ctx context.Context, req *wrappers.StringValue) (*
addrBody := addr.Body()
return &wrappers.BytesValue{Value: addrBody}, nil
}

func (s *Server) sendWavesBalance(rcp proto.Recipient, srv g.AccountsApi_GetBalancesServer) error {
var res g.BalanceResponse
balanceInfo, err := s.state.FullWavesBalance(rcp)
if err != nil {
res.Balance = &g.BalanceResponse_Waves{Waves: &g.BalanceResponse_WavesBalances{}}
} else {
res.Balance = &g.BalanceResponse_Waves{Waves: balanceInfo.ToProtobuf()}
}
return srv.Send(&res)
}

type getActiveLeasesHandler struct {
srv g.AccountsApi_GetActiveLeasesServer
s *Server
}

func (h *getActiveLeasesHandler) handle(tx proto.Transaction, failed bool) error {
res, err := h.s.transactionToTransactionResponse(tx, true, failed)
if err != nil {
return errors.Wrap(err, "failed to form transaction response")
}
err = h.srv.Send(res)
if err != nil {
return errors.Wrap(err, "failed to send")
}
return nil
}

0 comments on commit 7b05e8f

Please sign in to comment.