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

feat: Add gas reports for validium mode #38

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c944c92
set pubdata constants to 0
toni-calvin Jan 5, 2024
837cf6f
Set to 0
Jan 5, 2024
181d80c
Merge branch 'validium_mode_pubdata_price' into validium_mode_pubdata_0
Jan 5, 2024
cb7edd8
Create contract for writing bytes
jorbush Jan 9, 2024
714bd35
Deploy bytes contract
jorbush Jan 9, 2024
3f91e20
update full node to use rpc providers
toni-calvin Jan 10, 2024
c3373d2
fmt
toni-calvin Jan 10, 2024
ce4de82
add colored prints
toni-calvin Jan 10, 2024
cc32b58
add better debug logs
toni-calvin Jan 10, 2024
f8edfa4
add better logs
toni-calvin Jan 10, 2024
27ab211
Merge branch 'validium_mode_pubdata_0' of github.com:lambdaclass/zksy…
toni-calvin Jan 11, 2024
240c0a8
add erc20 contract and update test
toni-calvin Jan 12, 2024
c683f9a
update logs
toni-calvin Jan 15, 2024
4699ae9
print gas used
toni-calvin Jan 15, 2024
215b9d9
Use `ETH_SENDER_SENDER_VALIDIUM_MODE` to check whether we need to cha…
Oppen Jan 11, 2024
5d3d96c
wip
Oppen Jan 11, 2024
c71ff9c
wip 2: just change runtime behavior
Oppen Jan 15, 2024
183957d
Restore original values
Oppen Jan 15, 2024
cff8bfd
Add validium example readme (#34)
jordibonet-lambdaclass Jan 15, 2024
5bbeb3c
Only keep changes for vm_latest
Oppen Jan 15, 2024
aad41bb
Fix submodule
Oppen Jan 15, 2024
64e639a
Fix integration test license
Oppen Jan 15, 2024
557ec52
change contracts branch
mationorato Jan 15, 2024
b9ceb92
Remove l1 gas data
Jan 15, 2024
3aa9f50
Update submodule branch
Jan 15, 2024
5fa2e92
add basics scripts
ColoCarletti Jan 15, 2024
479061d
fix: update output readme (#39)
jordibonet-lambdaclass Jan 16, 2024
ef48b51
feat: remove logs pubdata (#42)
toni-calvin Jan 16, 2024
5c3dfc5
feat: refactor readme example (#44)
jordibonet-lambdaclass Jan 17, 2024
dfc053f
refactor script
ColoCarletti Jan 17, 2024
07f04fc
fix fmt
ColoCarletti Jan 17, 2024
9342b29
add graph script
ColoCarletti Jan 17, 2024
5c14f47
fix typo
ColoCarletti Jan 17, 2024
97c226d
add echo
ColoCarletti Jan 19, 2024
fbdbd95
move script
ColoCarletti Jan 19, 2024
07300dd
merge base branch
ColoCarletti Jan 19, 2024
12be3d2
add script info to readme
ColoCarletti Jan 19, 2024
06116e2
move scirpts files
ColoCarletti Jan 19, 2024
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
5 changes: 5 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[submodule "contracts"]
path = contracts
url = https://github.com/matter-labs/era-contracts.git

[submodule "era-contracts-lambda"]
path = era-contracts-lambda
url = https://github.com/lambdaclass/era-contracts.git
branch = validium_mode
37 changes: 25 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ members = [
"sdk/zksync-rs",

# Validium Example
"zksync_full_stack",
"validium_mode_example",
]
resolver = "2"

Expand Down
7 changes: 0 additions & 7 deletions core/lib/multivm/src/versions/vm_latest/old_vm/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use zk_evm_1_4_0::{
},
};
use zksync_state::WriteStorage;
use zksync_system_constants::L1_GAS_PER_PUBDATA_BYTE;
use zksync_types::{Address, U256};

use crate::vm_latest::{
Expand Down Expand Up @@ -96,12 +95,6 @@ pub(crate) fn precompile_calls_count_after_timestamp(
sorted_timestamps.len() - sorted_timestamps.partition_point(|t| *t < from_timestamp)
}

pub(crate) fn eth_price_per_pubdata_byte(l1_gas_price: u64) -> u64 {
// This value will typically be a lot less than u64
// unless the gas price on L1 goes beyond tens of millions of gwei
l1_gas_price * (L1_GAS_PER_PUBDATA_BYTE as u64)
}

pub(crate) fn vm_may_have_ended_inner<S: WriteStorage, H: HistoryMode>(
vm: &ZkSyncVmState<S, H>,
) -> Option<VmExecutionResult> {
Expand Down
12 changes: 10 additions & 2 deletions core/lib/multivm/src/versions/vm_latest/oracles/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,17 @@ fn get_pubdata_price_bytes(initial_value: U256, final_value: U256, is_initial: b
compress_with_best_strategy(initial_value, final_value).len() as u32;

if is_initial {
(BYTES_PER_DERIVED_KEY as u32) + compressed_value_size
println!("is initial");
let pubdata_price_bytes = (BYTES_PER_DERIVED_KEY as u32) + compressed_value_size;
println!("pubdata_price_bytes: {}", pubdata_price_bytes);
pubdata_price_bytes
// (BYTES_PER_DERIVED_KEY as u32) + compressed_value_size
} else {
(BYTES_PER_ENUMERATION_INDEX as u32) + compressed_value_size
println!("is repeated");
let pubdata_price_bytes = (BYTES_PER_ENUMERATION_INDEX as u32) + compressed_value_size;
println!("pubdata_price_bytes: {}", pubdata_price_bytes);
pubdata_price_bytes
// (BYTES_PER_ENUMERATION_INDEX as u32) + compressed_value_size
}
}

Expand Down
6 changes: 2 additions & 4 deletions core/lib/multivm/src/versions/vm_latest/tracers/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ use crate::{
vm_latest::{
bootloader_state::BootloaderState,
constants::{BOOTLOADER_HEAP_PAGE, OPERATOR_REFUNDS_OFFSET, TX_GAS_LIMIT_OFFSET},
old_vm::{
events::merge_events, history_recorder::HistoryMode, memory::SimpleMemory,
utils::eth_price_per_pubdata_byte,
},
old_vm::{events::merge_events, history_recorder::HistoryMode, memory::SimpleMemory},
tracers::{
traits::VmTracer,
utils::{
gas_spent_on_bytecodes_and_long_messages_this_opcode, get_vm_hook_params, VmHook,
},
},
types::internals::ZkSyncVmState,
utils::fee::eth_price_per_pubdata_byte,
},
};

Expand Down
10 changes: 8 additions & 2 deletions core/lib/multivm/src/versions/vm_latest/utils/fee.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
//! Utility functions for vm
use zksync_system_constants::MAX_GAS_PER_PUBDATA_BYTE;
use zksync_system_constants::{L1_GAS_PER_PUBDATA_BYTE, MAX_GAS_PER_PUBDATA_BYTE};
use zksync_utils::ceil_div;

use crate::vm_latest::old_vm::utils::eth_price_per_pubdata_byte;
pub(crate) fn eth_price_per_pubdata_byte(l1_gas_price: u64) -> u64 {
// This value will typically be a lot less than u64
// unless the gas price on L1 goes beyond tens of millions of gwei
// TODO: make this check only once
let validium_mode = std::env::var("ETH_SENDER_SENDER_VALIDIUM_MODE") == Ok("true".to_string());
l1_gas_price * (L1_GAS_PER_PUBDATA_BYTE as u64) * (!validium_mode as u64)
}

/// Calculates the amount of gas required to publish one byte of pubdata
pub fn base_fee_to_gas_per_pubdata(l1_gas_price: u64, base_fee: u64) -> u64 {
Expand Down
6 changes: 6 additions & 0 deletions core/tests/ts-integration/src/diff_output.txt

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions core/tests/ts-integration/src/diff_validium.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions core/tests/ts-integration/src/rollup_output.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions core/tests/ts-integration/src/validium_output.txt

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions era-contracts-lambda
Submodule era-contracts-lambda added at 648773
139 changes: 47 additions & 92 deletions validium.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,54 @@

In order to start the node as a validium:

- Make sure `zk` has been built and then run `zk init --validium-mode`. This will set up the Ethereum node with the
validium contracts, and also define an env var which the server will pick up in order to run as a validium node
- Make sure `zk` has been built and then run `zk && zk clean --all && zk init --validium-mode` This will set up the
Ethereum node with the validium contracts, and also define an env var which the server will pick up in order to run as
a validium node.
- Start the server (`zk server`)
- Execute transactions. For testing, `cargo run --release --bin zksync_full_stack` inits a wallet, deploys a contract
and executes a tx
- Query the node for the tx hash (output of the zksync_full_stack binary):
- Execute transactions. For testing, run `cargo run --release --bin validium_mode_example`, this test does the
following:
- Inits a wallet
- Deposits some funds into the wallet
- Deploys a sample ERC20 contract
- Query the contract for the token name and symbol
- Mint 100000 tokens into the address `CD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826`
- Transfer 1000 tokens from `CD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826` to `bBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB`

### Logs and prints

- For each transaction, we use the rpc client to query the transaction details and print them out. The following fields
are printed:
- `Transaction hash`: The hash of the transaction
- `Transaction gas used`: The gas used to perform this transaction.
- `L2 fee`: The total cost of this transaction.
- `L1 Gas`: The gas borrowed in order to run the transaction. Unused gas will be returned.
- `L1 Gas price`: The gas price used to run the transaction.

### Example output

```
curl -X POST -H 'content-type: application/json' 127.0.0.1:3050 -d '{"jsonrpc": "2.0", "id": 1, "method": "zks_getTransactionDetails", "params": ["0xa1ae38705aa3f3e65ec9f2ce2692aaff6e531d56031998d33ac3e015bf676680"]}' | jq


{
"jsonrpc": "2.0",
"result": {
"isL1Originated": false,
"status": "verified",
"fee": "0x1c72f149e000",
"gasPerPubdata": "0xc350",
"initiatorAddress": "0x36615cf349d7f6344891b1e7ca7c72883f5dc049",
"receivedAt": "2023-11-02T18:00:02.808Z",
"ethCommitTxHash": "0xf2daa7aeba7ded2c4dcb1ceb7892c6fa34ef57fec3a175f8593d313bf2c5d314",
"ethProveTxHash": "0x7b9385ac38937207ea54403e0dbefdd9000a33b01b61d777c134057863272b6c",
"ethExecuteTxHash": null
},
"id": 1
}
```

Not all the eth-related transaction hashes might be displayed depending on how much time passes between sending the tx
and querying the node, since the transactions are bundled into batches which get entered into the batch pipeline

- query the geth node with the `ethCommitTxHash` (remember to use your hash and not the one in the example below)

```
curl -X POST -H 'content-type: application/json' 127.0.0.1:8545 -d '{"jsonrpc": "2.0", "id": 1, "method": "eth_getTransactionByHash", "params": ["0xf2daa7aeba7ded2c4dcb1ceb7892c6fa34ef57fec3a175f8593d313bf2c5d314"]}' | jq

{
"jsonrpc": "2.0",
"id": 1,
"result": {
"blockHash": "0x67cf02bcb7752d71df784c5c1e21b860e78a8a2963c18833d5c6240eb141b4e3",
"blockNumber": "0x1d3b6",
"from": "0xde03a0b5963f75f1c8485b355ff6d30f3093bde7",
"gas": "0x3d0900",
"gasPrice": "0x3b9aca07",
"maxFeePerGas": "0x3b9aca0a",
"maxPriorityFeePerGas": "0x3b9aca00",
"hash": "0xf2daa7aeba7ded2c4dcb1ceb7892c6fa34ef57fec3a175f8593d313bf2c5d314",
"input": "0x701f58c500000000000000000000000000000000000000000000000000000000000000035a75c9fdb0ef4c97c74e433ac053b9afec3ad23563063b23890091324539c126000000000000000000000000000000000000000000000000000000000000005f0000000000000000000000000000000000000000000000000000000000000000c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a4706c86889f42762dc2a4d547d5daab9982825dba1944ea75d19c5b72a6f8816c0a000000000000000000000000000000000000000000000000000000006543e10ec341dfc85d9f9402a6d16d2b863e1c30ac8cc8853ac2c024feb960c7c121d51b0000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000006543e12600000000000000000000000000000000000000000000000000000000000000621cd68c6b5e0da5ab1354bd7523bb510155acf5ed85c2aa199b28695f90f7633a0000000000000000000000000000000000000000000000000000000000000000c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a4707fb8d37f4ce4c9fdd84f5965ec76dd3ccb89e049f92cf155bc2ddc71bfad8c5b470af434b6d50228459d0ed2e371f237933c9f3f4c3e1c1417e4b3d3fcd2b4b6000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000003e0000000000000000000000000000000000000000000000000000000000000026800000000000000000000000000000000000000000000800b00000000000000000000000000000000000000000000000000000000000000045a75c9fdb0ef4c97c74e433ac053b9afec3ad23563063b23890091324539c12600000001000000000000000000000000000000000000800b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000006543e1260000000000000000000000006543e1270001000100000000000000000000000000000000000080010000000000000000000000000000000000000000000000000000000000000005c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470000100010000000000000000000000000000000000008001000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000001000100000000000000000000000000000000000080080000000000000000000000000000000000000000000000000000000000000000fef7bd9f889811e59e4076a0174087135f080177302763019adaf531257e3a8700010001000000000000000000000000000000000000800800000000000000000000000000000000000000000000000000000000000000011c204342a59bae89d48807f747a363671740f0615de30530e12e8bbfc450ece000010001000000000000000000000000000000000000800800000000000000000000000000000000000000000000000000000000000000022b4fd9c63abc805a3c11cbc1264eb6c9bcdec326c3f8b41eb92d4b2ffef611c2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000010203090000000000000000000000000000000000000000",
"nonce": "0x38",
"to": "0x06b7877ba1a9bb15e5ae1aaac8c651969106a57a",
"transactionIndex": "0x0",
"value": "0x0",
"type": "0x2",
"accessList": [],
"chainId": "0x9",
"v": "0x0",
"r": "0x9f038380597cdc035e60916dd9bbd20d0b1a02f32eac7def479cf1f24633b100",
"s": "0x79c004b4b18d7132f980c501bc14804e6bd78cccc76a8e5fff6631b1403c832c"
}
}
```

if the same process is followed for a non-validium node, there is more data in the input field (see after the `01020309`
sub-array which was put in place for detecting these changes):

```
curl -X POST -H 'content-type: application/json' 127.0.0.1:8545 -d '{"jsonrpc": "2.0", "id": 1, "method": "eth_getTransactionByHash", "params": ["0xf2daa7aeba7ded2c4dcb1ceb7892c6fa34ef57fec3a175f8593d313bf2c5d314"]}' | jq


{
"jsonrpc": "2.0",
"id": 1,
"result": {
"blockHash": "0x2dc87056f8c8b7eba3f122da43bc8db0672492aa5915f944b4842b4ab57f21f8",
"blockNumber": "0x1d628",
"from": "0xde03a0b5963f75f1c8485b355ff6d30f3093bde7",
"gas": "0x3d0900",
"gasPrice": "0x3b9aca07",
"maxFeePerGas": "0x3b9aca0a",
"maxPriorityFeePerGas": "0x3b9aca00",
"hash": "0xf2daa7aeba7ded2c4dcb1ceb7892c6fa34ef57fec3a175f8593d313bf2c5d314",
"input": "0x701f58c50000000000000000000000000000000000000000000000000000000000000003abcc4c7388651eb753807814b2c5fce3c85d7501cf89dad6e5669df9c5ede5c8000000000000000000000000000000000000000000000000000000000000005e0000000000000000000000000000000000000000000000000000000000000000c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a4706c86889f42762dc2a4d547d5daab9982825dba1944ea75d19c5b72a6f8816c0a000000000000000000000000000000000000000000000000000000006543e38b2da007083449852aa93d1b2e48cf237dafe6d11eeee14dca1a2abb3525526b100000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000006543e3a3000000000000000000000000000000000000000000000000000000000000006122173cd9a05bc38fb465363fd6aed57d93141f8bfdbc75d19ea48f2af3beb52d0000000000000000000000000000000000000000000000000000000000000000c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a4706294138c217f7427db7f8e19c4d6bc144331827e856e379016e8b66cf9fc05e6470af434b6d50228459d0ed2e371f237933c9f3f4c3e1c1417e4b3d3fcd2b4b6000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000003e0000000000000000000000000000000000000000000000000000000000000026800000000000000000000000000000000000000000000800b0000000000000000000000000000000000000000000000000000000000000004abcc4c7388651eb753807814b2c5fce3c85d7501cf89dad6e5669df9c5ede5c800000001000000000000000000000000000000000000800b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000006543e3a30000000000000000000000006543e3a40001000100000000000000000000000000000000000080010000000000000000000000000000000000000000000000000000000000000005c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470000100010000000000000000000000000000000000008001000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000001000100000000000000000000000000000000000080080000000000000000000000000000000000000000000000000000000000000000fef7bd9f889811e59e4076a0174087135f080177302763019adaf531257e3a870001000100000000000000000000000000000000000080080000000000000000000000000000000000000000000000000000000000000001dc5fdb827f4535cf6d6552a98d75494dae47466825a5f2a2c8b28b15bc0fcf7a0001000100000000000000000000000000000000000080080000000000000000000000000000000000000000000000000000000000000002e7748039ceb32bda1670b1a1520470e929f74b5ce1bf29df23258c0a5301e225000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000015b00000000000000000102030900000000010001460400032c1818e4770f08c05b28829d7d5f9d401d492c7432c166dfecf4af04238ea3230058d1d0f8a5baff47f324e7d9b05e62deb67355dd311d0e6a6bf9e7c41ecec108ca1bbc31cc849a8092a36f9a321e17412dee200b956038af1c2dc83430a0e8b00058203175654ae01a169ff0277c30e5d00fb332f32855bf6204abe9a7168d00f3cf368ce957aed709b985423cd3ba11615de01ecafa15eb9a11bc6cdef4f6327900abcc4c7388651eb753807814b2c5fce3c85d7501cf89dad6e5669df9c5ede5c800000058090100000030311c72f149e00000000053321c72f149e0000000003389010000000000000000000000000000001800000034890200000000000000000000000000000018000000378902000000000000000000000000000000180000005df20c9390fffffffffffffffffffffffffffffffffffffffffffffffffffffc0000000000",
"nonce": "0x49",
"to": "0x540c25479dd1f0250e336b3abb21485afa7d8aba",
"transactionIndex": "0x0",
"value": "0x0",
"type": "0x2",
"accessList": [],
"chainId": "0x9",
"v": "0x0",
"r": "0x69874fbaab6e00210cbdc03b15aab565eeb0c706e1845601d194e243e71bd618",
"s": "0xdab5e41a8cfbe73295f57e1bd1ac409170ba251eefc822b8e2c6936aaf3ed"
}
}
Deposit transaction hash: 0xc01cf32c699943f8d751047514393a5e98d8cbeaa128fa50c32a3d7804b876a5
Deploy
Contract address: 0xf2fcc18ed5072b48c0a076693eca72fe840b3981
Transaction hash 0x9e817fcc8eeeda001793c9142161a11e3fd3ef3c64523be1f5c11b6cbff7b64f
Transaction gas used 161163
L2 fee: 40290750000000
L1 Gas: 4000000
L1 Gas price: 1000000007

Mint
Transaction hash 0x0e9bcc26addf1edfe0993767cc2d6ec959a135dc3087b63b5fc9d54d7ed854ef
Transaction gas used 124046
L2 fee: 31011500000000
L1 max fee per gas: 1000000010
L1 Gas: 4000000
L1 Gas price: 1000000007

Transfer 1000
Transaction hash 0x5a1f7130024b73c2d3de5256a72bddbc703983d69d3ad0f3f64d8e6122e0e85a
Transaction gas used 125466
L2 fee: 31366500000000
L1 max fee per gas: 1000000010
L1 Gas: 4000000
L1 Gas price: 1000000007
```
File renamed without changes.
39 changes: 39 additions & 0 deletions validium_mode_example/BytesWriter.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[
{
"inputs": [
{
"internalType": "bytes",
"name": "_message",
"type": "bytes"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "readBytes",
"outputs": [
{
"internalType": "bytes",
"name": "",
"type": "bytes"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes",
"name": "_message",
"type": "bytes"
}
],
"name": "writeBytes",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
Loading