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

Add override flag for client reuse #487

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
flagTimeoutTimeOffset = "timeout-time-offset"
flagMaxRetries = "max-retries"
flagThresholdTime = "time-threshold"
flagOverride = "override"
)

func ibcDenomFlags(cmd *cobra.Command) *cobra.Command {
Expand Down Expand Up @@ -268,3 +269,11 @@ func updateTimeFlags(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 @@ -122,7 +122,12 @@ func createClientsCmd() *cobra.Command {
return err
}

modified, err := c[src].CreateClients(c[dst])
override, err := cmd.Flags().GetBool(flagOverride)
if err != nil {
return err
}

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

return cmd
return overrideFlag(cmd)
}

func updateClientsCmd() *cobra.Command {
Expand Down Expand Up @@ -234,6 +239,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 @@ -243,7 +253,7 @@ $ %s tx conn demo-path --timeout 5s`,
}

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

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

func closeChannelCmd() *cobra.Command {
Expand Down Expand Up @@ -336,6 +346,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 @@ -345,7 +360,7 @@ $ %s tx connect demo-path`,
}

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

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

func linkThenStartCmd() *cobra.Command {
Expand Down Expand Up @@ -406,7 +421,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
26 changes: 17 additions & 9 deletions relayer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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) (modified bool, err error) {
func (c *Chain) CreateClients(dst *Chain, override bool) (modified bool, err error) {
// Handle off chain light clients
if err := c.ValidateLightInitialized(); err != nil {
return false, err
Expand Down Expand Up @@ -54,9 +54,13 @@ func (c *Chain) CreateClients(dst *Chain) (modified bool, err error) {
AllowUpdateAfterMisbehaviour,
)

// Check if an identical light client already exists
clientID, found := FindMatchingClient(c, dst, clientState)
if !found {
clientID, found := "", false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
clientID, found := "", false
var (
clientID string
found bool
)

You might want to commit these changes yourself since they probably aren't gofmt'd

// 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 @@ -117,11 +121,15 @@ func (c *Chain) CreateClients(dst *Chain) (modified bool, err error) {
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 {
clientID, found := "", false
// 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)
_, err = src.CreateClients(dst, false)
require.NoError(t, err)
testClientPair(t, src, dst)

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

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

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

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

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

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

Expand All @@ -147,4 +147,18 @@ 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)
require.NoError(t, err)
testClientPair(t, src, dst)

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