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

Move solana_sdk::pubkey functions to solana-pubkey #2980

Merged
merged 11 commits into from
Oct 15, 2024

Conversation

kevinheavey
Copy link

@kevinheavey kevinheavey commented Sep 25, 2024

Problem

solana_sdk::pubkey defines three helper functions. These would be useful to have outside solana-sdk. I can't think of a anywhere else to move them so I put them in solana-pubkey behind a not(target_os = "solana").

Summary of Changes

  • Move function definitions to solana-pubkey and re-export in solana_sdk::pubkey for backwards compatibility
  • Replace the serde_json usage in these functions with a simple inline parser

@kevinheavey kevinheavey force-pushed the move-sdk-pubkey branch 4 times, most recently from 2f95a37 to 39c2c85 Compare September 27, 2024 16:13
Copy link

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

Just a couple of comments of where to put some of these changes, let me know what you think!

Comment on lines 1123 to 1156
#[cfg(all(feature = "std", not(target_os = "solana")))]
pub fn write_pubkey_file(
outfile: &str,
pubkey: Pubkey,
) -> Result<(), std::boxed::Box<dyn std::error::Error>> {
use std::io::Write;
let serialized = std::format!("\"{pubkey}\"");

if let Some(outdir) = std::path::Path::new(&outfile).parent() {
std::fs::create_dir_all(outdir)?;
}
let mut f = std::fs::File::create(outfile)?;
f.write_all(&serialized.into_bytes())?;

Ok(())
}

#[cfg(all(feature = "std", not(target_os = "solana")))]
pub fn read_pubkey_file(infile: &str) -> Result<Pubkey, std::boxed::Box<dyn std::error::Error>> {
use std::io::Read;
let mut f = std::fs::File::open(infile)?;
let mut buffer = std::string::String::new();
f.read_to_string(&mut buffer)?;
let trimmed = buffer.trim();
if !trimmed.starts_with('"') || !trimmed.ends_with('"') {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Input must be a JSON string.",
)
.into());
}
let contents = &trimmed[1..trimmed.len() - 1];
Ok(Pubkey::from_str(contents)?)
}

Choose a reason for hiding this comment

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

Considering these are only used by solana-keygen, how about copying the implementations into there and deprecating them from sdk rather than moving them down into here?

Comment on lines +1117 to +1121
/// New random Pubkey for tests and benchmarks.
#[cfg(all(feature = "rand", not(target_os = "solana")))]
pub fn new_rand() -> Pubkey {
Pubkey::from(rand::random::<[u8; PUBKEY_BYTES]>())
}

Choose a reason for hiding this comment

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

I'm a bit torn about this one, since we should really favor using Pubkey::new_unique() in almost all cases. On the flipside, there are some legit usecases, and this is the best place to put it, so let's go with it.

Copy link

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

@joncinque joncinque added the automerge automerge Merge this Pull Request automatically once CI passes label Oct 15, 2024
@mergify mergify bot merged commit d08ef44 into anza-xyz:master Oct 15, 2024
53 checks passed
ray-kast pushed a commit to abklabs/agave that referenced this pull request Nov 27, 2024
* move functions from solana_sdk::pubkey to solana_pubkey

* fix test

* remove serde_json from write_pubkey_file

* remove serde_json

* fix import

* put new_rand behind a separate feature

* lint

* fix imports

* fmt

* deprecate sdk::pubkey::{read_pubkey_file, write_pubkey_file} and duplicate in solana-keygen

* lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge automerge Merge this Pull Request automatically once CI passes need:merge-assist
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants