Skip to content

Commit

Permalink
Add override flag for client reuse (cosmos#487)
Browse files Browse the repository at this point in the history
* Add override flag to not reuse same client

* Address suggested change
  • Loading branch information
akhilkumarpilli authored Apr 15, 2021
1 parent 72a1c1a commit 492378a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* [\#491](https://github.com/cosmos/relayer/pull/491) Add client parameter flags to allow governance to update the client if expiry or misbehaviour freezing occurs.
* [\#488](https://github.com/cosmos/relayer/pull/488) Fix state based relaying for acknowledgements.
* [\#487](https://github.com/cosmos/relayer/pull/487) Add override flag for client reuse.
* [\#474](https://github.com/cosmos/relayer/pull/474) Fix validator set mismatch when updating IBC client.
* [\#456](https://github.com/cosmos/relayer/pull/456) Fix bug which incorrectly set the timeout on a transfer.
* [\#455](https://github.com/cosmos/relayer/pull/455) Set default client parameter to allow governance to update the client if expiry or misbehaviour freezing occurs.
Expand Down
9 changes: 9 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
flagThresholdTime = "time-threshold"
flagUpdateAfterExpiry = "update-after-expiry"
flagUpdateAfterMisbehaviour = "update-after-misbehaviour"
flagOverride = "override"
)

func ibcDenomFlags(cmd *cobra.Command) *cobra.Command {
Expand Down Expand Up @@ -284,3 +285,11 @@ func clientParameterFlags(cmd *cobra.Command) *cobra.Command {
}
return cmd
}

func overrideFlag(cmd *cobra.Command) *cobra.Command {
cmd.Flags().Bool(flagOverride, false, "option to not reuse existing client")
if err := viper.BindPFlag(flagOverride, cmd.Flags().Lookup(flagOverride)); err != nil {
panic(err)
}
return cmd
}
29 changes: 22 additions & 7 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func createClientsCmd() *cobra.Command {
return err
}

override, err := cmd.Flags().GetBool(flagOverride)
if err != nil {
return err
}

c, src, dst, err := config.ChainsFromPath(args[0])
if err != nil {
return err
Expand All @@ -133,7 +138,7 @@ func createClientsCmd() *cobra.Command {
}

modified, err := c[src].CreateClients(c[dst], allowUpdateAfterExpiry,
allowUpdateAfterMisbehaviour)
allowUpdateAfterMisbehaviour, override)
if modified {
if err := overWriteConfig(config); err != nil {
return err
Expand All @@ -144,7 +149,7 @@ func createClientsCmd() *cobra.Command {
},
}

return clientParameterFlags(cmd)
return overrideFlag(clientParameterFlags(cmd))
}

func updateClientsCmd() *cobra.Command {
Expand Down Expand Up @@ -255,6 +260,11 @@ $ %s tx conn demo-path --timeout 5s`,
return err
}

override, err := cmd.Flags().GetBool(flagOverride)
if err != nil {
return err
}

// ensure that keys exist
if _, err = c[src].GetAddress(); err != nil {
return err
Expand All @@ -265,7 +275,7 @@ $ %s tx conn demo-path --timeout 5s`,

// ensure that the clients exist
modified, err := c[src].CreateClients(c[dst], allowUpdateAfterExpiry,
allowUpdateAfterMisbehaviour)
allowUpdateAfterMisbehaviour, override)
if modified {
if err := overWriteConfig(config); err != nil {
return err
Expand All @@ -286,7 +296,7 @@ $ %s tx conn demo-path --timeout 5s`,
},
}

return clientParameterFlags(retryFlag(timeoutFlag(cmd)))
return overrideFlag(clientParameterFlags(retryFlag(timeoutFlag(cmd))))
}

func closeChannelCmd() *cobra.Command {
Expand Down Expand Up @@ -368,6 +378,11 @@ $ %s tx connect demo-path`,
return err
}

override, err := cmd.Flags().GetBool(flagOverride)
if err != nil {
return err
}

// ensure that keys exist
if _, err = c[src].GetAddress(); err != nil {
return err
Expand All @@ -378,7 +393,7 @@ $ %s tx connect demo-path`,

// create clients if they aren't already created
modified, err := c[src].CreateClients(c[dst], allowUpdateAfterExpiry,
allowUpdateAfterMisbehaviour)
allowUpdateAfterMisbehaviour, override)
if modified {
if err := overWriteConfig(config); err != nil {
return err
Expand Down Expand Up @@ -411,7 +426,7 @@ $ %s tx connect demo-path`,
},
}

return clientParameterFlags(retryFlag(timeoutFlag(cmd)))
return overrideFlag(clientParameterFlags(retryFlag(timeoutFlag(cmd))))
}

func linkThenStartCmd() *cobra.Command {
Expand Down Expand Up @@ -439,7 +454,7 @@ $ %s tx link-then-start demo-path --timeout 5s`, appName, appName)),
},
}

return strategyFlag(retryFlag(timeoutFlag(cmd)))
return overrideFlag(strategyFlag(retryFlag(timeoutFlag(cmd))))
}

func relayMsgsCmd() *cobra.Command {
Expand Down
32 changes: 23 additions & 9 deletions relayer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// CreateClients creates clients for src on dst and dst on src if the client ids are unspecified.
// TODO: de-duplicate code
func (c *Chain) CreateClients(dst *Chain, allowUpdateAfterExpiry,
allowUpdateAfterMisbehaviour bool) (modified bool, err error) {
allowUpdateAfterMisbehaviour, override bool) (modified bool, err error) {
// Handle off chain light clients
if err := c.ValidateLightInitialized(); err != nil {
return false, err
Expand Down Expand Up @@ -55,9 +55,16 @@ func (c *Chain) CreateClients(dst *Chain, allowUpdateAfterExpiry,
allowUpdateAfterMisbehaviour,
)

// Check if an identical light client already exists
clientID, found := FindMatchingClient(c, dst, clientState)
if !found {
var (
clientID string
found bool
)
// Will not reuse same client if override is true
if !override {
// Check if an identical light client already exists
clientID, found = FindMatchingClient(c, dst, clientState)
}
if !found || override {
msgs := []sdk.Msg{
c.CreateClient(
clientState,
Expand Down Expand Up @@ -118,11 +125,18 @@ func (c *Chain) CreateClients(dst *Chain, allowUpdateAfterExpiry,
allowUpdateAfterMisbehaviour,
)

// Check if an identical light client already exists
// NOTE: we pass in 'dst' as the source and 'c' as the
// counterparty.
clientID, found := FindMatchingClient(dst, c, clientState)
if !found {
var (
clientID string
found bool
)
// Will not reuse same client if override is true
if !override {
// Check if an identical light client already exists
// NOTE: we pass in 'dst' as the source and 'c' as the
// counterparty.
clientID, found = FindMatchingClient(dst, c, clientState)
}
if !found || override {
msgs := []sdk.Msg{
dst.CreateClient(
clientState,
Expand Down
2 changes: 1 addition & 1 deletion test/relayer_akash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAkashToGaiaStreamingRelayer(t *testing.T) {
require.NoError(t, err)

// create path
_, err = src.CreateClients(dst, true, true)
_, err = src.CreateClients(dst, true, true, false)
require.NoError(t, err)
testClientPair(t, src, dst)

Expand Down
22 changes: 18 additions & 4 deletions test/relayer_gaia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestGaiaToGaiaStreamingRelayer(t *testing.T) {
require.NoError(t, err)

// create path
_, err = src.CreateClients(dst, true, true)
_, err = src.CreateClients(dst, true, true, false)
require.NoError(t, err)
testClientPair(t, src, dst)

Expand Down Expand Up @@ -120,7 +120,7 @@ func TestGaiaReuseIdentifiers(t *testing.T) {
require.NoError(t, err)

// create path
_, err = src.CreateClients(dst, true, true)
_, err = src.CreateClients(dst, true, true, false)
require.NoError(t, err)
testClientPair(t, src, dst)

Expand All @@ -143,7 +143,7 @@ func TestGaiaReuseIdentifiers(t *testing.T) {
dst.PathEnd.ConnectionID = ""
dst.PathEnd.ChannelID = ""

_, err = src.CreateClients(dst, true, true)
_, err = src.CreateClients(dst, true, true, false)
require.NoError(t, err)
testClientPair(t, src, dst)

Expand All @@ -157,6 +157,20 @@ func TestGaiaReuseIdentifiers(t *testing.T) {

require.Equal(t, expectedSrc, src)
require.Equal(t, expectedDst, dst)

expectedSrcClient := src.PathEnd.ClientID
expectedDstClient := dst.PathEnd.ClientID

// test client creation with override
src.PathEnd.ClientID = ""
dst.PathEnd.ClientID = ""

_, err = src.CreateClients(dst, true, true, true)
require.NoError(t, err)
testClientPair(t, src, dst)

require.NotEqual(t, expectedSrcClient, src.PathEnd.ClientID)
require.NotEqual(t, expectedDstClient, dst.PathEnd.ClientID)
}

func TestGaiaMisbehaviourMonitoring(t *testing.T) {
Expand All @@ -171,7 +185,7 @@ func TestGaiaMisbehaviourMonitoring(t *testing.T) {
require.NoError(t, err)

// create path
_, err = src.CreateClients(dst, true, true)
_, err = src.CreateClients(dst, true, true, false)
require.NoError(t, err)
testClientPair(t, src, dst)

Expand Down

0 comments on commit 492378a

Please sign in to comment.