refactor(server/v2/cometbft): use only protov1 and backport #21084 (backport #21681) (#21684)

Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
mergify[bot]
2024-09-12 13:53:16 +02:00
committed by GitHub
co-authored by Julien Robert
parent 830784bb21
commit 1ff55a78be
6 changed files with 58 additions and 620 deletions
+5 -5
View File
@@ -5,9 +5,9 @@ import (
"encoding/hex"
"fmt"
v11 "buf.build/gen/go/cometbft/cometbft/protocolbuffers/go/cometbft/types/v1"
cmttypes "github.com/cometbft/cometbft/api/cometbft/types/v1"
abciv1beta1 "cosmossdk.io/api/cosmos/base/abci/v1beta1"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GetChainHeight returns the current blockchain height.
@@ -39,7 +39,7 @@ func GetChainHeight(ctx context.Context, rpcClient CometRPC) (int64, error) {
// tx.height = 5 # all txs of the fifth block
//
// For more information, see the /subscribe CometBFT RPC endpoint documentation
func QueryBlocks(ctx context.Context, rpcClient CometRPC, page, limit int, query, orderBy string) (*abciv1beta1.SearchBlocksResult, error) {
func QueryBlocks(ctx context.Context, rpcClient CometRPC, page, limit int, query, orderBy string) (*sdk.SearchBlocksResult, error) {
resBlocks, err := rpcClient.BlockSearch(ctx, query, &page, &limit, orderBy)
if err != nil {
return nil, err
@@ -56,7 +56,7 @@ func QueryBlocks(ctx context.Context, rpcClient CometRPC, page, limit int, query
}
// GetBlockByHeight gets block by height
func GetBlockByHeight(ctx context.Context, rpcClient CometRPC, height *int64) (*v11.Block, error) {
func GetBlockByHeight(ctx context.Context, rpcClient CometRPC, height *int64) (*cmttypes.Block, error) {
// header -> BlockchainInfo
// header, tx -> Block
// results -> BlockResults
@@ -77,7 +77,7 @@ func GetBlockByHeight(ctx context.Context, rpcClient CometRPC, height *int64) (*
}
// GetBlockByHash gets block by hash
func GetBlockByHash(ctx context.Context, rpcClient CometRPC, hashHexString string) (*v11.Block, error) {
func GetBlockByHash(ctx context.Context, rpcClient CometRPC, hashHexString string) (*cmttypes.Block, error) {
hash, err := hex.DecodeString(hashHexString)
if err != nil {
return nil, err
+9 -10
View File
@@ -3,19 +3,18 @@ package rpc
import (
"fmt"
v11 "buf.build/gen/go/cometbft/cometbft/protocolbuffers/go/cometbft/types/v1"
cmttypes "github.com/cometbft/cometbft/api/cometbft/types/v1"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
gogoproto "github.com/cosmos/gogoproto/proto"
protov2 "google.golang.org/protobuf/proto"
abciv1beta1 "cosmossdk.io/api/cosmos/base/abci/v1beta1"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// formatBlockResults parses the indexed blocks into a slice of BlockResponse objects.
func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*v11.Block, error) {
func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*cmttypes.Block, error) {
var (
err error
out = make([]*v11.Block, len(resBlocks))
out = make([]*cmttypes.Block, len(resBlocks))
)
for i := range resBlocks {
out[i], err = NewResponseResultBlock(resBlocks[i])
@@ -30,9 +29,9 @@ func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*v11.Block, error
return out, nil
}
func NewSearchBlocksResult(totalCount, count, page, limit int64, blocks []*v11.Block) *abciv1beta1.SearchBlocksResult {
func NewSearchBlocksResult(totalCount, count, page, limit int64, blocks []*cmttypes.Block) *sdk.SearchBlocksResult {
totalPages := calcTotalPages(totalCount, limit)
return &abciv1beta1.SearchBlocksResult{
return &sdk.SearchBlocksResult{
TotalCount: totalCount,
Count: count,
PageNumber: page,
@@ -43,7 +42,7 @@ func NewSearchBlocksResult(totalCount, count, page, limit int64, blocks []*v11.B
}
// NewResponseResultBlock returns a BlockResponse given a ResultBlock from CometBFT
func NewResponseResultBlock(res *coretypes.ResultBlock) (*v11.Block, error) {
func NewResponseResultBlock(res *coretypes.ResultBlock) (*cmttypes.Block, error) {
blkProto, err := res.Block.ToProto()
if err != nil {
return nil, err
@@ -53,8 +52,8 @@ func NewResponseResultBlock(res *coretypes.ResultBlock) (*v11.Block, error) {
return nil, err
}
blk := &v11.Block{}
err = protov2.Unmarshal(blkBz, blk)
blk := &cmttypes.Block{}
err = gogoproto.Unmarshal(blkBz, blk)
if err != nil {
return nil, err
}