Merge vote new and register transactions
This commit is contained in:
parent
5c71f2a439
commit
fa359c6fc4
|
@ -169,7 +169,7 @@ fn main() {
|
||||||
loop {
|
loop {
|
||||||
let last_id = client.get_last_id();
|
let last_id = client.get_last_id();
|
||||||
let transaction =
|
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() {
|
if client.transfer_signed(&transaction).is_err() {
|
||||||
sleep(Duration::from_secs(2));
|
sleep(Duration::from_secs(2));
|
||||||
continue;
|
continue;
|
||||||
|
@ -183,26 +183,16 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the vote account to this node
|
|
||||||
loop {
|
loop {
|
||||||
let last_id = client.get_last_id();
|
let vote_account_user_data = client.get_account_userdata(&vote_account_id);
|
||||||
let transaction =
|
if let Ok(Some(vote_account_user_data)) = vote_account_user_data {
|
||||||
VoteTransaction::vote_account_register(&keypair, vote_account_id, last_id, 0);
|
if let Ok(vote_state) = VoteProgram::deserialize(&vote_account_user_data) {
|
||||||
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) {
|
|
||||||
if vote_state.node_id == pubkey {
|
if vote_state.node_id == pubkey {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
panic!("Expected successful vote account registration");
|
||||||
sleep(Duration::from_secs(2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
|
|
@ -468,38 +468,25 @@ pub fn make_active_set_entries(
|
||||||
) -> (Vec<Entry>, Keypair) {
|
) -> (Vec<Entry>, Keypair) {
|
||||||
// 1) Create transfer token entry
|
// 1) Create transfer token entry
|
||||||
let transfer_tx =
|
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 transfer_entry = Entry::new(last_entry_id, 1, vec![transfer_tx]);
|
||||||
let mut last_entry_id = transfer_entry.id;
|
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 vote_account = Keypair::new();
|
||||||
let create_vote_account_tx =
|
let new_vote_account_tx =
|
||||||
Transaction::vote_account_new(active_keypair, vote_account.pubkey(), *last_tick_id, 1);
|
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]);
|
// 3) Create vote entry
|
||||||
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
|
|
||||||
let vote = Vote { tick_height: 1 };
|
let vote = Vote { tick_height: 1 };
|
||||||
let vote_tx = Transaction::vote_new(&vote_account, vote, *last_tick_id, 0);
|
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]);
|
let vote_entry = Entry::new(&last_entry_id, 1, vec![vote_tx]);
|
||||||
last_entry_id = vote_entry.id;
|
last_entry_id = vote_entry.id;
|
||||||
|
|
||||||
// 5) Create the ending empty ticks
|
// 4) Create the ending empty ticks
|
||||||
let mut txs = vec![
|
let mut txs = vec![transfer_entry, new_vote_account_entry, vote_entry];
|
||||||
transfer_entry,
|
|
||||||
create_vote_account_entry,
|
|
||||||
register_vote_account_entry,
|
|
||||||
vote_entry,
|
|
||||||
];
|
|
||||||
let empty_ticks = create_ticks(num_ending_ticks, last_entry_id);
|
let empty_ticks = create_ticks(num_ending_ticks, last_entry_id);
|
||||||
txs.extend(empty_ticks);
|
txs.extend(empty_ticks);
|
||||||
(txs, vote_account)
|
(txs, vote_account)
|
||||||
|
|
|
@ -642,13 +642,13 @@ mod tests {
|
||||||
|
|
||||||
assert!(client.poll_for_signature(&signature).is_ok());
|
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 validator_vote_account_keypair = Keypair::new();
|
||||||
let vote_account_id = validator_vote_account_keypair.pubkey();
|
let vote_account_id = validator_vote_account_keypair.pubkey();
|
||||||
let last_id = client.get_last_id();
|
let last_id = client.get_last_id();
|
||||||
|
|
||||||
let transaction =
|
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();
|
let signature = client.transfer_signed(&transaction).unwrap();
|
||||||
assert!(client.poll_for_signature(&signature).is_ok());
|
assert!(client.poll_for_signature(&signature).is_ok());
|
||||||
|
|
||||||
|
@ -656,13 +656,6 @@ mod tests {
|
||||||
.expect("Expected balance for new account to exist");
|
.expect("Expected balance for new account to exist");
|
||||||
assert_eq!(balance, 1);
|
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;
|
const LAST: usize = 30;
|
||||||
for run in 0..=LAST {
|
for run in 0..=LAST {
|
||||||
println!("Checking for account registered: {}", run);
|
println!("Checking for account registered: {}", run);
|
||||||
|
|
|
@ -10,24 +10,21 @@ use signature::Keypair;
|
||||||
use signature::KeypairUtil;
|
use signature::KeypairUtil;
|
||||||
use solana_sdk::hash::Hash;
|
use solana_sdk::hash::Hash;
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use system_transaction::SystemTransaction;
|
use solana_sdk::system_instruction::SystemInstruction;
|
||||||
use transaction::Transaction;
|
use system_program;
|
||||||
|
use transaction::{Instruction, Transaction};
|
||||||
use vote_program::{self, Vote, VoteInstruction};
|
use vote_program::{self, Vote, VoteInstruction};
|
||||||
|
|
||||||
pub trait VoteTransaction {
|
pub trait VoteTransaction {
|
||||||
fn vote_new(vote_account: &Keypair, vote: Vote, last_id: Hash, fee: u64) -> Self;
|
fn vote_new(vote_account: &Keypair, vote: Vote, last_id: Hash, fee: u64) -> Self;
|
||||||
fn vote_account_new(
|
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,
|
validator_id: &Keypair,
|
||||||
vote_account_id: Pubkey,
|
vote_account_id: Pubkey,
|
||||||
last_id: Hash,
|
last_id: Hash,
|
||||||
|
num_tokens: u64,
|
||||||
fee: u64,
|
fee: u64,
|
||||||
) -> Self;
|
) -> Self;
|
||||||
|
|
||||||
fn get_votes(&self) -> Vec<(Pubkey, Vote, Hash)>;
|
fn get_votes(&self) -> Vec<(Pubkey, Vote, Hash)>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,36 +42,30 @@ impl VoteTransaction for Transaction {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn vote_account_new(
|
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,
|
validator_id: &Keypair,
|
||||||
vote_account_id: Pubkey,
|
vote_account_id: Pubkey,
|
||||||
last_id: Hash,
|
last_id: Hash,
|
||||||
|
num_tokens: u64,
|
||||||
fee: u64,
|
fee: u64,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let register_tx = VoteInstruction::RegisterAccount;
|
Transaction::new_with_instructions(
|
||||||
Transaction::new(
|
&[validator_id],
|
||||||
validator_id,
|
|
||||||
&[vote_account_id],
|
&[vote_account_id],
|
||||||
vote_program::id(),
|
|
||||||
®ister_tx,
|
|
||||||
last_id,
|
last_id,
|
||||||
fee,
|
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> {
|
) -> Result<Keypair> {
|
||||||
let new_vote_account = Keypair::new();
|
let new_vote_account = Keypair::new();
|
||||||
|
|
||||||
// Create the new vote account
|
// Create and register the new vote account
|
||||||
let tx =
|
let tx = Transaction::vote_account_new(
|
||||||
Transaction::vote_account_new(node_keypair, new_vote_account.pubkey(), last_id, num_tokens);
|
node_keypair,
|
||||||
bank.process_transaction(&tx)?;
|
new_vote_account.pubkey(),
|
||||||
|
last_id,
|
||||||
// Register the vote account to the validator
|
num_tokens,
|
||||||
let tx =
|
0,
|
||||||
Transaction::vote_account_register(node_keypair, new_vote_account.pubkey(), last_id, 0);
|
);
|
||||||
bank.process_transaction(&tx)?;
|
bank.process_transaction(&tx)?;
|
||||||
|
|
||||||
Ok(new_vote_account)
|
Ok(new_vote_account)
|
||||||
|
|
Loading…
Reference in New Issue