From 370b8f858382a6878900f6dd8a26785692ea7ab3 Mon Sep 17 00:00:00 2001 From: elfedy Date: Tue, 1 Oct 2024 09:31:23 -0300 Subject: [PATCH 1/7] feat: gas chapter --- src/SUMMARY.md | 1 + src/zksync-specifics/README.md | 1 + src/zksync-specifics/gas.md | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/zksync-specifics/gas.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index d3a238c1b..a2532c3c6 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -34,6 +34,7 @@ - [zkRegisterContract](./zksync-specifics/cheatcodes/zk-register-contract.md) - [zkVm](./zksync-specifics/cheatcodes/zk-vm.md) - [zkVmSkip](./zksync-specifics/cheatcodes/zk-vm-skip.md) +- [Gas](./zksync-specifics/gas.md) # Supported Commands - [Command List](./supported-commands/README.md) diff --git a/src/zksync-specifics/README.md b/src/zksync-specifics/README.md index fb091410e..35df952eb 100644 --- a/src/zksync-specifics/README.md +++ b/src/zksync-specifics/README.md @@ -9,3 +9,4 @@ - [Cheatcodes](limitations/cheatcodes.md) - [Additional Cheatcodes](cheatcodes/README.md) - [zkVm](cheatcodes/zkvm.md) +- [Gas](gas.md) diff --git a/src/zksync-specifics/gas.md b/src/zksync-specifics/gas.md new file mode 100644 index 000000000..10d3dd1da --- /dev/null +++ b/src/zksync-specifics/gas.md @@ -0,0 +1,36 @@ +## Gas + +### Gas reported back to the EVM +Foundry has an `isolate` mode for the EVM where all `CALL`/`CREATE` operations at the root level of a test (i.e: with depth 1) will be intercepted and treated as independent transactions. This allows for accounting of the actual transaction gas and include for example the fixed 21000 gas cost charged to the user. + +Running in zkVM mode is anologous to running in `isolate` mode but using the zkEVM instead. Every `CALL`/`CREATE` will be intercepted, a transaction representing the operation built, and finally a VM with that transaction in the bootloader's heap will be spawned and run in order to simulate the execution of that transaction. The gas used reported back to the EVM, and hence the one seen on traces and gas-reports, is what would be charged to the user for submitting that transaction. That value differs from the computational cost of running the called contract code and includes: + + 1. Intrinsic costs: Overhead charged on each transaction. + 2. Validation costs: Gas spent on transaction validation. May vary depending on the account making the transaction. See [Account Abstraction](https://docs.zksync.io/build/developer-reference/account-abstraction) docs. + 3. Execution costs: Gas spent on marking factory deps and executing the transaction. + 4. Pubdata costs: Gas spent on publishing pubdata, is influenced by the `gasPerPubdata` network value. + +More info about ZKSync Era's fee model can be found [here](https://docs.zksync.io/build/developer-reference/fee-model). + +### Transaction/Network values that impact gas cost +The gas cost mentioned above is influenced by transaction and network values. The values are set when running the VM in the following way: + +1. Transaction Params: + +* `max_fee_per_gas`: will be gas price of the root evm transaction (e.g: when running tests, the value of `--gas-price` option is used) with a minimum value of `0.26GWei`, which is the base fee used in some test environments/networks. +* `gas_limit`: The sender remaining balance capped to a max of `2^31 - 1`. + +2. Network Params: + +* `fair_l2_gas_price`: set to the minumum of `max_fee_per_gas` and the base fee of the root evm transaction (e.g: when running tests, the value of the `--base-fee` option). +* `l1_gas_price`: set to the same as `fair_l2_gas_price`, with a minimum value of `1000`. + +#### Deriving relevant transaction gas values + +From the params above we can get all gas related values used in the transaction: + +* `fair_pubdata_price`: `l1_gas_price` multiplied by `L1_GAS_PER_PUBDATA_BYTE`. +* `baseFee`: Maximum value between `fair_l2_gas_price` and `(fair_pubdata_price / MAX_L2_GAS_PER_PUBDATA)`. +* `gasPerPubdata`: `fairPubdataPrice / baseFee`. + +`L1_GAS_PER_PUBDATA_BYTE` and `MAX_L2_GAS_PER_PUBDATA` are system constants currently set to `17` and `50000` respectively. From 536d1d6361146431b7ae9ea18564689a969af261 Mon Sep 17 00:00:00 2001 From: elfedy Date: Tue, 1 Oct 2024 09:44:53 -0300 Subject: [PATCH 2/7] Add note about gas limit --- src/zksync-specifics/gas.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zksync-specifics/gas.md b/src/zksync-specifics/gas.md index 10d3dd1da..54fc59450 100644 --- a/src/zksync-specifics/gas.md +++ b/src/zksync-specifics/gas.md @@ -18,7 +18,7 @@ The gas cost mentioned above is influenced by transaction and network values. Th 1. Transaction Params: * `max_fee_per_gas`: will be gas price of the root evm transaction (e.g: when running tests, the value of `--gas-price` option is used) with a minimum value of `0.26GWei`, which is the base fee used in some test environments/networks. -* `gas_limit`: The sender remaining balance capped to a max of `2^31 - 1`. +* `gas_limit`: The sender remaining balance capped to a max of `2^31 - 1`. Note that, no matter the gas limit, the vm caps how much gas a single transaction can use to `MAX_GAS_PER_TRANSACTION`, currently set to `80_000_000`. 2. Network Params: @@ -33,4 +33,4 @@ From the params above we can get all gas related values used in the transaction: * `baseFee`: Maximum value between `fair_l2_gas_price` and `(fair_pubdata_price / MAX_L2_GAS_PER_PUBDATA)`. * `gasPerPubdata`: `fairPubdataPrice / baseFee`. -`L1_GAS_PER_PUBDATA_BYTE` and `MAX_L2_GAS_PER_PUBDATA` are system constants currently set to `17` and `50000` respectively. +`L1_GAS_PER_PUBDATA_BYTE` and `MAX_L2_GAS_PER_PUBDATA` are system constants currently set to `17` and `50_000` respectively. From 1128765bad9aac7955de8b1344a7424f84c8a2be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Rodr=C3=ADguez?= Date: Tue, 1 Oct 2024 10:48:48 -0300 Subject: [PATCH 3/7] Update src/zksync-specifics/gas.md Co-authored-by: Nisheeth Barthwal --- src/zksync-specifics/gas.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zksync-specifics/gas.md b/src/zksync-specifics/gas.md index 54fc59450..4a48a4ff6 100644 --- a/src/zksync-specifics/gas.md +++ b/src/zksync-specifics/gas.md @@ -29,7 +29,7 @@ The gas cost mentioned above is influenced by transaction and network values. Th From the params above we can get all gas related values used in the transaction: -* `fair_pubdata_price`: `l1_gas_price` multiplied by `L1_GAS_PER_PUBDATA_BYTE`. +* `fair_pubdata_price`: `l1_gas_price` * `L1_GAS_PER_PUBDATA_BYTE`. * `baseFee`: Maximum value between `fair_l2_gas_price` and `(fair_pubdata_price / MAX_L2_GAS_PER_PUBDATA)`. * `gasPerPubdata`: `fairPubdataPrice / baseFee`. From 25a93883dbc4f2994df5721c3deac3f3fc13b040 Mon Sep 17 00:00:00 2001 From: elfedy Date: Tue, 1 Oct 2024 10:51:53 -0300 Subject: [PATCH 4/7] zkVM -> zkEVM --- src/zksync-specifics/gas.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zksync-specifics/gas.md b/src/zksync-specifics/gas.md index 4a48a4ff6..2ea6a3538 100644 --- a/src/zksync-specifics/gas.md +++ b/src/zksync-specifics/gas.md @@ -3,7 +3,7 @@ ### Gas reported back to the EVM Foundry has an `isolate` mode for the EVM where all `CALL`/`CREATE` operations at the root level of a test (i.e: with depth 1) will be intercepted and treated as independent transactions. This allows for accounting of the actual transaction gas and include for example the fixed 21000 gas cost charged to the user. -Running in zkVM mode is anologous to running in `isolate` mode but using the zkEVM instead. Every `CALL`/`CREATE` will be intercepted, a transaction representing the operation built, and finally a VM with that transaction in the bootloader's heap will be spawned and run in order to simulate the execution of that transaction. The gas used reported back to the EVM, and hence the one seen on traces and gas-reports, is what would be charged to the user for submitting that transaction. That value differs from the computational cost of running the called contract code and includes: +Running in zkEVM mode is anologous to running in `isolate` mode but using the zkEVM instead. Every `CALL`/`CREATE` will be intercepted, a transaction representing the operation built, and finally a VM with that transaction in the bootloader's heap will be spawned and run in order to simulate the execution of that transaction. The gas used reported back to the EVM, and hence the one seen on traces and gas-reports, is what would be charged to the user for submitting that transaction. That value differs from the computational cost of running the called contract code and includes: 1. Intrinsic costs: Overhead charged on each transaction. 2. Validation costs: Gas spent on transaction validation. May vary depending on the account making the transaction. See [Account Abstraction](https://docs.zksync.io/build/developer-reference/account-abstraction) docs. From 0d81fad98f9aeb3c6d6cd866f161cf23fee90cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Rodr=C3=ADguez?= Date: Tue, 1 Oct 2024 15:08:18 -0300 Subject: [PATCH 5/7] Update src/zksync-specifics/README.md Co-authored-by: Nisheeth Barthwal --- src/zksync-specifics/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zksync-specifics/README.md b/src/zksync-specifics/README.md index 35df952eb..3e45066cc 100644 --- a/src/zksync-specifics/README.md +++ b/src/zksync-specifics/README.md @@ -9,4 +9,4 @@ - [Cheatcodes](limitations/cheatcodes.md) - [Additional Cheatcodes](cheatcodes/README.md) - [zkVm](cheatcodes/zkvm.md) -- [Gas](gas.md) +- [Gas Overview](gas.md) From a11e5ac93e8dede36e7258307ea33f1cca0047f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Rodr=C3=ADguez?= Date: Tue, 1 Oct 2024 15:08:24 -0300 Subject: [PATCH 6/7] Update src/zksync-specifics/gas.md Co-authored-by: Nisheeth Barthwal --- src/zksync-specifics/gas.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zksync-specifics/gas.md b/src/zksync-specifics/gas.md index 2ea6a3538..80315e26e 100644 --- a/src/zksync-specifics/gas.md +++ b/src/zksync-specifics/gas.md @@ -1,4 +1,4 @@ -## Gas +## Gas Overview ### Gas reported back to the EVM Foundry has an `isolate` mode for the EVM where all `CALL`/`CREATE` operations at the root level of a test (i.e: with depth 1) will be intercepted and treated as independent transactions. This allows for accounting of the actual transaction gas and include for example the fixed 21000 gas cost charged to the user. From 987f30b2741efe838b22fb992939959531bd9907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Rodr=C3=ADguez?= Date: Tue, 1 Oct 2024 16:55:29 -0300 Subject: [PATCH 7/7] Update src/SUMMARY.md Co-authored-by: Nisheeth Barthwal --- src/SUMMARY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index a2532c3c6..af09b25c0 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -34,7 +34,7 @@ - [zkRegisterContract](./zksync-specifics/cheatcodes/zk-register-contract.md) - [zkVm](./zksync-specifics/cheatcodes/zk-vm.md) - [zkVmSkip](./zksync-specifics/cheatcodes/zk-vm-skip.md) -- [Gas](./zksync-specifics/gas.md) +- [Gas Overview](./zksync-specifics/gas.md) # Supported Commands - [Command List](./supported-commands/README.md)