Skip to content

Commit 847bcb3

Browse files
feat: report native and pubdata in batch output (#170)
## What ❔ <!-- What are the changes this PR brings about? --> <!-- Example: This PR adds a PR template to the repo. --> <!-- (For bigger PRs adding more context is appreciated) --> ## Why ❔ <!-- Why are these changes done? What goal do they contribute to? What are the principles behind them? --> <!-- The `Why` has to be clear to non-Matter Labs entities running their own ZK Chain --> <!-- Example: PR templates ensure PR reviewers, observers, and future iterators are in context about the evolution of repos. --> ## Is this a breaking change? - [ ] Yes - [ ] No ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted. --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 0ede602 commit 847bcb3

File tree

17 files changed

+178
-104
lines changed

17 files changed

+178
-104
lines changed

basic_bootloader/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,5 @@ evm-compatibility = ["code_in_kernel_space", "transfers_to_kernel_space", "syste
3232
cycle_marker = ["system_hooks/cycle_marker", "cycle_marker/log_to_file"]
3333
resources_for_tester = []
3434
unlimited_native = []
35-
report_native = []
3635
disable_system_contracts = []
3736
error_origins = ["zk_ee/error_origins"]

basic_bootloader/src/bootloader/account_models/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ pub struct TxProcessingResult<'a> {
6161
pub is_upgrade_tx: bool,
6262
pub gas_used: u64,
6363
pub gas_refunded: u64,
64-
#[cfg(feature = "report_native")]
65-
pub native_used: u64,
64+
65+
pub computational_native_used: u64,
66+
67+
pub pubdata_used: u64,
6668
}
6769

6870
pub trait AccountModel<S: EthereumLikeTypes>

basic_bootloader/src/bootloader/gas_helpers.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ use zk_ee::system::{Computational, Resources};
77

88
use super::*;
99

10-
/// Returns the resources for the transaction and the withheld resources.
11-
/// The withheld resources are the resources that are withheld from the transaction's
12-
/// execution to ensure that it does not use too many native computational resources.
13-
/// They are reclaimed at the end of the transaction and used to charge the pubdata.
10+
pub struct ResourcesForTx<S: EthereumLikeTypes> {
11+
// Resources to run the transaction.
12+
// These will be capped to MAX_NATIVE_COMPUTATIONAL, to prevent
13+
// transaction from using too many native computational resources.
14+
pub main_resources: S::Resources,
15+
/// Resources in excess of MAX_NATIVE_COMPUTATIONAL.
16+
/// These resources can only be used for paying for pubdata.
17+
pub withheld: S::Resources,
18+
/// Computational native charged for as intrinsic
19+
pub intrinsic_computational_native_charged: u64,
20+
}
21+
1422
pub fn get_resources_for_tx<S: EthereumLikeTypes>(
1523
gas_limit: u64,
1624
native_per_pubdata: U256,
@@ -19,7 +27,7 @@ pub fn get_resources_for_tx<S: EthereumLikeTypes>(
1927
intrinsic_gas: usize,
2028
intrinsic_pubdata: usize,
2129
intrinsic_native: usize,
22-
) -> Result<(S::Resources, S::Resources), TxError> {
30+
) -> Result<ResourcesForTx<S>, TxError> {
2331
// TODO: operator trusted gas limit?
2432

2533
// This is the real limit, which we later use to compute native_used.
@@ -49,11 +57,11 @@ pub fn get_resources_for_tx<S: EthereumLikeTypes>(
4957
// EVM tester requires high native limits, so for it we never hold off resources.
5058
// But for the real world, we bound the available resources.
5159

52-
#[allow(unused_variables)]
53-
let withheld_resources = S::Resources::from_ergs(Ergs(0));
60+
#[cfg(feature = "resources_for_tester")]
61+
let withheld = S::Resources::from_ergs(Ergs(0));
5462

5563
#[cfg(not(feature = "resources_for_tester"))]
56-
let (native_limit, withheld_resources) = if native_limit <= MAX_NATIVE_COMPUTATIONAL {
64+
let (native_limit, withheld) = if native_limit <= MAX_NATIVE_COMPUTATIONAL {
5765
(native_limit, S::Resources::from_ergs(Ergs(0)))
5866
} else {
5967
let withheld =
@@ -70,9 +78,14 @@ pub fn get_resources_for_tx<S: EthereumLikeTypes>(
7078
// Charge for calldata and intrinsic native
7179
let (calldata_gas, calldata_native) = cost_for_calldata(calldata)?;
7280

81+
let intrinsic_computational_native_charged = calldata_native
82+
.checked_add(intrinsic_native as u64)
83+
.ok_or(TxError::Validation(
84+
errors::InvalidTransaction::OutOfNativeResourcesDuringValidation,
85+
))?;
86+
7387
let native_limit = native_limit
74-
.checked_sub(calldata_native)
75-
.and_then(|native| native.checked_sub(intrinsic_native as u64))
88+
.checked_sub(intrinsic_computational_native_charged)
7689
.ok_or(TxError::Validation(
7790
errors::InvalidTransaction::OutOfNativeResourcesDuringValidation,
7891
))?;
@@ -98,8 +111,12 @@ pub fn get_resources_for_tx<S: EthereumLikeTypes>(
98111
let ergs = gas_limit_for_tx
99112
.checked_mul(ERGS_PER_GAS)
100113
.ok_or(internal_error!("glft*EPF"))?;
101-
let resources = S::Resources::from_ergs_and_native(Ergs(ergs), native_limit);
102-
Ok((resources, withheld_resources))
114+
let main_resources = S::Resources::from_ergs_and_native(Ergs(ergs), native_limit);
115+
Ok(ResourcesForTx {
116+
main_resources,
117+
withheld,
118+
intrinsic_computational_native_charged,
119+
})
103120
}
104121
}
105122
///
@@ -151,21 +168,21 @@ pub fn get_resources_to_charge_for_pubdata<S: EthereumLikeTypes>(
151168
/// spent pubdata.
152169
/// If base_pubdata is Some, it's discounted from the current
153170
/// pubdata counter.
154-
/// Returns if the check succeeded and the resources to charge
155-
/// for pubdata.
171+
/// Returns if the check succeeded, the resources to charge
172+
/// for pubdata and the net pubdata used.
156173
///
157174
pub fn check_enough_resources_for_pubdata<S: EthereumLikeTypes>(
158175
system: &mut System<S>,
159176
native_per_pubdata: U256,
160177
resources: &S::Resources,
161178
base_pubdata: Option<u64>,
162-
) -> Result<(bool, S::Resources), InternalError> {
163-
let (_, resources_for_pubdata) =
179+
) -> Result<(bool, S::Resources, u64), InternalError> {
180+
let (pubdata_used, resources_for_pubdata) =
164181
get_resources_to_charge_for_pubdata(system, native_per_pubdata, base_pubdata)?;
165182
let _ = system.get_logger().write_fmt(format_args!(
166183
"Checking gas for pubdata, resources_for_pubdata: {:?}, resources: {:?}\n",
167184
resources_for_pubdata, resources
168185
));
169186
let enough = resources.has_enough(&resources_for_pubdata);
170-
Ok((enough, resources_for_pubdata))
187+
Ok((enough, resources_for_pubdata, pubdata_used))
171188
}

basic_bootloader/src/bootloader/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ impl<S: EthereumLikeTypes> BasicBootloader<S> {
282282
contract_address,
283283
gas_used: tx_processing_result.gas_used,
284284
gas_refunded: tx_processing_result.gas_refunded,
285-
#[cfg(feature = "report_native")]
286-
native_used: tx_processing_result.native_used,
285+
computational_native_used: tx_processing_result.computational_native_used,
286+
pubdata_used: tx_processing_result.pubdata_used,
287287
}));
288288

289289
let mut keccak = Keccak256::new();

0 commit comments

Comments
 (0)