Skip to content

Commit

Permalink
Initial population of solana-program-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Oct 24, 2020
1 parent 3718771 commit 63db324
Show file tree
Hide file tree
Showing 79 changed files with 1,475 additions and 1,221 deletions.
32 changes: 31 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ci/nits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ declare print_free_tree=(
':sdk/bpf/rust/rust-utils/**.rs'
':sdk/**.rs'
':^sdk/cargo-build-bpf/**.rs'
':^sdk/src/program_option.rs'
':^sdk/src/program_stubs.rs'
':^sdk/program/src/program_option.rs'
':^sdk/program/src/program_stubs.rs'
':programs/**.rs'
':^**bin**.rs'
':^**bench**.rs'
Expand Down
3 changes: 3 additions & 0 deletions ci/test-stable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ test-stable)
_ cargo +"$rust_stable" test --jobs "$NPROC" --all --exclude solana-local-cluster ${V:+--verbose} -- --nocapture
;;
test-stable-perf)
# BPF solana-sdk legacy compile test
./cargo-build-bpf --manifest-path sdk/Cargo.toml --no-default-features --features program

# BPF program tests
_ make -C programs/bpf/c tests
_ cargo +"$rust_stable" test \
Expand Down
29 changes: 27 additions & 2 deletions programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 12 additions & 13 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ license = "Apache-2.0"
edition = "2018"

[features]
# On-chain program specific dependencies
# "program" feature is a legacy feature retained to support v1.3 and older
# programs. New development should not use this feature. Instead use the
# solana-program-sdk crate
program = []
# Everything includes functionality that is not compatible or needed for on-chain programs

# "everything" includes functionality that is not compatible or needed for on-chain programs
default = [
"everything"
]
Expand Down Expand Up @@ -43,12 +46,11 @@ curve25519-dalek = { version = "2.1.0", optional = true }
generic-array = { version = "0.14.3", default-features = false, features = ["serde", "more_lengths"], optional = true }
hex = "0.4.2"
hmac = "0.7.0"
itertools = { version = "0.9.0" }
lazy_static = "1.4.0"
log = { version = "0.4.8" }
itertools = "0.9.0"
log = "0.4.8"
memmap = { version = "0.7.0", optional = true }
num-derive = { version = "0.3" }
num-traits = { version = "0.2" }
num-derive = "0.3"
num-traits = "0.2"
pbkdf2 = { version = "0.3.0", default-features = false }
rand = { version = "0.7.0", optional = true }
rand_chacha = { version = "0.2.2", optional = true }
Expand All @@ -70,15 +72,12 @@ libsecp256k1 = { version = "0.3.5", optional = true }
sha3 = { version = "0.9.1", optional = true }
digest = { version = "0.9.0", optional = true }

[target.'cfg(not(target_arch = "bpf"))'.dependencies]
curve25519-dalek = { version = "2.1.0" }

[dev-dependencies]
curve25519-dalek = "2.1.0"
tiny-bip39 = "0.7.0"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[build-dependencies]
rustc_version = "0.2"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
2 changes: 2 additions & 0 deletions sdk/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
127 changes: 88 additions & 39 deletions sdk/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,81 @@ use syn::{
Expr, Ident, LitByte, LitStr, Path, Token,
};

