Skip to content

Commit

Permalink
save snapshot for slot 0
Browse files Browse the repository at this point in the history
  • Loading branch information
sambley committed May 9, 2019
1 parent 2d3da5d commit 285e481
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 50 deletions.
33 changes: 25 additions & 8 deletions core/src/bank_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ impl BankForks {
let descendants = self.descendants();
self.banks
.retain(|slot, _| descendants[&root].contains(slot));
let diff: HashSet<_> = slots.symmetric_difference(&self.slots).collect();
for slot in diff.iter() {
if **slot > root {
let _ = self.add_snapshot(**slot);
} else {
self.remove_snapshot(**slot);
if self.use_snapshot {
let diff: HashSet<_> = slots.symmetric_difference(&self.slots).collect();
for slot in diff.iter() {
if **slot > root {
let _ = self.add_snapshot(**slot, root);
} else if **slot > 0 {
self.remove_snapshot(**slot);
}
}
}
self.slots = slots.clone();
Expand All @@ -171,7 +173,7 @@ impl BankForks {
Path::new(&snapshot_dir).to_path_buf()
}

pub fn add_snapshot(&self, slot: u64) -> Result<(), Error> {
pub fn add_snapshot(&self, slot: u64, root: u64) -> Result<(), Error> {
let path = BankForks::get_snapshot_path();
fs::create_dir_all(path.clone())?;
let bank_file = format!("{}", slot);
Expand All @@ -189,6 +191,8 @@ impl BankForks {
.map_err(|_| BankForks::get_io_error("serialize bank parent error"))?;
serialize_into(&mut stream, &bank.rc)
.map_err(|_| BankForks::get_io_error("serialize bank rc error"))?;
serialize_into(&mut stream, &root)
.map_err(|_| BankForks::get_io_error("serialize root error"))?;
Ok(())
}

Expand Down Expand Up @@ -249,6 +253,7 @@ impl BankForks {
names.sort();
let mut bank_maps = vec![];
let mut bank_rc: Option<BankRc> = None;
let mut root: u64 = 0;
for bank_slot in names.iter().rev() {
let bank_path = format!("{}", bank_slot);
let bank_file_path = path.join(bank_path.clone());
Expand All @@ -265,6 +270,11 @@ impl BankForks {
.map_err(|_| BankForks::get_io_error("deserialize bank rc error"));
if rc.is_ok() {
bank_rc = Some(rc.unwrap());
let r: Result<u64, std::io::Error> = deserialize_from(&mut stream)
.map_err(|_| BankForks::get_io_error("deserialize root error"));
if r.is_ok() {
root = r.unwrap();
}
}
}
match bank {
Expand All @@ -281,10 +291,16 @@ impl BankForks {
Ok(BankForks {
banks,
working_bank,
root,
slots,
use_snapshot: true,
})
}

#[cfg(test)]
fn enable_snapshot(&mut self, enable: bool) {
self.use_snapshot = enable;
}
}

#[cfg(test)]
Expand Down Expand Up @@ -411,7 +427,7 @@ mod tests {

fn save_and_load_snapshot(bank_forks: &BankForks) {
for (slot, _) in bank_forks.banks.iter() {
bank_forks.add_snapshot(*slot).unwrap();
bank_forks.add_snapshot(*slot, 0).unwrap();
}

let new = BankForks::load_from_snapshot().unwrap();
Expand All @@ -433,6 +449,7 @@ mod tests {
let bank0 = Bank::new_with_paths(&genesis_block, Some(path.paths.clone()));
bank0.freeze();
let mut bank_forks = BankForks::new(0, bank0);
bank_forks.enable_snapshot(true);
for index in 0..10 {
let bank = Bank::new_from_parent(&bank_forks[index], &Pubkey::default(), index + 1);
let key1 = Keypair::new().pubkey();
Expand Down
3 changes: 3 additions & 0 deletions core/src/fullnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ pub fn new_banks_from_blocktree(

let (bank_forks, bank_forks_info, leader_schedule_cache) =
get_bank_forks(&genesis_block, &blocktree, account_paths, use_snapshot);
if use_snapshot {
let _ = bank_forks.add_snapshot(0, 0);
}

(
bank_forks,
Expand Down
25 changes: 22 additions & 3 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,27 @@ export RUST_BACKTRACE=1
dataDir=$PWD/target/"$(basename "$0" .sh)"

set -x
solana-keygen -o "$dataDir"/config/leader-keypair.json
solana-keygen -o "$dataDir"/config/leader-vote-account-keypair.json
solana-keygen -o "$dataDir"/config/leader-stake-account-keypair.json
leader_keypair="$dataDir/config/leader-keypair.json"
if [ -e "$leader_keypair" ]
then
echo "Use existing leader keypair"
else
solana-keygen -o "$leader_keypair"
fi
leader_vote_account_keypair="$dataDir/config/leader-vote-account-keypair.json"
if [ -e "$leader_vote_account_keypair" ]
then
echo "Use existing leader vote account keypair"
else
solana-keygen -o "$leader_vote_account_keypair"
fi
leader_stake_account_keypair="$dataDir/config/leader-stake-account-keypair.json"
if [ -e "$leader_stake_account_keypair" ]
then
echo "Use existing leader stake account keypair"
else
solana-keygen -o "$leader_stake_account_keypair"
fi
solana-keygen -o "$dataDir"/config/drone-keypair.json

leaderVoteAccountPubkey=$(\
Expand Down Expand Up @@ -70,6 +88,7 @@ args=(
--ledger "$dataDir"/ledger/
--rpc-port 8899
--rpc-drone-address 127.0.0.1:9900
--use-snapshot
)
if [[ -n $blockstreamSocket ]]; then
args+=(--blockstream "$blockstreamSocket")
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,9 @@ mod tests {
use solana_sdk::instruction::CompiledInstruction;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::transaction::Transaction;
use std::io::Cursor;
use std::thread::{sleep, Builder};
use std::time::{Duration, Instant};
use std::io::Cursor;

fn load_accounts_with_fee(
tx: Transaction,
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ impl AccountsDB {
}

pub fn generate_index(&mut self) {
let forks: HashSet<Fork> = self
let mut forks: Vec<Fork> = self
.storage
.read()
.unwrap()
Expand All @@ -507,6 +507,7 @@ impl AccountsDB {
.map(|x| x.fork_id)
.collect();

forks.sort();
for fork_id in forks.iter() {
let mut accumulator: Vec<HashMap<Pubkey, (u64, AccountInfo)>> = self
.scan_account_storage(
Expand All @@ -532,7 +533,7 @@ impl AccountsDB {
}
let mut accounts_index = self.accounts_index.write().unwrap();
for (pubkey, (_, account_info)) in account_maps.iter() {
accounts_index.insert(*fork_id, pubkey, account_info.clone());
accounts_index.add_index(*fork_id, pubkey, account_info.clone());
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions runtime/src/accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ impl<T: Clone> AccountsIndex<T> {
};
rv
}

pub fn add_index(&mut self, fork: Fork, pubkey: &Pubkey, account_info: T) {
let entry = self.account_maps.entry(*pubkey).or_insert_with(|| vec![]);
entry.push((fork, account_info));
}

pub fn is_purged(&self, fork: Fork) -> bool {
fork < self.last_root
}
Expand Down
37 changes: 1 addition & 36 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,8 +1186,8 @@ impl Drop for Bank {
mod tests {
use super::*;
use crate::genesis_utils::{create_genesis_block_with_leader, BOOTSTRAP_LEADER_LAMPORTS};
use solana_sdk::genesis_block::create_genesis_block;
use bincode::{deserialize_from, serialize_into, serialized_size};
use solana_sdk::genesis_block::create_genesis_block;
use solana_sdk::hash;
use solana_sdk::instruction::InstructionError;
use solana_sdk::signature::{Keypair, KeypairUtil};
Expand All @@ -1197,41 +1197,6 @@ mod tests {
use solana_vote_api::vote_state::VoteState;
use std::io::Cursor;

// 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)
}
>>>>>>> 639513c0... Be able to create bank snapshots

#[test]
fn test_bank_new() {
let dummy_leader_id = Pubkey::new_rand();
Expand Down

0 comments on commit 285e481

Please sign in to comment.