Skip to content

Commit

Permalink
Merge branch 'main' into justin/ordered-timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
jtieri authored May 3, 2022
2 parents 0c8cd6b + 6cc35fa commit b92f1de
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 171 deletions.
22 changes: 11 additions & 11 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ $ %s query header ibc-0 1400`,
}
}

s, err := chain.Sprint(header, false, false)
s, err := chain.ChainProvider.Sprint(header)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal header: %v\n", err)
return err
Expand Down Expand Up @@ -375,7 +375,7 @@ $ %s q node-state ibc-1`,
return err
}

s, err := chain.Sprint(csRes, false, false)
s, err := chain.ChainProvider.Sprint(csRes)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal consensus state: %v\n", err)
return err
Expand Down Expand Up @@ -426,7 +426,7 @@ $ %s query client ibc-0 ibczeroclient --height 1205`,
return err
}

s, err := chain.Sprint(res, false, false)
s, err := chain.ChainProvider.Sprint(res)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal state: %v\n", err)
return err
Expand Down Expand Up @@ -469,7 +469,7 @@ $ %s query clients ibc-2 --offset 2 --limit 30`,
}

for _, client := range res {
s, err := chain.Sprint(&client, false, false)
s, err := chain.ChainProvider.Sprint(&client)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal state: %v\n", err)
continue
Expand Down Expand Up @@ -551,7 +551,7 @@ $ %s q conns ibc-1`,
}

for _, connection := range res {
s, err := chain.Sprint(connection, false, false)
s, err := chain.ChainProvider.Sprint(connection)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal connection: %v\n", err)
continue
Expand Down Expand Up @@ -605,7 +605,7 @@ $ %s query client-connections ibc-0 ibczeroclient --height 1205`,
return err
}

s, err := chain.Sprint(res, false, false)
s, err := chain.ChainProvider.Sprint(res)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal client connection state: %v\n", err)
return err
Expand Down Expand Up @@ -650,7 +650,7 @@ $ %s q conn ibc-1 ibconeconn`,
return err
}

s, err := chain.Sprint(res, false, false)
s, err := chain.ChainProvider.Sprint(res)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal connection state: %v\n", err)
return err
Expand Down Expand Up @@ -696,7 +696,7 @@ $ %s query connection-channels ibc-2 ibcconnection2 --offset 2 --limit 30`,
}

for _, channel := range chans {
s, err := chain.Sprint(channel, false, false)
s, err := chain.ChainProvider.Sprint(channel)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal channel: %v\n", err)
continue
Expand Down Expand Up @@ -752,7 +752,7 @@ $ %s query channel ibc-2 ibctwochannel transfer --height 1205`,
return err
}

s, err := chain.Sprint(res, false, false)
s, err := chain.ChainProvider.Sprint(res)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal channel state: %v\n", err)
return err
Expand Down Expand Up @@ -794,7 +794,7 @@ $ %s query channels ibc-2 --offset 2 --limit 30`,
}

