add passive staking to local_cluster (#4285)

* add passive staking to local_cluster

* add stake_program to genesis

* use equal stakes in local_cluster tests
This commit is contained in:
Rob Walker 2019-05-15 12:15:31 -07:00 committed by GitHub
parent 916017ca2c
commit 628128b376
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 18 deletions

1
Cargo.lock generated
View File

@ -2632,6 +2632,7 @@ dependencies = [
"solana-noop-program 0.15.0", "solana-noop-program 0.15.0",
"solana-sdk 0.15.0", "solana-sdk 0.15.0",
"solana-stake-api 0.15.0", "solana-stake-api 0.15.0",
"solana-stake-program 0.15.0",
"solana-vote-api 0.15.0", "solana-vote-api 0.15.0",
"solana-vote-program 0.15.0", "solana-vote-program 0.15.0",
] ]

View File

@ -17,6 +17,7 @@ use solana_sdk::system_transaction;
use solana_sdk::timing::DEFAULT_SLOTS_PER_EPOCH; use solana_sdk::timing::DEFAULT_SLOTS_PER_EPOCH;
use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT; use solana_sdk::timing::DEFAULT_TICKS_PER_SLOT;
use solana_sdk::transaction::Transaction; use solana_sdk::transaction::Transaction;
use solana_stake_api::stake_instruction;
use solana_vote_api::vote_instruction; use solana_vote_api::vote_instruction;
use solana_vote_api::vote_state::VoteState; use solana_vote_api::vote_state::VoteState;
use std::collections::HashMap; use std::collections::HashMap;
@ -228,24 +229,23 @@ impl LocalCluster {
// setup as a listener // setup as a listener
info!("listener {} ", validator_pubkey,); info!("listener {} ", validator_pubkey,);
} else { } else {
assert!(stake > 2);
// Send each validator some lamports to vote // Send each validator some lamports to vote
let validator_balance = Self::transfer_with_client( let validator_balance = Self::transfer_with_client(
&client, &client,
&self.funding_keypair, &self.funding_keypair,
&validator_pubkey, &validator_pubkey,
stake, stake * 2 + 1,
); );
info!( info!(
"validator {} balance {}", "validator {} balance {}",
validator_pubkey, validator_balance validator_pubkey, validator_balance
); );
Self::create_and_fund_vote_account( Self::setup_vote_and_stake_accounts(
&client, &client,
&voting_keypair, &voting_keypair,
&validator_keypair, &validator_keypair,
stake - 1, stake,
) )
.unwrap(); .unwrap();
} }
@ -360,7 +360,7 @@ impl LocalCluster {
.expect("get balance") .expect("get balance")
} }
fn create_and_fund_vote_account( fn setup_vote_and_stake_accounts(
client: &ThinClient, client: &ThinClient,
vote_account: &Keypair, vote_account: &Keypair,
from_account: &Arc<Keypair>, from_account: &Arc<Keypair>,
@ -368,35 +368,76 @@ impl LocalCluster {
) -> Result<()> { ) -> Result<()> {
let vote_account_pubkey = vote_account.pubkey(); let vote_account_pubkey = vote_account.pubkey();
let node_id = from_account.pubkey(); let node_id = from_account.pubkey();
// Create the vote account if necessary // Create the vote account if necessary
if client.poll_get_balance(&vote_account_pubkey).unwrap_or(0) == 0 { if client.poll_get_balance(&vote_account_pubkey).unwrap_or(0) == 0 {
// 1) Create vote account // 1) Create vote account
let instructions = vote_instruction::create_account(
&from_account.pubkey(),
&vote_account_pubkey,
&node_id,
0,
amount,
);
let (blockhash, _fee_calculator) = client.get_recent_blockhash().unwrap();
let mut transaction = Transaction::new_signed_instructions( let mut transaction = Transaction::new_signed_instructions(
&[from_account.as_ref()], &[from_account.as_ref()],
instructions, vote_instruction::create_account(
blockhash, &from_account.pubkey(),
&vote_account_pubkey,
&node_id,
0,
amount,
),
client.get_recent_blockhash().unwrap().0,
);
dbg!(vote_account_pubkey);
client
.retry_transfer(&from_account, &mut transaction, 5)
.expect("fund vote");
client
.wait_for_balance(&vote_account_pubkey, Some(amount))
.expect("get balance");
let stake_account_keypair = Keypair::new();
let stake_account_pubkey = stake_account_keypair.pubkey();
dbg!(stake_account_pubkey);
let mut transaction = Transaction::new_signed_instructions(
&[from_account.as_ref()],
vec![stake_instruction::create_account(
&from_account.pubkey(),
&stake_account_pubkey,
amount,
)],
client.get_recent_blockhash().unwrap().0,
); );
client client
.retry_transfer(&from_account, &mut transaction, 5) .retry_transfer(&from_account, &mut transaction, 5)
.expect("client transfer"); .expect("fund stake");
client client
.wait_for_balance(&vote_account_pubkey, Some(amount)) .wait_for_balance(&stake_account_pubkey, Some(amount))
.expect("get balance"); .expect("get balance");
dbg!(amount);
let mut transaction = Transaction::new_signed_instructions(
&[from_account.as_ref(), &stake_account_keypair],
vec![stake_instruction::delegate_stake(
&from_account.pubkey(),
&stake_account_pubkey,
&vote_account_pubkey,
)],
client.get_recent_blockhash().unwrap().0,
);
client
.send_and_confirm_transaction(
&[from_account.as_ref(), &stake_account_keypair],
&mut transaction,
5,
0,
)
.expect("delegate stake");
dbg!("delegated");
} }
info!("Checking for vote account registration"); info!("Checking for vote account registration");
let vote_account_user_data = client.get_account_data(&vote_account_pubkey); let vote_account_user_data = client.get_account_data(&vote_account_pubkey);
if let Ok(Some(vote_account_user_data)) = vote_account_user_data { if let Ok(Some(vote_account_user_data)) = vote_account_user_data {
if let Ok(vote_state) = VoteState::deserialize(&vote_account_user_data) { if let Ok(vote_state) = VoteState::deserialize(&vote_account_user_data) {
if vote_state.node_id == node_id { if vote_state.node_id == node_id {
info!("vote account registered");
return Ok(()); return Ok(());
} }
} }

View File

@ -29,6 +29,7 @@ solana-sdk = { path = "../sdk", version = "0.15.0" }
solana-stake-api = { path = "../programs/stake_api", version = "0.15.0" } solana-stake-api = { path = "../programs/stake_api", version = "0.15.0" }
solana-vote-api = { path = "../programs/vote_api", version = "0.15.0" } solana-vote-api = { path = "../programs/vote_api", version = "0.15.0" }
solana-vote-program = { path = "../programs/vote_program", version = "0.15.0" } solana-vote-program = { path = "../programs/vote_program", version = "0.15.0" }
solana-stake-program = { path = "../programs/stake_program", version = "0.15.0" }
solana-noop-program = { path = "../programs/noop_program", version = "0.15.0" } solana-noop-program = { path = "../programs/noop_program", version = "0.15.0" }
[lib] [lib]

View File

@ -53,7 +53,7 @@ pub fn create_genesis_block_with_leader(
), ),
), ),
], ],
&[solana_vote_program!()], &[solana_vote_program!(), solana_stake_program!()],
); );
(genesis_block, mint_keypair, voting_keypair) (genesis_block, mint_keypair, voting_keypair)

View File

@ -21,5 +21,8 @@ extern crate solana_metrics;
#[macro_use] #[macro_use]
extern crate solana_vote_program; extern crate solana_vote_program;
#[macro_use]
extern crate solana_stake_program;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;