From 7d64086d33795ad48c45e615b00e5083784671c4 Mon Sep 17 00:00:00 2001 From: Jack Zampolin Date: Tue, 1 Sep 2020 13:59:48 -0700 Subject: [PATCH] Ensure IBC Proto Registration (#7210) * 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 Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> --- baseapp/abci.go | 2 +- baseapp/grpcrouter.go | 2 +- client/grpc/simulate/simulate.go | 1 - client/tx/tx.go | 35 +++++++++++++--------- client/tx/tx_test.go | 8 ++--- x/ibc/02-client/types/codec.go | 8 +++++ x/ibc/03-connection/types/codec.go | 17 +++++++++++ x/ibc/04-channel/types/codec.go | 25 ++++++++++++++++ x/ibc/07-tendermint/types/codec.go | 4 +++ x/ibc/09-localhost/types/localhost_test.go | 2 +- 10 files changed, 82 insertions(+), 22 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index dc64fb9605..ade6af6ccc 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -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() { diff --git a/baseapp/grpcrouter.go b/baseapp/grpcrouter.go index 4591b58563..ea750980e2 100644 --- a/baseapp/grpcrouter.go +++ b/baseapp/grpcrouter.go @@ -120,6 +120,6 @@ func (qrt *GRPCQueryRouter) RegisterSimulateService( ) { simulate.RegisterSimulateServiceServer( qrt, - simulate.NewSimulateServer(simulateFn, qrt.interfaceRegistry, pubkeyCodec), + simulate.NewSimulateServer(simulateFn, interfaceRegistry, pubkeyCodec), ) } diff --git a/client/grpc/simulate/simulate.go b/client/grpc/simulate/simulate.go index 093b4207bb..9f9fd1acfb 100644 --- a/client/grpc/simulate/simulate.go +++ b/client/grpc/simulate/simulate.go @@ -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 diff --git a/client/tx/tx.go b/client/tx/tx.go index 963be2ba9e..5aa1d800cd 100644 --- a/client/tx/tx.go +++ b/client/tx/tx.go @@ -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" @@ -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 diff --git a/client/tx/tx_test.go b/client/tx/tx_test.go index fb3f36c578..117feb0b3d 100644 --- a/client/tx/tx_test.go +++ b/client/tx/tx_test.go @@ -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" @@ -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 } diff --git a/x/ibc/02-client/types/codec.go b/x/ibc/02-client/types/codec.go index 6b4f29ab01..d8939e4a08 100644 --- a/x/ibc/02-client/types/codec.go +++ b/x/ibc/02-client/types/codec.go @@ -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), diff --git a/x/ibc/03-connection/types/codec.go b/x/ibc/03-connection/types/codec.go index 23befac922..e76b3972df 100644 --- a/x/ibc/03-connection/types/codec.go +++ b/x/ibc/03-connection/types/codec.go @@ -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{}, diff --git a/x/ibc/04-channel/types/codec.go b/x/ibc/04-channel/types/codec.go index 3f4e62b0a2..f7170cb539 100644 --- a/x/ibc/04-channel/types/codec.go +++ b/x/ibc/04-channel/types/codec.go @@ -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{}, diff --git a/x/ibc/07-tendermint/types/codec.go b/x/ibc/07-tendermint/types/codec.go index 02ca445247..b85905e45f 100644 --- a/x/ibc/07-tendermint/types/codec.go +++ b/x/ibc/07-tendermint/types/codec.go @@ -25,6 +25,10 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { (*clientexported.Misbehaviour)(nil), &Misbehaviour{}, ) + registry.RegisterImplementations( + (*clientexported.Header)(nil), + &Header{}, + ) } var ( diff --git a/x/ibc/09-localhost/types/localhost_test.go b/x/ibc/09-localhost/types/localhost_test.go index af73381ac1..fbc63fa0f5 100644 --- a/x/ibc/09-localhost/types/localhost_test.go +++ b/x/ibc/09-localhost/types/localhost_test.go @@ -25,7 +25,7 @@ type LocalhostTestSuite struct { suite.Suite cdc codec.Marshaler - ctx sdk.Context + ctx sdk.Context store sdk.KVStore }