2018-09-26 10:07:53 -06:00
|
|
|
//! The `system_transaction` module provides functionality for creating system transactions.
|
|
|
|
|
2018-12-14 20:39:10 -08:00
|
|
|
use crate::hash::Hash;
|
|
|
|
use crate::pubkey::Pubkey;
|
|
|
|
use crate::signature::Keypair;
|
|
|
|
use crate::system_instruction::SystemInstruction;
|
|
|
|
use crate::system_program;
|
2019-03-15 09:47:25 -06:00
|
|
|
use crate::transaction::{CompiledInstruction, Transaction};
|
2018-11-16 08:04:46 -08:00
|
|
|
|
2019-02-01 08:36:35 -07:00
|
|
|
pub struct SystemTransaction {}
|
2018-09-26 10:07:53 -06:00
|
|
|
|
2019-02-01 08:36:35 -07:00
|
|
|
impl SystemTransaction {
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::CreateAccount transaction
|
2019-02-01 08:36:35 -07:00
|
|
|
pub fn new_program_account(
|
2018-09-26 10:07:53 -06:00
|
|
|
from_keypair: &Keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
to: &Pubkey,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash: Hash,
|
2019-03-05 16:28:14 -08:00
|
|
|
lamports: u64,
|
2018-09-26 10:07:53 -06:00
|
|
|
space: u64,
|
2019-03-09 19:28:43 -08:00
|
|
|
program_id: &Pubkey,
|
2018-11-05 09:36:22 -07:00
|
|
|
fee: u64,
|
2019-02-01 08:36:35 -07:00
|
|
|
) -> Transaction {
|
2018-11-16 08:04:46 -08:00
|
|
|
let create = SystemInstruction::CreateAccount {
|
2019-03-05 16:28:14 -08:00
|
|
|
lamports, //TODO, the lamports to allocate might need to be higher then 0 in the future
|
2018-09-26 10:07:53 -06:00
|
|
|
space,
|
2019-03-09 19:28:43 -08:00
|
|
|
program_id: *program_id,
|
2018-09-26 10:07:53 -06:00
|
|
|
};
|
2019-03-15 10:02:28 -06:00
|
|
|
Transaction::new_signed(
|
2018-09-26 10:07:53 -06:00
|
|
|
from_keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
&[*to],
|
|
|
|
&system_program::id(),
|
2018-11-06 06:50:00 -07:00
|
|
|
&create,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash,
|
2018-09-26 10:07:53 -06:00
|
|
|
fee,
|
|
|
|
)
|
|
|
|
}
|
2019-02-01 08:36:35 -07:00
|
|
|
|
|
|
|
/// Create and sign a transaction to create a system account
|
|
|
|
pub fn new_account(
|
|
|
|
from_keypair: &Keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
to: &Pubkey,
|
2019-03-05 16:28:14 -08:00
|
|
|
lamports: u64,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash: Hash,
|
2019-02-01 08:36:35 -07:00
|
|
|
fee: u64,
|
|
|
|
) -> Transaction {
|
|
|
|
let program_id = system_program::id();
|
2019-03-02 10:20:10 -08:00
|
|
|
Self::new_program_account(
|
|
|
|
from_keypair,
|
|
|
|
to,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash,
|
2019-03-05 16:28:14 -08:00
|
|
|
lamports,
|
2019-03-02 10:20:10 -08:00
|
|
|
0,
|
2019-03-09 19:28:43 -08:00
|
|
|
&program_id,
|
2019-03-02 10:20:10 -08:00
|
|
|
fee,
|
|
|
|
)
|
2019-02-01 08:36:35 -07:00
|
|
|
}
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::Assign transaction
|
2019-02-01 08:36:35 -07:00
|
|
|
pub fn new_assign(
|
|
|
|
from_keypair: &Keypair,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash: Hash,
|
2019-03-09 19:28:43 -08:00
|
|
|
program_id: &Pubkey,
|
2019-02-01 08:36:35 -07:00
|
|
|
fee: u64,
|
|
|
|
) -> Transaction {
|
2019-03-09 19:28:43 -08:00
|
|
|
let assign = SystemInstruction::Assign {
|
|
|
|
program_id: *program_id,
|
|
|
|
};
|
2019-03-15 10:02:28 -06:00
|
|
|
Transaction::new_signed(
|
2018-09-26 10:07:53 -06:00
|
|
|
from_keypair,
|
|
|
|
&[],
|
2019-03-09 19:28:43 -08:00
|
|
|
&system_program::id(),
|
2018-11-06 06:50:00 -07:00
|
|
|
&assign,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash,
|
2018-09-26 10:07:53 -06:00
|
|
|
fee,
|
|
|
|
)
|
|
|
|
}
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::Move transaction
|
2019-02-01 08:36:35 -07:00
|
|
|
pub fn new_move(
|
2018-09-26 10:07:53 -06:00
|
|
|
from_keypair: &Keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
to: &Pubkey,
|
2019-03-05 16:28:14 -08:00
|
|
|
lamports: u64,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash: Hash,
|
2018-11-05 09:36:22 -07:00
|
|
|
fee: u64,
|
2019-02-01 08:36:35 -07:00
|
|
|
) -> Transaction {
|
2019-03-05 16:28:14 -08:00
|
|
|
let move_lamports = SystemInstruction::Move { lamports };
|
2019-03-15 10:02:28 -06:00
|
|
|
Transaction::new_signed(
|
2018-09-26 10:07:53 -06:00
|
|
|
from_keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
&[*to],
|
|
|
|
&system_program::id(),
|
2019-03-05 16:28:14 -08:00
|
|
|
&move_lamports,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash,
|
2018-09-26 10:07:53 -06:00
|
|
|
fee,
|
|
|
|
)
|
|
|
|
}
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::Move transaction to many destinations
|
2019-02-01 08:36:35 -07:00
|
|
|
pub fn new_move_many(
|
|
|
|
from: &Keypair,
|
|
|
|
moves: &[(Pubkey, u64)],
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash: Hash,
|
2019-02-01 08:36:35 -07:00
|
|
|
fee: u64,
|
|
|
|
) -> Transaction {
|
2018-10-05 16:45:27 -07:00
|
|
|
let instructions: Vec<_> = moves
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, (_, amount))| {
|
2019-03-05 16:28:14 -08:00
|
|
|
let spend = SystemInstruction::Move { lamports: *amount };
|
2019-03-15 09:47:25 -06:00
|
|
|
CompiledInstruction::new(0, &spend, vec![0, i as u8 + 1])
|
2018-12-07 20:01:28 -07:00
|
|
|
})
|
|
|
|
.collect();
|
2018-10-05 16:45:27 -07:00
|
|
|
let to_keys: Vec<_> = moves.iter().map(|(to_key, _)| *to_key).collect();
|
|
|
|
|
2019-03-15 10:02:28 -06:00
|
|
|
Transaction::new_with_compiled_instructions(
|
2018-10-26 14:43:34 -07:00
|
|
|
&[from],
|
2018-10-05 16:45:27 -07:00
|
|
|
&to_keys,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash,
|
2018-10-05 16:45:27 -07:00
|
|
|
fee,
|
2018-11-23 13:45:34 -07:00
|
|
|
vec![system_program::id()],
|
2018-10-05 16:45:27 -07:00
|
|
|
instructions,
|
|
|
|
)
|
|
|
|
}
|
2018-09-26 10:07:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-12-14 20:39:10 -08:00
|
|
|
use crate::signature::KeypairUtil;
|
2018-09-26 10:07:53 -06: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)];
|
|
|
|
|
2019-02-09 09:20:43 -08:00
|
|
|
let tx = SystemTransaction::new_move_many(&from, &moves, Hash::default(), 0);
|
2018-10-10 17:23:06 -07:00
|
|
|
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 10:07:53 -06:00
|
|
|
}
|