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

[clap-v3-utils] Remove deprecated functions #34989

Closed
wants to merge 15 commits into from
Closed

[clap-v3-utils] Remove deprecated functions #34989

wants to merge 15 commits into from

Conversation

samkim-crypto
Copy link
Contributor

@samkim-crypto samkim-crypto commented Jan 28, 2024

Problem

Solana clap-v3-utils still uses deprecated functions from clap-v2.

Summary of Changes

  • Replaced a number of deprecated functions like possible_values, value_of, and multiple_occurrences with the updated functions from clap-v3.
  • Deprecated some existing parser functions that can be replaced by existing clap-v3 functions
  • Deprecated some panicking functions like keypair_of, keypairs_of, etc. in favor of their try_ variants

Clap-v3 has a feature deprecated that, when included, gives warnings on deprecated functions. With this PR, all the deprecated functions should either be removed or explicitly allowed (on deprecated functions). However, if I include the deprecated feature, it seems to turn this feature on in the rest of the repo on CI checks, so I left it out for now.

Fixes #

@samkim-crypto samkim-crypto added the work in progress This isn't quite right yet label Jan 28, 2024
Copy link

codecov bot commented Jan 29, 2024

Codecov Report

Attention: Patch coverage is 57.95455% with 37 lines in your changes are missing coverage. Please review.

Project coverage is 81.8%. Comparing base (7399178) to head (838fd65).
Report is 11 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #34989    +/-   ##
========================================
  Coverage    81.7%    81.8%            
========================================
  Files         836      836            
  Lines      224834   225069   +235     
========================================
+ Hits       183847   184136   +289     
+ Misses      40987    40933    -54     

Copy link
Contributor

@CriesofCarrots CriesofCarrots left a comment

Choose a reason for hiding this comment

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

Generally looks fine to me. One nit, plus a request for deduplication.

Comment on lines 240 to 245
if value == ASK_KEYWORD {
let skip_validation = matches.try_contains_id(SKIP_SEED_PHRASE_VALIDATION_ARG.name)?;
keypair_from_seed_phrase(name, skip_validation, true, None, true).map(Some)
} else {
read_keypair_file(value).map(Some)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to de-deupe this with the logic in try_keypairs_of -- I know the duplication predates you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes good point. If try_keypairs_of returns a keypair vector of length greater than 1, it is actually unclear what the behavior of this function should be. Whether multiple keypairs are allowed should be caught by Args, so technically this choice will not matter if the function is used correctly... I pushed a change that simply pops the keypair out of the returned vector. Please take a look.

Copy link
Contributor

Choose a reason for hiding this comment

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

Missing a push? I don't see any changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, sorry I don't quite know where those commit went... But I pushed the changes!

Copy link
Contributor

@CriesofCarrots CriesofCarrots Feb 1, 2024

Choose a reason for hiding this comment

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

Ohhh, I was confused about your comment until reading the code just now. By deduping, I actually meant put the following code in a helper fn that gets called from both methods:

        if value == ASK_KEYWORD {
            let skip_validation = matches.try_contains_id(SKIP_SEED_PHRASE_VALIDATION_ARG.name)?;
            keypair_from_seed_phrase(name, skip_validation, true, None, true).map(Some)
        } else {
            read_keypair_file(value).map(Some)
        }

(And elsewhere, if we use it other places)

Probably what you have written is fine, but I don't know if there might be costs to calling try_get_many when we only expect one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh oops! Sorry for the misinterpretation. I added a helper function extract_keypair to include the keypair extraction logic.

I think technically, the helper function should be named keypair_from_path, but we already have a public function with that name. The try_keypair_of and try_keypairs_of should probably use the existing keypair_from_path (unless we strictly want to restrict the source to just ASK or file), but that will be changing the functionality so we probably don't want to change it now.

clap-v3-utils/src/input_parsers/signer.rs Outdated Show resolved Hide resolved
clap-v3-utils/src/input_parsers/mod.rs Outdated Show resolved Hide resolved
Comment on lines 287 to 292
Ok(matches.try_get_many::<String>(name)?.map(|values| {
values
.map(|value| {
value.parse::<Pubkey>().unwrap_or_else(|_| {
read_keypair_file(value)
.expect("read_keypair_file failed")
Copy link
Contributor

Choose a reason for hiding this comment

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

i think this can be refactored to avoid the expect()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Error handling was a bit tricky to do inside maps and closures, so I ended up using a for-loop to propagate the error. If you see a more elegant solution, let me know!

Ok(matches.try_get_many::<String>(name)?.map(|values| {
values
.map(|pubkey_signer_string| {
let mut signer = pubkey_signer_string.split('=');
Copy link
Contributor

Choose a reason for hiding this comment

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

prefer splitn() to avoid succeeding on invalid inputs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, valid point. I ended up using split_once, which also returns an option.

Comment on lines 326 to 327
let key = Pubkey::from_str(signer.next().unwrap()).unwrap();
let sig = Signature::from_str(signer.next().unwrap()).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

we should avoid unwrap()ing on user inputs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, I ended up using a for-loop here, so let me know if you see a more elegant solution.

@github-actions github-actions bot added the stale [bot only] Added to stale content; results in auto-close after a week. label Feb 19, 2024
@samkim-crypto samkim-crypto removed the stale [bot only] Added to stale content; results in auto-close after a week. label Feb 20, 2024
@samkim-crypto
Copy link
Contributor Author

@CriesofCarrots @t-nelson, it would be great to get this PR merged before the migration 🙏 . Can you take one last look at the PR to check if the changes look sound?

@willhickey
Copy link
Contributor

This repository is no longer in use. Please re-open this pull request in the agave repo: https://github.com/anza-xyz/agave

@willhickey willhickey closed this Mar 3, 2024
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.

4 participants