mv common/ sdk/

This commit is contained in:
Michael Vines
2018-10-25 12:53:50 -07:00
committed by Grimes
parent 471d8f6ff9
commit 1733beabf7
11 changed files with 7 additions and 7 deletions

16
sdk/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "solana-sdk"
version = "0.10.0"
description = "Solana SDK"
authors = ["Solana Maintainers <maintainers@solana.com>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
[dependencies]
bincode = "1.0.0"
bs58 = "0.2.0"
generic-array = { version = "0.12.0", default-features = false, features = ["serde"] }
serde = "1.0.27"
serde_derive = "1.0.27"

56
sdk/src/account.rs Normal file
View File

@ -0,0 +1,56 @@
use pubkey::Pubkey;
/// An Account with userdata that is stored on chain
#[repr(C)]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Account {
/// tokens in the account
pub tokens: i64,
/// user data
/// A transaction can write to its userdata
pub userdata: Vec<u8>,
/// contract id this contract belongs to
pub program_id: Pubkey,
/// this account contains a program (and is strictly read-only)
pub executable: bool,
/// the loader for this program (Pubkey::default() for no loader)
pub loader_program_id: Pubkey,
}
impl Account {
// TODO do we want to add executable and leader_program_id even though they should always be false/default?
pub fn new(tokens: i64, space: usize, program_id: Pubkey) -> Account {
Account {
tokens,
userdata: vec![0u8; space],
program_id,
executable: false,
loader_program_id: Pubkey::default(),
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct KeyedAccount<'a> {
pub key: &'a Pubkey,
pub account: &'a mut Account,
}
impl<'a> From<(&'a Pubkey, &'a mut Account)> for KeyedAccount<'a> {
fn from((key, account): (&'a Pubkey, &'a mut Account)) -> Self {
KeyedAccount { key, account }
}
}
impl<'a> From<&'a mut (Pubkey, Account)> for KeyedAccount<'a> {
fn from((key, account): &'a mut (Pubkey, Account)) -> Self {
KeyedAccount { key, account }
}
}
pub fn create_keyed_accounts(accounts: &mut [(Pubkey, Account)]) -> Vec<KeyedAccount> {
accounts.iter_mut().map(Into::into).collect()
}

8
sdk/src/lib.rs Normal file
View File

@ -0,0 +1,8 @@
pub mod account;
pub mod loader_instruction;
pub mod pubkey;
extern crate bincode;
extern crate bs58;
extern crate generic_array;
#[macro_use]
extern crate serde_derive;

View File

@ -0,0 +1,18 @@
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum LoaderInstruction {
/// Write program data into an account
///
/// * key[0] - the account to write into.
///
/// The transaction must be signed by key[0]
Write { offset: u32, bytes: Vec<u8> },
/// Finalize an account loaded with program data for execution.
/// The exact preparation steps is loader specific but on success the loader must set the executable
/// bit of the Account
///
/// * key[0] - the account to prepare for execution
///
/// The transaction must be signed by key[0]
Finalize,
}

32
sdk/src/pubkey.rs Normal file
View File

@ -0,0 +1,32 @@
use bs58;
use generic_array::typenum::U32;
use generic_array::GenericArray;
use std::fmt;
#[repr(C)]
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Pubkey(GenericArray<u8, U32>);
impl Pubkey {
pub fn new(pubkey_vec: &[u8]) -> Self {
Pubkey(GenericArray::clone_from_slice(&pubkey_vec))
}
}
impl AsRef<[u8]> for Pubkey {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
impl fmt::Debug for Pubkey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", bs58::encode(self.0).into_string())
}
}
impl fmt::Display for Pubkey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", bs58::encode(self.0).into_string())
}
}