struct Id(proc_macro2::TokenStream);
impl Parse for Id {
fn parse(input: ParseStream) -> Result<Self> {
let token_stream = if input.peek(syn::LitStr) {
let id_literal: LitStr = input.parse()?;
parse_pubkey(&id_literal)?
} else {
let expr: Expr = input.parse()?;
quote! { #expr }
};
fn parse_id(
input: ParseStream,
pubkey_type: proc_macro2::TokenStream,
) -> Result<proc_macro2::TokenStream> {
let id = if input.peek(syn::LitStr) {
let id_literal: LitStr = input.parse()?;
parse_pubkey(&id_literal, &pubkey_type)?
} else {
let expr: Expr = input.parse()?;
quote! { #expr }
};

if !input.is_empty() {
let stream: proc_macro2::TokenStream = input.parse()?;
return Err(syn::Error::new_spanned(stream, "unexpected token"));
if !input.is_empty() {
let stream: proc_macro2::TokenStream = input.parse()?;
return Err(syn::Error::new_spanned(stream, "unexpected token"));
}
Ok(id)
}

fn id_to_tokens(
id: &proc_macro2::TokenStream,
pubkey_type: proc_macro2::TokenStream,
tokens: &mut proc_macro2::TokenStream,
) {
tokens.extend(quote! {
/// The static program ID
pub static ID: #pubkey_type = #id;

/// Confirms that a given pubkey is equivalent to the program ID
pub fn check_id(id: &#pubkey_type) -> bool {
id == &ID
}

/// Returns the program ID
pub fn id() -> #pubkey_type {
ID
}

#[cfg(test)]
#[test]
fn test_id() {
assert!(check_id(&id()));
}
});
}

Ok(Id(token_stream))
struct Id(proc_macro2::TokenStream);

impl Parse for Id {
fn parse(input: ParseStream) -> Result<Self> {
parse_id(input, quote! { ::solana_sdk::pubkey::Pubkey }).map(Self)
}
}

impl ToTokens for Id {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let id = &self.0;
tokens.extend(quote! {
/// The static program ID
pub static ID: ::solana_sdk::pubkey::Pubkey = #id;

/// Confirms that a given pubkey is equivalent to the program ID
pub fn check_id(id: &::solana_sdk::pubkey::Pubkey) -> bool {
id == &ID
}
id_to_tokens(&self.0, quote! { ::solana_sdk::pubkey::Pubkey }, tokens)
}
}

/// Returns the program ID
pub fn id() -> ::solana_sdk::pubkey::Pubkey {
ID
}
struct ProgramSdkId(proc_macro2::TokenStream);

#[cfg(test)]
#[test]
fn test_id() {
assert!(check_id(&id()));
}
});
impl Parse for ProgramSdkId {
fn parse(input: ParseStream) -> Result<Self> {
parse_id(input, quote! { ::solana_program_sdk::pubkey::Pubkey }).map(Self)
}
}

impl ToTokens for ProgramSdkId {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
id_to_tokens(
&self.0,
quote! { ::solana_program_sdk::pubkey::Pubkey },
tokens,
)
}
}

Expand Down Expand Up @@ -135,7 +167,16 @@ pub fn declare_id(input: TokenStream) -> TokenStream {
TokenStream::from(quote! {#id})
}

fn parse_pubkey(id_literal: &LitStr) -> Result<proc_macro2::TokenStream> {
#[proc_macro]
pub fn program_sdk_declare_id(input: TokenStream) -> TokenStream {
let id = parse_macro_input!(input as ProgramSdkId);
TokenStream::from(quote! {#id})
}

fn parse_pubkey(
id_literal: &LitStr,
pubkey_type: &proc_macro2::TokenStream,
) -> Result<proc_macro2::TokenStream> {
let id_vec = bs58::decode(id_literal.value())
.into_vec()
.map_err(|_| syn::Error::new_spanned(&id_literal, "failed to decode base58 string"))?;
Expand All @@ -147,7 +188,7 @@ fn parse_pubkey(id_literal: &LitStr) -> Result<proc_macro2::TokenStream> {
})?;
let bytes = id_array.iter().map(|b| LitByte::new(*b, Span::call_site()));
Ok(quote! {
::solana_sdk::pubkey::Pubkey::new_from_array(
#pubkey_type::new_from_array(
[#(#bytes,)*]
)
})
Expand All @@ -160,19 +201,23 @@ struct Pubkeys {
}
impl Parse for Pubkeys {
fn parse(input: ParseStream) -> Result<Self> {
let pubkey_type = quote! {
::solana_sdk::pubkey::Pubkey
};

let method = input.parse()?;
let _comma: Token![,] = input.parse()?;
let (num, pubkeys) = if input.peek(syn::LitStr) {
let id_literal: LitStr = input.parse()?;
(1, parse_pubkey(&id_literal)?)
(1, parse_pubkey(&id_literal, &pubkey_type)?)
} else if input.peek(Bracket) {
let pubkey_strings;
bracketed!(pubkey_strings in input);
let punctuated: Punctuated<LitStr, Token![,]> =
Punctuated::parse_terminated(&pubkey_strings)?;
let mut pubkeys: Punctuated<proc_macro2::TokenStream, Token![,]> = Punctuated::new();
for string in punctuated.iter() {
pubkeys.push(parse_pubkey(string)?);
pubkeys.push(parse_pubkey(string, &pubkey_type)?);
}
(pubkeys.len(), quote! {#pubkeys})
} else {
Expand All @@ -195,15 +240,19 @@ impl ToTokens for Pubkeys {
num,
pubkeys,
} = self;

let pubkey_type = quote! {
::solana_sdk::pubkey::Pubkey
};
if *num == 1 {
tokens.extend(quote! {
pub fn #method() -> ::solana_sdk::pubkey::Pubkey {
pub fn #method() -> #pubkey_type {
#pubkeys
}
});
} else {
tokens.extend(quote! {
pub fn #method() -> ::std::vec::Vec<::solana_sdk::pubkey::Pubkey> {
pub fn #method() -> ::std::vec::Vec<#pubkey_type> {
vec![#pubkeys]
}
});
Expand Down
Loading

0 comments on commit 63db324

Please sign in to comment.