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

Adding new topic for gas in Sui #10245

Merged
merged 19 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
New topic for gas budgets
  • Loading branch information
randall-Mysten committed Mar 31, 2023
commit c7f979256fc1d7d9485ad246e3a8e72c893093af
60 changes: 60 additions & 0 deletions doc/src/learn/tokenomics/gas-budgets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: Sui Gas Budgets
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved
---

Gas Budgets are important because they guarantee that an user never pays more transaction fees than what they budget for. Sui’s gas mechanism is such that users pay upfront for both the current computational cost of executing a transaction and the long-term cost of storing the data associated with a transaction’s objects. Thus, Gas Budgets need to cover both sets of fees.
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved

Specifically, [Sui’s Gas Pricing Mechanism](https://docs.sui.io/learn/tokenomics/gas-pricing) is such that any transaction $\tau$ pays the following gas fees:
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved

$$
\text{GasFees}[\tau] = \text{ComputationUnits}[\tau]\times \text{ReferenceGasPrice}\ + \ \text{StorageUnits}[\tau]\times \text{StoragePrice}
$$

While computation and storage fees are separate, they are conceptually similar in that they each translate computation or storage into SUI terms by multiplying computation or storage units by the relevant price.

### Gas Prices
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved

The [Reference Gas Price](gas-pricing.md#computation-gas-prices) translates the real-time cost of executing a transaction into SUI units and is updated at each epoch boundary by the validator set. Similarly, the [Storage Price](gas-pricing.md#storage-gas-prices) translates the long-term cost of storing data on-chain into SUI units and is updated infrequently; often remaining constant for various consecutive epochs. During regular network operations, all Sui users should expect to pay the Reference Gas Price and Storage Price for computation and storage, respectively.

### Gas Units
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved

Different Sui transactions require varying amounts of computational time in order to be processed and executed. Sui translates these varying operational loads into transaction fees by measuring each transaction in terms of Computation Units. All else equals, more complex transactions will require more Computation Units.

Importantly, though, Sui’s gas schedule is built coarsely with a bucketing approach. Two relatively similar transactions will translate into the exact same amount of Computation Units if they are in the same bucket, whereas two relatively different transactions will translate into different amounts of Computation Units if they fall in separate buckets. The smallest bucket maps into 1000 Computation Units, meaning that all transactions that fall into the smallest bucket will cost 1000 Computation Units. The largest bucket maps into 5,000,000 Computation Units; if a transaction were to require more Computation Units it would simply abort.
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved

randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved
Similarly, Sui transactions vary depending on the amount of new data written into on-chain storage. The variable Storage Units captures these difference by mapping the amount of bytes held in storage into storage units. Sui’s current schedule is linear and maps each byte into 100 storage units. So, for example, a transaction that stores 25 bytes will cost 2500 Storage Units while a transaction that stores 75 bytes will cost 7500 units.
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved

### Gas Budgets

All transactions need to be submitted together with a Gas Budget. This provides a cap to the amount of Gas Fees a user will pay, especially since in some cases it may be hard to perfectly forecast how much a transaction will cost before it is submitted to the Sui Network.

A transaction’s Gas Budget is defined in SUI units and transactions will be successfully executed if:

$$
\text{GasBudget}[\tau]\ \geq\ \text{GasFees}[\tau]
$$

If the Gas Budget does not fulfill this condition — and thus is insufficient to cover a transaction’s gas fees — then the transaction will fail and the entire Gas Budget will be charged.

The minimum Gas Budget is 2000 MIST. This ensures validators can be compensated with at least 2000 MIST even if the Gas Budget is mis-specified and transaction aborts. Additionally, this protects the Sui Network from being spammed with a large number of transactions with minimal gas budgets. The maximum Gas Budget is 50 billion MIST or 50 SUI. This protects the network against overflow of internal multiplications and gas limits for denial of service attack
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved

Finally, Sui’s [Storage mechanics](https://docs.sui.io/learn/tokenomics/storage-fund#storage-fund-rewards) provide storage fee rebates whenever a transaction deletes previously-stored objects. Hence, the net fees that a user pays equals Gas Fees minus the rebates associated with data deletion:
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved

$$
\text{NetGasFees}[\tau]\ =\ \text{GasFees}[\tau]\ -\text{StorageRebate}[\tau]
$$

Since the Gas Budget applies to the totality of Gas Fees, it will often be the case that a transaction will only go through if the Gas Budget is considerably higher than the Net Gas Fees that a user ultimately pays.
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved

### Gas Budget Examples

The following table provides some examples of gas accounting in the Sui network. Note that within the first two rows and within the last two rows, Computation Units are the same because transactions fall within the same buckets. However, the last two transactions are more complex than the first two and thus fall in a higher bucket. Finally, note that in the last transaction the storage rebate is large enough to fully offset the transactions Gas Fees and actually pays the user back a positive amount of SUI.

These examples also showcase the importance of the Gas Budget. The minimum Gas Budget is the smallest amount a transaction can specify in order to successfully execute. Importantly, note that when there is a storage rebate, the minimum Gas Budget is larger than the amount of Net Gas Fees a user ultimately pays — this is especially stark in the last example where the user receives a positive amount back for executing the transaction. This is because the minimum Gas Budget is applied to the raw Gas Fees value, not including the Storage Rebate.

| | Reference Gas Price | Computation Units | Storage Price | Storage Units | Storage Rebate | Minimum Gas Budget | Net Gas Fees |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Simple transaction storing 10 bytes | 1,000 MIST | 1,000 | 75 MIST | 1,000 | 0 MIST | 1,075,000 MIST | 1,075,000 MIST |
| Simple transaction storing 10 bytes and deleting data | 500 MIST | 1,000 | 75 MIST | 1,000 | 100,000 MIST | 575,000 MIST | 475,000 MIST |
| Complex transaction storing 120 bytes | 1,000 MIST | 5,000 | 200 MIST | 12,000 | 0 MIST | 7,400,000 MIST | 7,400,000 MIST |
| Complex transaction storing 120 bytes and deleting data | 500 MIST | 5,000 | 200 MIST | 12,000 | 5,000,000 MIST | 2,400,000 MIST | -100,000 MIST |
4 changes: 4 additions & 0 deletions doc/src/navconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
"label": "Sui Gas Pricing",
"fileName": "learn/tokenomics/gas-pricing"
},
{
"label": "Sui Gas Budgets",
randall-Mysten marked this conversation as resolved.
Show resolved Hide resolved
"fileName": "learn/tokenomics/gas-budgets"
},
{
"label": "Sui Storage Fund",
"fileName": "learn/tokenomics/storage-fund"
Expand Down