lang: Cleanup private apis and documentation (#169)

This commit is contained in:
Armani Ferrante 2021-04-11 08:47:14 +08:00 committed by GitHub
parent ae990e21d7
commit 81e03c5e37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 27 deletions

View File

@ -921,12 +921,12 @@ fn test(skip_deploy: bool, skip_local_validator: bool, file: Option<String>) ->
} }
}; };
let log_streams = stream_logs(&cfg.cluster.url())?; // Setup log reader.
let log_streams = stream_logs(&cfg.cluster.url());
let result: Result<_> = {
let ts_config_exist = Path::new("tsconfig.json").exists();
// Run the tests. // Run the tests.
let test_result: Result<_> = {
let ts_config_exist = Path::new("tsconfig.json").exists();
let mut args = vec!["-t", "1000000"]; let mut args = vec!["-t", "1000000"];
if let Some(ref file) = file { if let Some(ref file) = file {
args.push(file); args.push(file);
@ -959,19 +959,18 @@ fn test(skip_deploy: bool, skip_local_validator: bool, file: Option<String>) ->
exit exit
}; };
// Check all errors and shut down.
if let Some(mut child) = validator_handle { if let Some(mut child) = validator_handle {
if let Err(err) = child.kill() { if let Err(err) = child.kill() {
println!("Failed to kill subprocess {}: {}", child.id(), err); println!("Failed to kill subprocess {}: {}", child.id(), err);
} }
} }
for mut child in log_streams? {
for mut child in log_streams {
if let Err(err) = child.kill() { if let Err(err) = child.kill() {
println!("Failed to kill subprocess {}: {}", child.id(), err); println!("Failed to kill subprocess {}: {}", child.id(), err);
} }
} }
match test_result {
match result {
Ok(exit) => { Ok(exit) => {
if !exit.status.success() { if !exit.status.success() {
std::process::exit(exit.status.code().unwrap()); std::process::exit(exit.status.code().unwrap());

View File

@ -120,7 +120,7 @@ impl Program {
self.program_id self.program_id
} }
pub fn on<T: anchor_lang::EventData + anchor_lang::AnchorDeserialize>( pub fn on<T: anchor_lang::Event + anchor_lang::AnchorDeserialize>(
&self, &self,
f: impl Fn(&EventContext, T) -> () + Send + 'static, f: impl Fn(&EventContext, T) -> () + Send + 'static,
) -> Result<EventHandle, ClientError> { ) -> Result<EventHandle, ClientError> {
@ -187,7 +187,7 @@ impl Program {
} }
} }
fn handle_program_log<T: anchor_lang::EventData + anchor_lang::AnchorDeserialize>( fn handle_program_log<T: anchor_lang::Event + anchor_lang::AnchorDeserialize>(
self_program_str: &str, self_program_str: &str,
l: &str, l: &str,
) -> Result<(Option<T>, Option<String>, bool), ClientError> { ) -> Result<(Option<T>, Option<String>, bool), ClientError> {

View File

@ -26,10 +26,10 @@ pub fn event(
}; };
proc_macro::TokenStream::from(quote! { proc_macro::TokenStream::from(quote! {
#[derive(anchor_lang::EventIndex, AnchorSerialize, AnchorDeserialize)] #[derive(anchor_lang::__private::EventIndex, AnchorSerialize, AnchorDeserialize)]
#event_strct #event_strct
impl anchor_lang::EventData for #event_name { impl anchor_lang::Event for #event_name {
fn data(&self) -> Vec<u8> { fn data(&self) -> Vec<u8> {
let mut d = #discriminator.to_vec(); let mut d = #discriminator.to_vec();
d.append(&mut self.try_to_vec().unwrap()); d.append(&mut self.try_to_vec().unwrap());
@ -45,16 +45,16 @@ pub fn event(
}) })
} }
/// Creates an event in an Anchor program, which can subsequently be subscribed /// Creates an event that can be subscribed to by clients. Calling this method
/// to by clients. Calling this method will internally borsh serialize the /// will internally borsh serialize the [event](./attr.event.html), base64
/// [event](./attr.event.html), base64 encode the bytes, and then add a /// encode the bytes, and then add a [msg!](../solana_program/macro.msg.html)
/// [msg!](../solana_program/macro.msg.html) log to the transaction. /// log to the transaction.
#[proc_macro] #[proc_macro]
pub fn emit(input: proc_macro::TokenStream) -> proc_macro::TokenStream { pub fn emit(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let data: proc_macro2::TokenStream = input.into(); let data: proc_macro2::TokenStream = input.into();
proc_macro::TokenStream::from(quote! { proc_macro::TokenStream::from(quote! {
{ {
let data = anchor_lang::EventData::data(&#data); let data = anchor_lang::Event::data(&#data);
let msg_str = &anchor_lang::__private::base64::encode(data); let msg_str = &anchor_lang::__private::base64::encode(data);
anchor_lang::solana_program::msg!(msg_str); anchor_lang::solana_program::msg!(msg_str);
} }

View File

@ -9,6 +9,7 @@ pub struct Context<'a, 'b, 'c, 'info, T> {
/// Deserialized accounts. /// Deserialized accounts.
pub accounts: &'b mut T, pub accounts: &'b mut T,
/// Remaining accounts given but not deserialized or validated. /// Remaining accounts given but not deserialized or validated.
/// Be very careful when using this directly.
pub remaining_accounts: &'c [AccountInfo<'info>], pub remaining_accounts: &'c [AccountInfo<'info>],
} }

View File

@ -1,5 +1,7 @@
use solana_program::program_error::ProgramError; use solana_program::program_error::ProgramError;
// Error type that can be returned by internal framework code.
#[doc(hidden)]
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
#[error(transparent)] #[error(transparent)]

View File

@ -1,4 +1,4 @@
//! idl.rs defines the instructions and account state used to store a program's //! Defines the instructions and account state used to store a program's
//! IDL on-chain at a canonical account address, which can be derived as a //! IDL on-chain at a canonical account address, which can be derived as a
//! function of nothing other than the program's ID. //! function of nothing other than the program's ID.
//! //!

View File

@ -35,6 +35,7 @@ mod context;
mod cpi_account; mod cpi_account;
mod ctor; mod ctor;
mod error; mod error;
#[doc(hidden)]
pub mod idl; pub mod idl;
mod program_account; mod program_account;
mod state; mod state;
@ -44,26 +45,27 @@ mod vec;
// Internal module used by macros. // Internal module used by macros.
#[doc(hidden)] #[doc(hidden)]
pub mod __private { pub mod __private {
pub use crate::ctor::Ctor;
pub use crate::error::Error;
pub use anchor_attribute_event::EventIndex;
pub use base64; pub use base64;
} }
pub use crate::context::{Context, CpiContext}; pub use crate::context::{Context, CpiContext};
pub use crate::cpi_account::CpiAccount; pub use crate::cpi_account::CpiAccount;
pub use crate::ctor::Ctor;
pub use crate::program_account::ProgramAccount; pub use crate::program_account::ProgramAccount;
pub use crate::state::ProgramState; pub use crate::state::ProgramState;
pub use crate::sysvar::Sysvar; pub use crate::sysvar::Sysvar;
pub use anchor_attribute_access_control::access_control; pub use anchor_attribute_access_control::access_control;
pub use anchor_attribute_account::account; pub use anchor_attribute_account::account;
pub use anchor_attribute_error::error; pub use anchor_attribute_error::error;
pub use anchor_attribute_event::{emit, event, EventIndex}; pub use anchor_attribute_event::{emit, event};
pub use anchor_attribute_interface::interface; pub use anchor_attribute_interface::interface;
pub use anchor_attribute_program::program; pub use anchor_attribute_program::program;
pub use anchor_attribute_state::state; pub use anchor_attribute_state::state;
pub use anchor_derive_accounts::Accounts; pub use anchor_derive_accounts::Accounts;
/// Borsh is the default serialization format for instructions and accounts. /// Borsh is the default serialization format for instructions and accounts.
pub use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize}; pub use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize};
pub use error::Error;
pub use solana_program; pub use solana_program;
/// A data structure of validated accounts that can be deserialized from the /// A data structure of validated accounts that can be deserialized from the
@ -176,18 +178,27 @@ pub trait InstructionData: AnchorSerialize {
fn data(&self) -> Vec<u8>; fn data(&self) -> Vec<u8>;
} }
/// Calculates the size of an account, which may be larger than the deserialized // Calculates the size of an account, which may be larger than the deserialized
/// data in it. This trait is currently only used for `#[state]` accounts. // data in it. This trait is currently only used for `#[state]` accounts.
#[doc(hidden)]
pub trait AccountSize: AnchorSerialize { pub trait AccountSize: AnchorSerialize {
fn size(&self) -> Result<u64, ProgramError>; fn size(&self) -> Result<u64, ProgramError>;
} }
/// The serialized event data to be emitted via a Solana log. /// An event that can be emitted via a Solana log.
pub trait Event: AnchorSerialize + AnchorDeserialize + Discriminator {
fn data(&self) -> Vec<u8>;
}
// The serialized event data to be emitted via a Solana log.
// TODO: remove this on the next major version upgrade.
#[doc(hidden)]
#[deprecated(since = "0.4.2", note = "Please use Event instead")]
pub trait EventData: AnchorSerialize + Discriminator { pub trait EventData: AnchorSerialize + Discriminator {
fn data(&self) -> Vec<u8>; fn data(&self) -> Vec<u8>;
} }
/// 8 byte identifier for a type. /// 8 byte unique identifier for a type.
pub trait Discriminator { pub trait Discriminator {
fn discriminator() -> [u8; 8]; fn discriminator() -> [u8; 8];
} }
@ -198,7 +209,7 @@ pub mod prelude {
pub use super::{ pub use super::{
access_control, account, emit, error, event, interface, program, state, AccountDeserialize, access_control, account, emit, error, event, interface, program, state, AccountDeserialize,
AccountSerialize, Accounts, AccountsExit, AccountsInit, AnchorDeserialize, AnchorSerialize, AccountSerialize, Accounts, AccountsExit, AccountsInit, AnchorDeserialize, AnchorSerialize,
Context, CpiAccount, CpiContext, Ctor, ProgramAccount, ProgramState, Sysvar, ToAccountInfo, Context, CpiAccount, CpiContext, ProgramAccount, ProgramState, Sysvar, ToAccountInfo,
ToAccountInfos, ToAccountMetas, ToAccountInfos, ToAccountMetas,
}; };

View File

@ -401,7 +401,7 @@ pub fn generate_non_inlined_handlers(program: &Program) -> proc_macro2::TokenStr
let mut remaining_accounts: &[AccountInfo] = accounts; let mut remaining_accounts: &[AccountInfo] = accounts;
// Deserialize accounts. // Deserialize accounts.
let ctor_accounts = anchor_lang::Ctor::try_accounts(program_id, &mut remaining_accounts)?; let ctor_accounts = anchor_lang::__private::Ctor::try_accounts(program_id, &mut remaining_accounts)?;
let mut ctor_user_def_accounts = #anchor_ident::try_accounts(program_id, &mut remaining_accounts)?; let mut ctor_user_def_accounts = #anchor_ident::try_accounts(program_id, &mut remaining_accounts)?;
// Invoke the ctor. // Invoke the ctor.