Skip to content

Commit

Permalink
reverse add so it works like rust std sets
Browse files Browse the repository at this point in the history
  • Loading branch information
joonazan committed Sep 3, 2024
1 parent bc008f1 commit cfa7531
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ pub struct RollbackableSet<K: Ord> {
}

impl<T: Ord + Clone> RollbackableSet<T> {
/// Adds `key` to the set and returns if it was already present.
/// Adds `key` to the set and returns if it was added (not present earlier).
pub fn add(&mut self, key: T) -> bool {
let is_new = self.map.insert(key.clone(), ()).is_none();
if is_new {
self.old_entries.push(key);
}
!is_new
is_new
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/world_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ impl WorldDiff {
.copied()
.unwrap_or_else(|| world.read_storage(contract, key).unwrap_or_default());

let already_accessed = self.read_storage_slots.add((contract, key));
if !already_accessed {
let newly_added = self.read_storage_slots.add((contract, key));
if newly_added {
tracer.on_extra_prover_cycles(CycleStats::StorageRead);
}

let refund = if already_accessed || world.is_free_storage_slot(&contract, &key) {
let refund = if !newly_added || world.is_free_storage_slot(&contract, &key) {
WARM_READ_REFUND
} else {
0
Expand All @@ -140,7 +140,7 @@ impl WorldDiff {
.or_insert_with(|| world.read_storage(contract, key));

if world.is_free_storage_slot(&contract, &key) {
if !self.written_storage_slots.add((contract, key)) {
if self.written_storage_slots.add((contract, key)) {
tracer.on_extra_prover_cycles(CycleStats::StorageWrite);
}
self.read_storage_slots.add((contract, key));
Expand All @@ -156,12 +156,12 @@ impl WorldDiff {
.insert((contract, key), update_cost)
.unwrap_or(0);

let refund = if self.written_storage_slots.add((contract, key)) {
let refund = if !self.written_storage_slots.add((contract, key)) {
WARM_WRITE_REFUND
} else {
tracer.on_extra_prover_cycles(CycleStats::StorageWrite);

if self.read_storage_slots.add((contract, key)) {
if !self.read_storage_slots.add((contract, key)) {
COLD_WRITE_AFTER_WARM_READ_REFUND
} else {
0
Expand Down

0 comments on commit cfa7531

Please sign in to comment.