2018-09-26 09:07:53 -07:00
|
|
|
//! The `system_transaction` module provides functionality for creating system transactions.
|
|
|
|
|
2018-12-04 15:37:11 -08:00
|
|
|
use hash::Hash;
|
|
|
|
use pubkey::Pubkey;
|
|
|
|
use signature::Keypair;
|
|
|
|
use system_instruction::SystemInstruction;
|
|
|
|
use system_program;
|
|
|
|
use transaction::{Instruction, Transaction};
|
2018-11-16 08:04:46 -08:00
|
|
|
|
2018-09-26 09:07:53 -07:00
|
|
|
pub trait SystemTransaction {
|
|
|
|
fn system_create(
|
|
|
|
from_keypair: &Keypair,
|
|
|
|
to: Pubkey,
|
|
|
|
last_id: Hash,
|
2018-11-05 08:36:22 -08:00
|
|
|
tokens: u64,
|
2018-09-26 09:07:53 -07:00
|
|
|
space: u64,
|
|
|
|
program_id: Pubkey,
|
2018-11-05 08:36:22 -08:00
|
|
|
fee: u64,
|
2018-09-26 09:07:53 -07:00
|
|
|
) -> Self;
|
|
|
|
|
2018-11-05 08:36:22 -08:00
|
|
|
fn system_assign(from_keypair: &Keypair, last_id: Hash, program_id: Pubkey, fee: u64) -> Self;
|
2018-09-26 09:07:53 -07:00
|
|
|
|
2018-11-05 08:36:22 -08:00
|
|
|
fn system_new(from_keypair: &Keypair, to: Pubkey, tokens: u64, last_id: Hash) -> Self;
|
2018-09-26 09:07:53 -07:00
|
|
|
|
|
|
|
fn system_move(
|
|
|
|
from_keypair: &Keypair,
|
|
|
|
to: Pubkey,
|
2018-11-05 08:36:22 -08:00
|
|
|
tokens: u64,
|
2018-09-26 09:07:53 -07:00
|
|
|
last_id: Hash,
|
2018-11-05 08:36:22 -08:00
|
|
|
fee: u64,
|
2018-09-26 09:07:53 -07:00
|
|
|
) -> Self;
|
|
|
|
|
2018-10-05 16:45:27 -07:00
|
|
|
fn system_move_many(
|
|
|
|
from_keypair: &Keypair,
|
2018-11-05 08:36:22 -08:00
|
|
|
moves: &[(Pubkey, u64)],
|
2018-10-05 16:45:27 -07:00
|
|
|
last_id: Hash,
|
2018-11-05 08:36:22 -08:00
|
|
|
fee: u64,
|
2018-10-05 16:45:27 -07:00
|
|
|
) -> Self;
|
2018-10-18 10:33:30 -07:00
|
|
|
|
2018-11-05 08:36:22 -08:00
|
|
|
fn system_spawn(from_keypair: &Keypair, last_id: Hash, fee: u64) -> Self;
|
2018-09-26 09:07:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SystemTransaction for Transaction {
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::CreateAccount transaction
|
2018-09-26 09:07:53 -07:00
|
|
|
fn system_create(
|
|
|
|
from_keypair: &Keypair,
|
|
|
|
to: Pubkey,
|
|
|
|
last_id: Hash,
|
2018-11-05 08:36:22 -08:00
|
|
|
tokens: u64,
|
2018-09-26 09:07:53 -07:00
|
|
|
space: u64,
|
|
|
|
program_id: Pubkey,
|
2018-11-05 08:36:22 -08:00
|
|
|
fee: u64,
|
2018-09-26 09:07:53 -07:00
|
|
|
) -> Self {
|
2018-11-16 08:04:46 -08:00
|
|
|
let create = SystemInstruction::CreateAccount {
|
2018-09-26 09:07:53 -07:00
|
|
|
tokens, //TODO, the tokens to allocate might need to be higher then 0 in the future
|
|
|
|
space,
|
|
|
|
program_id,
|
|
|
|
};
|
|
|
|
Transaction::new(
|
|
|
|
from_keypair,
|
|
|
|
&[to],
|
2018-11-23 12:45:34 -08:00
|
|
|
system_program::id(),
|
2018-11-06 05:50:00 -08:00
|
|
|
&create,
|
2018-09-26 09:07:53 -07:00
|
|
|
last_id,
|
|
|
|
fee,
|
|
|
|
)
|
|
|
|
}
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::Assign transaction
|
2018-11-05 08:36:22 -08:00
|
|
|
fn system_assign(from_keypair: &Keypair, last_id: Hash, program_id: Pubkey, fee: u64) -> Self {
|
2018-11-16 08:04:46 -08:00
|
|
|
let assign = SystemInstruction::Assign { program_id };
|
2018-09-26 09:07:53 -07:00
|
|
|
Transaction::new(
|
|
|
|
from_keypair,
|
|
|
|
&[],
|
2018-11-23 12:45:34 -08:00
|
|
|
system_program::id(),
|
2018-11-06 05:50:00 -08:00
|
|
|
&assign,
|
2018-09-26 09:07:53 -07:00
|
|
|
last_id,
|
|
|
|
fee,
|
|
|
|
)
|
|
|
|
}
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::CreateAccount transaction with some defaults
|
2018-11-05 08:36:22 -08:00
|
|
|
fn system_new(from_keypair: &Keypair, to: Pubkey, tokens: u64, last_id: Hash) -> Self {
|
2018-09-26 09:07:53 -07:00
|
|
|
Transaction::system_create(from_keypair, to, last_id, tokens, 0, Pubkey::default(), 0)
|
|
|
|
}
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::Move transaction
|
2018-09-26 09:07:53 -07:00
|
|
|
fn system_move(
|
|
|
|
from_keypair: &Keypair,
|
|
|
|
to: Pubkey,
|
2018-11-05 08:36:22 -08:00
|
|
|
tokens: u64,
|
2018-09-26 09:07:53 -07:00
|
|
|
last_id: Hash,
|
2018-11-05 08:36:22 -08:00
|
|
|
fee: u64,
|
2018-09-26 09:07:53 -07:00
|
|
|
) -> Self {
|
2018-11-16 08:04:46 -08:00
|
|
|
let move_tokens = SystemInstruction::Move { tokens };
|
2018-09-26 09:07:53 -07:00
|
|
|
Transaction::new(
|
|
|
|
from_keypair,
|
|
|
|
&[to],
|
2018-11-23 12:45:34 -08:00
|
|
|
system_program::id(),
|
2018-11-06 05:50:00 -08:00
|
|
|
&move_tokens,
|
2018-09-26 09:07:53 -07:00
|
|
|
last_id,
|
|
|
|
fee,
|
|
|
|
)
|
|
|
|
}
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::Move transaction to many destinations
|
2018-11-05 08:36:22 -08:00
|
|
|
fn system_move_many(from: &Keypair, moves: &[(Pubkey, u64)], last_id: Hash, fee: u64) -> Self {
|
2018-10-05 16:45:27 -07:00
|
|
|
let instructions: Vec<_> = moves
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, (_, amount))| {
|
2018-11-16 08:04:46 -08:00
|
|
|
let spend = SystemInstruction::Move { tokens: *amount };
|
2018-11-06 05:50:00 -08:00
|
|
|
Instruction::new(0, &spend, vec![0, i as u8 + 1])
|
2018-10-05 16:45:27 -07:00
|
|
|
}).collect();
|
|
|
|
let to_keys: Vec<_> = moves.iter().map(|(to_key, _)| *to_key).collect();
|
|
|
|
|
|
|
|
Transaction::new_with_instructions(
|
2018-10-26 14:43:34 -07:00
|
|
|
&[from],
|
2018-10-05 16:45:27 -07:00
|
|
|
&to_keys,
|
|
|
|
last_id,
|
|
|
|
fee,
|
2018-11-23 12:45:34 -08:00
|
|
|
vec![system_program::id()],
|
2018-10-05 16:45:27 -07:00
|
|
|
instructions,
|
|
|
|
)
|
|
|
|
}
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::Spawn transaction
|
2018-11-05 08:36:22 -08:00
|
|
|
fn system_spawn(from_keypair: &Keypair, last_id: Hash, fee: u64) -> Self {
|
2018-11-16 08:04:46 -08:00
|
|
|
let spawn = SystemInstruction::Spawn;
|
2018-11-23 12:45:34 -08:00
|
|
|
Transaction::new(
|
|
|
|
from_keypair,
|
|
|
|
&[],
|
|
|
|
system_program::id(),
|
|
|
|
&spawn,
|
|
|
|
last_id,
|
|
|
|
fee,
|
|
|
|
)
|
2018-10-18 10:33:30 -07:00
|
|
|
}
|
2018-09-26 09:07:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-12-04 15:37:11 -08:00
|
|
|
use signature::KeypairUtil;
|
2018-09-26 09:07:53 -07:00
|
|
|
|
2018-10-10 17:23:06 -07:00
|
|
|
#[test]
|
|
|
|
fn test_move_many() {
|
|
|
|
let from = Keypair::new();
|
|
|
|
let t1 = Keypair::new();
|
|
|
|
let t2 = Keypair::new();
|
|
|
|
let moves = vec![(t1.pubkey(), 1), (t2.pubkey(), 2)];
|
|
|
|
|
|
|
|
let tx = Transaction::system_move_many(&from, &moves, Default::default(), 0);
|
|
|
|
assert_eq!(tx.account_keys[0], from.pubkey());
|
|
|
|
assert_eq!(tx.account_keys[1], t1.pubkey());
|
|
|
|
assert_eq!(tx.account_keys[2], t2.pubkey());
|
|
|
|
assert_eq!(tx.instructions.len(), 2);
|
|
|
|
assert_eq!(tx.instructions[0].accounts, vec![0, 1]);
|
|
|
|
assert_eq!(tx.instructions[1].accounts, vec![0, 2]);
|
|
|
|
}
|
2018-09-26 09:07:53 -07:00
|
|
|
}
|