Skip to content

Commit

Permalink
fix(normal operation): make normal relaying work again (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
danwt authored and omritoptix committed May 15, 2024
1 parent ff1b8ca commit 304ed79
Show file tree
Hide file tree
Showing 54 changed files with 617 additions and 604 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- stuck packet search now syncs correctly
- will no longer unnecessarily wait for a block to elapse before syncing (good for slow chains)
- recommended to use fast query loop for fast rollapp chains (`min-loop-duration: 100ms`)
- in the upstream version, the relayer will try to flush every 5 seconds if acks have not yet been produced by the hub. Get around this by setting `--hub-chain-id dymension_1100-1 --flush-ignore-hub-acks`

![banner](./docs/images/comp.gif)

Expand Down
20 changes: 9 additions & 11 deletions cmd/appstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ func (a *appState) loadConfigFile(ctx context.Context) error {
// read the config file bytes
file, err := os.ReadFile(cfgPath)
if err != nil {
return fmt.Errorf("error reading file: %w", err)
return fmt.Errorf("reading file: %w", err)
}

// unmarshall them into the wrapper struct
cfgWrapper := &ConfigInputWrapper{}
err = yaml.Unmarshal(file, cfgWrapper)
if err != nil {
return fmt.Errorf("error unmarshalling config: %w", err)
return fmt.Errorf("unmarshalling config: %w", err)
}

if a.log == nil {
Expand All @@ -88,7 +88,7 @@ func (a *appState) loadConfigFile(ctx context.Context) error {

// validate runtime configuration
if err := newCfg.validateConfig(); err != nil {
return fmt.Errorf("error parsing chain config: %w", err)
return fmt.Errorf("parsing chain config: %w", err)
}

// save runtime configuration in app state
Expand Down Expand Up @@ -201,11 +201,11 @@ func (a *appState) performConfigLockingOperation(ctx context.Context, operation
fileLock := flock.New(lockFilePath)
_, err := fileLock.TryLock()
if err != nil {
return fmt.Errorf("failed to acquire config lock: %w", err)
return fmt.Errorf("acquire config lock: %w", err)
}
defer func() {
if err := fileLock.Unlock(); err != nil {
a.log.Error("error unlocking config file lock, please manually delete",
a.log.Error("Unlocking config file lock, please manually delete.",
zap.String("filepath", lockFilePath),
)
}
Expand All @@ -214,7 +214,7 @@ func (a *appState) performConfigLockingOperation(ctx context.Context, operation
// load config from file and validate it. don't want to miss
// any changes that may have been made while unlocked.
if err := a.loadConfigFile(ctx); err != nil {
return fmt.Errorf("failed to initialize config from file: %w", err)
return fmt.Errorf("initialize config from file: %w", err)
}

// perform the operation that requires config flock.
Expand All @@ -224,7 +224,7 @@ func (a *appState) performConfigLockingOperation(ctx context.Context, operation

// validate config after changes have been made.
if err := a.config.validateConfig(); err != nil {
return fmt.Errorf("error parsing chain config: %w", err)
return fmt.Errorf("parsing chain config: %w", err)
}

// marshal the new config
Expand All @@ -236,8 +236,8 @@ func (a *appState) performConfigLockingOperation(ctx context.Context, operation
cfgPath := a.configPath()

// Overwrite the config file.
if err := os.WriteFile(cfgPath, out, 0600); err != nil {
return fmt.Errorf("failed to write config file at %s: %w", cfgPath, err)
if err := os.WriteFile(cfgPath, out, 0o600); err != nil {
return fmt.Errorf("write config file at %s: %w", cfgPath, err)
}

return nil
Expand Down Expand Up @@ -277,7 +277,6 @@ func (a *appState) updatePathConfig(
}

func (a *appState) useKey(chainName, key string) error {

chain, exists := a.config.Chains[chainName]
if !exists {
return fmt.Errorf("chain %s not found in config", chainName)
Expand Down Expand Up @@ -308,7 +307,6 @@ func (a *appState) useKey(chainName, key string) error {
}

func (a *appState) useRpcAddr(chainName string, rpcAddr string) error {

_, exists := a.config.Chains[chainName]
if !exists {
return fmt.Errorf("chain %s not found in config", chainName)
Expand Down
26 changes: 13 additions & 13 deletions cmd/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func addChainFromFile(a *appState, chainName string, file string) error {
a.homePath, a.debug, chainName,
)
if err != nil {
return fmt.Errorf("failed to build ChainProvider for %s: %w", file, err)
return fmt.Errorf("build ChainProvider for %s: %w", file, err)
}

c := relayer.NewChain(a.log, prov, a.debug)
Expand Down Expand Up @@ -449,7 +449,7 @@ func addChainFromURL(a *appState, chainName string, rawurl string) error {
a.homePath, a.debug, chainName,
)
if err != nil {
return fmt.Errorf("failed to build ChainProvider for %s: %w", rawurl, err)
return fmt.Errorf("build ChainProvider for %s: %w", rawurl, err)
}

c := relayer.NewChain(a.log, prov, a.debug)
Expand All @@ -466,8 +466,8 @@ func addChainsFromRegistry(ctx context.Context, a *appState, forceAdd, testnet b

for _, chain := range chains {
if _, ok := a.config.Chains[chain]; ok {
a.log.Warn(
"Chain already exists",
a.log.Error(
"Chain already exists.",
zap.String("chain", chain),
zap.String("source_link", chainRegistry.SourceLink()),
)
Expand All @@ -477,8 +477,8 @@ func addChainsFromRegistry(ctx context.Context, a *appState, forceAdd, testnet b

chainInfo, err := chainRegistry.GetChain(ctx, testnet, chain)
if err != nil {
a.log.Warn(
"Error retrieving chain",
a.log.Error(
"Get chain.",
zap.String("chain", chain),
zap.Error(err),
)
Expand All @@ -488,8 +488,8 @@ func addChainsFromRegistry(ctx context.Context, a *appState, forceAdd, testnet b

chainConfig, err := chainInfo.GetChainConfig(ctx, forceAdd, testnet, chain)
if err != nil {
a.log.Warn(
"Error generating chain config",
a.log.Error(
"Get chain config.",
zap.String("chain", chain),
zap.Error(err),
)
Expand All @@ -505,8 +505,8 @@ func addChainsFromRegistry(ctx context.Context, a *appState, forceAdd, testnet b
a.homePath, a.debug, chainInfo.ChainName,
)
if err != nil {
a.log.Warn(
"Failed to build ChainProvider",
a.log.Error(
"Chain config new provider.",
zap.String("chain_id", chainConfig.ChainID),
zap.Error(err),
)
Expand All @@ -517,8 +517,8 @@ func addChainsFromRegistry(ctx context.Context, a *appState, forceAdd, testnet b
// add to config
c := relayer.NewChain(a.log, prov, a.debug)
if err = a.config.AddChain(c); err != nil {
a.log.Warn(
"Failed to add chain to config",
a.log.Error(
"Config add chain.",
zap.String("chain", chain),
zap.Error(err),
)
Expand All @@ -530,7 +530,7 @@ func addChainsFromRegistry(ctx context.Context, a *appState, forceAdd, testnet b
// found the correct chain so move on to next chain in chains

}
a.log.Info("Config update status",
a.log.Info("Config update status.",
zap.Any("added", added),
zap.Any("failed", failed),
zap.Any("already existed", existed),
Expand Down
14 changes: 7 additions & 7 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,21 @@ func addPathsFromDirectory(ctx context.Context, stderr io.Writer, a *appState, d

byt, err := os.ReadFile(pth)
if err != nil {
return fmt.Errorf("failed to read file %s: %w", pth, err)
return fmt.Errorf("read file %s: %w", pth, err)
}

p := &relayer.Path{}
if err = json.Unmarshal(byt, p); err != nil {
return fmt.Errorf("failed to unmarshal file %s: %w", pth, err)
return fmt.Errorf("unmarshal file %s: %w", pth, err)
}

pthName := strings.Split(f.Name(), ".")[0]
if err := a.config.ValidatePath(ctx, stderr, p); err != nil {
return fmt.Errorf("failed to validate path %s: %w", pth, err)
return fmt.Errorf("validate path %s: %w", pth, err)
}

if err := a.config.AddPath(pthName, p); err != nil {
return fmt.Errorf("failed to add path %s: %w", pth, err)
return fmt.Errorf("add path %s: %w", pth, err)
}

fmt.Fprintf(stderr, "added path %s...\n\n", pthName)
Expand Down Expand Up @@ -339,11 +339,11 @@ func (c *ConfigInputWrapper) RuntimeConfig(ctx context.Context, a *appState) (*C
a.homePath, a.debug, chainName,
)
if err != nil {
return nil, fmt.Errorf("failed to build ChainProviders: %w", err)
return nil, fmt.Errorf("build ChainProviders: %w", err)
}

if err := prov.Init(ctx); err != nil {
return nil, fmt.Errorf("failed to initialize provider: %w", err)
return nil, fmt.Errorf("initialize provider: %w", err)
}

chain := relayer.NewChain(a.log, prov, a.debug)
Expand Down Expand Up @@ -591,7 +591,7 @@ func (c *Config) validateConfig() error {
// verify that the channel filter rule is valid for every path in the config
for _, p := range c.Paths {
if err := p.ValidateChannelFilterRule(); err != nil {
return fmt.Errorf("error initializing the relayer config for path %s: %w", p.String(), err)
return fmt.Errorf("initializing the relayer config for path %s: %w", p.String(), err)
}
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/feegrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
}

if delete {
a.log.Info("Deleting feegrant configuration", zap.String("chain", chain))

cfgErr := a.performConfigLockingOperation(cmd.Context(), func() error {
chain := a.config.Chains[chain]
Expand All @@ -84,6 +83,7 @@ func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
return nil
})
cobra.CheckErr(cfgErr)
a.log.Info("Deleted fee grant configuration.", zap.String("chain", chain))
return nil
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
ctx := cmd.Context()
_, err = prov.EnsureBasicGrants(ctx, memo, gas)
if err != nil {
return fmt.Errorf("error writing grants on chain: '%s'", err.Error())
return fmt.Errorf("writing grants on chain: '%s'", err.Error())
}

// Get latest height from the chain, mark feegrant configuration as verified up to that height.
Expand All @@ -157,7 +157,7 @@ func feegrantConfigureBasicCmd(a *appState) *cobra.Command {
prov.PCfg.FeeGrants.IsExternalGranter = externalGranter
oldProv.PCfg.FeeGrants = prov.PCfg.FeeGrants
oldProv.PCfg.FeeGrants.BlockHeightVerified = h
a.log.Info("feegrant configured", zap.Int64("height", h))
a.log.Info("Configured feegrant.", zap.Int64("height", h))
return nil
})
cobra.CheckErr(cfgErr)
Expand Down Expand Up @@ -205,7 +205,7 @@ func feegrantBasicGrantsCmd(a *appState) *cobra.Command {

granterAcc, err := prov.AccountFromKeyOrAddress(keyNameOrAddress)
if err != nil {
a.log.Error("Unknown account", zap.String("key_or_address", keyNameOrAddress), zap.Error(err))
a.log.Error("Unknown account.", zap.String("key_or_address", keyNameOrAddress), zap.Error(err))
return err
}
granterAddr := prov.MustEncodeAccAddr(granterAcc)
Expand All @@ -218,7 +218,7 @@ func feegrantBasicGrantsCmd(a *appState) *cobra.Command {
for _, grant := range res {
allowance, e := prov.Sprint(grant.Allowance)
cobra.CheckErr(e)
a.log.Info("feegrant", zap.String("granter", grant.Granter), zap.String("grantee", grant.Grantee), zap.String("allowance", allowance))
a.log.Info("Feegrant.", zap.String("granter", grant.Granter), zap.String("grantee", grant.Grantee), zap.String("allowance", allowance))
}

return nil
Expand Down
14 changes: 14 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const (
flagStuckPacketChainID = "stuck-packet-chain-id"
flagStuckPacketHeightStart = "stuck-packet-height-start"
flagStuckPacketHeightEnd = "stuck-packet-height-end"
flagHubChainID = "hub-chain-id"
flagFlushIgnoreHubAcks = "flush-ignore-hub-acks"
)

const blankValue = "blank"
Expand Down Expand Up @@ -494,6 +496,18 @@ func addOutputFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
return cmd
}

func addHubAckRetryFlags(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
cmd.Flags().String(flagHubChainID, "", "hub chain id")
if err := v.BindPFlag(flagHubChainID, cmd.Flags().Lookup(flagHubChainID)); err != nil {
panic(err)
}
cmd.Flags().Bool(flagFlushIgnoreHubAcks, false, "ignore missing acks on the hub when flushing")
if err := v.BindPFlag(flagFlushIgnoreHubAcks, cmd.Flags().Lookup(flagFlushIgnoreHubAcks)); err != nil {
panic(err)
}
return cmd
}

func stuckPacketFlags(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
cmd.Flags().String(flagStuckPacketChainID, "", "chain ID with the stuck packet(s)")
if err := v.BindPFlag(flagStuckPacketChainID, cmd.Flags().Lookup(flagStuckPacketChainID)); err != nil {
Expand Down
6 changes: 2 additions & 4 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func keysCmd(a *appState) *cobra.Command {
}

func keysUseCmd(a *appState) *cobra.Command {

cmd := &cobra.Command{
Use: "use chain_name key_name",
Aliases: []string{"u"},
Expand Down Expand Up @@ -125,7 +124,7 @@ $ %s k a cosmoshub testkey`, appName, appName, appName)),

ko, err := chain.ChainProvider.AddKey(keyName, uint32(coinType), algo)
if err != nil {
return fmt.Errorf("failed to add key: %w", err)
return fmt.Errorf("add key: %w", err)
}

out, err := json.Marshal(&ko)
Expand Down Expand Up @@ -155,7 +154,6 @@ $ %s keys restore ibc-0 testkey "[mnemonic-words]"
$ %s k r cosmoshub faucet-key "[mnemonic-words]"
$ %s k r demo-key "[mnemonic-words]" --restore-all`, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {

cmdFlags := cmd.Flags()

restoreAll, err := cmdFlags.GetBool(flagRestoreAll)
Expand Down Expand Up @@ -312,7 +310,7 @@ func askForConfirmation(a *appState, stdin io.Reader, stderr io.Writer) bool {

_, err := fmt.Fscanln(stdin, &response)
if err != nil {
a.log.Fatal("Failed to read input", zap.Error(err))
a.log.Fatal("Fscanln.", zap.Error(err))
}

switch strings.ToLower(response) {
Expand Down
7 changes: 3 additions & 4 deletions cmd/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ $ %s pth fch`, appName, defaultHome, appName, appName)),
regPath = path.Join("testnets", "_IBC", fileName)
} else {
regPath = path.Join("_IBC", fileName)

}
client, _, err := client.Repositories.DownloadContents(cmd.Context(), "cosmos", "chain-registry", regPath, nil)
if err != nil {
Expand All @@ -441,12 +440,12 @@ $ %s pth fch`, appName, defaultHome, appName, appName)),

b, err := io.ReadAll(client)
if err != nil {
return fmt.Errorf("error reading response body: %w", err)
return fmt.Errorf("reading response body: %w", err)
}

ibc := &relayer.IBCdata{}
if err = json.Unmarshal(b, &ibc); err != nil {
return fmt.Errorf("failed to unmarshal: %w ", err)
return fmt.Errorf("unmarshal: %w ", err)
}

srcChainName := ibc.Chain1.ChainName
Expand All @@ -469,7 +468,7 @@ $ %s pth fch`, appName, defaultHome, appName, appName)),
client.Close()

if err = a.config.AddPath(pthName, newPath); err != nil {
return fmt.Errorf("failed to add path %s: %w", pthName, err)
return fmt.Errorf("add path %s: %w", pthName, err)
}
fmt.Fprintf(cmd.ErrOrStderr(), "added: %s\n", pthName)

Expand Down
Loading

0 comments on commit 304ed79

Please sign in to comment.