Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow update of a specific height from the latest height stored in contract #206

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
// Inside persistent pre-run because this takes effect after flags are parsed.
if log == nil {
log, err := newRootLogger(a.viper.GetString("log-format"), a.viper.GetBool("debug"))
log, err := newRootLogger(a.viper.GetString(flagHome), a.viper.GetString("log-format"), a.viper.GetBool("debug"))

Check warning on line 80 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L80

Added line #L80 was not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -180,16 +180,16 @@

func (lumberjackSink) Sync() error { return nil }

func newRootLogger(format string, debug bool) (*zap.Logger, error) {
func newRootLogger(homepath string, format string, debug bool) (*zap.Logger, error) {

Check warning on line 183 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L183

Added line #L183 was not covered by tests
config := zap.NewProductionEncoderConfig()
config.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(ts.UTC().Format("2006-01-02T15:04:05.000000Z07:00"))
}
config.LevelKey = "lvl"

ll := lumberjack.Logger{
Filename: path.Join(defaultHome, "relay.log"),
MaxSize: 10, //MB
Filename: path.Join(homepath, "relay.log"),
MaxSize: 100, //MB

Check warning on line 192 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L191-L192

Added lines #L191 - L192 were not covered by tests
MaxBackups: 30,
MaxAge: 28, //days
Compress: false,
Expand Down
49 changes: 49 additions & 0 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -44,6 +45,7 @@
lineBreakCommand(),
createClientsCmd(a),
createClientCmd(a),
updateClientCmd(a),
updateClientsCmd(a),
upgradeClientsCmd(a),
createConnectionCmd(a),
Expand Down Expand Up @@ -265,6 +267,53 @@
return cmd
}

func updateClientCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "update-client src_chain_name dst_chain_name path_name [heights]",
Short: "update block [heights] of src_chain on dst_chain",
Args: withUsage(cobra.ExactArgs(4)),
Example: strings.TrimSpace(fmt.Sprintf(`$ %s transact update-client icon archway icon-archway 75830974,75830975`, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
src, ok := a.config.Chains[args[0]]
if !ok {
return errChainNotFound(args[0])
}
dst, ok := a.config.Chains[args[1]]
if !ok {
return errChainNotFound(args[1])
}
_, _, _, err := a.config.ChainsFromPath(args[2])
if err != nil {
return err
}

Check warning on line 288 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L277-L288

Added lines #L277 - L288 were not covered by tests

// ensure that keys exist
if exists := src.ChainProvider.KeyExists(src.ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", src.ChainProvider.Key(), src.ChainID())
}
if exists := dst.ChainProvider.KeyExists(dst.ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", dst.ChainProvider.Key(), dst.ChainID())
}

Check warning on line 296 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L291-L296

Added lines #L291 - L296 were not covered by tests

var heights []int64

numStr := strings.Split(args[3], ",")

for _, s := range numStr {
num, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return fmt.Errorf("error converting string to int64: %w", err)

}
heights = append(heights, num)

Check warning on line 308 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L298-L308

Added lines #L298 - L308 were not covered by tests
}

return relayer.UpdateClient(cmd.Context(), src, dst, a.config.memo(cmd), heights)

Check warning on line 311 in cmd/tx.go

View check run for this annotation

Codecov / codecov/patch

cmd/tx.go#L311

Added line #L311 was not covered by tests
},
}
return memoFlag(a.viper, cmd)
}

func updateClientsCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "update-clients path_name",
Expand Down
10 changes: 7 additions & 3 deletions relayer/chains/wasm/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,12 +736,16 @@

wasmMsg, ok := msg.(*WasmContractMessage)
if !ok {
return fmt.Errorf("Wasm Message is not valid %s", wasmMsg.Type())
return fmt.Errorf("wasm Message is not valid %s", wasmMsg.Type())

Check warning on line 739 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L739

Added line #L739 was not covered by tests
}

txBytes, sequence, err := ap.buildMessages(cliCtx, factory, wasmMsg.Msg)
if err != nil {
return err
if strings.Contains(err.Error(), sdkerrors.ErrWrongSequence.Error()) {
ap.handleAccountSequenceMismatchError(err)
} else {
return err
}

Check warning on line 748 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L744-L748

Added lines #L744 - L748 were not covered by tests
}

