Skip to content

Commit

Permalink
Ensure IBC Proto Registration (cosmos#7210)
Browse files Browse the repository at this point in the history
* init commit

* Add simulation request

* Push progress

* ensure exported interfaces and their concrete implementations are registered in ibc modules

* revert CalculateGas changes

* Fix simulation response unmarshal

* ensure exported interfaces and their concrete implementations are registered in ibc modules

* Passing tests

* Fix lint issues

* Update client/tx/tx.go

Co-authored-by: Aleksandr Bezobchuk <[email protected]>
Co-authored-by: Federico Kunze <[email protected]>
  • Loading branch information
3 people authored Sep 1, 2020
1 parent b52131d commit 7d64086
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 22 deletions.
2 changes: 1 addition & 1 deletion baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (app *BaseApp) handleQueryGRPC(handler GRPCQueryHandler, req abci.RequestQu
func gRPCErrorToSDKError(err error) error {
status, ok := grpcstatus.FromError(err)
if !ok {
return err
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}

switch status.Code() {
Expand Down
2 changes: 1 addition & 1 deletion baseapp/grpcrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ func (qrt *GRPCQueryRouter) RegisterSimulateService(
) {
simulate.RegisterSimulateServiceServer(
qrt,
simulate.NewSimulateServer(simulateFn, qrt.interfaceRegistry, pubkeyCodec),
simulate.NewSimulateServer(simulateFn, interfaceRegistry, pubkeyCodec),
)
}
1 change: 0 additions & 1 deletion client/grpc/simulate/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (s simulateServer) Simulate(ctx context.Context, req *SimulateRequest) (*Si
return nil, err
}
txBuilder := authtx.WrapTx(req.Tx, s.pubkeyCodec)

txBytes, err := req.Tx.Marshal()
if err != nil {
return nil, err
Expand Down
35 changes: 21 additions & 14 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"fmt"
"net/http"
"os"
"strings"

"github.com/gogo/protobuf/jsonpb"
"github.com/spf13/pflag"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sim "github.com/cosmos/cosmos-sdk/client/grpc/simulate"
"github.com/cosmos/cosmos-sdk/client/input"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -248,43 +247,51 @@ func BuildUnsignedTx(txf Factory, msgs ...sdk.Msg) (client.TxBuilder, error) {
// the encoded transaction or an error if the unsigned transaction cannot be
// built.
func BuildSimTx(txf Factory, msgs ...sdk.Msg) ([]byte, error) {
tx, err := BuildUnsignedTx(txf, msgs...)
txb, err := BuildUnsignedTx(txf, msgs...)
if err != nil {
return nil, err
}

// Create an empty signature literal as the ante handler will populate with a
// sentinel pubkey.
sig := signing.SignatureV2{}
sig := signing.SignatureV2{
Data: &signing.SingleSignatureData{
SignMode: txf.WithSignMode(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON).signMode,
},
Sequence: txf.Sequence(),
}

if err := tx.SetSignatures(sig); err != nil {
if err := txb.SetSignatures(sig); err != nil {
return nil, err
}

return txf.txConfig.TxEncoder()(tx.GetTx())
simReq := sim.SimulateRequest{Tx: txb.GetProtoTx()}

return simReq.Marshal()
}

// CalculateGas simulates the execution of a transaction and returns the
// simulation response obtained by the query and the adjusted gas amount.
func CalculateGas(
queryFunc func(string, []byte) ([]byte, int64, error), txf Factory, msgs ...sdk.Msg,
) (sdk.SimulationResponse, uint64, error) {
) (sim.SimulateResponse, uint64, error) {
txBytes, err := BuildSimTx(txf, msgs...)
if err != nil {
return sdk.SimulationResponse{}, 0, err
return sim.SimulateResponse{}, 0, err
}

bz, _, err := queryFunc("/app/simulate", txBytes)
bz, _, err := queryFunc("/cosmos.base.simulate.v1beta1.SimulateService/Simulate", txBytes)
if err != nil {
return sdk.SimulationResponse{}, 0, err
return sim.SimulateResponse{}, 0, err
}

var simRes sdk.SimulationResponse
if err := jsonpb.Unmarshal(strings.NewReader(string(bz)), &simRes); err != nil {
return sdk.SimulationResponse{}, 0, err
var simRes sim.SimulateResponse

if err := simRes.Unmarshal(bz); err != nil {
return sim.SimulateResponse{}, 0, err
}

return simRes, uint64(txf.GasAdjustment() * float64(simRes.GasUsed)), nil
return simRes, uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed)), nil
}

// PrepareFactory ensures the account defined by ctx.GetFromAddress() exists and
Expand Down
8 changes: 4 additions & 4 deletions client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keyring"

"github.com/cosmos/cosmos-sdk/client"
sim "github.com/cosmos/cosmos-sdk/client/grpc/simulate"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -32,12 +32,12 @@ func TestCalculateGas(t *testing.T) {
if wantErr {
return nil, 0, errors.New("query failed")
}
simRes := &sdk.SimulationResponse{
GasInfo: sdk.GasInfo{GasUsed: gasUsed, GasWanted: gasUsed},
simRes := &sim.SimulateResponse{
GasInfo: &sdk.GasInfo{GasUsed: gasUsed, GasWanted: gasUsed},
Result: &sdk.Result{Data: []byte("tx data"), Log: "log"},
}

bz, err := codec.ProtoMarshalJSON(simRes)
bz, err := simRes.Marshal()
if err != nil {
return nil, 0, err
}
Expand Down
8 changes: 8 additions & 0 deletions x/ibc/02-client/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
"cosmos_sdk.ibc.v1.client.Header",
(*exported.Header)(nil),
)
registry.RegisterInterface(
"cosmos_sdk.ibc.v1.client.Height",
(*exported.Height)(nil),
)
registry.RegisterImplementations(
(*exported.Height)(nil),
&Height{},
)
registry.RegisterInterface(
"cosmos_sdk.ibc.v1.client.Misbehaviour",
(*exported.Misbehaviour)(nil),
Expand Down
17 changes: 17 additions & 0 deletions x/ibc/03-connection/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/exported"
)

// RegisterInterfaces register the ibc interfaces submodule implementations to protobuf
// Any.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterInterface(
"cosmos_sdk.ibc.v1.connection.ConnectionI",
(*exported.ConnectionI)(nil),
)
registry.RegisterInterface(
"cosmos_sdk.ibc.v1.connection.CounterpartyI",
(*exported.CounterpartyI)(nil),
)
registry.RegisterImplementations(
(*exported.ConnectionI)(nil),
&ConnectionEnd{},
)
registry.RegisterImplementations(
(*exported.CounterpartyI)(nil),
&Counterparty{},
)
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgConnectionOpenInit{},
Expand Down
25 changes: 25 additions & 0 deletions x/ibc/04-channel/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,36 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported"
)

// RegisterInterfaces register the ibc channel submodule interfaces to protobuf
// Any.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterInterface(
"cosmos_sdk.ibc.v1.channel.ChannelI",
(*exported.ChannelI)(nil),
)
registry.RegisterInterface(
"cosmos_sdk.ibc.v1.channel.CounterpartyI",
(*exported.CounterpartyI)(nil),
)
registry.RegisterInterface(
"cosmos_sdk.ibc.v1.channel.PacketI",
(*exported.PacketI)(nil),
)
registry.RegisterImplementations(
(*exported.ChannelI)(nil),
&Channel{},
)
registry.RegisterImplementations(
(*exported.CounterpartyI)(nil),
&Counterparty{},
)
registry.RegisterImplementations(
(*exported.PacketI)(nil),
&Packet{},
)
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgChannelOpenInit{},
Expand Down
4 changes: 4 additions & 0 deletions x/ibc/07-tendermint/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
(*clientexported.Misbehaviour)(nil),
&Misbehaviour{},
)
registry.RegisterImplementations(
(*clientexported.Header)(nil),
&Header{},
)
}

var (
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/09-localhost/types/localhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type LocalhostTestSuite struct {
suite.Suite

cdc codec.Marshaler
ctx sdk.Context
ctx sdk.Context
store sdk.KVStore
}

Expand Down

0 comments on commit 7d64086

Please sign in to comment.