Skip to content

Commit

Permalink
Remove rc feature dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sambley committed May 3, 2019
1 parent effb016 commit deb5108
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 93 deletions.
57 changes: 43 additions & 14 deletions core/src/bank_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl BankForks {
let bank_file_path = path.join(bank_file);
let file = File::create(bank_file_path)?;
let mut stream = BufWriter::new(file);
serialize_into(&mut stream, self.get(slot).unwrap())
serialize_into(&mut stream, &*self.get(slot).unwrap().clone())
.map_err(|_| BankForks::get_io_error("serialize bank error"))?;
Ok(())
}
Expand All @@ -185,6 +185,37 @@ impl BankForks {
self.use_snapshot = use_snapshot;
}

fn setup_banks(
bank_maps: &mut Vec<(u64, Bank)>,
) -> (HashMap<u64, Arc<Bank>>, HashSet<u64>, u64) {
let mut banks = HashMap::new();
let mut slots = HashSet::new();
let (last_slot, mut last_bank) = bank_maps.remove(0);
let accounts = last_bank.accounts();
let status_cache = last_bank.status_cache();

while let Some((slot, mut bank)) = bank_maps.pop() {
bank.set_accounts(&accounts);
bank.set_status_cache(&status_cache);
if bank.parent_slot() != 0 {
if let Some(parent) = banks.get(&bank.parent_slot()) {
bank.set_parent(parent);
}
}
banks.insert(slot, Arc::new(bank));
slots.insert(slot);
}
if last_bank.parent_slot() != 0 {
if let Some(parent) = banks.get(&last_bank.parent_slot()) {
last_bank.set_parent(parent);
}
}
banks.insert(last_slot, Arc::new(last_bank));
slots.insert(last_slot);

(banks, slots, last_slot)
}

pub fn load_from_snapshot() -> Result<Self, Error> {
let path = BankForks::get_snapshot_path();
let paths = fs::read_dir(path.clone())?;
Expand All @@ -199,9 +230,7 @@ impl BankForks {
.collect::<Vec<u64>>();

names.sort();
let mut banks: HashMap<u64, Arc<Bank>> = HashMap::new();
let mut slots = HashSet::new();
let mut last_slot: u64 = 0;
let mut bank_maps = vec![];
for bank_slot in names.clone() {
let bank_path = format!("{}", bank_slot);
let bank_file_path = path.join(bank_path.clone());
Expand All @@ -211,15 +240,15 @@ impl BankForks {
let bank: Result<Bank, std::io::Error> = deserialize_from(&mut stream)
.map_err(|_| BankForks::get_io_error("deserialize bank error"));
match bank {
Ok(v) => {
banks.insert(bank_slot, Arc::new(v));
slots.insert(bank_slot);
last_slot = bank_slot;
}
Ok(v) => bank_maps.insert(0, (bank_slot, v)),
Err(_) => warn!("Load snapshot failed for {}", bank_slot),
}
}
info!("last slot: {}", last_slot);
if bank_maps.is_empty() {
return Err(Error::new(ErrorKind::Other, "no snapshots loaded"));
}

let (banks, slots, last_slot) = BankForks::setup_banks(&mut bank_maps);
let working_bank = banks[&last_slot].clone();
Ok(BankForks {
banks,
Expand Down Expand Up @@ -354,11 +383,11 @@ mod tests {
}

fn save_and_load_snapshot(bank_forks: &BankForks) {
let bank = bank_forks.banks.get(&0).unwrap();
let bank = bank_forks.banks.get(&0).unwrap().clone();
let tick_height = bank.tick_height();
let bank_ser = serialize(&bank).unwrap();
let child_bank = bank_forks.banks.get(&1).unwrap();
let child_bank_ser = serialize(&child_bank).unwrap();
let bank_ser = serialize(&*bank).unwrap();
let child_bank = bank_forks.banks.get(&1).unwrap().clone();
let child_bank_ser = serialize(&*child_bank).unwrap();
for (slot, _) in bank_forks.banks.iter() {
bank_forks.add_snapshot(*slot).unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ log = "0.4.2"
memmap = "0.6.2"
rand = "0.6.5"
rayon = "1.0.0"
serde = { version = "1.0.88", features = ["rc"] }
serde = "1.0.88"
serde_derive = "1.0.88"
serde_json = "1.0.38"
solana-logger = { path = "../logger", version = "0.15.0" }
Expand Down
11 changes: 8 additions & 3 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type AccountLocks = (
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Accounts {
/// Single global AccountsDB
#[serde(skip)]
pub accounts_db: Arc<AccountsDB>,

/// set of accounts which are currently in the pipeline
Expand All @@ -60,7 +61,6 @@ impl Drop for Accounts {
if self.own_paths && (Arc::strong_count(&self.accounts_db) == 1) {
let paths = get_paths_vec(&self.paths);
paths.iter().for_each(|p| {
info!("drop remove {:?}", p);
let _ignored = remove_dir_all(p);

// it is safe to delete the parent
Expand Down Expand Up @@ -1054,12 +1054,17 @@ mod tests {
check_accounts(&accounts, &pubkeys, 100);
accounts.add_root(0);

let mut buf = vec![0u8; serialized_size(&accounts).unwrap() as usize];
let sz =
serialized_size(&accounts).unwrap() + serialized_size(&*accounts.accounts_db).unwrap();
let mut buf = vec![0u8; sz as usize];
let mut writer = Cursor::new(&mut buf[..]);
serialize_into(&mut writer, &accounts).unwrap();
serialize_into(&mut writer, &*accounts.accounts_db).unwrap();

let mut reader = Cursor::new(&mut buf[..]);
let daccounts: Accounts = deserialize_from(&mut reader).unwrap();
let mut daccounts: Accounts = deserialize_from(&mut reader).unwrap();
let accounts_db: AccountsDB = deserialize_from(&mut reader).unwrap();
daccounts.accounts_db = Arc::new(accounts_db);
check_accounts(&daccounts, &pubkeys, 100);
assert_eq!(
accounts.hash_internal_state(0),
Expand Down
Loading

0 comments on commit deb5108

Please sign in to comment.