reduce replicode, introduce passive staking support (#4207)

This commit is contained in:
Rob Walker 2019-05-07 22:22:43 -07:00 committed by GitHub
parent 7609a007c6
commit b49f8c0984
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 96 additions and 86 deletions

2
Cargo.lock generated
View File

@ -2208,6 +2208,7 @@ dependencies = [
"solana-netutil 0.15.0", "solana-netutil 0.15.0",
"solana-runtime 0.15.0", "solana-runtime 0.15.0",
"solana-sdk 0.15.0", "solana-sdk 0.15.0",
"solana-stake-api 0.15.0",
"solana-storage-api 0.15.0", "solana-storage-api 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",
@ -2612,6 +2613,7 @@ dependencies = [
"solana-logger 0.15.0", "solana-logger 0.15.0",
"solana-metrics 0.15.0", "solana-metrics 0.15.0",
"solana-sdk 0.15.0", "solana-sdk 0.15.0",
"solana-stake-api 0.15.0",
"solana-vote-api 0.15.0", "solana-vote-api 0.15.0",
] ]

View File

@ -56,6 +56,7 @@ solana-metrics = { path = "../metrics", version = "0.15.0" }
solana-netutil = { path = "../netutil", version = "0.15.0" } solana-netutil = { path = "../netutil", version = "0.15.0" }
solana-runtime = { path = "../runtime", version = "0.15.0" } solana-runtime = { path = "../runtime", version = "0.15.0" }
solana-sdk = { path = "../sdk", version = "0.15.0" } solana-sdk = { path = "../sdk", version = "0.15.0" }
solana-stake-api = { path = "../programs/stake_api", version = "0.15.0" }
solana-storage-api = { path = "../programs/storage_api", version = "0.15.0" } solana-storage-api = { path = "../programs/storage_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-signer = { path = "../vote-signer", version = "0.15.0" } solana-vote-signer = { path = "../vote-signer", version = "0.15.0" }

View File

@ -1,46 +1,12 @@
use solana_sdk::account::Account; pub use solana_runtime::genesis_utils::{
create_genesis_block_with_leader, BOOTSTRAP_LEADER_LAMPORTS,
};
use solana_sdk::genesis_block::GenesisBlock; use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::Keypair;
use solana_sdk::system_program;
use solana_vote_api::vote_state;
// The default stake placed with the bootstrap leader // same as genesis_block::create_genesis_block, but with bootstrap_leader staking logic
pub const BOOTSTRAP_LEADER_LAMPORTS: u64 = 42; // for the core crate tests
pub fn create_genesis_block_with_leader(
mint_lamports: u64,
leader_id: &Pubkey,
leader_stake_lamports: u64,
) -> (GenesisBlock, Keypair, Keypair) {
let mint_keypair = Keypair::new();
let voting_keypair = Keypair::new();
let genesis_block = GenesisBlock::new(
&leader_id,
&[
(
mint_keypair.pubkey(),
Account::new(mint_lamports, 0, &system_program::id()),
),
(
voting_keypair.pubkey(),
vote_state::create_bootstrap_leader_account(
&voting_keypair.pubkey(),
&leader_id,
0,
leader_stake_lamports,
),
),
],
&[],
);
(genesis_block, mint_keypair, voting_keypair)
}
// same as genesis_block::create_genesis_block, but with leader staking logic specific
// to this crate
pub fn create_genesis_block(mint_lamports: u64) -> (GenesisBlock, Keypair) { pub fn create_genesis_block(mint_lamports: u64) -> (GenesisBlock, Keypair) {
let (genesis_block, mint_keypair, _vote_keypair) = create_genesis_block_with_leader( let (genesis_block, mint_keypair, _vote_keypair) = create_genesis_block_with_leader(
mint_lamports, mint_lamports,

View File

@ -56,8 +56,10 @@ fn sort_stakes(stakes: &mut Vec<(Pubkey, u64)>) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::genesis_utils::{create_genesis_block_with_leader, BOOTSTRAP_LEADER_LAMPORTS};
use crate::staking_utils; use crate::staking_utils;
use solana_runtime::genesis_utils::{
create_genesis_block_with_leader, BOOTSTRAP_LEADER_LAMPORTS,
};
#[test] #[test]
fn test_leader_schedule_via_bank() { fn test_leader_schedule_via_bank() {

View File

@ -113,7 +113,8 @@ fn main() -> Result<(), Box<dyn error::Error>> {
&bootstrap_leader_keypair.pubkey(), &bootstrap_leader_keypair.pubkey(),
0, 0,
bootstrap_leader_lamports, bootstrap_leader_lamports,
), )
.0,
), ),
], ],
&[ &[

View File

@ -277,13 +277,15 @@ pub fn create_bootstrap_leader_account(
node_id: &Pubkey, node_id: &Pubkey,
commission: u32, commission: u32,
lamports: u64, lamports: u64,
) -> Account { ) -> (Account, VoteState) {
// Construct a vote account for the bootstrap_leader such that the leader_scheduler // Construct a vote account for the bootstrap_leader such that the leader_scheduler
// will be forced to select it as the leader for height 0 // will be forced to select it as the leader for height 0
let mut account = create_account(&vote_id, &node_id, commission, lamports); let mut vote_account = create_account(&vote_id, &node_id, commission, lamports);
vote(&vote_id, &mut account, &Vote::new(0)).unwrap(); vote(&vote_id, &mut vote_account, &Vote::new(0)).unwrap();
account
let vote_state = vote_account.state().expect("account.state()");
(vote_account, vote_state)
} }
// utility function, used by Bank, tests // utility function, used by Bank, tests
@ -332,6 +334,16 @@ mod tests {
) )
} }
#[test]
fn test_vote_create_bootstrap_leader_account() {
let vote_id = Pubkey::new_rand();
let (_vote_account, vote_state) =
vote_state::create_bootstrap_leader_account(&vote_id, &Pubkey::new_rand(), 0, 100);
assert_eq!(vote_state.votes.len(), 1);
assert_eq!(vote_state.votes[0], Lockout::new(&Vote::new(0)));
}
#[test] #[test]
fn test_vote_serialize() { fn test_vote_serialize() {
let mut buffer: Vec<u8> = vec![0; VoteState::size_of()]; let mut buffer: Vec<u8> = vec![0; VoteState::size_of()];

View File

@ -26,9 +26,9 @@ serde_json = "1.0.38"
solana-logger = { path = "../logger", version = "0.15.0" } solana-logger = { path = "../logger", version = "0.15.0" }
solana-metrics = { path = "../metrics", version = "0.15.0" } solana-metrics = { path = "../metrics", version = "0.15.0" }
solana-sdk = { path = "../sdk", version = "0.15.0" } solana-sdk = { path = "../sdk", 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" }
[lib] [lib]
name = "solana_runtime" name = "solana_runtime"
crate-type = ["lib"] crate-type = ["lib"]

View File

@ -1042,52 +1042,17 @@ impl Drop for Bank {
} }
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { mod tests {
use super::*; use super::*;
use crate::genesis_utils::{create_genesis_block_with_leader, BOOTSTRAP_LEADER_LAMPORTS};
use solana_sdk::genesis_block::create_genesis_block; use solana_sdk::genesis_block::create_genesis_block;
use solana_sdk::hash; use solana_sdk::hash;
use solana_sdk::instruction::InstructionError; use solana_sdk::instruction::InstructionError;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction; use solana_sdk::system_instruction;
use solana_sdk::system_program;
use solana_sdk::system_transaction; use solana_sdk::system_transaction;
use solana_vote_api::vote_instruction; use solana_vote_api::vote_instruction;
use solana_vote_api::vote_state::{self, VoteState}; use solana_vote_api::vote_state::VoteState;
// The default stake placed with the bootstrap leader
pub(crate) const BOOTSTRAP_LEADER_LAMPORTS: u64 = 42;
pub(crate) fn create_genesis_block_with_leader(
mint_lamports: u64,
leader_id: &Pubkey,
leader_stake_lamports: u64,
) -> (GenesisBlock, Keypair, Keypair) {
let mint_keypair = Keypair::new();
let voting_keypair = Keypair::new();
let genesis_block = GenesisBlock::new(
&leader_id,
&[
(
mint_keypair.pubkey(),
Account::new(mint_lamports, 0, &system_program::id()),
),
(
voting_keypair.pubkey(),
vote_state::create_bootstrap_leader_account(
&voting_keypair.pubkey(),
&leader_id,
0,
leader_stake_lamports,
),
),
],
&[],
);
(genesis_block, mint_keypair, voting_keypair)
}
#[test] #[test]
fn test_bank_new() { fn test_bank_new() {

View File

@ -0,0 +1,60 @@
use solana_sdk::account::Account;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_program;
use solana_stake_api::stake_state;
use solana_vote_api::vote_state;
// The default stake placed with the bootstrap leader
pub const BOOTSTRAP_LEADER_LAMPORTS: u64 = 42;
pub fn create_genesis_block_with_leader(
mint_lamports: u64,
bootstrap_leader_id: &Pubkey,
bootstrap_leader_stake_lamports: u64,
) -> (GenesisBlock, Keypair, Keypair) {
let mint_keypair = Keypair::new();
let voting_keypair = Keypair::new();
let staking_keypair = Keypair::new();
// TODO: de-duplicate the stake once passive staking
// is fully implemented
let (vote_account, vote_state) = vote_state::create_bootstrap_leader_account(
&voting_keypair.pubkey(),
&bootstrap_leader_id,
0,
bootstrap_leader_stake_lamports,
);
let genesis_block = GenesisBlock::new(
&bootstrap_leader_id,
&[
// the mint
(
mint_keypair.pubkey(),
Account::new(mint_lamports, 0, &system_program::id()),
),
// node needs an account to issue votes from, this will require
// airdrops at some point to cover fees...
(
*bootstrap_leader_id,
Account::new(1, 0, &system_program::id()),
),
// where votes go to
(voting_keypair.pubkey(), vote_account),
// passive bootstrap leader stake, duplicates above temporarily
(
staking_keypair.pubkey(),
stake_state::create_delegate_stake_account(
&voting_keypair.pubkey(),
&vote_state,
bootstrap_leader_stake_lamports,
),
),
],
&[],
);
(genesis_block, mint_keypair, voting_keypair)
}

View File

@ -6,6 +6,7 @@ pub mod bank;
pub mod bank_client; pub mod bank_client;
mod blockhash_queue; mod blockhash_queue;
pub mod bloom; pub mod bloom;
pub mod genesis_utils;
pub mod loader_utils; pub mod loader_utils;
pub mod locked_accounts_results; pub mod locked_accounts_results;
pub mod message_processor; pub mod message_processor;

View File

@ -53,7 +53,7 @@ impl<'a, 'b, I: Borrow<Transaction>> Drop for LockedAccountsResults<'a, 'b, I> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::bank::tests::create_genesis_block_with_leader; use crate::genesis_utils::create_genesis_block_with_leader;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_transaction; use solana_sdk::system_transaction;