for _, channel := range res {
s, err := chain.Sprint(channel, false, false)
s, err := chain.ChainProvider.Sprint(channel)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal channel: %v\n", err)
continue
Expand Down Expand Up @@ -840,7 +840,7 @@ $ %s q packet-commit ibc-1 ibconechannel transfer 31`,
return err
}

s, err := chain.Sprint(res, false, false)
s, err := chain.ChainProvider.Sprint(res)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "Failed to marshal packet-commit state: %v\n", err)
return err
Expand Down
119 changes: 4 additions & 115 deletions relayer/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,8 @@ import (
"time"

"github.com/avast/retry-go/v4"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
simparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
authz "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/capability"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/distribution"
distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client"
feegrant "github.com/cosmos/cosmos-sdk/x/feegrant/module"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/params"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/upgrade"
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
"github.com/cosmos/ibc-go/v3/modules/apps/transfer"
ibc "github.com/cosmos/ibc-go/v3/modules/core"
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
"github.com/cosmos/relayer/v2/relayer/provider"
"github.com/gogo/protobuf/proto"
"go.uber.org/zap"
)

Expand All @@ -44,26 +19,6 @@ var (
RtyDel = retry.Delay(time.Millisecond * 400)
RtyErr = retry.LastErrorOnly(true)

ModuleBasics = []module.AppModuleBasic{
auth.AppModuleBasic{},
authz.AppModuleBasic{},
bank.AppModuleBasic{},
capability.AppModuleBasic{},
gov.NewAppModuleBasic(
paramsclient.ProposalHandler, distrclient.ProposalHandler, upgradeclient.ProposalHandler, upgradeclient.CancelProposalHandler,
),
crisis.AppModuleBasic{},
distribution.AppModuleBasic{},
feegrant.AppModuleBasic{},
mint.AppModuleBasic{},
params.AppModuleBasic{},
slashing.AppModuleBasic{},
staking.AppModuleBasic{},
upgrade.AppModuleBasic{},
transfer.AppModuleBasic{},
ibc.AppModuleBasic{},
}

defaultCoinType uint32 = 118
)

Expand All @@ -76,33 +31,11 @@ type Chain struct {
Chainid string `yaml:"chain-id" json:"chain-id"`
RPCAddr string `yaml:"rpc-addr" json:"rpc-addr"`

PathEnd *PathEnd `yaml:"-" json:"-"`
Encoding simparams.EncodingConfig `yaml:"-" json:"-"`
PathEnd *PathEnd `yaml:"-" json:"-"`

debug bool
}

func MakeCodec(moduleBasics []module.AppModuleBasic) simparams.EncodingConfig {
modBasic := module.NewBasicManager(moduleBasics...)
encodingConfig := MakeCodecConfig()
std.RegisterLegacyAminoCodec(encodingConfig.Amino)
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)
modBasic.RegisterLegacyAminoCodec(encodingConfig.Amino)
modBasic.RegisterInterfaces(encodingConfig.InterfaceRegistry)
return encodingConfig
}

func MakeCodecConfig() simparams.EncodingConfig {
interfaceRegistry := cdctypes.NewInterfaceRegistry()
marshaler := codec.NewProtoCodec(interfaceRegistry)
return simparams.EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
TxConfig: tx.NewTxConfig(marshaler, tx.DefaultSignModes),
Amino: codec.NewLegacyAmino(),
}
}

// ValidatePaths takes two chains and validates their paths
func ValidatePaths(src, dst *Chain) error {
if err := src.PathEnd.ValidateFull(); err != nil {
Expand Down Expand Up @@ -145,13 +78,9 @@ func ValidateConnectionPaths(src, dst *Chain) error {

func NewChain(log *zap.Logger, prov provider.ChainProvider, debug bool) *Chain {
return &Chain{
log: log,

log: log,
ChainProvider: prov,

Encoding: MakeCodec(ModuleBasics),

debug: debug,
debug: debug,
}
}

Expand Down Expand Up @@ -182,45 +111,6 @@ func (c *Chain) String() string {
return string(out)
}

// Sprint returns the json or yaml representation of whatever is passed in.
// CONTRACT: The cmd calling this function needs to have the "json" and "indent" flags set
// TODO: better "text" printing here would be a nice to have
// TODO: fix indenting all over the code base
func (c *Chain) Sprint(toPrint proto.Message, text, indent bool) (string, error) {
switch {
case indent && text:
return "", fmt.Errorf("must pass either indent or text, not both")
case text:
// TODO: This isn't really a good option,
return fmt.Sprintf("%v", toPrint), nil
default:
out, err := c.Encoding.Marshaler.MarshalJSON(toPrint)
if err != nil {
return "", err
}
return string(out), nil
}
}

//// SendAndPrint sends a transaction and prints according to the passed args
//func (c *Chain) SendAndPrint(txs []sdk.Msg, text, indent bool) (err error) {
// if c.debug {
// for _, msg := range txs {
// if err = c.Print(msg, text, indent); err != nil {
// return err
// }
// }
// }
// // SendAndPrint sends the transaction with printing options from the CLI
// res, _, err := c.SendMsgs(txs)
// if err != nil {
// return err
// }
//
// return c.Print(res, text, indent)
//
//}

// Chains is a collection of Chain
type Chains []*Chain

Expand Down Expand Up @@ -257,7 +147,6 @@ func (c Chains) Gets(chainIDs ...string) (map[string]*Chain, error) {
}

// GetRPCPort returns the port configured for the chain
// TODO this needs to be exposed on the ChainProvider or rpc-addr needs to be persisted on the Chain struct still
func (c *Chain) GetRPCPort() string {
u, _ := url.Parse(c.RPCAddr)
return u.Port()
Expand All @@ -276,7 +165,7 @@ func (c *Chain) CreateTestKey() error {
func (c *Chain) GetTimeout() (time.Duration, error) {
timeout, err := time.ParseDuration(c.ChainProvider.Timeout())
if err != nil {
return 0, fmt.Errorf("failed to parse timeout (%s) for chain %s. Err: %w", c.ChainProvider.Timeout(), c.ChainID(), err)
return 0, fmt.Errorf("failed to parse timeout (%s) for chain %s: %w", c.ChainProvider.Timeout(), c.ChainID(), err)
}
return timeout, nil
}
Expand Down
10 changes: 5 additions & 5 deletions relayer/log-chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ func logFailedTx(log *zap.Logger, chainID string, res *provider.RelayerTxRespons
fields := make([]zap.Field, 1+len(msgs), 2+len(msgs))
fields[0] = zap.String("chain_id", chainID)
for i, msg := range msgs {
cm, ok := msg.(cosmos.CosmosMessage)
if ok {
// TODO add case here for other implementations of provider.RelayerMessage
switch m := msg.(type) {
case cosmos.CosmosMessage:
fields[i+1] = zap.Object(
fmt.Sprintf("msg-%d", i),
cm,
m,
)
} else {
// TODO: choose another encoding instead of skipping?
default:
fields[i+1] = zap.Skip()
}
}
Expand Down
9 changes: 9 additions & 0 deletions relayer/provider/cosmos/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ func (cc *CosmosProvider) TrustingPeriod(ctx context.Context) (time.Duration, er
return tp.Truncate(time.Hour), nil
}

// Sprint returns the json representation of the specified proto message.
func (cc *CosmosProvider) Sprint(toPrint proto.Message) (string, error) {
out, err := cc.Codec.Marshaler.MarshalJSON(toPrint)
if err != nil {
return "", err
}
return string(out), nil
}

// CreateClient creates an sdk.Msg to update the client on src with consensus state from dst
func (cc *CosmosProvider) CreateClient(clientState ibcexported.ClientState, dstHeader ibcexported.Header) (provider.RelayerMessage, error) {
var (
Expand Down
2 changes: 2 additions & 0 deletions relayer/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
conntypes "github.com/cosmos/ibc-go/v3/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/v3/modules/core/exported"
"github.com/gogo/protobuf/proto"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand Down Expand Up @@ -121,6 +122,7 @@ type ChainProvider interface {
Timeout() string
TrustingPeriod(ctx context.Context) (time.Duration, error)
WaitForNBlocks(ctx context.Context, n int64) error
Sprint(toPrint proto.Message) (string, error)
}

// Do we need intermediate types? i.e. can we use the SDK types for both substrate and cosmos?
Expand Down
40 changes: 0 additions & 40 deletions relayer/relayMsgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sync"

"github.com/cosmos/relayer/v2/relayer/provider"
"github.com/cosmos/relayer/v2/relayer/provider/cosmos"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -39,45 +38,6 @@ func (r *RelayMsgs) IsMaxTx(msgLen, txSize uint64) bool {
(r.MaxTxSize != 0 && txSize > r.MaxTxSize)
}

func EncodeMsgs(c *Chain, msgs []provider.RelayerMessage) []string {
outMsgs := make([]string, 0, len(msgs))
for _, msg := range msgs {
bz, err := c.Encoding.Amino.MarshalJSON(msg)
if err != nil {
msgField := zap.Skip()
if cm, ok := msg.(cosmos.CosmosMessage); ok {
msgField = zap.Object("msg", cm)
}
c.log.Warn(
"Failed to marshal message to amino JSON",
msgField,
zap.Error(err),
)
} else {
outMsgs = append(outMsgs, string(bz))
}
}
return outMsgs
}

func DecodeMsgs(c *Chain, msgs []string) []provider.RelayerMessage {
outMsgs := make([]provider.RelayerMessage, 0, len(msgs))
for _, msg := range msgs {
var sm provider.RelayerMessage
err := c.Encoding.Amino.UnmarshalJSON([]byte(msg), &sm)
if err != nil {
c.log.Warn(
"Failed to unmarshal amino JSON message",
zap.Binary("msg", []byte(msg)), // Although presented as a string, this is a binary blob.
zap.Error(err),
)
} else {
outMsgs = append(outMsgs, sm)
}
}
return outMsgs
}

// RelayMsgSender is a narrow subset of a Chain,
// to simplify testing methods on RelayMsgs.
type RelayMsgSender struct {
Expand Down

0 comments on commit b92f1de

Please sign in to comment.