Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(revm): leverage StorageSlot methods, where appropriate #899

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/revm/src/db/states/bundle_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ impl BundleAccount {
match slot {
RevertToSlot::Some(value) => {
// Don't overwrite original values if present
// if storage is not present set original values as current value.
// if storage is not present set original value as current value.
self.storage
.entry(key)
.or_insert(StorageSlot::new_changed(value, U256::ZERO))
.or_insert(StorageSlot::new(value))
Copy link
Member

@rakita rakita Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a different behaviour

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is (I hope you're referring to the differences between the logic of new() and new_changed()).

Why create a "changed" StorageSlot, if its present_value is being changed to the very same value as its previous_or_original_value after the execution of .or_insert() - practically, making the StorageSlot instance lose its "changed" status?

FWIW, it's still kinda weird to have .present_value = value; happen after .or_insert(), but this was weird before, as well. Now, present_value is, at least, not set to U256::ZERO, just to be re-set to value right after this.

Or am I missing something here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed something, I just looked at the changed code and saw that it is different, but I didn't see that we override the present_value so it does not matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Glad we're on the same page now! 🙌🏻

.present_value = value;
}
RevertToSlot::Destroyed => {
Expand Down Expand Up @@ -140,7 +140,7 @@ impl BundleAccount {
|updated_storage: &StorageWithOriginalValues| -> HashMap<U256, RevertToSlot> {
updated_storage
.iter()
.filter(|s| s.1.previous_or_original_value != s.1.present_value)
.filter(|s| s.1.is_changed())
.map(|(key, value)| {
(*key, RevertToSlot::Some(value.previous_or_original_value))
})
Expand Down
106 changes: 28 additions & 78 deletions crates/revm/src/db/states/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,10 @@ mod tests {
previous_info: Some(existing_account_initial_info.clone()),
storage: HashMap::from([(
slot1,
StorageSlot {
previous_or_original_value: *existing_account_initial_storage
.get(&slot1)
.unwrap(),
present_value: U256::from(1000),
},
StorageSlot::new_changed(
*existing_account_initial_storage.get(&slot1).unwrap(),
U256::from(1000),
),
)]),
storage_was_destroyed: false,
},
Expand Down Expand Up @@ -431,10 +429,7 @@ mod tests {
previous_info: Some(new_account_changed_info),
storage: HashMap::from([(
slot1,
StorageSlot {
previous_or_original_value: U256::ZERO,
present_value: U256::from(1),
},
StorageSlot::new_changed(U256::ZERO, U256::from(1)),
)]),
storage_was_destroyed: false,
},
Expand All @@ -449,27 +444,19 @@ mod tests {
storage: HashMap::from([
(
slot1,
StorageSlot {
previous_or_original_value: U256::from(100),
present_value: U256::from(1_000),
},
StorageSlot::new_changed(U256::from(100), U256::from(1_000)),
),
(
slot2,
StorageSlot {
previous_or_original_value: *existing_account_initial_storage
.get(&slot2)
.unwrap(),
present_value: U256::from(2_000),
},
StorageSlot::new_changed(
*existing_account_initial_storage.get(&slot2).unwrap(),
U256::from(2_000),
),
),
// Create new slot
(
slot3,
StorageSlot {
previous_or_original_value: U256::ZERO,
present_value: U256::from(3_000),
},
StorageSlot::new_changed(U256::ZERO, U256::from(3_000)),
),
]),
storage_was_destroyed: false,
Expand Down Expand Up @@ -531,10 +518,7 @@ mod tests {
status: AccountStatus::InMemoryChange,
storage: HashMap::from([(
slot1,
StorageSlot {
previous_or_original_value: U256::ZERO,
present_value: U256::from(1),
}
StorageSlot::new_changed(U256::ZERO, U256::from(1))
)]),
}),
"The latest state of the new account is incorrect"
Expand All @@ -551,29 +535,22 @@ mod tests {
storage: HashMap::from([
(
slot1,
StorageSlot {
previous_or_original_value: *existing_account_initial_storage
.get(&slot1)
.unwrap(),
present_value: U256::from(1_000),
},
StorageSlot::new_changed(
*existing_account_initial_storage.get(&slot1).unwrap(),
U256::from(1_000)
)
),
(
slot2,
StorageSlot {
previous_or_original_value: *existing_account_initial_storage
.get(&slot2)
.unwrap(),
present_value: U256::from(2_000),
},
StorageSlot::new_changed(
*existing_account_initial_storage.get(&slot2).unwrap(),
U256::from(2_000)
)
),
// Create new slot
(
slot3,
StorageSlot {
previous_or_original_value: U256::ZERO,
present_value: U256::from(3_000),
},
StorageSlot::new_changed(U256::ZERO, U256::from(3_000))
),
]),
}),
Expand Down Expand Up @@ -646,18 +623,9 @@ mod tests {
storage: HashMap::from([
(
slot1,
StorageSlot {
previous_or_original_value: U256::from(1),
present_value: U256::from(10),
},
),
(
slot2,
StorageSlot {
previous_or_original_value: U256::ZERO,
present_value: U256::from(20),
},
StorageSlot::new_changed(U256::from(1), U256::from(10)),
),
(slot2, StorageSlot::new_changed(U256::ZERO, U256::from(20))),
]),
storage_was_destroyed: false,
},
Expand Down Expand Up @@ -696,18 +664,9 @@ mod tests {
storage: HashMap::from([
(
slot1,
StorageSlot {
previous_or_original_value: U256::from(10),
present_value: U256::from(1),
},
),
(
slot2,
StorageSlot {
previous_or_original_value: U256::from(20),
present_value: U256::ZERO,
},
StorageSlot::new_changed(U256::from(10), U256::from(1)),
),
(slot2, StorageSlot::new_changed(U256::from(20), U256::ZERO)),
]),
storage_was_destroyed: false,
},
Expand Down Expand Up @@ -761,10 +720,7 @@ mod tests {
previous_info: None,
storage: HashMap::from([(
slot1,
StorageSlot {
previous_or_original_value: U256::ZERO,
present_value: U256::from(1),
},
StorageSlot::new_changed(U256::ZERO, U256::from(1)),
)]),
storage_was_destroyed: false,
},
Expand Down Expand Up @@ -794,10 +750,7 @@ mod tests {
previous_info: None,
storage: HashMap::from([(
slot2,
StorageSlot {
previous_or_original_value: U256::ZERO,
present_value: U256::from(2),
},
StorageSlot::new_changed(U256::ZERO, U256::from(2)),
)]),
storage_was_destroyed: false,
},
Expand All @@ -816,10 +769,7 @@ mod tests {
original_info: Some(existing_account_info.clone()),
storage: HashMap::from([(
slot2,
StorageSlot {
previous_or_original_value: U256::ZERO,
present_value: U256::from(2),
},
StorageSlot::new_changed(U256::ZERO, U256::from(2))
)]),
status: AccountStatus::DestroyedChanged,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/db/states/transition_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl TransitionAccount {
if value.original_value() == slot.present_value() {
entry.remove();
} else {
// is value is different, update transition present value;
// if value is different, update transition present value;
value.present_value = slot.present_value;
}
}
Expand Down