Skip to content

Commit

Permalink
Move solana_sdk::pubkey functions to solana-pubkey (#2980)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kevinheavey authored Oct 15, 2024
1 parent cc1873f commit d08ef44
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 32 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions keygen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bs58 = { workspace = true }
clap = { version = "3.1.5", features = ["cargo"] }
dirs-next = { workspace = true }
num_cpus = { workspace = true }
serde_json = { workspace = true }
solana-clap-v3-utils = { workspace = true }
solana-cli-config = { workspace = true }
solana-derivation-path = { workspace = true }
Expand Down
42 changes: 38 additions & 4 deletions keygen/src/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use {
solana_sdk::{
instruction::{AccountMeta, Instruction},
message::Message,
pubkey::{write_pubkey_file, Pubkey},
pubkey::Pubkey,
signature::{
keypair_from_seed, keypair_from_seed_and_derivation_path, write_keypair,
write_keypair_file, Keypair, Signer,
Expand Down Expand Up @@ -424,6 +424,21 @@ fn app<'a>(num_threads: &'a str, crate_version: &'a str) -> Command<'a> {
)
}

fn write_pubkey_file(outfile: &str, pubkey: Pubkey) -> Result<(), Box<dyn std::error::Error>> {
use std::io::Write;

let printable = format!("{pubkey}");
let serialized = serde_json::to_string(&printable)?;

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(())
}

fn main() -> Result<(), Box<dyn error::Error>> {
let default_num_threads = num_cpus::get().to_string();
let matches = app(&default_num_threads, solana_version::version!())
Expand Down Expand Up @@ -768,6 +783,14 @@ mod tests {
tempfile::{tempdir, TempDir},
};

fn read_pubkey_file(infile: &str) -> Result<Pubkey, Box<dyn std::error::Error>> {
let f = std::fs::File::open(infile)?;
let printable: String = serde_json::from_reader(f)?;

use std::str::FromStr;
Ok(Pubkey::from_str(&printable)?)
}

fn process_test_command(args: &[&str]) -> Result<(), Box<dyn error::Error>> {
let default_num_threads = num_cpus::get().to_string();
let solana_version = solana_version::version!();
Expand Down Expand Up @@ -919,7 +942,7 @@ mod tests {
])
.unwrap();

let result_pubkey = solana_sdk::pubkey::read_pubkey_file(&outfile_path).unwrap();
let result_pubkey = read_pubkey_file(&outfile_path).unwrap();
assert_eq!(result_pubkey, expected_pubkey);
}

Expand All @@ -938,7 +961,7 @@ mod tests {
])
.unwrap();

let result_pubkey = solana_sdk::pubkey::read_pubkey_file(&outfile_path).unwrap();
let result_pubkey = read_pubkey_file(&outfile_path).unwrap();
assert_eq!(result_pubkey, expected_pubkey);
}

Expand All @@ -962,7 +985,7 @@ mod tests {
])
.unwrap();

let result_pubkey = solana_sdk::pubkey::read_pubkey_file(&outfile_path).unwrap();
let result_pubkey = read_pubkey_file(&outfile_path).unwrap();
assert_eq!(result_pubkey, expected_pubkey);
}

Expand Down Expand Up @@ -1129,4 +1152,15 @@ mod tests {
])
.unwrap();
}

