Skip to content

Commit 26aa983

Browse files
authoredNov 2, 2023
Create new client with custom unbonding period (cosmos#1313)
* client state unbonding period * remove else
1 parent e4e9f27 commit 26aa983

File tree

7 files changed

+48
-22
lines changed

7 files changed

+48
-22
lines changed
 

‎cmd/flags.go

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
flagUpdateAfterExpiry = "update-after-expiry"
3333
flagUpdateAfterMisbehaviour = "update-after-misbehaviour"
3434
flagClientTrustingPeriod = "client-tp"
35+
flagClientUnbondingPeriod = "client-unbonding-period"
3536
flagOverride = "override"
3637
flagSrcPort = "src-port"
3738
flagDstPort = "dst-port"
@@ -332,6 +333,14 @@ func channelParameterFlags(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
332333
return srcPortFlag(v, dstPortFlag(v, versionFlag(v, orderFlag(v, cmd))))
333334
}
334335

336+
func clientUnbondingPeriodFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
337+
cmd.Flags().Duration(flagClientUnbondingPeriod, 0, "custom unbonding period for client state. This is useful when you need to create a new client matching an older client state")
338+
if err := v.BindPFlag(flagClientUnbondingPeriod, cmd.Flags().Lookup(flagClientUnbondingPeriod)); err != nil {
339+
panic(err)
340+
}
341+
return cmd
342+
}
343+
335344
func overrideFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
336345
cmd.Flags().Bool(flagOverride, false, "option to not reuse existing client or channel")
337346
if err := v.BindPFlag(flagOverride, cmd.Flags().Lookup(flagOverride)); err != nil {

‎cmd/tx.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ func createClientCmd(a *appState) *cobra.Command {
143143
return err
144144
}
145145

146+
overrideUnbondingPeriod, err := cmd.Flags().GetDuration(flagClientUnbondingPeriod)
147+
if err != nil {
148+
return err
149+
}
150+
146151
override, err := cmd.Flags().GetBool(flagOverride)
147152
if err != nil {
148153
return err
@@ -210,7 +215,7 @@ func createClientCmd(a *appState) *cobra.Command {
210215
return err
211216
}
212217

213-
clientID, err := relayer.CreateClient(cmd.Context(), src, dst, srcUpdateHeader, dstUpdateHeader, allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override, customClientTrustingPeriod, a.config.memo(cmd))
218+
clientID, err := relayer.CreateClient(cmd.Context(), src, dst, srcUpdateHeader, dstUpdateHeader, allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override, customClientTrustingPeriod, overrideUnbondingPeriod, a.config.memo(cmd))
214219
if err != nil {
215220
return err
216221
}
@@ -231,6 +236,7 @@ func createClientCmd(a *appState) *cobra.Command {
231236
}
232237

233238
cmd = clientParameterFlags(a.viper, cmd)
239+
cmd = clientUnbondingPeriodFlag(a.viper, cmd)
234240
cmd = overrideFlag(a.viper, cmd)
235241
cmd = memoFlag(a.viper, cmd)
236242
return cmd

‎relayer/chain.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func (c *Chain) GetSelfVersion() uint64 {
9696
}
9797

9898
// GetTrustingPeriod returns the trusting period for the chain
99-
func (c *Chain) GetTrustingPeriod(ctx context.Context) (time.Duration, error) {
100-
return c.ChainProvider.TrustingPeriod(ctx)
99+
func (c *Chain) GetTrustingPeriod(ctx context.Context, overrideUnbondingPeriod time.Duration) (time.Duration, error) {
100+
return c.ChainProvider.TrustingPeriod(ctx, overrideUnbondingPeriod)
101101
}
102102

103103
func (c *Chain) String() string {

‎relayer/chains/cosmos/provider.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,17 @@ func (cc *CosmosProvider) AccountFromKeyOrAddress(keyOrAddress string) (out sdk.
225225
return
226226
}
227227

228-
func (cc *CosmosProvider) TrustingPeriod(ctx context.Context) (time.Duration, error) {
228+
func (cc *CosmosProvider) TrustingPeriod(ctx context.Context, overrideUnbondingPeriod time.Duration) (time.Duration, error) {
229229

230-
unbondingTime, err := cc.QueryUnbondingPeriod(ctx)
231-
if err != nil {
232-
return 0, err
230+
unbondingTime := overrideUnbondingPeriod
231+
var err error
232+
if unbondingTime == 0 {
233+
unbondingTime, err = cc.QueryUnbondingPeriod(ctx)
234+
if err != nil {
235+
return 0, err
236+
}
233237
}
238+
234239
// We want the trusting period to be 85% of the unbonding time.
235240
// Go mentions that the time.Duration type can track approximately 290 years.
236241
// We don't want to lose precision if the duration is a very long duration

‎relayer/chains/penumbra/provider.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (cc *PenumbraProvider) Address() (string, error) {
196196
return out, err
197197
}
198198

199-
func (cc *PenumbraProvider) TrustingPeriod(ctx context.Context) (time.Duration, error) {
199+
func (cc *PenumbraProvider) TrustingPeriod(ctx context.Context, overrideUnbondingPeriod time.Duration) (time.Duration, error) {
200200
// TODO
201201
return time.Hour * 2, nil
202202
/*

‎relayer/client.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ func (c *Chain) CreateClients(ctx context.Context, dst *Chain, allowUpdateAfterE
5555
return "", "", err
5656
}
5757

58+
// overriding the unbonding period should only be possible when creating single clients at a time (CreateClient)
59+
var overrideUnbondingPeriod = time.Duration(0)
60+
5861
var clientSrc, clientDst string
5962
eg, egCtx := errgroup.WithContext(ctx)
6063
eg.Go(func() error {
6164
var err error
6265
// Create client on src for dst if the client id is unspecified
63-
clientSrc, err = CreateClient(egCtx, c, dst, srcUpdateHeader, dstUpdateHeader, allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override, customClientTrustingPeriod, memo)
66+
clientSrc, err = CreateClient(egCtx, c, dst, srcUpdateHeader, dstUpdateHeader, allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override, customClientTrustingPeriod, overrideUnbondingPeriod, memo)
6467
if err != nil {
6568
return fmt.Errorf("failed to create client on src chain{%s}: %w", c.ChainID(), err)
6669
}
@@ -70,7 +73,7 @@ func (c *Chain) CreateClients(ctx context.Context, dst *Chain, allowUpdateAfterE
7073
eg.Go(func() error {
7174
var err error
7275
// Create client on dst for src if the client id is unspecified
73-
clientDst, err = CreateClient(egCtx, dst, c, dstUpdateHeader, srcUpdateHeader, allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override, customClientTrustingPeriod, memo)
76+
clientDst, err = CreateClient(egCtx, dst, c, dstUpdateHeader, srcUpdateHeader, allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override, customClientTrustingPeriod, overrideUnbondingPeriod, memo)
7477
if err != nil {
7578
return fmt.Errorf("failed to create client on dst chain{%s}: %w", dst.ChainID(), err)
7679
}
@@ -102,6 +105,7 @@ func CreateClient(
102105
allowUpdateAfterMisbehaviour bool,
103106
override bool,
104107
customClientTrustingPeriod time.Duration,
108+
overrideUnbondingPeriod time.Duration,
105109
memo string) (string, error) {
106110
// If a client ID was specified in the path and override is not set, ensure the client exists.
107111
if !override && src.PathEnd.ClientID != "" {
@@ -122,7 +126,7 @@ func CreateClient(
122126
if tp == 0 {
123127
if err := retry.Do(func() error {
124128
var err error
125-
tp, err = dst.GetTrustingPeriod(ctx)
129+
tp, err = dst.GetTrustingPeriod(ctx, overrideUnbondingPeriod)
126130
if err != nil {
127131
return fmt.Errorf("failed to get trusting period for chain{%s}: %w", dst.ChainID(), err)
128132
}
@@ -143,17 +147,19 @@ func CreateClient(
143147
zap.Duration("trust_period", tp),
144148
)
145149

146-
// Query the unbonding period for dst and retry if the query fails
147-
var ubdPeriod time.Duration
148-
if err := retry.Do(func() error {
149-
var err error
150-
ubdPeriod, err = dst.ChainProvider.QueryUnbondingPeriod(ctx)
151-
if err != nil {
152-
return fmt.Errorf("failed to query unbonding period for chain{%s}: %w", dst.ChainID(), err)
150+
ubdPeriod := overrideUnbondingPeriod
151+
if ubdPeriod == 0 {
152+
// Query the unbonding period for dst and retry if the query fails
153+
if err := retry.Do(func() error {
154+
var err error
155+
ubdPeriod, err = dst.ChainProvider.QueryUnbondingPeriod(ctx)
156+
if err != nil {
157+
return fmt.Errorf("failed to query unbonding period for chain{%s}: %w", dst.ChainID(), err)
158+
}
159+
return nil
160+
}, retry.Context(ctx), RtyAtt, RtyDel, RtyErr); err != nil {
161+
return "", err
153162
}
154-
return nil
155-
}, retry.Context(ctx), RtyAtt, RtyDel, RtyErr); err != nil {
156-
return "", err
157163
}
158164

159165
// We want to create a light client on the src chain which tracks the state of the dst chain.

‎relayer/provider/provider.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ type ChainProvider interface {
404404
Key() string
405405
Address() (string, error)
406406
Timeout() string
407-
TrustingPeriod(ctx context.Context) (time.Duration, error)
407+
TrustingPeriod(ctx context.Context, overrideUnbondingPeriod time.Duration) (time.Duration, error)
408408
WaitForNBlocks(ctx context.Context, n int64) error
409409
Sprint(toPrint proto.Message) (string, error)
410410

0 commit comments

Comments
 (0)