2019-03-13 14:37:24 -06:00
|
|
|
use solana_runtime::bank::Bank;
|
2019-03-05 00:14:51 -07:00
|
|
|
use solana_sdk::genesis_block::GenesisBlock;
|
2019-03-05 13:11:20 -07:00
|
|
|
use solana_sdk::native_program::ProgramError;
|
2019-03-05 00:14:51 -07:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2019-03-05 13:11:20 -07:00
|
|
|
use solana_sdk::system_instruction::SystemInstruction;
|
2019-03-05 00:14:51 -07:00
|
|
|
use solana_sdk::system_program;
|
2019-03-14 17:22:56 -06:00
|
|
|
use solana_sdk::transaction::{InstructionError, Transaction, TransactionError};
|
2019-03-05 13:11:20 -07:00
|
|
|
use solana_sdk::transaction_builder::{BuilderInstruction, TransactionBuilder};
|
2019-03-05 00:14:51 -07:00
|
|
|
|
2019-03-14 17:22:56 -06:00
|
|
|
struct SystemClient<'a> {
|
2019-03-05 00:14:51 -07:00
|
|
|
bank: &'a Bank,
|
2019-03-14 17:22:56 -06:00
|
|
|
keypair: Keypair,
|
2019-03-05 00:14:51 -07:00
|
|
|
}
|
|
|
|
|
2019-03-14 17:22:56 -06:00
|
|
|
impl<'a> SystemClient<'a> {
|
|
|
|
fn new(bank: &'a Bank, keypair: Keypair) -> Self {
|
2019-03-05 00:14:51 -07:00
|
|
|
bank.add_native_program("solana_system_program", &system_program::id());
|
2019-03-14 17:22:56 -06:00
|
|
|
Self { bank, keypair }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_transaction(&self, mut tx: Transaction) -> Result<(), TransactionError> {
|
|
|
|
tx.sign_unchecked(&[&self.keypair], self.bank.last_blockhash());
|
|
|
|
self.bank.process_transaction(&tx)
|
2019-03-05 00:14:51 -07:00
|
|
|
}
|
|
|
|
}
|
2019-03-14 17:22:56 -06:00
|
|
|
|
2019-03-05 00:14:51 -07:00
|
|
|
#[test]
|
2019-03-05 13:11:20 -07:00
|
|
|
fn test_system_unsigned_transaction() {
|
|
|
|
let (genesis_block, from_keypair) = GenesisBlock::new(100);
|
2019-03-05 00:14:51 -07:00
|
|
|
let bank = Bank::new(&genesis_block);
|
2019-03-14 17:22:56 -06:00
|
|
|
let from_pubkey = from_keypair.pubkey();
|
|
|
|
let alice_client = SystemClient::new(&bank, from_keypair);
|
2019-03-05 00:14:51 -07:00
|
|
|
|
2019-03-05 13:11:20 -07:00
|
|
|
let to_keypair = Keypair::new();
|
2019-03-14 17:22:56 -06:00
|
|
|
let to_pubkey = Keypair::new().pubkey();
|
|
|
|
let mallory_client = SystemClient::new(&bank, to_keypair);
|
|
|
|
|
|
|
|
// Fund to account to bypass AccountNotFound error
|
|
|
|
let ix = SystemInstruction::new_move(&from_pubkey, &to_pubkey, 50);
|
|
|
|
let tx = TransactionBuilder::new_singleton(ix);
|
|
|
|
alice_client.process_transaction(tx).unwrap();
|
2019-03-05 00:14:51 -07:00
|
|
|
|
2019-03-05 13:11:20 -07:00
|
|
|
// Erroneously sign transaction with recipient account key
|
|
|
|
// No signature case is tested by bank `test_zero_signatures()`
|
2019-03-14 17:22:56 -06:00
|
|
|
let ix = BuilderInstruction::new(
|
|
|
|
system_program::id(),
|
|
|
|
&SystemInstruction::Move { lamports: 10 },
|
|
|
|
vec![(from_pubkey, false), (to_pubkey, true)],
|
|
|
|
);
|
|
|
|
let tx = TransactionBuilder::new_singleton(ix);
|
2019-03-05 00:14:51 -07:00
|
|
|
assert_eq!(
|
2019-03-14 17:22:56 -06:00
|
|
|
mallory_client.process_transaction(tx),
|
2019-03-13 13:58:44 -06:00
|
|
|
Err(TransactionError::InstructionError(
|
2019-03-13 11:46:49 -06:00
|
|
|
0,
|
2019-03-13 12:48:11 -06:00
|
|
|
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
2019-03-13 11:46:49 -06:00
|
|
|
))
|
2019-03-05 00:14:51 -07:00
|
|
|
);
|
2019-03-14 17:22:56 -06:00
|
|
|
assert_eq!(bank.get_balance(&from_pubkey), 50);
|
|
|
|
assert_eq!(bank.get_balance(&to_pubkey), 50);
|
2019-03-05 00:14:51 -07:00
|
|
|
}
|