Skip to content

Commit

Permalink
bug fix: reliable deterministic ordering of keys in wl_storage Prefix…
Browse files Browse the repository at this point in the history
…Iter that fixes apply_inflation bug
  • Loading branch information
brentstone authored and bengtlofgren committed May 11, 2023
1 parent 2593ad6 commit 51ac7c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
4 changes: 2 additions & 2 deletions core/src/ledger/storage/wl_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ where
write_log::StorageModification::Write { value }
| write_log::StorageModification::Temp { value } => {
let gas = value.len() as u64;
return Some((key.to_string(), value, gas));
return Some((key, value, gas));
}
write_log::StorageModification::InitAccount {
vp,
} => {
let gas = vp.len() as u64;
return Some((key.to_string(), vp, gas));
return Some((key, vp, gas));
}
write_log::StorageModification::Delete => {
continue;
Expand Down
29 changes: 15 additions & 14 deletions core/src/ledger/storage/write_log.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Write log is temporary storage for modifications performed by a transaction.
//! before they are committed to the ledger's storage.
use std::collections::{BTreeSet, HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use itertools::Itertools;
use thiserror::Error;
Expand Down Expand Up @@ -76,11 +76,12 @@ pub struct WriteLog {
#[derive(Debug)]
pub struct PrefixIter {
/// The concrete iterator for modifications sorted by storage keys
pub iter: std::vec::IntoIter<(storage::Key, StorageModification)>,
pub iter:
std::collections::btree_map::IntoIter<String, StorageModification>,
}

impl Iterator for PrefixIter {
type Item = (storage::Key, StorageModification);
type Item = (String, StorageModification);

fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
Expand Down Expand Up @@ -495,35 +496,35 @@ impl WriteLog {
/// Iterate modifications prior to the current transaction, whose storage
/// key matches the given prefix, sorted by their storage key.
pub fn iter_prefix_pre(&self, prefix: &storage::Key) -> PrefixIter {
let mut matches = HashMap::new();
let mut matches = BTreeMap::new();

for (key, modification) in &self.block_write_log {
if key.split_prefix(prefix).is_some() {
matches.insert(key.clone(), modification.clone());
matches.insert(key.to_string(), modification.clone());
}
}
let iter = matches
.into_iter()
.sorted_unstable_by_key(|(key, _val)| key.clone());

let iter = matches.into_iter();
PrefixIter { iter }
}

/// Iterate modifications posterior of the current tx, whose storage key
/// matches the given prefix, sorted by their storage key.
pub fn iter_prefix_post(&self, prefix: &storage::Key) -> PrefixIter {
let mut matches = HashMap::new();
let mut matches = BTreeMap::new();

for (key, modification) in &self.block_write_log {
if key.split_prefix(prefix).is_some() {
matches.insert(key.clone(), modification.clone());
matches.insert(key.to_string(), modification.clone());
}
}
for (key, modification) in &self.tx_write_log {
if key.split_prefix(prefix).is_some() {
matches.insert(key.clone(), modification.clone());
matches.insert(key.to_string(), modification.clone());
}
}
let iter = matches
.into_iter()
.sorted_unstable_by_key(|(key, _val)| key.clone());

let iter = matches.into_iter();
PrefixIter { iter }
}
}
Expand Down

0 comments on commit 51ac7c2

Please sign in to comment.