Skip to content

Commit

Permalink
feat: add GetProtoBlock method
Browse files Browse the repository at this point in the history
  • Loading branch information
technicallyty committed Feb 22, 2022
1 parent ad6110a commit 85738c2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
16 changes: 15 additions & 1 deletion client/grpc/tmservice/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package tmservice

import (
"context"

"github.com/cosmos/cosmos-sdk/client"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/rpc/coretypes"
)

Expand All @@ -16,3 +16,17 @@ func getBlock(ctx context.Context, clientCtx client.Context, height *int64) (*co

return node.Block(ctx, height)
}

func GetProtoBlock(ctx context.Context, clientCtx client.Context, height *int64) (tmproto.BlockID, *tmproto.Block, error) {
block, err := getBlock(ctx, clientCtx, height)
if err != nil {
return tmproto.BlockID{}, nil, err
}
protoBlock, err := block.Block.ToProto()
if err != nil {
return tmproto.BlockID{}, nil, err
}
protoBlockId := block.BlockID.ToProto()

return protoBlockId, protoBlock, nil
}
7 changes: 1 addition & 6 deletions client/grpc/tmservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ func (s queryServer) GetBlockByHeight(ctx context.Context, req *GetBlockByHeight
return nil, status.Error(codes.InvalidArgument, "requested block height is bigger then the chain length")
}

res, err := getBlock(ctx, s.clientCtx, &req.Height)
if err != nil {
return nil, err
}
protoBlockID := res.BlockID.ToProto()
protoBlock, err := res.Block.ToProto()
protoBlockID, protoBlock, err := GetProtoBlock(ctx, s.clientCtx, &req.Height)
if err != nil {
return nil, err
}
Expand Down
25 changes: 7 additions & 18 deletions x/auth/tx/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tx
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"strings"

Expand Down Expand Up @@ -178,18 +179,11 @@ func (s txServer) GetBlockWithTxs(ctx context.Context, req *txtypes.GetBlockWith
"or greater than the current height %d", req.Height, currentHeight)
}

node, err := s.clientCtx.GetNode()
blockId, block, err := tmservice.GetProtoBlock(ctx, s.clientCtx, &req.Height)
if err != nil {
return nil, err
}

blockRes, err := node.Block(ctx, &req.Height)
if err != nil {
return nil, err
}
block := blockRes.Block
blockId := blockRes.BlockID

var offset, limit int
if req.Pagination != nil {
offset = int(req.Pagination.Offset)
Expand All @@ -199,13 +193,14 @@ func (s txServer) GetBlockWithTxs(ctx context.Context, req *txtypes.GetBlockWith
limit = pagination.DefaultLimit
}

blockTxsLn := len(block.Txs)
blockTxs := block.Data.Txs
blockTxsLn := len(blockTxs)
txs := make([]*txtypes.Tx, 0, limit)
if offset >= blockTxsLn {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("out of range: cannot paginate %d txs with offset %d and limit %d", blockTxsLn, offset, limit)
}
decodeTxAt := func(i int) error {
tx := block.Txs[i]
tx := blockTxs[i]
txb, err := s.clientCtx.TxConfig.TxDecoder()(tx)
if err != nil {
return err
Expand All @@ -231,16 +226,10 @@ func (s txServer) GetBlockWithTxs(ctx context.Context, req *txtypes.GetBlockWith
}
}

protoBlockId := blockId.ToProto()
protoBlock, err := block.ToProto()
if err != nil {
return nil, err
}

return &txtypes.GetBlockWithTxsResponse{
Txs: txs,
BlockId: &protoBlockId,
Block: protoBlock,
BlockId: &blockId,
Block: block,
Pagination: &pagination.PageResponse{
Total: uint64(blockTxsLn),
},
Expand Down

0 comments on commit 85738c2

Please sign in to comment.