Files
solana/src/system_program.rs

274 lines
9.1 KiB
Rust
Raw Normal View History

2018-09-19 17:25:57 -07:00
//! system program
use bank::Account;
use bincode::deserialize;
use signature::Pubkey;
use transaction::Transaction;
#[derive(Serialize, Deserialize, Debug, Clone)]
2018-09-19 17:25:57 -07:00
pub enum SystemProgram {
/// Create a new account
/// * Transaction::keys[0] - source
/// * Transaction::keys[1] - new account key
/// * tokens - number of tokens to transfer to the new account
/// * space - memory to allocate if greater then zero
2018-09-19 17:25:57 -07:00
/// * program_id - the program id of the new account
CreateAccount {
tokens: i64,
space: u64,
2018-09-19 17:25:57 -07:00
program_id: Pubkey,
},
2018-09-19 17:25:57 -07:00
/// Assign account to a program
/// * Transaction::keys[0] - account to assign
2018-09-19 17:25:57 -07:00
Assign { program_id: Pubkey },
/// Move tokens
/// * Transaction::keys[0] - source
/// * Transaction::keys[1] - destination
Move { tokens: i64 },
}
2018-09-19 17:25:57 -07:00
pub const SYSTEM_PROGRAM_ID: [u8; 32] = [0u8; 32];
2018-09-19 17:25:57 -07:00
impl SystemProgram {
pub fn check_id(program_id: &Pubkey) -> bool {
program_id.as_ref() == SYSTEM_PROGRAM_ID
}
pub fn id() -> Pubkey {
2018-09-19 17:25:57 -07:00
Pubkey::new(&SYSTEM_PROGRAM_ID)
}
pub fn get_balance(account: &Account) -> i64 {
account.tokens
}
pub fn process_transaction(tx: &Transaction, accounts: &mut [Account]) {
2018-09-19 17:25:57 -07:00
if let Ok(syscall) = deserialize(&tx.userdata){
2018-09-20 13:54:42 -07:00
trace!("process_transaction: {:?}", syscall);
match syscall {
2018-09-19 17:25:57 -07:00
SystemProgram::CreateAccount {
2018-09-20 13:54:42 -07:00
tokens,
space,
2018-09-19 17:25:57 -07:00
program_id,
2018-09-20 13:54:42 -07:00
} => {
2018-09-19 17:25:57 -07:00
if !Self::check_id(&accounts[0].program_id) {
2018-09-20 13:54:42 -07:00
return;
}
if space > 0
&& (!accounts[1].userdata.is_empty()
2018-09-19 17:25:57 -07:00
|| !Self::check_id(&accounts[1].program_id))
2018-09-20 13:54:42 -07:00
{
return;
}
accounts[0].tokens -= tokens;
accounts[1].tokens += tokens;
2018-09-19 17:25:57 -07:00
accounts[1].program_id = program_id;
2018-09-20 13:54:42 -07:00
accounts[1].userdata = vec![0; space as usize];
}
2018-09-19 17:25:57 -07:00
SystemProgram::Assign { program_id } => {
if !Self::check_id(&accounts[0].program_id) {
2018-09-20 13:54:42 -07:00
return;
}
2018-09-19 17:25:57 -07:00
accounts[0].program_id = program_id;
}
2018-09-19 17:25:57 -07:00
SystemProgram::Move { tokens } => {
2018-09-20 13:54:42 -07:00
//bank should be verifying correctness
accounts[0].tokens -= tokens;
accounts[1].tokens += tokens;
}
2018-09-20 13:54:42 -07:00
} else {
info!("Invalid transaction userdata: {:?}", tx.userdata);
}
}
}
2018-09-17 14:29:20 -07:00
#[cfg(test)]
mod test {
use bank::Account;
use hash::Hash;
use signature::{Keypair, KeypairUtil, Pubkey};
2018-09-19 17:25:57 -07:00
use system_program::SystemProgram;
2018-09-17 14:29:20 -07:00
use transaction::Transaction;
#[test]
fn test_create_noop() {
let from = Keypair::new();
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
let tx = Transaction::system_new(&from, to.pubkey(), 0, Hash::default());
2018-09-19 17:25:57 -07:00
SystemProgram::process_transaction(&tx, &mut accounts);
2018-09-17 14:29:20 -07:00
assert_eq!(accounts[0].tokens, 0);
assert_eq!(accounts[1].tokens, 0);
}
#[test]
fn test_create_spend() {
let from = Keypair::new();
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
accounts[0].tokens = 1;
let tx = Transaction::system_new(&from, to.pubkey(), 1, Hash::default());
2018-09-19 17:25:57 -07:00
SystemProgram::process_transaction(&tx, &mut accounts);
2018-09-17 14:29:20 -07:00
assert_eq!(accounts[0].tokens, 0);
assert_eq!(accounts[1].tokens, 1);
}
#[test]
fn test_create_spend_wrong_source() {
let from = Keypair::new();
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
accounts[0].tokens = 1;
2018-09-19 17:25:57 -07:00
accounts[0].program_id = from.pubkey();
2018-09-17 14:29:20 -07:00
let tx = Transaction::system_new(&from, to.pubkey(), 1, Hash::default());
2018-09-19 17:25:57 -07:00
SystemProgram::process_transaction(&tx, &mut accounts);
2018-09-17 14:29:20 -07:00
assert_eq!(accounts[0].tokens, 1);
assert_eq!(accounts[1].tokens, 0);
}
#[test]
fn test_create_assign_and_allocate() {
let from = Keypair::new();
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
2018-09-20 13:38:28 -06:00
let tx =
Transaction::system_create(&from, to.pubkey(), Hash::default(), 0, 1, to.pubkey(), 0);
2018-09-17 14:29:20 -07:00
SystemContract::process_transaction(&tx, &mut accounts);
assert!(accounts[0].userdata.is_empty());
assert_eq!(accounts[1].userdata.len(), 1);
2018-09-19 17:25:57 -07:00
assert_eq!(accounts[1].program_id, to.pubkey());
2018-09-17 14:29:20 -07:00
}
#[test]
2018-09-19 17:25:57 -07:00
fn test_create_allocate_wrong_dest_program() {
2018-09-17 14:29:20 -07:00
let from = Keypair::new();
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
accounts[1].contract_id = to.pubkey();
2018-09-20 13:38:28 -06:00
let tx = Transaction::system_create(
&from,
to.pubkey(),
Hash::default(),
0,
1,
Pubkey::default(),
0,
);
2018-09-17 14:29:20 -07:00
SystemContract::process_transaction(&tx, &mut accounts);
assert!(accounts[1].userdata.is_empty());
}
#[test]
2018-09-19 17:25:57 -07:00
fn test_create_allocate_wrong_source_program() {
2018-09-17 14:29:20 -07:00
let from = Keypair::new();
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
2018-09-19 17:25:57 -07:00
accounts[0].program_id = to.pubkey();
2018-09-20 13:38:28 -06:00
let tx = Transaction::system_create(
&from,
to.pubkey(),
Hash::default(),
0,
1,
Pubkey::default(),
0,
);
2018-09-17 14:29:20 -07:00
SystemContract::process_transaction(&tx, &mut accounts);
assert!(accounts[1].userdata.is_empty());
}
#[test]
fn test_create_allocate_already_allocated() {
let from = Keypair::new();
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
accounts[1].userdata = vec![0, 0, 0];
2018-09-20 13:38:28 -06:00
let tx = Transaction::system_create(
&from,
to.pubkey(),
Hash::default(),
0,
2,
Pubkey::default(),
0,
);
2018-09-17 14:29:20 -07:00
SystemContract::process_transaction(&tx, &mut accounts);
assert_eq!(accounts[1].userdata.len(), 3);
}
#[test]
fn test_create_assign() {
let from = Keypair::new();
2018-09-19 17:25:57 -07:00
let program = Keypair::new();
2018-09-17 14:29:20 -07:00
let mut accounts = vec![Account::default()];
2018-09-19 17:25:57 -07:00
let tx = Transaction::system_assign(&from, Hash::default(), program.pubkey(), 0);
SystemProgram::process_transaction(&tx, &mut accounts);
assert_eq!(accounts[0].program_id, program.pubkey());
2018-09-17 14:29:20 -07:00
}
#[test]
fn test_move() {
let from = Keypair::new();
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
accounts[0].tokens = 1;
let tx = Transaction::new(&from, to.pubkey(), 1, Hash::default());
2018-09-19 17:25:57 -07:00
SystemProgram::process_transaction(&tx, &mut accounts);
2018-09-17 14:29:20 -07:00
assert_eq!(accounts[0].tokens, 0);
assert_eq!(accounts[1].tokens, 1);
}
2018-09-19 17:25:57 -07:00
/// Detect binary changes in the serialized program userdata, which could have a downstream
/// affect on SDKs and DApps
#[test]
fn test_sdk_serialize() {
let keypair = Keypair::new();
2018-09-19 17:25:57 -07:00
use budget_program::BUDGET_PROGRAM_ID;
// CreateAccount
let tx = Transaction::system_create(
&keypair,
keypair.pubkey(),
Hash::default(),
111,
222,
2018-09-19 17:25:57 -07:00
Pubkey::new(&BUDGET_PROGRAM_ID),
0,
);
assert_eq!(
tx.userdata,
vec![
2018-09-20 13:38:28 -06:00
0, 0, 0, 0, 111, 0, 0, 0, 0, 0, 0, 0, 222, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
);
// CreateAccount
let tx = Transaction::system_create(
&keypair,
keypair.pubkey(),
Hash::default(),
111,
222,
2018-09-20 13:38:28 -06:00
Pubkey::default(),
0,
);
assert_eq!(
tx.userdata,
2018-09-20 13:38:28 -06:00
vec![
0, 0, 0, 0, 111, 0, 0, 0, 0, 0, 0, 0, 222, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
);
// Assign
let tx = Transaction::system_assign(
&keypair,
Hash::default(),
2018-09-19 17:25:57 -07:00
Pubkey::new(&BUDGET_PROGRAM_ID),
0,
);
assert_eq!(
tx.userdata,
vec![
1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
]
);
// Move
let tx = Transaction::system_move(&keypair, keypair.pubkey(), 123, Hash::default(), 0);
assert_eq!(tx.userdata, vec![2, 0, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0]);
}
2018-09-17 14:29:20 -07:00
}