Merge vote new and register transactions

This commit is contained in:
Michael Vines 2018-11-28 20:10:30 -08:00
parent 5c71f2a439
commit fa359c6fc4
4 changed files with 45 additions and 84 deletions

View File

@ -169,7 +169,7 @@ fn main() {
loop {
let last_id = client.get_last_id();
let transaction =
VoteTransaction::vote_account_new(&keypair, vote_account_id, last_id, 1);
VoteTransaction::vote_account_new(&keypair, vote_account_id, last_id, 1, 1);
if client.transfer_signed(&transaction).is_err() {
sleep(Duration::from_secs(2));
continue;
@ -183,26 +183,16 @@ fn main() {
}
}
// Register the vote account to this node
loop {
let last_id = client.get_last_id();
let transaction =
VoteTransaction::vote_account_register(&keypair, vote_account_id, last_id, 0);
if client.transfer_signed(&transaction).is_err() {
sleep(Duration::from_secs(2));
continue;
}
let account_user_data = client.get_account_userdata(&vote_account_id);
if let Ok(Some(account_user_data)) = account_user_data {
if let Ok(vote_state) = VoteProgram::deserialize(&account_user_data) {
let vote_account_user_data = client.get_account_userdata(&vote_account_id);
if let Ok(Some(vote_account_user_data)) = vote_account_user_data {
if let Ok(vote_state) = VoteProgram::deserialize(&vote_account_user_data) {
if vote_state.node_id == pubkey {
break;
}
}
}
sleep(Duration::from_secs(2));
panic!("Expected successful vote account registration");
}
loop {

View File

@ -468,38 +468,25 @@ pub fn make_active_set_entries(
) -> (Vec<Entry>, Keypair) {
// 1) Create transfer token entry
let transfer_tx =
Transaction::system_new(&token_source, active_keypair.pubkey(), 2, *last_tick_id);
Transaction::system_new(&token_source, active_keypair.pubkey(), 3, *last_tick_id);
let transfer_entry = Entry::new(last_entry_id, 1, vec![transfer_tx]);
let mut last_entry_id = transfer_entry.id;
// 2) Create the vote account
// 2) Create and register the vote account
let vote_account = Keypair::new();
let create_vote_account_tx =
Transaction::vote_account_new(active_keypair, vote_account.pubkey(), *last_tick_id, 1);
let new_vote_account_tx =
Transaction::vote_account_new(active_keypair, vote_account.pubkey(), *last_tick_id, 1, 1);
let new_vote_account_entry = Entry::new(&last_entry_id, 1, vec![new_vote_account_tx]);
last_entry_id = new_vote_account_entry.id;
let create_vote_account_entry = Entry::new(&last_entry_id, 1, vec![create_vote_account_tx]);
last_entry_id = create_vote_account_entry.id;
// 3) Register the vote account
let register_vote_account_tx =
Transaction::vote_account_register(active_keypair, vote_account.pubkey(), *last_tick_id, 0);
let register_vote_account_entry = Entry::new(&last_entry_id, 1, vec![register_vote_account_tx]);
last_entry_id = register_vote_account_entry.id;
// 4) Create vote entry
// 3) Create vote entry
let vote = Vote { tick_height: 1 };
let vote_tx = Transaction::vote_new(&vote_account, vote, *last_tick_id, 0);
let vote_entry = Entry::new(&last_entry_id, 1, vec![vote_tx]);
last_entry_id = vote_entry.id;
// 5) Create the ending empty ticks
let mut txs = vec![
transfer_entry,
create_vote_account_entry,
register_vote_account_entry,
vote_entry,
];
// 4) Create the ending empty ticks
let mut txs = vec![transfer_entry, new_vote_account_entry, vote_entry];
let empty_ticks = create_ticks(num_ending_ticks, last_entry_id);
txs.extend(empty_ticks);
(txs, vote_account)

View File

@ -642,13 +642,13 @@ mod tests {
assert!(client.poll_for_signature(&signature).is_ok());
// Create the vote account
// Create and register the vote account
let validator_vote_account_keypair = Keypair::new();
let vote_account_id = validator_vote_account_keypair.pubkey();
let last_id = client.get_last_id();
let transaction =
VoteTransaction::vote_account_new(&validator_keypair, vote_account_id, last_id, 1);
VoteTransaction::vote_account_new(&validator_keypair, vote_account_id, last_id, 1, 1);
let signature = client.transfer_signed(&transaction).unwrap();
assert!(client.poll_for_signature(&signature).is_ok());
@ -656,13 +656,6 @@ mod tests {
.expect("Expected balance for new account to exist");
assert_eq!(balance, 1);
// Register the vote account to the validator
let last_id = client.get_last_id();
let transaction =
VoteTransaction::vote_account_register(&validator_keypair, vote_account_id, last_id, 0);
let signature = client.transfer_signed(&transaction).unwrap();
assert!(client.poll_for_signature(&signature).is_ok());
const LAST: usize = 30;
for run in 0..=LAST {
println!("Checking for account registered: {}", run);

View File

@ -10,24 +10,21 @@ use signature::Keypair;
use signature::KeypairUtil;
use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey;
use system_transaction::SystemTransaction;
use transaction::Transaction;
use solana_sdk::system_instruction::SystemInstruction;
use system_program;
use transaction::{Instruction, Transaction};
use vote_program::{self, Vote, VoteInstruction};
pub trait VoteTransaction {
fn vote_new(vote_account: &Keypair, vote: Vote, last_id: Hash, fee: u64) -> Self;
fn vote_account_new(
validator_id: &Keypair,
new_vote_account_id: Pubkey,
last_id: Hash,
num_tokens: u64,
) -> Self;
fn vote_account_register(
validator_id: &Keypair,
vote_account_id: Pubkey,
last_id: Hash,
num_tokens: u64,
fee: u64,
) -> Self;
fn get_votes(&self) -> Vec<(Pubkey, Vote, Hash)>;
}
@ -45,36 +42,30 @@ impl VoteTransaction for Transaction {
}
fn vote_account_new(
validator_id: &Keypair,
new_vote_account_id: Pubkey,
last_id: Hash,
num_tokens: u64,
) -> Self {
Transaction::system_create(
validator_id,
new_vote_account_id,
last_id,
num_tokens,
vote_program::get_max_size() as u64,
vote_program::id(),
0,
)
}
fn vote_account_register(
validator_id: &Keypair,
vote_account_id: Pubkey,
last_id: Hash,
num_tokens: u64,
fee: u64,
) -> Self {
let register_tx = VoteInstruction::RegisterAccount;
Transaction::new(
validator_id,
Transaction::new_with_instructions(
&[validator_id],
&[vote_account_id],
vote_program::id(),
&register_tx,
last_id,
fee,
vec![system_program::id(), vote_program::id()],
vec![
Instruction::new(
0,
&SystemInstruction::CreateAccount {
tokens: num_tokens,
space: vote_program::get_max_size() as u64,
program_id: vote_program::id(),
},
vec![0, 1],
),
Instruction::new(1, &VoteInstruction::RegisterAccount, vec![0, 1]),
],
)
}
@ -101,14 +92,14 @@ pub fn create_vote_account(
) -> Result<Keypair> {
let new_vote_account = Keypair::new();
// Create the new vote account
let tx =
Transaction::vote_account_new(node_keypair, new_vote_account.pubkey(), last_id, num_tokens);
bank.process_transaction(&tx)?;
// Register the vote account to the validator
let tx =
Transaction::vote_account_register(node_keypair, new_vote_account.pubkey(), last_id, 0);
// Create and register the new vote account
let tx = Transaction::vote_account_new(
node_keypair,
new_vote_account.pubkey(),
last_id,
num_tokens,
0,
);
bank.process_transaction(&tx)?;
Ok(new_vote_account)