* Add pubkey_new_rand(), mark Pubkey::new_rand() deprecated (cherry picked from commit0e68ed6a8d) * Add hash_new_rand(), mark Hash::new_rand() as deprecated (cherry picked from commit76f11c7dae) * Run `codemod --extensions rs Pubkey::new_rand solana_sdk::pubkey::new_rand` (cherry picked from commit7bc073defe) # Conflicts: # programs/bpf/benches/bpf_loader.rs # runtime/benches/accounts.rs # runtime/src/accounts.rs * Run `codemod --extensions rs Hash::new_rand solana_sdk:#️⃣:new_rand` (cherry picked from commit17c391121a) * Remove unused pubkey::Pubkey imports (cherry picked from commit959880db60) # Conflicts: # runtime/src/accounts_index.rs * Resolve conflicts Co-authored-by: Michael Vines <mvines@gmail.com>
This commit is contained in:
@@ -67,6 +67,12 @@ impl FromStr for Pubkey {
|
||||
}
|
||||
}
|
||||
|
||||
/// New random Pubkey for tests and benchmarks.
|
||||
#[cfg(feature = "everything")]
|
||||
pub fn new_rand() -> Pubkey {
|
||||
Pubkey::new(&rand::random::<[u8; 32]>())
|
||||
}
|
||||
|
||||
impl Pubkey {
|
||||
pub fn new(pubkey_vec: &[u8]) -> Self {
|
||||
Self(
|
||||
@@ -188,9 +194,11 @@ impl Pubkey {
|
||||
panic!("Unable to find a viable program address bump seed");
|
||||
}
|
||||
|
||||
#[cfg(feature = "everything")]
|
||||
#[cfg(all(feature = "everything", not(target_arch = "bpf")))]
|
||||
#[deprecated(since = "1.3.9", note = "Please use 'pubkey::new_rand' instead")]
|
||||
pub fn new_rand() -> Self {
|
||||
Self::new(&rand::random::<[u8; 32]>())
|
||||
// Consider removing Pubkey::new_rand() entirely in the v1.5 or v1.6 timeframe
|
||||
new_rand()
|
||||
}
|
||||
|
||||
pub fn to_bytes(self) -> [u8; 32] {
|
||||
@@ -260,7 +268,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn pubkey_fromstr() {
|
||||
let pubkey = Pubkey::new_rand();
|
||||
let pubkey = solana_sdk::pubkey::new_rand();
|
||||
let mut pubkey_base58_str = bs58::encode(pubkey.0).into_string();
|
||||
|
||||
assert_eq!(pubkey_base58_str.parse::<Pubkey>(), Ok(pubkey));
|
||||
@@ -293,43 +301,53 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_create_with_seed() {
|
||||
assert!(Pubkey::create_with_seed(&Pubkey::new_rand(), "☉", &Pubkey::new_rand()).is_ok());
|
||||
assert!(Pubkey::create_with_seed(
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
"☉",
|
||||
&solana_sdk::pubkey::new_rand()
|
||||
)
|
||||
.is_ok());
|
||||
assert_eq!(
|
||||
Pubkey::create_with_seed(
|
||||
&Pubkey::new_rand(),
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
from_utf8(&[127; MAX_SEED_LEN + 1]).unwrap(),
|
||||
&Pubkey::new_rand()
|
||||
&solana_sdk::pubkey::new_rand()
|
||||
),
|
||||
Err(PubkeyError::MaxSeedLengthExceeded)
|
||||
);
|
||||
assert!(Pubkey::create_with_seed(
|
||||
&Pubkey::new_rand(),
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
"\
|
||||
\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\
|
||||
",
|
||||
&Pubkey::new_rand()
|
||||
&solana_sdk::pubkey::new_rand()
|
||||
)
|
||||
.is_ok());
|
||||
// utf-8 abuse ;)
|
||||
assert_eq!(
|
||||
Pubkey::create_with_seed(
|
||||
&Pubkey::new_rand(),
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
"\
|
||||
x\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\u{10FFFF}\
|
||||
",
|
||||
&Pubkey::new_rand()
|
||||
&solana_sdk::pubkey::new_rand()
|
||||
),
|
||||
Err(PubkeyError::MaxSeedLengthExceeded)
|
||||
);
|
||||
|
||||
assert!(Pubkey::create_with_seed(
|
||||
&Pubkey::new_rand(),
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
std::str::from_utf8(&[0; MAX_SEED_LEN]).unwrap(),
|
||||
&Pubkey::new_rand(),
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
assert!(Pubkey::create_with_seed(&Pubkey::new_rand(), "", &Pubkey::new_rand(),).is_ok());
|
||||
assert!(Pubkey::create_with_seed(
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
"",
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
assert_eq!(
|
||||
Pubkey::create_with_seed(
|
||||
@@ -395,7 +413,7 @@ mod tests {
|
||||
// addresses must land off the curve and be unique
|
||||
let mut addresses = vec![];
|
||||
for _ in 0..1_000 {
|
||||
let program_id = Pubkey::new_rand();
|
||||
let program_id = solana_sdk::pubkey::new_rand();
|
||||
let bytes1 = rand::random::<[u8; 10]>();
|
||||
let bytes2 = rand::random::<[u8; 32]>();
|
||||
if let Ok(program_address) =
|
||||
@@ -416,7 +434,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_find_program_address() {
|
||||
for _ in 0..1_000 {
|
||||
let program_id = Pubkey::new_rand();
|
||||
let program_id = solana_sdk::pubkey::new_rand();
|
||||
let (address, bump_seed) =
|
||||
Pubkey::find_program_address(&[b"Lil'", b"Bits"], &program_id);
|
||||
assert_eq!(
|
||||
@@ -430,7 +448,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_read_write_pubkey() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let filename = "test_pubkey.json";
|
||||
let pubkey = Pubkey::new_rand();
|
||||
let pubkey = solana_sdk::pubkey::new_rand();
|
||||
write_pubkey_file(filename, pubkey)?;
|
||||
let read = read_pubkey_file(filename)?;
|
||||
assert_eq!(read, pubkey);
|
||||
|
||||
Reference in New Issue
Block a user