#[test]
fn test_read_write_pubkey() -> Result<(), std::boxed::Box<dyn std::error::Error>> {
let filename = "test_pubkey.json";
let pubkey = solana_sdk::pubkey::new_rand();
write_pubkey_file(filename, pubkey)?;
let read = read_pubkey_file(filename)?;
assert_eq!(read, pubkey);
std::fs::remove_file(filename)?;
Ok(())
}
}
1 change: 1 addition & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ full = [
"libsecp256k1",
"sha3",
"digest",
"solana-pubkey/rand",
]
borsh = ["dep:borsh", "solana-program/borsh", "solana-secp256k1-recover/borsh"]
dev-context-only-utils = ["qualifier_attr", "solana-account/dev-context-only-utils"]
Expand Down Expand Up @@ -97,7 +98,7 @@ solana-frozen-abi-macro = { workspace = true, optional = true, features = [
] }
solana-program = { workspace = true }
solana-program-memory = { workspace = true }
solana-pubkey = { workspace = true }
solana-pubkey = { workspace = true, default-features = false, features = ["std"] }
solana-sanitize = { workspace = true }
solana-sdk-macro = { workspace = true }
solana-secp256k1-recover = { workspace = true }
Expand Down
5 changes: 3 additions & 2 deletions sdk/pubkey/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bytemuck = { workspace = true, optional = true }
bytemuck_derive = { workspace = true, optional = true }
five8_const = { workspace = true }
num-traits = { workspace = true }
rand = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-atomic-u64 = { workspace = true }
Expand Down Expand Up @@ -47,7 +48,6 @@ wasm-bindgen = { workspace = true }
anyhow = { workspace = true }
arbitrary = { workspace = true, features = ["derive"] }
bs58 = { workspace = true, features = ["alloc"] }
rand = { workspace = true }
# circular dev deps need to be path deps for `cargo publish` to be happy,
# and for now the doc tests need solana-program
solana-program = { path = "../program" }
Expand All @@ -65,11 +65,12 @@ borsh = ["dep:borsh", "dep:borsh0-10", "std"]
bytemuck = ["dep:bytemuck", "dep:bytemuck_derive"]
curve25519 = ["dep:curve25519-dalek", "sha2"]
default = ["std"]
dev-context-only-utils = ["dep:arbitrary", "std"]
dev-context-only-utils = ["dep:arbitrary", "rand"]
frozen-abi = [
"dep:solana-frozen-abi",
"dep:solana-frozen-abi-macro"
]
rand = ["dep:rand", "std"]
serde = ["dep:serde", "dep:serde_derive"]
sha2 = ["dep:solana-sha256-hasher", "solana-sha256-hasher/sha2"]
std = []
Expand Down
6 changes: 6 additions & 0 deletions sdk/pubkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,12 @@ macro_rules! pubkey {
};
}

/// 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]>())
}

#[cfg(test)]
mod tests {
use {super::*, strum::IntoEnumIterator};
Expand Down
35 changes: 10 additions & 25 deletions sdk/src/pubkey.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Solana account addresses.
pub use solana_program::pubkey::*;

/// New random Pubkey for tests and benchmarks.
#[cfg(feature = "full")]
pub fn new_rand() -> Pubkey {
Pubkey::from(rand::random::<[u8; PUBKEY_BYTES]>())
}

pub use solana_pubkey::new_rand;
#[cfg(target_os = "solana")]
pub use solana_pubkey::syscalls;
pub use solana_pubkey::{
bytes_are_curve_point, ParsePubkeyError, Pubkey, PubkeyError, MAX_SEEDS, MAX_SEED_LEN,
PUBKEY_BYTES,
};

#[deprecated(since = "2.1.0")]
#[cfg(feature = "full")]
pub fn write_pubkey_file(outfile: &str, pubkey: Pubkey) -> Result<(), Box<dyn std::error::Error>> {
use std::io::Write;
Expand All @@ -24,6 +24,7 @@ pub fn write_pubkey_file(outfile: &str, pubkey: Pubkey) -> Result<(), Box<dyn st
Ok(())
}

#[deprecated(since = "2.1.0")]
#[cfg(feature = "full")]
pub fn read_pubkey_file(infile: &str) -> Result<Pubkey, Box<dyn std::error::Error>> {
let f = std::fs::File::open(infile)?;
Expand All @@ -32,19 +33,3 @@ pub fn read_pubkey_file(infile: &str) -> Result<Pubkey, Box<dyn std::error::Erro
use std::str::FromStr;
Ok(Pubkey::from_str(&printable)?)
}

#[cfg(test)]
mod tests {
use {super::*, std::fs::remove_file};

#[test]
fn test_read_write_pubkey() -> Result<(), Box<dyn std::error::Error>> {
let filename = "test_pubkey.json";
let pubkey = solana_sdk::pubkey::new_rand();
write_pubkey_file(filename, pubkey)?;
let read = read_pubkey_file(filename)?;
assert_eq!(read, pubkey);
remove_file(filename)?;
Ok(())
}
}

0 comments on commit d08ef44

Please sign in to comment.