Skip to content

Commit 8199ee8

Browse files
authored
Use Address instead of Pubkey in more places (#346)
* Use Address instead of Pubkey in more places #### Problem The sdk repo still uses Pubkey mostly, which makes it tougher to make more of the SDK no-std. #### Summary of changes Use Address in more places, to avoid adding the std feature on solana-pubkey as much as possible. * Mark more crates as no-std * Update pubkey -> address
1 parent 5f52f96 commit 8199ee8

File tree

25 files changed

+330
-311
lines changed

25 files changed

+330
-311
lines changed

Cargo.lock

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

account-info/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ bincode = ["dep:bincode", "dep:serde"]
2020
[dependencies]
2121
bincode = { workspace = true, optional = true }
2222
serde = { workspace = true, optional = true }
23+
solana-address = { workspace = true, features = ["decode"] }
2324
solana-program-error = { workspace = true }
2425
solana-program-memory = { workspace = true }
25-
solana-pubkey = { workspace = true, default-features = false }
26+
27+
[dev-dependencies]
28+
solana-address = { workspace = true, features = ["atomic"] }

account-info/src/lib.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Account information.
22
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
33
use {
4+
solana_address::Address,
45
solana_program_error::ProgramError,
56
solana_program_memory::sol_memset,
6-
solana_pubkey::Pubkey,
77
std::{
88
cell::{Ref, RefCell, RefMut},
99
fmt,
@@ -20,14 +20,14 @@ pub const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10;
2020
#[derive(Clone)]
2121
#[repr(C)]
2222
pub struct AccountInfo<'a> {
23-
/// Public key of the account
24-
pub key: &'a Pubkey,
23+
/// Address of the account
24+
pub key: &'a Address,
2525
/// The lamports in the account. Modifiable by programs.
2626
pub lamports: Rc<RefCell<&'a mut u64>>,
2727
/// The data held in this account. Modifiable by programs.
2828
pub data: Rc<RefCell<&'a mut [u8]>>,
2929
/// Program that owns this account
30-
pub owner: &'a Pubkey,
30+
pub owner: &'a Address,
3131
/// Formerly, the epoch at which this account will next owe rent. A field
3232
/// must remain because the runtime depends on the exact layout of this
3333
/// struct.
@@ -62,15 +62,15 @@ impl fmt::Debug for AccountInfo<'_> {
6262
}
6363

6464
impl<'a> AccountInfo<'a> {
65-
pub fn signer_key(&self) -> Option<&Pubkey> {
65+
pub fn signer_key(&self) -> Option<&Address> {
6666
if self.is_signer {
6767
Some(self.key)
6868
} else {
6969
None
7070
}
7171
}
7272

73-
pub fn unsigned_key(&self) -> &Pubkey {
73+
pub fn unsigned_key(&self) -> &Address {
7474
self.key
7575
}
7676

@@ -182,23 +182,23 @@ impl<'a> AccountInfo<'a> {
182182
}
183183

184184
#[allow(invalid_reference_casting)]
185-
pub fn assign(&self, new_owner: &Pubkey) {
185+
pub fn assign(&self, new_owner: &Address) {
186186
// Set the non-mut owner field
187187
unsafe {
188188
std::ptr::write_volatile(
189-
self.owner as *const Pubkey as *mut [u8; 32],
189+
self.owner as *const Address as *mut [u8; 32],
190190
new_owner.to_bytes(),
191191
);
192192
}
193193
}
194194

195195
pub fn new(
196-
key: &'a Pubkey,
196+
key: &'a Address,
197197
is_signer: bool,
198198
is_writable: bool,
199199
lamports: &'a mut u64,
200200
data: &'a mut [u8],
201-
owner: &'a Pubkey,
201+
owner: &'a Address,
202202
executable: bool,
203203
) -> Self {
204204
#[allow(deprecated)]
@@ -241,30 +241,30 @@ impl<'a, T: IntoAccountInfo<'a>> From<T> for AccountInfo<'a> {
241241
/// Provides information required to construct an `AccountInfo`, used in
242242
/// conversion implementations.
243243
pub trait Account {
244-
fn get(&mut self) -> (&mut u64, &mut [u8], &Pubkey, bool);
244+
fn get(&mut self) -> (&mut u64, &mut [u8], &Address, bool);
245245
}
246246

247-
/// Convert (&'a Pubkey, &'a mut T) where T: Account into an `AccountInfo`
248-
impl<'a, T: Account> IntoAccountInfo<'a> for (&'a Pubkey, &'a mut T) {
247+
/// Convert (&'a Address, &'a mut T) where T: Account into an `AccountInfo`
248+
impl<'a, T: Account> IntoAccountInfo<'a> for (&'a Address, &'a mut T) {
249249
fn into_account_info(self) -> AccountInfo<'a> {
250250
let (key, account) = self;
251251
let (lamports, data, owner, executable) = account.get();
252252
AccountInfo::new(key, false, false, lamports, data, owner, executable)
253253
}
254254
}
255255

256-
/// Convert (&'a Pubkey, bool, &'a mut T) where T: Account into an
256+
/// Convert (&'a Address, bool, &'a mut T) where T: Account into an
257257
/// `AccountInfo`.
258-
impl<'a, T: Account> IntoAccountInfo<'a> for (&'a Pubkey, bool, &'a mut T) {
258+
impl<'a, T: Account> IntoAccountInfo<'a> for (&'a Address, bool, &'a mut T) {
259259
fn into_account_info(self) -> AccountInfo<'a> {
260260
let (key, is_signer, account) = self;
261261
let (lamports, data, owner, executable) = account.get();
262262
AccountInfo::new(key, is_signer, false, lamports, data, owner, executable)
263263
}
264264
}
265265

266-
/// Convert &'a mut (Pubkey, T) where T: Account into an `AccountInfo`.
267-
impl<'a, T: Account> IntoAccountInfo<'a> for &'a mut (Pubkey, T) {
266+
/// Convert &'a mut (Address, T) where T: Account into an `AccountInfo`.
267+
impl<'a, T: Account> IntoAccountInfo<'a> for &'a mut (Address, T) {
268268
fn into_account_info(self) -> AccountInfo<'a> {
269269
let (ref key, account) = self;
270270
let (lamports, data, owner, executable) = account.get();
@@ -288,11 +288,11 @@ impl<'a, T: Account> IntoAccountInfo<'a> for &'a mut (Pubkey, T) {
288288
/// ```
289289
/// use solana_program_error::ProgramResult;
290290
/// use solana_account_info::{AccountInfo, next_account_info};
291-
/// use solana_pubkey::Pubkey;
291+
/// use solana_address::Address;
292292
/// # use solana_program_error::ProgramError;
293293
///
294294
/// pub fn process_instruction(
295-
/// program_id: &Pubkey,
295+
/// program_id: &Address,
296296
/// accounts: &[AccountInfo],
297297
/// instruction_data: &[u8],
298298
/// ) -> ProgramResult {
@@ -304,13 +304,13 @@ impl<'a, T: Account> IntoAccountInfo<'a> for &'a mut (Pubkey, T) {
304304
///
305305
/// Ok(())
306306
/// }
307-
/// # let p = Pubkey::new_unique();
307+
/// # let p = Address::new_unique();
308308
/// # let l = &mut 0;
309309
/// # let d = &mut [0u8];
310310
/// # let a = AccountInfo::new(&p, false, false, l, d, &p, false);
311311
/// # let accounts = &[a.clone(), a];
312312
/// # process_instruction(
313-
/// # &Pubkey::new_unique(),
313+
/// # &Address::new_unique(),
314314
/// # accounts,
315315
/// # &[],
316316
/// # )?;
@@ -337,11 +337,11 @@ pub fn next_account_info<'a, 'b, I: Iterator<Item = &'a AccountInfo<'b>>>(
337337
/// ```
338338
/// use solana_program_error::ProgramResult;
339339
/// use solana_account_info::{AccountInfo, next_account_info, next_account_infos};
340-
/// use solana_pubkey::Pubkey;
340+
/// use solana_address::Address;
341341
/// # use solana_program_error::ProgramError;
342342
///
343343
/// pub fn process_instruction(
344-
/// program_id: &Pubkey,
344+
/// program_id: &Address,
345345
/// accounts: &[AccountInfo],
346346
/// instruction_data: &[u8],
347347
/// ) -> ProgramResult {
@@ -354,13 +354,13 @@ pub fn next_account_info<'a, 'b, I: Iterator<Item = &'a AccountInfo<'b>>>(
354354
///
355355
/// Ok(())
356356
/// }
357-
/// # let p = Pubkey::new_unique();
357+
/// # let p = Address::new_unique();
358358
/// # let l = &mut 0;
359359
/// # let d = &mut [0u8];
360360
/// # let a = AccountInfo::new(&p, false, false, l, d, &p, false);
361361
/// # let accounts = &[a.clone(), a.clone(), a.clone(), a.clone(), a];
362362
/// # process_instruction(
363-
/// # &Pubkey::new_unique(),
363+
/// # &Address::new_unique(),
364364
/// # accounts,
365365
/// # &[],
366366
/// # )?;
@@ -390,16 +390,16 @@ impl<'a> AsRef<AccountInfo<'a>> for AccountInfo<'a> {
390390
pub fn check_type_assumptions() {
391391
use std::mem::offset_of;
392392

393-
let key = Pubkey::new_from_array([10; 32]);
393+
let key = Address::new_from_array([10; 32]);
394394
let mut lamports = 31;
395395
let mut data = vec![1, 2, 3, 4, 5];
396-
let owner = Pubkey::new_from_array([22; 32]);
396+
let owner = Address::new_from_array([22; 32]);
397397
let account_info = AccountInfo::new(&key, true, false, &mut lamports, &mut data, &owner, true);
398398
let account_info_addr = &account_info as *const _ as u64;
399399

400400
// key
401401
assert_eq!(offset_of!(AccountInfo, key), 0);
402-
let key_ptr = (account_info_addr) as *const &Pubkey;
402+
let key_ptr = (account_info_addr) as *const &Address;
403403
unsafe {
404404
assert_eq!(**key_ptr, key);
405405
}
@@ -420,7 +420,7 @@ pub fn check_type_assumptions() {
420420

421421
// owner
422422
assert_eq!(offset_of!(AccountInfo, owner), 24);
423-
let owner_ptr = (account_info_addr + 24) as *const &Pubkey;
423+
let owner_ptr = (account_info_addr + 24) as *const &Address;
424424
unsafe {
425425
assert_eq!(**owner_ptr, owner);
426426
}
@@ -466,11 +466,11 @@ mod tests {
466466

467467
#[test]
468468
fn test_next_account_infos() {
469-
let k1 = Pubkey::new_unique();
470-
let k2 = Pubkey::new_unique();
471-
let k3 = Pubkey::new_unique();
472-
let k4 = Pubkey::new_unique();
473-
let k5 = Pubkey::new_unique();
469+
let k1 = Address::new_unique();
470+
let k2 = Address::new_unique();
471+
let k3 = Address::new_unique();
472+
let k4 = Address::new_unique();
473+
let k5 = Address::new_unique();
474474
let l1 = &mut 0;
475475
let l2 = &mut 0;
476476
let l3 = &mut 0;
@@ -503,7 +503,7 @@ mod tests {
503503

504504
#[test]
505505
fn test_account_info_as_ref() {
506-
let k = Pubkey::new_unique();
506+
let k = Address::new_unique();
507507
let l = &mut 0;
508508
let d = &mut [0u8];
509509
let info = AccountInfo::new(&k, false, false, l, d, &k, false);
@@ -512,7 +512,7 @@ mod tests {
512512

513513
#[test]
514514
fn test_account_info_debug_data() {
515-
let key = Pubkey::new_unique();
515+
let key = Address::new_unique();
516516
let mut lamports = 42;
517517
let mut data = vec![5; 80];
518518
let data_str = format!("{:?}", Hex(&data[..MAX_DEBUG_ACCOUNT_DATA]));

address-lookup-table-interface/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ solana-address-lookup-table-interface = { path = ".", features = [
5454
"dev-context-only-utils",
5555
] }
5656
solana-hash = { workspace = true }
57+
solana-pubkey = { workspace = true, features = ["std"] }
5758

5859
[lints]
5960
workspace = true

epoch-rewards-hasher/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ targets = ["x86_64-unknown-linux-gnu"]
1414

1515
[dependencies]
1616
siphasher = { workspace = true }
17+
solana-address = { workspace = true }
1718
solana-hash = { workspace = true }
18-
solana-pubkey = { workspace = true }
19+
20+
[dev-dependencies]
21+
solana-address = { workspace = true, features = ["atomic"] }
1922

2023
[lints]
2124
workspace = true

epoch-rewards-hasher/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2-
use {siphasher::sip::SipHasher13, solana_hash::Hash, solana_pubkey::Pubkey, std::hash::Hasher};
2+
use {siphasher::sip::SipHasher13, solana_address::Address, solana_hash::Hash, std::hash::Hasher};
33

44
#[derive(Debug, Clone)]
55
pub struct EpochRewardsHasher {
@@ -16,7 +16,7 @@ impl EpochRewardsHasher {
1616
}
1717

1818
/// Return partition index (0..partitions) by hashing `address` with the `hasher`
19-
pub fn hash_address_to_partition(self, address: &Pubkey) -> usize {
19+
pub fn hash_address_to_partition(self, address: &Address) -> usize {
2020
let Self {
2121
mut hasher,
2222
partitions,
@@ -163,7 +163,7 @@ mod tests {
163163
let partitions = 10;
164164
let hasher = EpochRewardsHasher::new(partitions, &seed);
165165

166-
let pk = Pubkey::new_unique();
166+
let pk = Address::new_unique();
167167

168168
let b1 = hasher.clone().hash_address_to_partition(&pk);
169169
let b2 = hasher.hash_address_to_partition(&pk);

example-mocks/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ solana-instruction = { workspace = true }
2727
solana-keccak-hasher = { workspace = true }
2828
solana-message = { workspace = true, features = ["serde"] }
2929
solana-nonce = { workspace = true }
30-
solana-pubkey = { workspace = true }
30+
solana-pubkey = { workspace = true, features = ["std"] }
3131
solana-sdk-ids = { workspace = true }
3232
solana-system-interface = { workspace = true }
3333
thiserror = { workspace = true }

instructions-sysvar/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ solana-sysvar-id = { workspace = true }
3232
[target.'cfg(not(target_os = "solana"))'.dependencies]
3333
bitflags = { workspace = true }
3434

35+
[dev-dependencies]
36+
solana-address = { workspace = true, features = ["atomic"] }
37+
3538
[lints]
3639
workspace = true

0 commit comments

Comments
 (0)