Switch programs to use Pubkey from SolPubkey (#5821)

This commit is contained in:
Jack May 2019-09-06 12:40:01 -07:00 committed by GitHub
parent 6057768fdc
commit d9817c153a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 25 deletions

View File

@ -22,7 +22,7 @@ fn return_sstruct() -> SStruct {
entrypoint!(process_instruction); entrypoint!(process_instruction);
fn process_instruction(ka: &mut [SolKeyedAccount], info: &SolClusterInfo, data: &[u8]) -> bool { fn process_instruction(ka: &mut [SolKeyedAccount], info: &SolClusterInfo, data: &[u8]) -> bool {
info!("Program identifier:"); info!("Program identifier:");
sol_log_key(&info.program_id); info.program_id.log();
// Log the provided account keys and instruction input data. In the case of // Log the provided account keys and instruction input data. In the case of
// the no-op program, no account keys or input data are expected but real // the no-op program, no account keys or input data are expected but real

View File

@ -4,14 +4,12 @@ extern crate alloc;
use alloc::vec::Vec; use alloc::vec::Vec;
use core::mem::size_of; use core::mem::size_of;
use core::slice::{from_raw_parts, from_raw_parts_mut}; use core::slice::{from_raw_parts, from_raw_parts_mut};
use crate::pubkey::Pubkey;
/// Public key
pub type SolPubkey = [u8; 32];
/// Keyed Account /// Keyed Account
pub struct SolKeyedAccount<'a> { pub struct SolKeyedAccount<'a> {
/// Public key of the account /// Public key of the account
pub key: &'a SolPubkey, pub key: &'a Pubkey,
/// Public key of the account /// Public key of the account
pub is_signer: bool, pub is_signer: bool,
/// Number of lamports owned by this account /// Number of lamports owned by this account
@ -19,14 +17,14 @@ pub struct SolKeyedAccount<'a> {
/// On-chain data within this account /// On-chain data within this account
pub data: &'a mut [u8], pub data: &'a mut [u8],
/// Program that owns this account /// Program that owns this account
pub owner: &'a SolPubkey, pub owner: &'a Pubkey,
} }
/// Information about the state of the cluster immediately before the program /// Information about the state of the cluster immediately before the program
/// started executing the current instruction /// started executing the current instruction
pub struct SolClusterInfo<'a> { pub struct SolClusterInfo<'a> {
/// program_id of the currently executing program /// program_id of the currently executing program
pub program_id: &'a SolPubkey, pub program_id: &'a Pubkey,
} }
/// Declare entrypoint of the program. /// Declare entrypoint of the program.
@ -75,8 +73,8 @@ pub unsafe fn deserialize<'a>(
}; };
offset += size_of::<u64>(); offset += size_of::<u64>();
let key: &SolPubkey = &*(input.add(offset) as *const [u8; size_of::<SolPubkey>()]); let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
offset += size_of::<SolPubkey>(); offset += size_of::<Pubkey>();
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
let lamports = &mut *(input.add(offset) as *mut u64); let lamports = &mut *(input.add(offset) as *mut u64);
@ -89,8 +87,8 @@ pub unsafe fn deserialize<'a>(
let data = { from_raw_parts_mut(input.add(offset), data_length) }; let data = { from_raw_parts_mut(input.add(offset), data_length) };
offset += data_length; offset += data_length;
let owner: &SolPubkey = &*(input.add(offset) as *const [u8; size_of::<SolPubkey>()]); let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
offset += size_of::<SolPubkey>(); offset += size_of::<Pubkey>();
kas.push(SolKeyedAccount { kas.push(SolKeyedAccount {
key, key,
@ -112,7 +110,7 @@ pub unsafe fn deserialize<'a>(
// Program Id // Program Id
let program_id: &SolPubkey = &*(input.add(offset) as *const [u8; size_of::<SolPubkey>()]); let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
let info = SolClusterInfo { program_id }; let info = SolClusterInfo { program_id };
Ok((kas, info, data)) Ok((kas, info, data))

View File

@ -1,6 +1,6 @@
//! @brief Solana Rust-based BPF program logging //! @brief Solana Rust-based BPF program logging
use crate::entrypoint::{SolKeyedAccount, SolPubkey}; use crate::entrypoint::SolKeyedAccount;
/// Prints a string /// Prints a string
/// There are two forms and are fast /// There are two forms and are fast
@ -48,16 +48,6 @@ extern "C" {
fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64); fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64);
} }
/// Prints the hexadecimal representation of a public key
///
/// @param - key The public key to print
#[allow(dead_code)]
pub fn sol_log_key(key: &SolPubkey) {
for (i, k) in key.iter().enumerate() {
sol_log_64(0, 0, 0, i as u64, u64::from(*k));
}
}
/// Prints the hexadecimal representation of a slice /// Prints the hexadecimal representation of a slice
/// ///
/// @param slice - The array to print /// @param slice - The array to print
@ -80,13 +70,13 @@ pub fn sol_log_params(ka: &[SolKeyedAccount], data: &[u8]) {
sol_log("- Is signer"); sol_log("- Is signer");
sol_log_64(0, 0, 0, 0, k.is_signer as u64); sol_log_64(0, 0, 0, 0, k.is_signer as u64);
sol_log("- Key"); sol_log("- Key");
sol_log_key(&k.key); k.key.log();
sol_log("- Lamports"); sol_log("- Lamports");
sol_log_64(0, 0, 0, 0, *k.lamports); sol_log_64(0, 0, 0, 0, *k.lamports);
sol_log("- AccountData"); sol_log("- AccountData");
sol_log_slice(k.data); sol_log_slice(k.data);
sol_log("- Owner"); sol_log("- Owner");
sol_log_key(&k.owner); k.owner.log();
} }
sol_log("Instruction data"); sol_log("Instruction data");
sol_log_slice(data); sol_log_slice(data);

View File

@ -51,6 +51,14 @@ impl Pubkey {
pub fn new_rand() -> Self { pub fn new_rand() -> Self {
Self::new(&rand::random::<[u8; 32]>()) Self::new(&rand::random::<[u8; 32]>())
} }
#[cfg(feature = "program")]
pub fn log(&self) {
use crate::log::sol_log_64;
for (i, k) in self.0.iter().enumerate() {
sol_log_64(0, 0, 0, i as u64, u64::from(*k));
}
}
} }
impl AsRef<[u8]> for Pubkey { impl AsRef<[u8]> for Pubkey {