Introducing Scripts

A sequence of instructions. A client compiles the script and then uses
the compiled script to construction a transaction. Then it adds a
adds a blockhash, signs the transaction, and sends it off for
processing.
This commit is contained in:
Greg Fitzgerald
2019-03-16 14:30:10 -06:00
committed by Grimes
parent 55cdbedb52
commit ae4d14a2ad
6 changed files with 268 additions and 178 deletions

View File

@ -1,6 +1,8 @@
use crate::bank::Bank;
use solana_sdk::signature::Keypair;
use solana_sdk::transaction::{Transaction, TransactionError};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::transaction::{Script, Transaction, TransactionError};
pub struct BankClient<'a> {
bank: &'a Bank,
@ -12,8 +14,23 @@ impl<'a> BankClient<'a> {
Self { bank, keypair }
}
pub fn process_transaction(&self, tx: &mut Transaction) -> Result<(), TransactionError> {
pub fn pubkey(&self) -> Pubkey {
self.keypair.pubkey()
}
pub fn process_transaction(&self, mut tx: Transaction) -> Result<(), TransactionError> {
tx.sign(&[&self.keypair], self.bank.last_blockhash());
self.bank.process_transaction(tx)
self.bank.process_transaction(&mut tx)
}
/// Create and process a transaction.
pub fn process_script(&self, script: Script) -> Result<(), TransactionError> {
self.process_transaction(Transaction::new(script))
}
/// Transfer lamports to pubkey
pub fn transfer(&self, lamports: u64, pubkey: &Pubkey) -> Result<(), TransactionError> {
let move_instruction = SystemInstruction::new_move(&self.pubkey(), pubkey, lamports);
self.process_script(vec![move_instruction])
}
}

View File

@ -5,39 +5,36 @@ use solana_sdk::native_program::ProgramError;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::system_program;
use solana_sdk::transaction::{Instruction, InstructionError, Transaction, TransactionError};
use solana_sdk::transaction::{Instruction, InstructionError, TransactionError};
#[test]
fn test_system_unsigned_transaction() {
let (genesis_block, from_keypair) = GenesisBlock::new(100);
let (genesis_block, mint_keypair) = GenesisBlock::new(100);
let bank = Bank::new(&genesis_block);
let from_pubkey = from_keypair.pubkey();
let alice_client = BankClient::new(&bank, from_keypair);
let to_keypair = Keypair::new();
let to_pubkey = to_keypair.pubkey();
let mallory_client = BankClient::new(&bank, to_keypair);
let alice_client = BankClient::new(&bank, mint_keypair);
let alice_pubkey = alice_client.pubkey();
let mallory_client = BankClient::new(&bank, Keypair::new());
let mallory_pubkey = mallory_client.pubkey();
// Fund to account to bypass AccountNotFound error
let ix = SystemInstruction::new_move(&from_pubkey, &to_pubkey, 50);
let mut tx = Transaction::new(vec![ix]);
alice_client.process_transaction(&mut tx).unwrap();
alice_client.transfer(50, &mallory_pubkey).unwrap();
// Erroneously sign transaction with recipient account key
// No signature case is tested by bank `test_zero_signatures()`
let ix = Instruction::new(
let malicious_script = vec![Instruction::new(
system_program::id(),
&SystemInstruction::Move { lamports: 10 },
vec![(from_pubkey, false), (to_pubkey, true)],
);
let mut tx = Transaction::new(vec![ix]);
vec![(alice_pubkey, false), (mallory_pubkey, true)],
)];
assert_eq!(
mallory_client.process_transaction(&mut tx),
mallory_client.process_script(malicious_script),
Err(TransactionError::InstructionError(
0,
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
))
);
assert_eq!(bank.get_balance(&from_pubkey), 50);
assert_eq!(bank.get_balance(&to_pubkey), 50);
assert_eq!(bank.get_balance(&alice_pubkey), 50);
assert_eq!(bank.get_balance(&mallory_pubkey), 50);
}