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

Conserve storage rebates in system transactions #10143

Merged
merged 1 commit into from
Mar 30, 2023

Conversation

lxfind
Copy link
Contributor

@lxfind lxfind commented Mar 30, 2023

This PR accumulates storage rebates during system transaction, and stuff it all into 0x5 object in the result state.
This ensures that we conserve SUI during system transaction.
In order to do this, I made the following changes:

  1. Always go through gas charging process even for unmetered transaction
  2. In unmetered mode, we track storage rebate in a separate field.

Enabled system transaction conservation check.

@vercel
Copy link

vercel bot commented Mar 30, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

4 Ignored Deployments
Name Status Preview Comments Updated
explorer ⬜️ Ignored (Inspect) Mar 30, 2023 at 11:28PM (UTC)
explorer-storybook ⬜️ Ignored (Inspect) Mar 30, 2023 at 11:28PM (UTC)
sui-wallet-kit ⬜️ Ignored (Inspect) Mar 30, 2023 at 11:28PM (UTC)
wallet-adapter ⬜️ Ignored (Inspect) Mar 30, 2023 at 11:28PM (UTC)

@lxfind lxfind requested review from oxade, tnowacki and tzakian March 30, 2023 15:26
@lxfind lxfind force-pushed the fix-storage-rebate-for-system-tx branch from 24e5f3c to 60af05b Compare March 30, 2023 15:33
@lxfind lxfind force-pushed the fix-storage-rebate-for-system-tx branch from 60af05b to d145a0c Compare March 30, 2023 15:52
crates/sui-types/src/temporary_store.rs Show resolved Hide resolved
crates/sui-types/src/temporary_store.rs Outdated Show resolved Hide resolved
crates/sui-types/src/temporary_store.rs Show resolved Hide resolved
crates/sui-types/src/temporary_store.rs Outdated Show resolved Hide resolved
.clone();
// In unmetered execution, storage_rebate field of mutated object must be 0.
// If not, we would be dropping SUI on the floor by overriding it.
assert_eq!(system_state_wrapper.storage_rebate, 0);
Copy link
Collaborator

@sblackshear sblackshear Mar 30, 2023

Choose a reason for hiding this comment

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

What is the intuition for why this will be always be 0, given that we set it to a nonzero value below? E.g., if the system's storage_rebate is 0 at the end of epoch 1 and we set it to 100, won't this fail at the end of epoch 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the mutated state, not the original state. And when we mutate an object, we reset its storage_rebate (indicating that a user is paying for the new storage). But since in this case nobody is paying, its new storage_rebate must be 0

Copy link
Collaborator

@sblackshear sblackshear Mar 30, 2023

Choose a reason for hiding this comment

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

This makes sense to me as a high-level explanation.

But in the current gas charging implementation, I'm not sure there's a place where we reset the object's storage_rebate to 0 before setting it to its new value--I think we directly set it to the new value. I'm also not sure how unmetered code interacts with all of this. I think @dariorussi will know better.

Copy link
Contributor

@dariorussi dariorussi left a comment

Choose a reason for hiding this comment

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

so we are only tracking non refundable fee?
I may be confused a bit here but why not track all the other as well (storage cost and rebate)?
will that make some conservation checks not possible.
I feel I am missing something. Can someone explain?

@@ -350,6 +350,9 @@ pub struct SuiGasStatus<'a> {
/// was the storage cost paid when the object was last mutated. It is not affected
/// by the current storage gas unit price.
storage_rebate: SuiGas,
/// Amount of storage rebate accumulated when we are running in unmetered mode (i.e. system transaction).
/// This allows us to track how much storage rebate we need to retain in system transactions.
unmetered_storage_rebate: SuiGas,
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this is just to avoid the headache of tracking the existing storage_rebate?
in my PR charging and tracking are distinct and wondering if we could use the regular rebate field.
Mostly curiosity as we do not have to change this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tracking in a separate field is easier to deal with. But it should certainly be possible to track it in storage_rebate.
For example, we want to make sure that the resulting gas summary still have storage_rebate=0, which is important because nobody is getting these rebates (they all go to the next object).

@@ -350,6 +350,9 @@ pub struct SuiGasStatus<'a> {
/// was the storage cost paid when the object was last mutated. It is not affected
/// by the current storage gas unit price.
storage_rebate: SuiGas,
/// Amount of storage rebate accumulated when we are running in unmetered mode (i.e. system transaction).
/// This allows us to track how much storage rebate we need to retain in system transactions.
unmetered_storage_rebate: SuiGas,
Copy link
Collaborator

Choose a reason for hiding this comment

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

What would go wrong if we put the this amount into the ordinary storage_rebate field?

Copy link
Contributor

Choose a reason for hiding this comment

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

and to add to that, why not track the storage cost? mostly for my eduction

@lxfind lxfind force-pushed the fix-storage-rebate-for-system-tx branch from d145a0c to 2f5340a Compare March 30, 2023 17:10
@lxfind lxfind force-pushed the fix-storage-rebate-for-system-tx branch from d44a2cd to a21524e Compare March 30, 2023 21:07
@lxfind lxfind enabled auto-merge (squash) March 30, 2023 21:20
@lxfind lxfind force-pushed the fix-storage-rebate-for-system-tx branch 3 times, most recently from 25519d9 to 4559f02 Compare March 30, 2023 22:45
@lxfind lxfind force-pushed the fix-storage-rebate-for-system-tx branch 2 times, most recently from d9a7e20 to f6180c2 Compare March 30, 2023 23:21
@lxfind lxfind force-pushed the fix-storage-rebate-for-system-tx branch from f6180c2 to d7152db Compare March 30, 2023 23:27
@@ -10,9 +10,13 @@ use test_utils::authority::start_node;
#[should_panic]
async fn test_validator_panics_on_unsupported_protocol_version() {
let dir = tempfile::TempDir::new().unwrap();
let latest_version = ProtocolVersion::MAX;
Copy link
Contributor

Choose a reason for hiding this comment

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

this is fine, but this is actually causing a panic for a different reason, so this test is broken anyway. i need to fix it. but go ahead with this for now.

@lxfind lxfind merged commit fa787a6 into main Mar 30, 2023
@lxfind lxfind deleted the fix-storage-rebate-for-system-tx branch March 30, 2023 23:54
@dariorussi dariorussi restored the fix-storage-rebate-for-system-tx branch April 2, 2023 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants