Skip to content

Commit

Permalink
Merge pull request #419 from klim0v/v1.2
Browse files Browse the repository at this point in the history
replace new(api_pb.Response) to nil in api v2
  • Loading branch information
danil-lashin authored Oct 1, 2020
2 parents 01d72a0 + 36f6e03 commit 9916272
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 124 deletions.
12 changes: 6 additions & 6 deletions api/v2/service/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ type stakeUser struct {
// Address returns coins list, balance and transaction count of an address.
func (s *Service) Address(ctx context.Context, req *pb.AddressRequest) (*pb.AddressResponse, error) {
if !strings.HasPrefix(strings.Title(req.Address), "Mx") {
return new(pb.AddressResponse), status.Error(codes.InvalidArgument, "invalid address")
return nil, status.Error(codes.InvalidArgument, "invalid address")
}

decodeString, err := hex.DecodeString(req.Address[2:])
if err != nil {
return new(pb.AddressResponse), status.Error(codes.InvalidArgument, "invalid address")
return nil, status.Error(codes.InvalidArgument, "invalid address")
}

address := types.BytesToAddress(decodeString)

cState, err := s.blockchain.GetStateForHeight(req.Height)
if err != nil {
return new(pb.AddressResponse), status.Error(codes.NotFound, err.Error())
return nil, status.Error(codes.NotFound, err.Error())
}

if req.Height != 0 && req.Delegated {
Expand Down Expand Up @@ -65,7 +65,7 @@ func (s *Service) Address(ctx context.Context, req *pb.AddressRequest) (*pb.Addr
}

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.AddressResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

if req.Delegated {
Expand All @@ -88,7 +88,7 @@ func (s *Service) Address(ctx context.Context, req *pb.AddressRequest) (*pb.Addr
}

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.AddressResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

res.Delegated = make([]*pb.AddressDelegatedBalance, 0, len(userDelegatedStakesGroupByCoin))
Expand All @@ -113,7 +113,7 @@ func (s *Service) Address(ctx context.Context, req *pb.AddressRequest) (*pb.Addr
}

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.AddressResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

coinsBipValue := big.NewInt(0)
Expand Down
12 changes: 6 additions & 6 deletions api/v2/service/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func (s *Service) Addresses(ctx context.Context, req *pb.AddressesRequest) (*pb.AddressesResponse, error) {
cState, err := s.blockchain.GetStateForHeight(req.Height)
if err != nil {
return new(pb.AddressesResponse), status.Error(codes.NotFound, err.Error())
return nil, status.Error(codes.NotFound, err.Error())
}

if req.Height != 0 && req.Delegated {
Expand All @@ -39,12 +39,12 @@ func (s *Service) Addresses(ctx context.Context, req *pb.AddressesRequest) (*pb.
}

if len(addr) < 3 {
return new(pb.AddressesResponse), status.Error(codes.InvalidArgument, fmt.Sprintf("invalid address %s", addr))
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid address %s", addr))
}

decodeString, err := hex.DecodeString(addr[2:])
if err != nil {
return new(pb.AddressesResponse), status.Error(codes.InvalidArgument, err.Error())
return nil, status.Error(codes.InvalidArgument, err.Error())
}
address := types.BytesToAddress(decodeString)

Expand All @@ -67,7 +67,7 @@ func (s *Service) Addresses(ctx context.Context, req *pb.AddressesRequest) (*pb.
}

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.AddressesResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

if req.Delegated {
Expand All @@ -90,7 +90,7 @@ func (s *Service) Addresses(ctx context.Context, req *pb.AddressesRequest) (*pb.
}

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.AddressesResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

res.Delegated = make([]*pb.AddressDelegatedBalance, 0, len(userDelegatedStakesGroupByCoin))
Expand All @@ -115,7 +115,7 @@ func (s *Service) Addresses(ctx context.Context, req *pb.AddressesRequest) (*pb.
}

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.AddressesResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

coinsBipValue := big.NewInt(0)
Expand Down
34 changes: 17 additions & 17 deletions api/v2/service/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func (s *Service) Block(ctx context.Context, req *pb.BlockRequest) (*pb.BlockRes
height := int64(req.Height)
block, err := s.client.Block(&height)
if err != nil {
return new(pb.BlockResponse), status.Error(codes.NotFound, "Block not found")
return nil, status.Error(codes.NotFound, "Block not found")
}

blockResults, err := s.client.BlockResults(&height)
if err != nil {
return new(pb.BlockResponse), status.Error(codes.NotFound, "Block results not found")
return nil, status.Error(codes.NotFound, "Block results not found")
}

valHeight := height - 1
Expand All @@ -52,52 +52,52 @@ func (s *Service) Block(ctx context.Context, req *pb.BlockRequest) (*pb.BlockRes
response.BlockReward = rewards.GetRewardForBlock(uint64(height)).String()

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.BlockResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

cState, err = s.blockchain.GetStateForHeight(uint64(height))
if err != nil {
return new(pb.BlockResponse), status.Error(codes.NotFound, err.Error())
return nil, status.Error(codes.NotFound, err.Error())
}

response.Transactions, err = s.blockTransaction(block, blockResults, cState.Coins())
if err != nil {
return new(pb.BlockResponse), err
return nil, err
}

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.BlockResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

tmValidators, err := s.client.Validators(&valHeight, 1, 100)
if err != nil {
return new(pb.BlockResponse), status.Error(codes.Internal, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
totalValidators = tmValidators.Validators

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.BlockResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

response.Proposer, err = blockProposer(block, totalValidators)
if err != nil {
return new(pb.BlockResponse), err
return nil, err
}

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.BlockResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

response.Validators = blockValidators(totalValidators, block)

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.BlockResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

response.Evidence = blockEvidence(block)

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.BlockResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

response.Missed = missedBlockValidators(cState)
Expand All @@ -107,7 +107,7 @@ func (s *Service) Block(ctx context.Context, req *pb.BlockRequest) (*pb.BlockRes

for _, field := range req.Fields {
if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.BlockResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}
switch field {
case pb.BlockRequest_size:
Expand All @@ -118,7 +118,7 @@ func (s *Service) Block(ctx context.Context, req *pb.BlockRequest) (*pb.BlockRes
if cState == nil {
cState, err = s.blockchain.GetStateForHeight(uint64(height))
if err != nil {
return new(pb.BlockResponse), status.Error(codes.NotFound, err.Error())
return nil, status.Error(codes.NotFound, err.Error())
}
}

Expand All @@ -129,14 +129,14 @@ func (s *Service) Block(ctx context.Context, req *pb.BlockRequest) (*pb.BlockRes

response.Transactions, err = s.blockTransaction(block, blockResults, cState.Coins())
if err != nil {
return new(pb.BlockResponse), err
return nil, err
}

case pb.BlockRequest_proposer, pb.BlockRequest_validators:
if len(totalValidators) == 0 {
tmValidators, err := s.client.Validators(&valHeight, 1, 100)
if err != nil {
return new(pb.BlockResponse), status.Error(codes.Internal, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
totalValidators = tmValidators.Validators
}
Expand All @@ -148,7 +148,7 @@ func (s *Service) Block(ctx context.Context, req *pb.BlockRequest) (*pb.BlockRes

response.Proposer, err = blockProposer(block, totalValidators)
if err != nil {
return new(pb.BlockResponse), err
return nil, err
}
case pb.BlockRequest_evidence:
response.Evidence = blockEvidence(block)
Expand Down
8 changes: 4 additions & 4 deletions api/v2/service/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import (
// Candidate returns candidate’s info by provided public_key. It will respond with 404 code if candidate is not found.
func (s *Service) Candidate(ctx context.Context, req *pb.CandidateRequest) (*pb.CandidateResponse, error) {
if len(req.PublicKey) < 3 {
return new(pb.CandidateResponse), status.Error(codes.InvalidArgument, "invalid public_key")
return nil, status.Error(codes.InvalidArgument, "invalid public_key")
}
decodeString, err := hex.DecodeString(req.PublicKey[2:])
if err != nil {
return new(pb.CandidateResponse), status.Error(codes.InvalidArgument, err.Error())
return nil, status.Error(codes.InvalidArgument, err.Error())
}

pubkey := types.BytesToPubkey(decodeString)

cState, err := s.blockchain.GetStateForHeight(req.Height)
if err != nil {
return new(pb.CandidateResponse), status.Error(codes.NotFound, err.Error())
return nil, status.Error(codes.NotFound, err.Error())
}

if req.Height != 0 {
Expand All @@ -41,7 +41,7 @@ func (s *Service) Candidate(ctx context.Context, req *pb.CandidateRequest) (*pb.

candidate := cState.Candidates().GetCandidate(pubkey)
if candidate == nil {
return new(pb.CandidateResponse), status.Error(codes.NotFound, "Candidate not found")
return nil, status.Error(codes.NotFound, "Candidate not found")
}

result := makeResponseCandidate(cState, candidate, true)
Expand Down
4 changes: 2 additions & 2 deletions api/v2/service/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func (s *Service) Candidates(ctx context.Context, req *pb.CandidatesRequest) (*pb.CandidatesResponse, error) {
cState, err := s.blockchain.GetStateForHeight(req.Height)
if err != nil {
return new(pb.CandidatesResponse), status.Error(codes.NotFound, err.Error())
return nil, status.Error(codes.NotFound, err.Error())
}

if req.Height != 0 {
Expand All @@ -32,7 +32,7 @@ func (s *Service) Candidates(ctx context.Context, req *pb.CandidatesRequest) (*p
for _, candidate := range candidates {

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.CandidatesResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

if req.Status != pb.CandidatesRequest_all && req.Status != pb.CandidatesRequest_CandidateStatus(candidate.Status) {
Expand Down
12 changes: 6 additions & 6 deletions api/v2/service/coin_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ import (
func (s *Service) CoinInfo(ctx context.Context, req *pb.CoinInfoRequest) (*pb.CoinInfoResponse, error) {
cState, err := s.blockchain.GetStateForHeight(req.Height)
if err != nil {
return new(pb.CoinInfoResponse), status.Error(codes.NotFound, err.Error())
return nil, status.Error(codes.NotFound, err.Error())
}

cState.RLock()
defer cState.RUnlock()

coin := cState.Coins().GetCoinBySymbol(types.StrToCoinSymbol(req.Symbol), types.GetVersionFromSymbol(req.Symbol))
if coin == nil {
return new(pb.CoinInfoResponse), s.createError(status.New(codes.NotFound, "Coin not found"), transaction.EncodeError(code.NewCoinNotExists(req.Symbol, "")))
return nil, s.createError(status.New(codes.NotFound, "Coin not found"), transaction.EncodeError(code.NewCoinNotExists(req.Symbol, "")))
}

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.CoinInfoResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

var ownerAddress *wrappers.StringValue
Expand All @@ -56,19 +56,19 @@ func (s *Service) CoinInfo(ctx context.Context, req *pb.CoinInfoRequest) (*pb.Co
func (s *Service) CoinInfoById(ctx context.Context, req *pb.CoinIdRequest) (*pb.CoinInfoResponse, error) {
cState, err := s.blockchain.GetStateForHeight(req.Height)
if err != nil {
return new(pb.CoinInfoResponse), status.Error(codes.NotFound, err.Error())
return nil, status.Error(codes.NotFound, err.Error())
}

cState.RLock()
defer cState.RUnlock()

coin := cState.Coins().GetCoin(types.CoinID(req.Id))
if coin == nil {
return new(pb.CoinInfoResponse), s.createError(status.New(codes.NotFound, "Coin not found"), transaction.EncodeError(code.NewCoinNotExists("", strconv.Itoa(int(req.Id)))))
return nil, s.createError(status.New(codes.NotFound, "Coin not found"), transaction.EncodeError(code.NewCoinNotExists("", strconv.Itoa(int(req.Id)))))
}

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return new(pb.CoinInfoResponse), timeoutStatus.Err()
return nil, timeoutStatus.Err()
}

var ownerAddress *wrappers.StringValue
Expand Down
20 changes: 10 additions & 10 deletions api/v2/service/estimate_coin_buy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
func (s *Service) EstimateCoinBuy(ctx context.Context, req *pb.EstimateCoinBuyRequest) (*pb.EstimateCoinBuyResponse, error) {
valueToBuy, ok := big.NewInt(0).SetString(req.ValueToBuy, 10)
if !ok {
return new(pb.EstimateCoinBuyResponse), status.Error(codes.InvalidArgument, "Value to buy not specified")
return nil, status.Error(codes.InvalidArgument, "Value to buy not specified")
}

cState, err := s.blockchain.GetStateForHeight(req.Height)
if err != nil {
return new(pb.EstimateCoinBuyResponse), status.Error(codes.NotFound, err.Error())
return nil, status.Error(codes.NotFound, err.Error())
}

cState.RLock()
Expand All @@ -33,32 +33,32 @@ func (s *Service) EstimateCoinBuy(ctx context.Context, req *pb.EstimateCoinBuyRe
if req.GetCoinToBuy() != "" {
symbol := cState.Coins().GetCoinBySymbol(types.StrToCoinSymbol(req.GetCoinToBuy()), types.GetVersionFromSymbol(req.GetCoinToBuy()))
if symbol == nil {
return new(pb.EstimateCoinBuyResponse), s.createError(status.New(codes.NotFound, "Coin to sell not exists"), transaction.EncodeError(code.NewCoinNotExists(req.GetCoinToBuy(), "")))
return nil, s.createError(status.New(codes.NotFound, "Coin to sell not exists"), transaction.EncodeError(code.NewCoinNotExists(req.GetCoinToBuy(), "")))
}
coinToBuy = symbol.ID()
} else {
coinToBuy = types.CoinID(req.GetCoinIdToBuy())
if !cState.Coins().Exists(coinToBuy) {
return new(pb.EstimateCoinBuyResponse), s.createError(status.New(codes.NotFound, "Coin to buy not exists"), transaction.EncodeError(code.NewCoinNotExists("", coinToBuy.String())))
return nil, s.createError(status.New(codes.NotFound, "Coin to buy not exists"), transaction.EncodeError(code.NewCoinNotExists("", coinToBuy.String())))
}
}

var coinToSell types.CoinID
if req.GetCoinToSell() != "" {
symbol := cState.Coins().GetCoinBySymbol(types.StrToCoinSymbol(req.GetCoinToSell()), types.GetVersionFromSymbol(req.GetCoinToSell()))
if symbol == nil {
return new(pb.EstimateCoinBuyResponse), s.createError(status.New(codes.NotFound, "Coin to sell not exists"), transaction.EncodeError(code.NewCoinNotExists(req.GetCoinToSell(), "")))
return nil, s.createError(status.New(codes.NotFound, "Coin to sell not exists"), transaction.EncodeError(code.NewCoinNotExists(req.GetCoinToSell(), "")))
}
coinToSell = symbol.ID()
} else {
coinToSell = types.CoinID(req.GetCoinIdToSell())
if !cState.Coins().Exists(coinToSell) {
return new(pb.EstimateCoinBuyResponse), s.createError(status.New(codes.NotFound, "Coin to sell not exists"), transaction.EncodeError(code.NewCoinNotExists("", coinToSell.String())))
return nil, s.createError(status.New(codes.NotFound, "Coin to sell not exists"), transaction.EncodeError(code.NewCoinNotExists("", coinToSell.String())))
}
}

if coinToSell == coinToBuy {
return new(pb.EstimateCoinBuyResponse), s.createError(status.New(codes.InvalidArgument, "\"From\" coin equals to \"to\" coin"),
return nil, s.createError(status.New(codes.InvalidArgument, "\"From\" coin equals to \"to\" coin"),
transaction.EncodeError(code.NewCrossConvert(coinToSell.String(), cState.Coins().GetCoin(coinToSell).Symbol().String(), coinToBuy.String(), cState.Coins().GetCoin(coinToBuy).Symbol().String())))
}

Expand All @@ -70,7 +70,7 @@ func (s *Service) EstimateCoinBuy(ctx context.Context, req *pb.EstimateCoinBuyRe

if !coinToSell.IsBaseCoin() {
if coinFrom.Reserve().Cmp(commissionInBaseCoin) < 0 {
return new(pb.EstimateCoinBuyResponse), s.createError(
return nil, s.createError(
status.New(codes.InvalidArgument, fmt.Sprintf("Coin reserve balance is not sufficient for transaction. Has: %s, required %s",
coinFrom.Reserve().String(), commissionInBaseCoin.String())),
transaction.EncodeError(code.NewCoinReserveNotSufficient(
Expand All @@ -89,15 +89,15 @@ func (s *Service) EstimateCoinBuy(ctx context.Context, req *pb.EstimateCoinBuyRe

if !coinToBuy.IsBaseCoin() {
if errResp := transaction.CheckForCoinSupplyOverflow(coinTo, value); errResp != nil {
return new(pb.EstimateCoinBuyResponse), s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
value = formula.CalculatePurchaseAmount(coinTo.Volume(), coinTo.Reserve(), coinTo.Crr(), value)
}

if !coinToSell.IsBaseCoin() {
value = formula.CalculateSaleAmount(coinFrom.Volume(), coinFrom.Reserve(), coinFrom.Crr(), valueToBuy)
if errResp := transaction.CheckReserveUnderflow(coinFrom, value); errResp != nil {
return new(pb.EstimateCoinBuyResponse), s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
return nil, s.createError(status.New(codes.FailedPrecondition, errResp.Log), errResp.Info)
}
}

Expand Down
Loading

0 comments on commit 9916272

Please sign in to comment.