Skip to content

Commit

Permalink
merge_parents() => squash()
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-solana committed Feb 26, 2019
1 parent 6a61f25 commit fb9b069
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
14 changes: 7 additions & 7 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl AccountsDB {
}

/// become the root accountsDB
fn merge_parents<U>(&mut self, parents: &[U])
fn squash<U>(&mut self, parents: &[U])
where
U: Deref<Target = Self>,
{
Expand Down Expand Up @@ -407,8 +407,8 @@ impl Accounts {
}

/// accounts starts with an empty data structure for every child/fork
/// this merges all the parents/checkpoints
pub fn merge_parents<U>(&self, parents: &[U])
/// this function squashes all the parents into this instance
pub fn squash<U>(&self, parents: &[U])
where
U: Deref<Target = Self>,
{
Expand All @@ -419,7 +419,7 @@ impl Accounts {
.map(|obj| obj.accounts_db.read().unwrap())
.collect();

self.accounts_db.write().unwrap().merge_parents(&dbs);
self.accounts_db.write().unwrap().squash(&dbs);
}
}

Expand Down Expand Up @@ -832,7 +832,7 @@ mod tests {
}

#[test]
fn test_accounts_merge_parents() {
fn test_accounts_squash() {
let mut db0 = AccountsDB::default();
let key = Pubkey::default();
let account = Account::new(1, 0, key);
Expand All @@ -844,8 +844,8 @@ mod tests {
let mut db1 = AccountsDB::default();
db1.store(false, &key, &Account::new(0, 0, key));

// merge, which should whack key's account
db1.merge_parents(&[&db0]);
// squash, which should whack key's account
db1.squash(&[&db0]);

assert_eq!(AccountsDB::load(&[&db1], &key), None);
}
Expand Down
19 changes: 8 additions & 11 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,22 @@ impl Bank {
}
}

/// merge (i.e. pull) the parent's state up into this Bank,
/// squash the parent's state up into this Bank,
/// this Bank becomes a root
pub fn merge_parents(&self) {
pub fn squash(&self) {
self.freeze();

let parents = self.parents();
*self.parent.write().unwrap() = None;

let parent_accounts: Vec<_> = parents.iter().map(|b| &b.accounts).collect();
self.accounts.merge_parents(&parent_accounts);
self.accounts.squash(&parent_accounts);

let parent_caches: Vec<_> = parents
.iter()
.map(|b| b.status_cache.read().unwrap())
.collect();
self.status_cache
.write()
.unwrap()
.merge_parents(&parent_caches);
self.status_cache.write().unwrap().squash(&parent_caches);
}

/// Return the more recent checkpoint of this bank instance.
Expand Down Expand Up @@ -1472,15 +1469,15 @@ mod tests {
}

#[test]
fn test_bank_hash_internal_state_merge_parents() {
fn test_bank_hash_internal_state_squash() {
let bank0 = Arc::new(Bank::new(&GenesisBlock::new(10).0));
let bank1 = Bank::new_from_parent_and_id(&bank0, 1);

// no delta in bank1, hashes match
assert_eq!(bank0.hash_internal_state(), bank1.hash_internal_state());

// remove parent
bank1.merge_parents();
bank1.squash();
assert!(bank1.parents().is_empty());

// hash should still match
Expand All @@ -1489,7 +1486,7 @@ mod tests {

/// Verifies that last ids and accounts are correctly referenced from parent
#[test]
fn test_bank_merge_parents() {
fn test_bank_squash() {
let (genesis_block, mint_keypair) = GenesisBlock::new(2);
let key1 = Keypair::new();
let key2 = Keypair::new();
Expand Down Expand Up @@ -1526,7 +1523,7 @@ mod tests {
);

// works iteration 0, no-ops on iteration 1 and 2
bank.merge_parents();
bank.squash();
}
}

Expand Down
17 changes: 9 additions & 8 deletions runtime/src/status_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ impl<T: Clone> StatusCache<T> {
self.get_signature_status_merged(sig)
}

fn merge_parent_is_full(&mut self, parent: &Self) -> bool {
// flatten the parent and their merges into self.merges, limit
fn squash_parent_is_full(&mut self, parent: &Self) -> bool {
// flatten and squash the parent and its merges into self.merges,
// returns true if self is full

self.merges.push_back(StatusCache {
signatures: parent.signatures.clone(),
Expand All @@ -106,12 +107,12 @@ impl<T: Clone> StatusCache<T> {

/// copy the parents and parents' merges up to this instance, up to
/// MAX_CACHE_ENTRIES deep
pub fn merge_parents<U>(&mut self, parents: &[U])
pub fn squash<U>(&mut self, parents: &[U])
where
U: Deref<Target = Self>,
{
for parent in parents {
if self.merge_parent_is_full(parent) {
if self.squash_parent_is_full(parent) {
break;
}
}
Expand Down Expand Up @@ -233,7 +234,7 @@ mod tests {
}

#[test]
fn test_status_cache_merge_parents_has_signature() {
fn test_status_cache_squash_has_signature() {
let sig = Signature::default();
let last_id = hash(Hash::default().as_ref());
let mut first = BankStatusCache::new(&last_id);
Expand All @@ -247,15 +248,15 @@ mod tests {
let last_id = hash(last_id.as_ref());
let mut second = BankStatusCache::new(&last_id);

second.merge_parents(&[&first]);
second.squash(&[&first]);

assert_eq!(second.get_signature_status(&sig), Some(Ok(())));
assert!(second.has_signature(&sig));
}

#[test]
#[ignore] // takes a lot of time or RAM or both..
fn test_status_cache_merge_parents_overflow() {
fn test_status_cache_squash_overflow() {
let mut last_id = hash(Hash::default().as_ref());
let mut cache = BankStatusCache::new(&last_id);

Expand All @@ -281,7 +282,7 @@ mod tests {
assert!(root.has_signature(&sig));

// will overflow
cache.merge_parents(&parents_refs);
cache.squash(&parents_refs);

assert_eq!(cache.get_signature_status(&sig), None);
assert!(!cache.has_signature(&sig));
Expand Down

0 comments on commit fb9b069

Please sign in to comment.