if msg.Type() == MethodUpdateClient {
Expand Down Expand Up @@ -997,7 +1001,7 @@
if isFailed {
err = ap.sdkError(res.Codespace, res.Code)
if err == nil {
err = fmt.Errorf("transaction failed to execute")
err = fmt.Errorf("transaction failed to execute: %s", res.RawLog)

Check warning on line 1004 in relayer/chains/wasm/tx.go

View check run for this annotation

Codecov / codecov/patch

relayer/chains/wasm/tx.go#L1004

Added line #L1004 was not covered by tests
}
}
ap.LogFailedTx(rlyResp, err, msgs)
Expand Down
95 changes: 95 additions & 0 deletions relayer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,101 @@
return dst.ChainProvider.MsgUpdateClient(dst.ClientID(), updateHeader)
}

func msgUpdateClientOneWay(ctx context.Context, src, dst *Chain, height int64) (provider.RelayerMessage, error) {

var updateHeader ibcexported.ClientMessage
if err := retry.Do(func() error {
var err error

dstHeight, err := dst.ChainProvider.QueryLatestHeight(ctx)
if err != nil {
return err
}

Check warning on line 369 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L360-L369

Added lines #L360 - L369 were not covered by tests

dstClientState, err := dst.ChainProvider.QueryClientState(ctx, dstHeight, dst.ClientID())
if err != nil {
return err
}

Check warning on line 374 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L371-L374

Added lines #L371 - L374 were not covered by tests

trustedHdr, err := src.ChainProvider.QueryIBCHeader(ctx, int64(dstClientState.GetLatestHeight().GetRevisionHeight()))
if err != nil {
return err
}

Check warning on line 379 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L376-L379

Added lines #L376 - L379 were not covered by tests

latestHdr, err := src.ChainProvider.QueryIBCHeader(ctx, height)
if err != nil {
return err
}

Check warning on line 384 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L381-L384

Added lines #L381 - L384 were not covered by tests

trustedHeight := clienttypes.Height{
RevisionNumber: 0,
RevisionHeight: trustedHdr.Height(),
}

updateHeader, err = src.ChainProvider.MsgUpdateClientHeader(latestHdr, trustedHeight, trustedHdr)
return err
}, retry.Context(ctx), RtyAtt, RtyDel, RtyErr, retry.OnRetry(func(n uint, err error) {
dst.log.Info(
"Failed to build update message",
zap.String("client_id", dst.ClientID()),
zap.Uint("attempt", n+1),
zap.Uint("max_attempts", RtyAttNum),
zap.Error(err),
)
})); err != nil {
return nil, err
}

Check warning on line 403 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L386-L403

Added lines #L386 - L403 were not covered by tests

return dst.ChainProvider.MsgUpdateClient(dst.ClientID(), updateHeader)

Check warning on line 405 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L405

Added line #L405 was not covered by tests
}

func UpdateClient(ctx context.Context, src, dst *Chain, memo string, heights []int64) error {
eg, egCtx := errgroup.WithContext(ctx)
for _, height := range heights {

var dstMsgUpdateClient provider.RelayerMessage
eg.Go(func() error {
var err error
dstMsgUpdateClient, err = msgUpdateClientOneWay(egCtx, src, dst, height)
return err
})

Check warning on line 417 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L408-L417

Added lines #L408 - L417 were not covered by tests

if err := eg.Wait(); err != nil {
return err
}

Check warning on line 421 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L419-L421

Added lines #L419 - L421 were not covered by tests

clients := &RelayMsgs{
Src: []provider.RelayerMessage{},
Dst: []provider.RelayerMessage{dstMsgUpdateClient},
}

result := clients.Send(ctx, src.log, AsRelayMsgSender(src), AsRelayMsgSender(dst), memo)

if err := result.Error(); err != nil {
if result.PartiallySent() {
src.log.Info(
"Partial success when updating clients",
zap.String("src_chain_id", src.ChainID()),
zap.String("dst_chain_id", dst.ChainID()),
zap.Object("send_result", result),
)
}
return err

Check warning on line 439 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L423-L439

Added lines #L423 - L439 were not covered by tests
}

src.log.Info(
"Client updated",
zap.String("src_chain_id", src.ChainID()),
zap.String("src_client", src.PathEnd.ClientID),

zap.String("dst_chain_id", dst.ChainID()),
zap.String("dst_client", dst.PathEnd.ClientID),
)

Check warning on line 449 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L442-L449

Added lines #L442 - L449 were not covered by tests
}

return nil

Check warning on line 452 in relayer/client.go

View check run for this annotation

Codecov / codecov/patch

relayer/client.go#L452

Added line #L452 was not covered by tests
}

// UpdateClients updates clients for src on dst and dst on src given the configured paths.
func UpdateClients(
ctx context.Context,
Expand Down
Loading