Skip to content

Commit

Permalink
refactor(server/v2/cometbft): use only protov1
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Sep 12, 2024
1 parent 5475903 commit aac9a4e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 619 deletions.
10 changes: 5 additions & 5 deletions server/v2/cometbft/client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
19 changes: 9 additions & 10 deletions server/v2/cometbft/client/rpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions server/v2/cometbft/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"strconv"
"strings"

"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

cmtcfg "github.com/cometbft/cometbft/config"
cmtjson "github.com/cometbft/cometbft/libs/json"
"github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/p2p"
pvm "github.com/cometbft/cometbft/privval"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
cmtversion "github.com/cometbft/cometbft/version"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
"sigs.k8s.io/yaml"
gogoproto "github.com/cosmos/gogoproto/proto"

"cosmossdk.io/server/v2/cometbft/client/rpc"

Expand Down Expand Up @@ -200,7 +201,7 @@ for. Each module documents its respective events under 'xx_events.md'.
return err
}

bz, err := protojson.Marshal(blocks)
bz, err := gogoproto.Marshal(blocks)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion server/v2/cometbft/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ replace (
)

require (
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2
cosmossdk.io/api v0.7.5
cosmossdk.io/core v1.0.0-alpha.2
cosmossdk.io/errors v1.0.1
Expand All @@ -45,6 +44,7 @@ require (
)

require (
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2 // indirect
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/core/testing v0.0.0-00010101000000-000000000000 // indirect
Expand Down
26 changes: 12 additions & 14 deletions server/v2/cometbft/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ import (
"strings"
"time"

abciv1 "buf.build/gen/go/cometbft/cometbft/protocolbuffers/go/cometbft/abci/v1"
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
gogoproto "github.com/cosmos/gogoproto/proto"
gogoany "github.com/cosmos/gogoproto/types/any"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/anypb"

v1beta1 "cosmossdk.io/api/cosmos/base/abci/v1beta1"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/event"
"cosmossdk.io/core/server"
"cosmossdk.io/core/transaction"
errorsmod "cosmossdk.io/errors"
consensus "cosmossdk.io/x/consensus/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func queryResponse(res transaction.Msg, height int64) (*abci.QueryResponse, error) {
Expand Down Expand Up @@ -148,47 +146,47 @@ func intoABCIEvents(events []event.Event, indexSet map[string]struct{}) []abci.E

func intoABCISimulationResponse(txRes server.TxResult, indexSet map[string]struct{}) ([]byte, error) {
indexAll := len(indexSet) == 0
abciEvents := make([]*abciv1.Event, len(txRes.Events))
abciEvents := make([]abci.Event, len(txRes.Events))
for i, e := range txRes.Events {
abciEvents[i] = &abciv1.Event{
abciEvents[i] = abci.Event{
Type: e.Type,
Attributes: make([]*abciv1.EventAttribute, len(e.Attributes)),
Attributes: make([]abci.EventAttribute, len(e.Attributes)),
}

for j, attr := range e.Attributes {
_, index := indexSet[fmt.Sprintf("%s.%s", e.Type, attr.Key)]
abciEvents[i].Attributes[j] = &abciv1.EventAttribute{
abciEvents[i].Attributes[j] = abci.EventAttribute{
Key: attr.Key,
Value: attr.Value,
Index: index || indexAll,
}
}
}

msgResponses := make([]*anypb.Any, len(txRes.Resp))
msgResponses := make([]*gogoany.Any, len(txRes.Resp))
for i, resp := range txRes.Resp {
// use this hack to maintain the protov2 API here for now
anyMsg, err := gogoany.NewAnyWithCacheWithValue(resp)
if err != nil {
return nil, err
}
msgResponses[i] = &anypb.Any{TypeUrl: anyMsg.TypeUrl, Value: anyMsg.Value}
msgResponses[i] = anyMsg
}

res := &v1beta1.SimulationResponse{
GasInfo: &v1beta1.GasInfo{
res := &sdk.SimulationResponse{
GasInfo: sdk.GasInfo{
GasWanted: txRes.GasWanted,
GasUsed: txRes.GasUsed,
},
Result: &v1beta1.Result{
Result: &sdk.Result{
Data: []byte{},
Log: txRes.Error.Error(),
Events: abciEvents,
MsgResponses: msgResponses,
},
}

return protojson.Marshal(res)
return gogoproto.Marshal(res)
}

// ToSDKEvidence takes comet evidence and returns sdk evidence
Expand Down
Loading

0 comments on commit aac9a4e

Please sign in to comment.