Skip to content

Commit

Permalink
deprecates Pubkey::new in favor of Pubkey::{,try_}from (solana-labs#2…
Browse files Browse the repository at this point in the history
…9805)

The commit deprecates Pubkey::new which lacks type-safety and instead
implements TryFrom<&[u8]> and TryFrom<Vec<u8>> for Pubkey.
  • Loading branch information
behzadnouri authored Jan 21, 2023
1 parent f4339bc commit 272e667
Show file tree
Hide file tree
Showing 47 changed files with 258 additions and 237 deletions.
10 changes: 4 additions & 6 deletions account-decoder/src/parse_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,9 @@ pub struct UiMultisig {
}

pub fn get_token_account_mint(data: &[u8]) -> Option<Pubkey> {
if Account::valid_account_data(data) {
Some(Pubkey::new(&data[0..32]))
} else {
None
}
Account::valid_account_data(data)
.then(|| Pubkey::try_from(data.get(..32)?).ok())
.flatten()
}

#[cfg(test)]
Expand Down Expand Up @@ -402,7 +400,7 @@ mod test {
account.state = AccountState::Initialized;
Account::pack(account, &mut account_data).unwrap();

let expected_mint_pubkey = Pubkey::new(&[2; 32]);
let expected_mint_pubkey = Pubkey::from([2; 32]);
assert_eq!(
get_token_account_mint(&account_data),
Some(expected_mint_pubkey)
Expand Down
8 changes: 4 additions & 4 deletions cli-output/src/cli_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2926,10 +2926,10 @@ mod tests {
}

let present: Box<dyn Signer> = Box::new(keypair_from_seed(&[2u8; 32]).unwrap());
let absent: Box<dyn Signer> = Box::new(NullSigner::new(&Pubkey::new(&[3u8; 32])));
let bad: Box<dyn Signer> = Box::new(BadSigner::new(Pubkey::new(&[4u8; 32])));
let to = Pubkey::new(&[5u8; 32]);
let nonce = Pubkey::new(&[6u8; 32]);
let absent: Box<dyn Signer> = Box::new(NullSigner::new(&Pubkey::from([3u8; 32])));
let bad: Box<dyn Signer> = Box::new(BadSigner::new(Pubkey::from([4u8; 32])));
let to = Pubkey::from([5u8; 32]);
let nonce = Pubkey::from([6u8; 32]);
let from = present.pubkey();
let fee_payer = absent.pubkey();
let nonce_auth = bad.pubkey();
Expand Down
8 changes: 4 additions & 4 deletions cli/src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ mod tests {
});
let pubkey = solana_sdk::pubkey::new_rand();

let pubkey0 = Pubkey::new(&[0; 32]);
let pubkey1 = Pubkey::new(&[1; 32]);
let pubkey0 = Pubkey::from([0; 32]);
let pubkey1 = Pubkey::from([1; 32]);
let ix0 = system_instruction::transfer(&pubkey0, &pubkey1, 1);
let message0 = Message::new(&[ix0], Some(&pubkey0));

Expand Down Expand Up @@ -290,8 +290,8 @@ mod tests {
assert_eq!(get_fee_for_messages(&rpc_client, &[]).unwrap(), 0);

// One message w/ one signature, a fee.
let pubkey0 = Pubkey::new(&[0; 32]);
let pubkey1 = Pubkey::new(&[1; 32]);
let pubkey0 = Pubkey::from([0; 32]);
let pubkey1 = Pubkey::from([1; 32]);
let ix0 = system_instruction::transfer(&pubkey0, &pubkey1, 1);
let message0 = Message::new(&[ix0], Some(&pubkey0));
assert_eq!(get_fee_for_messages(&rpc_client, &[&message0]).unwrap(), 1);
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2563,7 +2563,7 @@ mod tests {
);

//Test Transfer Subcommand, with nonce
let nonce_address = Pubkey::new(&[1u8; 32]);
let nonce_address = Pubkey::from([1u8; 32]);
let nonce_address_string = nonce_address.to_string();
let nonce_authority = keypair_from_seed(&[2u8; 32]).unwrap();
let nonce_authority_file = make_tmp_path("nonce_authority_file");
Expand Down
2 changes: 1 addition & 1 deletion cli/src/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl WithMemo for Vec<Instruction> {
if let Some(memo) = &memo {
let memo = memo.as_ref();
let memo_ix = Instruction {
program_id: Pubkey::new(&id().to_bytes()),
program_id: Pubkey::from(id().to_bytes()),
accounts: vec![],
data: memo.as_bytes().to_vec(),
};
Expand Down
8 changes: 4 additions & 4 deletions cli/src/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ mod tests {
let valid = Account::new_data(1, &data, &system_program::ID);
assert!(check_nonce_account(&valid.unwrap(), &nonce_pubkey, &blockhash).is_ok());

let invalid_owner = Account::new_data(1, &data, &Pubkey::new(&[1u8; 32]));
let invalid_owner = Account::new_data(1, &data, &Pubkey::from([1u8; 32]));
if let CliError::InvalidNonce(err) =
check_nonce_account(&invalid_owner.unwrap(), &nonce_pubkey, &blockhash).unwrap_err()
{
Expand Down Expand Up @@ -1151,7 +1151,7 @@ mod tests {
Err(Error::UnexpectedDataSize),
);

let other_program = Pubkey::new(&[1u8; 32]);
let other_program = Pubkey::from([1u8; 32]);
let other_account_no_data = Account::new(1, 0, &other_program);
assert_eq!(
account_identity_ok(&other_account_no_data),
Expand All @@ -1165,7 +1165,7 @@ mod tests {
assert_eq!(state_from_account(&nonce_account), Ok(State::Uninitialized));

let durable_nonce = DurableNonce::from_blockhash(&Hash::new(&[42u8; 32]));
let data = nonce::state::Data::new(Pubkey::new(&[1u8; 32]), durable_nonce, 42);
let data = nonce::state::Data::new(Pubkey::from([1u8; 32]), durable_nonce, 42);
nonce_account
.set_state(&Versions::new(State::Initialized(data.clone())))
.unwrap();
Expand Down Expand Up @@ -1195,7 +1195,7 @@ mod tests {
);

let durable_nonce = DurableNonce::from_blockhash(&Hash::new(&[42u8; 32]));
let data = nonce::state::Data::new(Pubkey::new(&[1u8; 32]), durable_nonce, 42);
let data = nonce::state::Data::new(Pubkey::from([1u8; 32]), durable_nonce, 42);
nonce_account
.set_state(&Versions::new(State::Initialized(data.clone())))
.unwrap();
Expand Down
10 changes: 5 additions & 5 deletions cli/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2686,9 +2686,9 @@ mod tests {

// stake-authorize subcommand
let stake_account_string = stake_account_pubkey.to_string();
let new_stake_authority = Pubkey::new(&[1u8; 32]);
let new_stake_authority = Pubkey::from([1u8; 32]);
let new_stake_string = new_stake_authority.to_string();
let new_withdraw_authority = Pubkey::new(&[2u8; 32]);
let new_withdraw_authority = Pubkey::from([2u8; 32]);
let new_withdraw_string = new_withdraw_authority.to_string();
let test_stake_authorize = test_commands.clone().get_matches_from(vec![
"test",
Expand Down Expand Up @@ -3537,7 +3537,7 @@ mod tests {
let pubkey2 = keypair2.pubkey();
let sig2 = keypair.sign_message(&[0u8]);
let signer2 = format!("{}={}", keypair2.pubkey(), sig2);
let nonce_account = Pubkey::new(&[1u8; 32]);
let nonce_account = Pubkey::from([1u8; 32]);
let test_authorize = test_commands.clone().get_matches_from(vec![
"test",
"stake-authorize",
Expand Down Expand Up @@ -3914,7 +3914,7 @@ mod tests {
assert!(parse_command(&test_create_stake_account, &default_signer, &mut None).is_err());

// CreateStakeAccount offline and nonce
let nonce_account = Pubkey::new(&[1u8; 32]);
let nonce_account = Pubkey::from([1u8; 32]);
let nonce_account_string = nonce_account.to_string();
let offline = keypair_from_seed(&[2u8; 32]).unwrap();
let offline_pubkey = offline.pubkey();
Expand Down Expand Up @@ -4829,7 +4829,7 @@ mod tests {
);

// Split stake offline nonced submission
let nonce_account = Pubkey::new(&[1u8; 32]);
let nonce_account = Pubkey::from([1u8; 32]);
let nonce_account_string = nonce_account.to_string();
let nonce_auth = keypair_from_seed(&[2u8; 32]).unwrap();
let nonce_auth_pubkey = nonce_auth.pubkey();
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn test_create_account_with_seed() {

let offline_nonce_authority_signer = keypair_from_seed(&[1u8; 32]).unwrap();
let online_nonce_creator_signer = keypair_from_seed(&[2u8; 32]).unwrap();
let to_address = Pubkey::new(&[3u8; 32]);
let to_address = Pubkey::from([3u8; 32]);

// Setup accounts
let rpc_client =
Expand Down
10 changes: 5 additions & 5 deletions cli/tests/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn test_transfer() {
config.signers = vec![&default_signer];

let sender_pubkey = config.signers[0].pubkey();
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
let recipient_pubkey = Pubkey::from([1u8; 32]);

request_and_confirm_airdrop(&rpc_client, &config, &sender_pubkey, sol_to_lamports(5.0))
.unwrap();
Expand Down Expand Up @@ -336,7 +336,7 @@ fn test_transfer_multisession_signing() {
SocketAddrSpace::Unspecified,
);

let to_pubkey = Pubkey::new(&[1u8; 32]);
let to_pubkey = Pubkey::from([1u8; 32]);
let offline_from_signer = keypair_from_seed(&[2u8; 32]).unwrap();
let offline_fee_payer_signer = keypair_from_seed(&[3u8; 32]).unwrap();
let from_null_signer = NullSigner::new(&offline_from_signer.pubkey());
Expand Down Expand Up @@ -498,7 +498,7 @@ fn test_transfer_all() {
config.signers = vec![&default_signer];

let sender_pubkey = config.signers[0].pubkey();
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
let recipient_pubkey = Pubkey::from([1u8; 32]);

request_and_confirm_airdrop(&rpc_client, &config, &sender_pubkey, 500_000).unwrap();
check_balance!(500_000, &rpc_client, &sender_pubkey);
Expand Down Expand Up @@ -552,7 +552,7 @@ fn test_transfer_unfunded_recipient() {
config.signers = vec![&default_signer];

let sender_pubkey = config.signers[0].pubkey();
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
let recipient_pubkey = Pubkey::from([1u8; 32]);

request_and_confirm_airdrop(&rpc_client, &config, &sender_pubkey, 50_000).unwrap();
check_balance!(50_000, &rpc_client, &sender_pubkey);
Expand Down Expand Up @@ -607,7 +607,7 @@ fn test_transfer_with_seed() {
config.signers = vec![&default_signer];

let sender_pubkey = config.signers[0].pubkey();
let recipient_pubkey = Pubkey::new(&[1u8; 32]);
let recipient_pubkey = Pubkey::from([1u8; 32]);
let derived_address_seed = "seed".to_string();
let derived_address_program_id = stake::program::id();
let derived_address = Pubkey::create_with_seed(
Expand Down
4 changes: 2 additions & 2 deletions faucet/src/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl Faucet {
)
);
let memo_instruction = Instruction {
program_id: Pubkey::new(&spl_memo::id().to_bytes()),
program_id: Pubkey::from(spl_memo::id().to_bytes()),
accounts: vec![],
data: memo.as_bytes().to_vec(),
};
Expand Down Expand Up @@ -635,7 +635,7 @@ mod tests {
assert_eq!(tx.signatures.len(), 1);
assert_eq!(
message.account_keys,
vec![mint_pubkey, Pubkey::new(&spl_memo::id().to_bytes())]
vec![mint_pubkey, Pubkey::from(spl_memo::id().to_bytes())]
);
assert_eq!(message.recent_blockhash, blockhash);

Expand Down
2 changes: 1 addition & 1 deletion genesis/src/stakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ mod tests {
// print(
// "\n\"{}\", // {:?}",
// hex,
// Pubkey::new(&hex::decode(hex).unwrap())
// Pubkey::try_from(&hex::decode(hex).unwrap()).unwrap()
// );
// });
// println();
Expand Down
10 changes: 5 additions & 5 deletions gossip/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4188,7 +4188,7 @@ RPC Enabled Nodes: 1"#;

#[test]
fn test_tvu_peers_and_stakes() {
let d = ContactInfo::new_localhost(&Pubkey::new(&[0; 32]), timestamp());
let d = ContactInfo::new_localhost(&Pubkey::from([0; 32]), timestamp());
let cluster_info = ClusterInfo::new(
d.clone(),
Arc::new(Keypair::new()),
Expand All @@ -4197,12 +4197,12 @@ RPC Enabled Nodes: 1"#;
let mut stakes = HashMap::new();

// no stake
let id = Pubkey::new(&[1u8; 32]);
let id = Pubkey::from([1u8; 32]);
let contact_info = ContactInfo::new_localhost(&id, timestamp());
cluster_info.insert_info(contact_info);

// normal
let id2 = Pubkey::new(&[2u8; 32]);
let id2 = Pubkey::from([2u8; 32]);
let mut contact_info = ContactInfo::new_localhost(&id2, timestamp());
cluster_info.insert_info(contact_info.clone());
stakes.insert(id2, 10);
Expand All @@ -4212,14 +4212,14 @@ RPC Enabled Nodes: 1"#;
cluster_info.insert_info(contact_info);

// no tvu
let id3 = Pubkey::new(&[3u8; 32]);
let id3 = Pubkey::from([3u8; 32]);
let mut contact_info = ContactInfo::new_localhost(&id3, timestamp());
contact_info.tvu = "0.0.0.0:0".parse().unwrap();
cluster_info.insert_info(contact_info);
stakes.insert(id3, 10);

// normal but with different shred version
let id4 = Pubkey::new(&[4u8; 32]);
let id4 = Pubkey::from([4u8; 32]);
let mut contact_info = ContactInfo::new_localhost(&id4, timestamp());
contact_info.shred_version = 1;
assert_ne!(contact_info.shred_version, d.shred_version);
Expand Down
6 changes: 3 additions & 3 deletions gossip/src/crds_gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ mod test {
let crds_gossip = CrdsGossip::default();
let keypair = Keypair::new();
let id = keypair.pubkey();
let ci = ContactInfo::new_localhost(&Pubkey::new(&[1; 32]), 0);
let prune_pubkey = Pubkey::new(&[2; 32]);
let ci = ContactInfo::new_localhost(&Pubkey::from([1; 32]), 0);
let prune_pubkey = Pubkey::from([2; 32]);
crds_gossip
.crds
.write()
Expand Down Expand Up @@ -467,7 +467,7 @@ mod test {
let mut res = crds_gossip.process_prune_msg(
&id,
&ci.id,
&Pubkey::new(hash(&[1; 32]).as_ref()),
&Pubkey::from(hash(&[1; 32]).to_bytes()),
&[prune_pubkey],
now,
now,
Expand Down
12 changes: 6 additions & 6 deletions gossip/tests/crds_gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,8 @@ fn test_prune_errors() {
let crds_gossip = CrdsGossip::default();
let keypair = Keypair::new();
let id = keypair.pubkey();
let ci = ContactInfo::new_localhost(&Pubkey::new(&[1; 32]), 0);
let prune_pubkey = Pubkey::new(&[2; 32]);
let ci = ContactInfo::new_localhost(&Pubkey::from([1; 32]), 0);
let prune_pubkey = Pubkey::from([2; 32]);
crds_gossip
.crds
.write()
Expand All @@ -758,10 +758,10 @@ fn test_prune_errors() {
let stakes = HashMap::<Pubkey, u64>::default();
//incorrect dest
let mut res = crds_gossip.process_prune_msg(
&id, // self_pubkey
&ci.id, // peer
&Pubkey::new(hash(&[1; 32]).as_ref()), // destination
&[prune_pubkey], // origins
&id, // self_pubkey
&ci.id, // peer
&Pubkey::from(hash(&[1; 32]).to_bytes()), // destination
&[prune_pubkey], // origins
now,
now,
&stakes,
Expand Down
8 changes: 4 additions & 4 deletions ledger/src/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6979,8 +6979,8 @@ pub mod tests {
.write_transaction_status(
slot0,
Signature::new(&random_bytes),
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
Expand Down Expand Up @@ -7045,8 +7045,8 @@ pub mod tests {
.write_transaction_status(
slot1,
Signature::new(&random_bytes),
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
Expand Down
20 changes: 10 additions & 10 deletions ledger/src/blockstore/blockstore_purge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ pub mod tests {
.write_transaction_status(
x,
Signature::new(&random_bytes),
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
Expand All @@ -491,8 +491,8 @@ pub mod tests {
.write_transaction_status(
x,
Signature::new(&random_bytes),
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
Expand Down Expand Up @@ -526,8 +526,8 @@ pub mod tests {
.write_transaction_status(
slot,
Signature::new(&random_bytes),
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
Expand Down Expand Up @@ -726,8 +726,8 @@ pub mod tests {
.write_transaction_status(
x,
signature,
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
Expand Down Expand Up @@ -769,8 +769,8 @@ pub mod tests {
.write_transaction_status(
x,
signature,
vec![&Pubkey::new(&random_bytes[0..32])],
vec![&Pubkey::new(&random_bytes[32..])],
vec![&Pubkey::try_from(&random_bytes[..32]).unwrap()],
vec![&Pubkey::try_from(&random_bytes[32..]).unwrap()],
TransactionStatusMeta::default(),
)
.unwrap();
Expand Down
Loading

0 comments on commit 272e667

Please sign in to comment.