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 <aleks.bezobchuk@gmail.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
Jack Zampolin 2020-09-01 13:59:48 -07:00 committed by GitHub
parent b52131d60c
commit 7d64086d33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 82 additions and 22 deletions

View File

@ -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() {

View File

@ -120,6 +120,6 @@ func (qrt *GRPCQueryRouter) RegisterSimulateService(
) {
simulate.RegisterSimulateServiceServer(
qrt,
simulate.NewSimulateServer(simulateFn, qrt.interfaceRegistry, pubkeyCodec),
simulate.NewSimulateServer(simulateFn, interfaceRegistry, pubkeyCodec),
)
}

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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),

View File

@ -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{},

View File

@ -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{},

View File

@ -25,6 +25,10 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
(*clientexported.Misbehaviour)(nil),
&Misbehaviour{},
)
registry.RegisterImplementations(
(*clientexported.Header)(nil),
&Header{},
)
}
var (

View File

@ -25,7 +25,7 @@ type LocalhostTestSuite struct {
suite.Suite
cdc codec.Marshaler
ctx sdk.Context
ctx sdk.Context
store sdk.KVStore
}