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

Adding support for RecoverKey in the chain interface. #176

Merged
merged 4 commits into from
Jun 28, 2022
Merged

Conversation

chatton
Copy link
Contributor

@chatton chatton commented Jun 27, 2022

Description

This PR adds the functionality to recover a test user from a given mnemonic.

Checklist

  • I have made sure the upstream branch for this PR is correct
  • I have made sure this PR is ready to merge
  • I have made sure that I have assigned reviewers related to this project

Changes

  • Added RecoverKey to ChainNode and CosmosChain types.
  • Added RecoverKey to Chain interface.
  • Added Shell field to ChainConfig. This is to allow you to specify which shell commands which run using -c <script> will use, as not all images will have the same shells.
  • Added GetAndFundTestUserWithMnemonic which generates a user using a given mnemonic.
  • Changed GetAndFundTestUsers to delegate to GetAndFundTestUserWithMnemonic
  • Changed generateUserWallet to either Create or Recover based on the presence of a mnemonic.

Testing

Tests have been added which verify that users can be created using CreateKey and RecoverKey

closes #169

…helper method which generates a user using a provided mnemonic.
@chatton chatton requested a review from a team as a code owner June 27, 2022 14:33
@chatton chatton requested review from DavidNix and removed request for a team June 27, 2022 14:33
@chatton chatton mentioned this pull request Jun 27, 2022
33 tasks
Comment on lines +143 to +145
func (c *PenumbraChain) RecoverKey(ctx context.Context, name, mnemonic string) error {
return fmt.Errorf("RecoverKey not implemented for PenumbraChain")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure exactly what would need to be done here.

Copy link
Member

Choose a reason for hiding this comment

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

I think this error is fine for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, Penumbra is highly experimental right now.

Copy link
Member

@mark-rushakoff mark-rushakoff left a comment

Choose a reason for hiding this comment

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

There are a few things to clean up, but I think we'll be able to get this merged soon.

Comment on lines +143 to +145
func (c *PenumbraChain) RecoverKey(ctx context.Context, name, mnemonic string) error {
return fmt.Errorf("RecoverKey not implemented for PenumbraChain")
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this error is fine for now.

test_user.go Outdated
Denom: chainCfg.Denom,
})
require.NoError(t, err, "failed to get funds from faucet")
require.NoError(t, test.WaitForBlocks(ctx, 5, chain), "failed to wait for blocks")
Copy link
Member

Choose a reason for hiding this comment

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

Did 5 come from anywhere special? Would 4 or 3 work?

Some of these tests can take a long time to complete, so if there is a way to minimize time spent waiting, we should take it.

Maybe it would make more sense to not wait inside this function, and indicate in Godoc that the caller should wait for some blocks to complete before the funds will be accessible. There is one wait at the end of GetAndFundTestUsers that would be redundant with this call, if it stays.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This wait was just taken from the existing GetAndFundTestUsers function, however I think it's no harm removing it and letting the caller know as you suggested. I can remove from both.

require.NotEmpty(t, users[0].KeyName)
})

_ = ic.Close()
Copy link
Member

Choose a reason for hiding this comment

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

Preferable to defer ic.Close() immediately after the initial assignment.

If the linter complains about a discarded error value, defer func() { _ = ic.Close() } is fine.

ibc/types.go Outdated
Comment on lines 66 to 73
// GetShell returns the shell that should be used when executing scripts.
func (c ChainConfig) GetShell() string {
if c.Shell == "" {
return "sh"
}
return c.Shell
}

Copy link
Member

Choose a reason for hiding this comment

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

For now, I think we should assume all containers have sh available. It will likely be a long time before we run into something we can't do with plain sh.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. The benefit is keeping ChainConfig simple.

Comment on lines 176 to 184
ic := ibctest.NewInterchain().
AddChain(gaia0).
AddChain(gaia1).
AddRelayer(r, "r").
AddLink(ibctest.InterchainLink{
Chain1: gaia0,
Chain2: gaia1,
Relayer: r,
})
Copy link
Member

Choose a reason for hiding this comment

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

Because this test is only exercising funding a user, I think this can be reduced to ic := ibctest.NewInterchain().AddChain(gaia0).

This should be testable even without an Interchain, but since that does the heavy lifting to start a chain, it's probably more convenient than calling several methods directly on a chain.

NetworkID: network,
}))

t.Run("Test User creation with mnemonic", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Names of subtests should be shorter because they are displayed after the outer test's name. This line can be t.Run("with mnemonic", ...).

require.NotEmpty(t, user.KeyName)
})

t.Run("Test user creation without mnemonic", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

t.Run("without mnemonic", ...)

interchain_test.go Show resolved Hide resolved
// RecoverKey restores a key from a given mnemonic.
func (tn *ChainNode) RecoverKey(ctx context.Context, keyName, mnemonic string) error {
command := []string{
tn.Chain.Config().GetShell(),
Copy link
Member

Choose a reason for hiding this comment

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

This can be hardcoded to "sh" after removing the GetShell method.

command := []string{
tn.Chain.Config().GetShell(),
"-c",
fmt.Sprintf(`echo "%s" | %s keys add %s --recover --keyring-backend %s --home %s --output json`, mnemonic, tn.Chain.Config().Bin, keyName, keyring.BackendTest, tn.NodeHome()),
Copy link
Member

Choose a reason for hiding this comment

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

echo %q | ... should be safer, as that will surround the string with quotes and add some escaping of special characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this, I wasn't familiar with %q here!

DavidNix
DavidNix previously approved these changes Jun 27, 2022
ibc/types.go Outdated
Comment on lines 66 to 73
// GetShell returns the shell that should be used when executing scripts.
func (c ChainConfig) GetShell() string {
if c.Shell == "" {
return "sh"
}
return c.Shell
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. The benefit is keeping ChainConfig simple.

Comment on lines +143 to +145
func (c *PenumbraChain) RecoverKey(ctx context.Context, name, mnemonic string) error {
return fmt.Errorf("RecoverKey not implemented for PenumbraChain")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, Penumbra is highly experimental right now.

Copy link
Contributor

@DavidNix DavidNix left a comment

Choose a reason for hiding this comment

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

Apologies, I did not mean to approve previously, just comment.

@DavidNix DavidNix self-requested a review June 27, 2022 18:14
@DavidNix DavidNix dismissed their stale review June 27, 2022 18:15

Hit approve by mistake.

@chatton
Copy link
Contributor Author

chatton commented Jun 28, 2022

@mark-rushakoff ready for another look, I've addressed your feedback, thanks for taking a look so quickly!

Tests seem to be failing but it looks like an environmental issue on the test host?

@agouin
Copy link
Member

agouin commented Jun 28, 2022

Hi @chatton, looking at the test failure logs:

tn.NodeJob undefined (type *ChainNode has no field or method NodeJob)

Earlier today, NodeJob was replaced by Exec in #173, so those calls will need to be updated in chain_node.go.

@chatton
Copy link
Contributor Author

chatton commented Jun 28, 2022

@agouin thanks for letting me know! I had missed that PR being merged.

Copy link
Member

@mark-rushakoff mark-rushakoff left a comment

Choose a reason for hiding this comment

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

Thanks @chatton!

@mark-rushakoff mark-rushakoff merged commit f3054ff into strangelove-ventures:main Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable a RestoreKey function for the chains
4 participants