require to account signature (#6658)

* require to signature

* fixing invocation to create_account

* fix create_account references

* address review comment

* whacking bugs in tests

* fixing stake program tests
This commit is contained in:
Parth
2019-11-08 15:57:35 +05:30
committed by GitHub
parent f7b6e777bf
commit 5bd05fba09
29 changed files with 277 additions and 153 deletions

View File

@ -82,8 +82,7 @@ pub fn create_account(
}
/// Create a new payment script.
pub fn payment(from: &Pubkey, to: &Pubkey, lamports: u64) -> Vec<Instruction> {
let contract = Pubkey::new_rand();
pub fn payment(from: &Pubkey, to: &Pubkey, contract: &Pubkey, lamports: u64) -> Vec<Instruction> {
let expr = BudgetExpr::new_payment(lamports, to);
create_account(from, &contract, lamports, expr)
}
@ -181,7 +180,8 @@ mod tests {
fn test_budget_instruction_verify() {
let alice_pubkey = Pubkey::new_rand();
let bob_pubkey = Pubkey::new_rand();
payment(&alice_pubkey, &bob_pubkey, 1); // No panic! indicates success.
let budget_pubkey = Pubkey::new_rand();
payment(&alice_pubkey, &bob_pubkey, &budget_pubkey, 1); // No panic! indicates success.
}
#[test]

View File

@ -210,10 +210,13 @@ mod tests {
let bank_client = BankClient::new(bank);
let alice_pubkey = alice_keypair.pubkey();
let bob_pubkey = Pubkey::new_rand();
let instructions = budget_instruction::payment(&alice_pubkey, &bob_pubkey, 100);
let budget_keypair = Keypair::new();
let budget_pubkey = budget_keypair.pubkey();
let instructions =
budget_instruction::payment(&alice_pubkey, &bob_pubkey, &budget_pubkey, 100);
let message = Message::new(instructions);
bank_client
.send_message(&[&alice_keypair], message)
.send_message(&[&alice_keypair, &budget_keypair], message)
.unwrap();
assert_eq!(bank_client.get_balance(&bob_pubkey).unwrap(), 100);
}
@ -225,7 +228,8 @@ mod tests {
let alice_pubkey = alice_keypair.pubkey();
// Initialize BudgetState
let budget_pubkey = Pubkey::new_rand();
let budget_keypair = Keypair::new();
let budget_pubkey = budget_keypair.pubkey();
let bob_pubkey = Pubkey::new_rand();
let witness = Pubkey::new_rand();
let instructions = budget_instruction::when_signed(
@ -238,7 +242,7 @@ mod tests {
);
let message = Message::new(instructions);
bank_client
.send_message(&[&alice_keypair], message)
.send_message(&[&alice_keypair, &budget_keypair], message)
.unwrap();
// Attack! Part 1: Sign a witness transaction with a random key.
@ -273,7 +277,8 @@ mod tests {
let alice_pubkey = alice_keypair.pubkey();
// Initialize BudgetState
let budget_pubkey = Pubkey::new_rand();
let budget_keypair = Keypair::new();
let budget_pubkey = budget_keypair.pubkey();
let bob_pubkey = Pubkey::new_rand();
let dt = Utc::now();
let instructions = budget_instruction::on_date(
@ -287,7 +292,7 @@ mod tests {
);
let message = Message::new(instructions);
bank_client
.send_message(&[&alice_keypair], message)
.send_message(&[&alice_keypair, &budget_keypair], message)
.unwrap();
// Attack! Part 1: Sign a timestamp transaction with a random key.
@ -320,10 +325,12 @@ mod tests {
let (bank, alice_keypair) = create_bank(2);
let bank_client = BankClient::new(bank);
let alice_pubkey = alice_keypair.pubkey();
let budget_pubkey = Pubkey::new_rand();
let budget_keypair = Keypair::new();
let budget_pubkey = budget_keypair.pubkey();
let bob_pubkey = Pubkey::new_rand();
let mallory_pubkey = Pubkey::new_rand();
let dt = Utc::now();
let instructions = budget_instruction::on_date(
&alice_pubkey,
&bob_pubkey,
@ -335,7 +342,7 @@ mod tests {
);
let message = Message::new(instructions);
bank_client
.send_message(&[&alice_keypair], message)
.send_message(&[&alice_keypair, &budget_keypair], message)
.unwrap();
assert_eq!(bank_client.get_balance(&alice_pubkey).unwrap(), 1);
assert_eq!(bank_client.get_balance(&budget_pubkey).unwrap(), 1);
@ -389,7 +396,8 @@ mod tests {
let (bank, alice_keypair) = create_bank(3);
let bank_client = BankClient::new(bank);
let alice_pubkey = alice_keypair.pubkey();
let budget_pubkey = Pubkey::new_rand();
let budget_keypair = Keypair::new();
let budget_pubkey = budget_keypair.pubkey();
let bob_pubkey = Pubkey::new_rand();
let dt = Utc::now();
@ -404,7 +412,7 @@ mod tests {
);
let message = Message::new(instructions);
bank_client
.send_message(&[&alice_keypair], message)
.send_message(&[&alice_keypair, &budget_keypair], message)
.unwrap();
assert_eq!(bank_client.get_balance(&alice_pubkey).unwrap(), 2);
assert_eq!(bank_client.get_balance(&budget_pubkey).unwrap(), 1);
@ -461,7 +469,8 @@ mod tests {
let alice_pubkey = alice_keypair.pubkey();
let game_hash = hash(&[1, 2, 3]);
let budget_pubkey = Pubkey::new_rand();
let budget_keypair = Keypair::new();
let budget_pubkey = budget_keypair.pubkey();
let bob_keypair = Keypair::new();
let bob_pubkey = bob_keypair.pubkey();
@ -481,7 +490,7 @@ mod tests {
);
let message = Message::new(instructions);
bank_client
.send_message(&[&alice_keypair], message)
.send_message(&[&alice_keypair, &budget_keypair], message)
.unwrap();
assert_eq!(bank_client.get_balance(&alice_pubkey).unwrap(), 0);
assert_eq!(bank_client.get_balance(&budget_pubkey).unwrap(), 41);

View File

@ -371,10 +371,11 @@ fn test_config_updates_requiring_config() {
fn test_config_initialize_no_panic() {
let (bank, alice_keypair) = create_bank(3);
let bank_client = BankClient::new(bank);
let config_account = Keypair::new();
let mut instructions = config_instruction::create_account::<MyConfig>(
&alice_keypair.pubkey(),
&Pubkey::new_rand(),
&config_account.pubkey(),
1,
vec![],
);
@ -383,7 +384,7 @@ fn test_config_initialize_no_panic() {
let message = Message::new(instructions);
assert_eq!(
bank_client
.send_message(&[&alice_keypair], message)
.send_message(&[&alice_keypair, &config_account], message)
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(1, InstructionError::NotEnoughAccountKeys)

View File

@ -458,6 +458,7 @@ mod test {
use solana_runtime::bank_client::BankClient;
use solana_sdk::client::SyncClient;
use solana_sdk::genesis_block::create_genesis_block;
use solana_sdk::message::Message;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction;
use std::mem;
@ -557,18 +558,20 @@ mod test {
}
fn create_account(client: &BankClient, owner: &Keypair) -> Pubkey {
let new = Pubkey::new_rand();
let new = Keypair::new();
let instruction = system_instruction::create_account(
&owner.pubkey(),
&new,
&new.pubkey(),
1,
mem::size_of::<ExchangeState>() as u64,
&id(),
);
client
.send_instruction(&owner, instruction)
.send_message(&[owner, &new], Message::new(vec![instruction]))
.expect(&format!("{}:{}", line!(), file!()));
new
new.pubkey()
}
fn create_token_account(client: &BankClient, owner: &Keypair) -> Pubkey {

View File

@ -36,7 +36,13 @@ pub fn create_genesis<T: Client>(from_key: &Keypair, client: &T, amount: u64) ->
as u64,
&solana_sdk::move_loader::id(),
);
client.send_instruction(&from_key, instruction).unwrap();
client
.send_message(
&[&from_key, &libra_genesis_key],
Message::new(vec![instruction]),
)
.unwrap();
let instruction = librapay_instruction::genesis(&libra_genesis_key.pubkey(), amount);
let message = Message::new_with_payer(vec![instruction], Some(&from_key.pubkey()));

View File

@ -63,32 +63,35 @@ pub fn transfer(
pub fn create_accounts(
from: &Keypair,
tos: &[Pubkey],
to: &[&Keypair],
lamports: u64,
recent_blockhash: Hash,
) -> Transaction {
let instructions = tos
let instructions = to
.iter()
.map(|to| {
system_instruction::create_account(
&from.pubkey(),
to,
&to.pubkey(),
lamports,
400,
&solana_sdk::move_loader::id(),
)
})
.collect();
Transaction::new_signed_instructions(&[from], instructions, recent_blockhash)
let mut from_signers = vec![from];
from_signers.extend_from_slice(to);
Transaction::new_signed_instructions(&from_signers, instructions, recent_blockhash)
}
pub fn create_account(
from: &Keypair,
to: &Pubkey,
to: &Keypair,
lamports: u64,
recent_blockhash: Hash,
) -> Transaction {
create_accounts(from, &[*to], lamports, recent_blockhash)
create_accounts(from, &[to], lamports, recent_blockhash)
}
#[derive(Debug)]
@ -172,12 +175,7 @@ mod tests {
let from = Keypair::new();
let to = Keypair::new();
let tx = create_accounts(
&mint_keypair,
&[from.pubkey(), to.pubkey()],
1,
bank.last_blockhash(),
);
let tx = create_accounts(&mint_keypair, &[&from, &to], 1, bank.last_blockhash());
bank.process_transaction(&tx).unwrap();
info!(

View File

@ -92,7 +92,7 @@ fn test_stake_account_delegate() {
10,
));
bank_client
.send_message(&[&mint_keypair], message)
.send_message(&[&mint_keypair, &vote_keypair], message)
.expect("failed to create vote account");
let authorized = stake_state::Authorized::auto(&staker_pubkey);

View File

@ -56,8 +56,10 @@ fn test_instruction(
#[test]
fn test_account_owner() {
let account_owner = Pubkey::new_rand();
let validator_storage_pubkey = Pubkey::new_rand();
let archiver_storage_pubkey = Pubkey::new_rand();
let validator_storage_keypair = Keypair::new();
let validator_storage_pubkey = validator_storage_keypair.pubkey();
let archiver_storage_keypair = Keypair::new();
let archiver_storage_pubkey = archiver_storage_keypair.pubkey();
let GenesisBlockInfo {
genesis_block,
@ -78,7 +80,7 @@ fn test_account_owner() {
StorageAccountType::Validator,
));
bank_client
.send_message(&[&mint_keypair], message)
.send_message(&[&mint_keypair, &validator_storage_keypair], message)
.expect("failed to create account");
let account = bank
.get_account(&validator_storage_pubkey)
@ -98,7 +100,7 @@ fn test_account_owner() {
StorageAccountType::Archiver,
));
bank_client
.send_message(&[&mint_keypair], message)
.send_message(&[&mint_keypair, &archiver_storage_keypair], message)
.expect("failed to create account");
let account = bank
.get_account(&archiver_storage_pubkey)
@ -290,8 +292,8 @@ fn test_validate_mining() {
&owner_pubkey,
&bank_client,
&mint_keypair,
&[&validator_storage_id],
&[&archiver_1_storage_id, &archiver_2_storage_id],
&[&validator_storage_keypair],
&[&archiver_1_storage_keypair, &archiver_2_storage_keypair],
10,
);
@ -465,19 +467,21 @@ fn init_storage_accounts(
owner: &Pubkey,
client: &BankClient,
mint: &Keypair,
validator_accounts_to_create: &[&Pubkey],
archiver_accounts_to_create: &[&Pubkey],
validator_accounts_to_create: &[&Keypair],
archiver_accounts_to_create: &[&Keypair],
lamports: u64,
) {
let mut signers = vec![mint];
let mut ixs: Vec<_> = vec![system_instruction::transfer(&mint.pubkey(), owner, 1)];
ixs.append(
&mut validator_accounts_to_create
.into_iter()
.flat_map(|account| {
signers.push(&account);
storage_instruction::create_storage_account(
&mint.pubkey(),
owner,
account,
&account.pubkey(),
lamports,
StorageAccountType::Validator,
)
@ -485,16 +489,17 @@ fn init_storage_accounts(
.collect(),
);
archiver_accounts_to_create.into_iter().for_each(|account| {
signers.push(&account);
ixs.append(&mut storage_instruction::create_storage_account(
&mint.pubkey(),
owner,
account,
&account.pubkey(),
lamports,
StorageAccountType::Archiver,
))
});
let message = Message::new(ixs);
client.send_message(&[mint], message).unwrap();
client.send_message(&signers, message).unwrap();
}
fn get_storage_segment<C: SyncClient>(client: &C, account: &Pubkey) -> u64 {
@ -594,7 +599,9 @@ fn test_bank_storage() {
11,
StorageAccountType::Archiver,
));
bank_client.send_message(&[&mint_keypair], message).unwrap();
bank_client
.send_message(&[&mint_keypair, &archiver_keypair], message)
.unwrap();
let message = Message::new(storage_instruction::create_storage_account(
&mint_pubkey,
@ -603,7 +610,9 @@ fn test_bank_storage() {
1,
StorageAccountType::Validator,
));
bank_client.send_message(&[&mint_keypair], message).unwrap();
bank_client
.send_message(&[&mint_keypair, &validator_keypair], message)
.unwrap();
let message = Message::new_with_payer(
vec![storage_instruction::advertise_recent_blockhash(

View File

@ -192,7 +192,7 @@ mod tests {
fn create_vest_account(
bank_client: &BankClient,
contract_pubkey: &Pubkey,
contract_keypair: &Keypair,
payer_keypair: &Keypair,
terminator_pubkey: &Pubkey,
payee_pubkey: &Pubkey,
@ -203,14 +203,14 @@ mod tests {
let instructions = vest_instruction::create_account(
&payer_keypair.pubkey(),
&terminator_pubkey,
&contract_pubkey,
&contract_keypair.pubkey(),
&payee_pubkey,
start_date,
&date_pubkey,
lamports,
);
let message = Message::new(instructions);
bank_client.send_message(&[&payer_keypair], message)
bank_client.send_message(&[&payer_keypair, &contract_keypair], message)
}
fn send_set_terminator(
@ -327,10 +327,12 @@ mod tests {
fn test_initialize_no_panic() {
let (bank_client, alice_keypair) = create_bank_client(3);
let contract_keypair = Keypair::new();
let mut instructions = vest_instruction::create_account(
&alice_keypair.pubkey(),
&Pubkey::new_rand(),
&Pubkey::new_rand(),
&contract_keypair.pubkey(),
&Pubkey::new_rand(),
Utc::now().date(),
&Pubkey::new_rand(),
@ -341,7 +343,7 @@ mod tests {
let message = Message::new(instructions);
assert_eq!(
bank_client
.send_message(&[&alice_keypair], message)
.send_message(&[&alice_keypair, &contract_keypair], message)
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(1, InstructionError::NotEnoughAccountKeys)
@ -352,14 +354,15 @@ mod tests {
let (bank_client, alice_keypair) = create_bank_client(39);
let alice_pubkey = alice_keypair.pubkey();
let date_pubkey = Pubkey::new_rand();
let contract_pubkey = Pubkey::new_rand();
let contract_keypair = Keypair::new();
let contract_pubkey = contract_keypair.pubkey();
let bob_keypair = Keypair::new();
let bob_pubkey = bob_keypair.pubkey();
let start_date = Utc.ymd(2018, 1, 1);
create_vest_account(
&bank_client,
&contract_pubkey,
&contract_keypair,
&alice_keypair,
&alice_pubkey,
&bob_pubkey,
@ -422,14 +425,15 @@ mod tests {
let (bank_client, alice_keypair) = create_bank_client(38);
let alice_pubkey = alice_keypair.pubkey();
let date_pubkey = Pubkey::new_rand();
let contract_pubkey = Pubkey::new_rand();
let contract_keypair = Keypair::new();
let contract_pubkey = contract_keypair.pubkey();
let bob_keypair = Keypair::new();
let bob_pubkey = bob_keypair.pubkey();
let start_date = Utc.ymd(2018, 1, 1);
create_vest_account(
&bank_client,
&contract_pubkey,
&contract_keypair,
&alice_keypair,
&alice_pubkey,
&bob_pubkey,
@ -481,13 +485,14 @@ mod tests {
let current_date = Utc.ymd(2019, 1, 1);
create_date_account(&bank_client, &date_keypair, &alice_keypair, current_date).unwrap();
let contract_pubkey = Pubkey::new_rand();
let contract_keypair = Keypair::new();
let contract_pubkey = contract_keypair.pubkey();
let bob_pubkey = Pubkey::new_rand();
let start_date = Utc.ymd(2018, 1, 1);
create_vest_account(
&bank_client,
&contract_pubkey,
&contract_keypair,
&alice_keypair,
&alice_pubkey,
&bob_pubkey,
@ -542,7 +547,8 @@ mod tests {
fn test_terminate_and_refund() {
let (bank_client, alice_keypair) = create_bank_client(3);
let alice_pubkey = alice_keypair.pubkey();
let contract_pubkey = Pubkey::new_rand();
let contract_keypair = Keypair::new();
let contract_pubkey = contract_keypair.pubkey();
let bob_pubkey = Pubkey::new_rand();
let start_date = Utc::now().date();
@ -554,7 +560,7 @@ mod tests {
create_vest_account(
&bank_client,
&contract_pubkey,
&contract_keypair,
&alice_keypair,
&alice_pubkey,
&bob_pubkey,
@ -585,7 +591,8 @@ mod tests {
fn test_terminate_and_send_funds() {
let (bank_client, alice_keypair) = create_bank_client(3);
let alice_pubkey = alice_keypair.pubkey();
let contract_pubkey = Pubkey::new_rand();
let contract_keypair = Keypair::new();
let contract_pubkey = contract_keypair.pubkey();
let bob_pubkey = Pubkey::new_rand();
let start_date = Utc::now().date();
@ -597,7 +604,7 @@ mod tests {
create_vest_account(
&bank_client,
&contract_pubkey,
&contract_keypair,
&alice_keypair,
&alice_pubkey,
&bob_pubkey,