From 85738c25169d29a83f0c7ca6a835669ee3c372b9 Mon Sep 17 00:00:00 2001 From: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Mon, 21 Feb 2022 19:44:00 -0800 Subject: [PATCH] feat: add GetProtoBlock method --- client/grpc/tmservice/block.go | 16 +++++++++++++++- client/grpc/tmservice/service.go | 7 +------ x/auth/tx/service.go | 25 +++++++------------------ 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/client/grpc/tmservice/block.go b/client/grpc/tmservice/block.go index 433155c69696..3bed4268f303 100644 --- a/client/grpc/tmservice/block.go +++ b/client/grpc/tmservice/block.go @@ -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" ) @@ -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 +} diff --git a/client/grpc/tmservice/service.go b/client/grpc/tmservice/service.go index efbe6a4e47d8..7404c79e70e8 100644 --- a/client/grpc/tmservice/service.go +++ b/client/grpc/tmservice/service.go @@ -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 } diff --git a/x/auth/tx/service.go b/x/auth/tx/service.go index e1b79258c40b..4c09bef48288 100644 --- a/x/auth/tx/service.go +++ b/x/auth/tx/service.go @@ -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" @@ -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) @@ -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 @@ -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), },