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

Charge for new storage bytes #634

Merged
merged 17 commits into from
Nov 13, 2023
Merged

Conversation

Dentosal
Copy link
Member

@Dentosal Dentosal commented Nov 10, 2023

Closes #602
Spec PR: FuelLabs/fuel-specs#536

Breaking changes:

  • Gas is charged for new storage slots, StorageSlot::LEN * gas_costs.new_storage_per_byte * num_slots
  • Write opcodes now return the number of new storage slots instead of just a boolean on whether the value existed. The return value is currently ignored by the Sway stdlib, so it shouldn't be too bad

@Dentosal Dentosal added breaking A breaking api change fuel-vm Related to the `fuel-vm` crate. fuel-storage Related to the `fuel-storage` crate. labels Nov 10, 2023
@Dentosal Dentosal self-assigned this Nov 10, 2023
@@ -321,6 +321,7 @@ pub struct GasCostsValues {
// Non-opcode costs
pub contract_root: DependentCost,
pub state_root: DependentCost,
pub new_storage_per_byte: Word,
Copy link
Member Author

Choose a reason for hiding this comment

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

Name still subject to bikeshedding

Comment on lines 901 to 906
pub profiler: &'vm mut Profiler,
pub new_storage_gas_per_byte: Word,
pub current_contract: Option<ContractId>,
pub cgas: RegMut<'vm, CGAS>,
pub ggas: RegMut<'vm, GGAS>,
pub is: Reg<'vm, IS>,
Copy link
Member Author

Choose a reason for hiding this comment

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

These are not needed for state read opcodes, so maybe it makes sense to split this struct into two? On the other hand, we're handing over over half of the VM anyway, so just passing the VM directly might make sense.

@Dentosal Dentosal requested a review from a team November 10, 2023 09:07
@Dentosal Dentosal marked this pull request as ready for review November 10, 2023 09:08
Copy link
Collaborator

@xgreenx xgreenx left a comment

Choose a reason for hiding this comment

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

The issues says:

We need to find all opcodes that modify the storage first. For that, we can find all places where we use methods from the InterpreterStorage trait.

It is similar to the issue FuelLabs/fuel-core#1239

@@ -321,6 +321,7 @@ pub struct GasCostsValues {
// Non-opcode costs
pub contract_root: DependentCost,
pub state_root: DependentCost,
pub new_storage_per_byte: Word,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it is better to use DependentCost here because we need to charge for a new storage slot, plus, in the future(when we support dynamic storage values), charge per byte.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think it makes any sense to future-proof this for the dynamic storage value support. The whole API is going to change anyway.

cgas,
ggas,
profiler,
(Bytes32::LEN as u64) * new_storage_gas_per_byte,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we need to charge static cost and per byte=)

Copy link
Member Author

Choose a reason for hiding this comment

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

What static cost? The opcode itself already costs for instertion, this is only for the new slot which has a fixed cost.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is related to the comment about DepenentCost=) base cost can include inserting the new entry into the storage(it will represent the burden of the SMT), and deps_per_unit will be gas per byte that you will multiply with 32 or 64, but in the later versions with dynamic storage by the size of the value.

@Voxelot
Copy link
Member

Voxelot commented Nov 10, 2023

Should we also charge for 32 bytes(asset id) + 8 bytes (amount) when inserting a new asset type into the balances tree?

@xgreenx
Copy link
Collaborator

xgreenx commented Nov 10, 2023

Should we also charge for 32 bytes(asset id) + 8 bytes (amount) when inserting a new asset type into the balances tree?

Yep

We need to find all opcodes that modify the storage first. For that, we can find all places where we use methods from the InterpreterStorage trait.

@Dentosal Dentosal marked this pull request as draft November 10, 2023 11:22
@Dentosal Dentosal marked this pull request as ready for review November 10, 2023 13:41
fuel-vm/src/interpreter/blockchain.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/blockchain.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/blockchain.rs Outdated Show resolved Hide resolved
Dentosal and others added 5 commits November 10, 2023 13:58
Co-authored-by: Green Baneling <XgreenX9999@gmail.com>
Co-authored-by: Green Baneling <XgreenX9999@gmail.com>
Co-authored-by: Green Baneling <XgreenX9999@gmail.com>
Copy link
Collaborator

@xgreenx xgreenx left a comment

Choose a reason for hiding this comment

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

Looks good=) Waiting for rest of opcodes

fuel-vm/src/interpreter/blockchain.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/contract.rs Outdated Show resolved Hide resolved
fuel-vm/src/interpreter/flow.rs Outdated Show resolved Hide resolved
@@ -196,8 +230,12 @@ struct TransferCtx<'vm, S, Tx> {
context: &'vm Context,
balances: &'vm mut RuntimeBalances,
receipts: &'vm mut ReceiptsCtx,
profiler: &'vm mut Profiler,
Copy link
Collaborator

Choose a reason for hiding this comment

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

You forgot to update transfer_output

Copy link
Member Author

Choose a reason for hiding this comment

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

I though TRO doesn't create markle tree entries? Am I missing something here?

@Dentosal Dentosal added this pull request to the merge queue Nov 13, 2023
Merged via the queue into master with commit da5b9af Nov 13, 2023
37 checks passed
@Dentosal Dentosal deleted the dento/charge-for-new-storage-bytes branch November 13, 2023 14:09
@xgreenx xgreenx mentioned this pull request Nov 13, 2023
Dentosal added a commit to FuelLabs/fuel-specs that referenced this pull request Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking A breaking api change fuel-storage Related to the `fuel-storage` crate. fuel-vm Related to the `fuel-vm` crate.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Increase the cost of storage opcodes by the gas per byte fee in addition to the execution cost
3 participants