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

refactor: estimate gas and fee #9889

Merged
merged 10 commits into from
Nov 12, 2024
29 changes: 14 additions & 15 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ library Constants {
uint256 internal constant DEFAULT_TEARDOWN_GAS_LIMIT = 12000000;
uint256 internal constant MAX_L2_GAS_PER_ENQUEUED_CALL = 12000000;
uint256 internal constant DEFAULT_MAX_FEE_PER_GAS = 10;
uint256 internal constant DEFAULT_INCLUSION_FEE = 0;
uint256 internal constant DA_BYTES_PER_FIELD = 32;
uint256 internal constant DA_GAS_PER_BYTE = 16;
uint256 internal constant FIXED_DA_GAS = 512;
Expand Down Expand Up @@ -160,7 +159,7 @@ library Constants {
uint256 internal constant AZTEC_ADDRESS_LENGTH = 1;
uint256 internal constant GAS_FEES_LENGTH = 2;
uint256 internal constant GAS_LENGTH = 2;
uint256 internal constant GAS_SETTINGS_LENGTH = 7;
uint256 internal constant GAS_SETTINGS_LENGTH = 6;
uint256 internal constant CALL_CONTEXT_LENGTH = 4;
uint256 internal constant CONTENT_COMMITMENT_LENGTH = 4;
uint256 internal constant CONTRACT_INSTANCE_LENGTH = 16;
Expand Down Expand Up @@ -199,13 +198,13 @@ library Constants {
uint256 internal constant ROLLUP_VALIDATION_REQUESTS_LENGTH = 2;
uint256 internal constant STATE_REFERENCE_LENGTH = 8;
uint256 internal constant TREE_SNAPSHOTS_LENGTH = 8;
uint256 internal constant TX_CONTEXT_LENGTH = 9;
uint256 internal constant TX_REQUEST_LENGTH = 13;
uint256 internal constant TX_CONTEXT_LENGTH = 8;
uint256 internal constant TX_REQUEST_LENGTH = 12;
uint256 internal constant TOTAL_FEES_LENGTH = 1;
uint256 internal constant HEADER_LENGTH = 24;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 500;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 499;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 866;
uint256 internal constant PRIVATE_CONTEXT_INPUTS_LENGTH = 38;
uint256 internal constant PRIVATE_CONTEXT_INPUTS_LENGTH = 37;
uint256 internal constant PUBLIC_CONTEXT_INPUTS_LENGTH = 41;
uint256 internal constant FEE_RECIPIENT_LENGTH = 2;
uint256 internal constant AGGREGATION_OBJECT_LENGTH = 16;
Expand All @@ -215,20 +214,20 @@ library Constants {
uint256 internal constant NUM_PUBLIC_VALIDATION_REQUEST_ARRAYS = 5;
uint256 internal constant PUBLIC_VALIDATION_REQUESTS_LENGTH = 834;
uint256 internal constant PUBLIC_DATA_UPDATE_REQUEST_LENGTH = 3;
uint256 internal constant COMBINED_ACCUMULATED_DATA_LENGTH = 547;
uint256 internal constant TX_CONSTANT_DATA_LENGTH = 35;
uint256 internal constant COMBINED_CONSTANT_DATA_LENGTH = 44;
uint256 internal constant COMBINED_ACCUMULATED_DATA_LENGTH = 545;
uint256 internal constant TX_CONSTANT_DATA_LENGTH = 34;
uint256 internal constant COMBINED_CONSTANT_DATA_LENGTH = 43;
uint256 internal constant PRIVATE_ACCUMULATED_DATA_LENGTH = 1064;
uint256 internal constant PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1878;
uint256 internal constant PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1877;
uint256 internal constant PUBLIC_ACCUMULATED_DATA_LENGTH = 1023;
uint256 internal constant NUM_PUBLIC_ACCUMULATED_DATA_ARRAYS = 8;
uint256 internal constant PRIVATE_TO_PUBLIC_ACCUMULATED_DATA_LENGTH = 578;
uint256 internal constant PRIVATE_TO_PUBLIC_ACCUMULATED_DATA_LENGTH = 576;
uint256 internal constant PRIVATE_TO_AVM_ACCUMULATED_DATA_LENGTH = 160;
uint256 internal constant NUM_PRIVATE_TO_AVM_ACCUMULATED_DATA_ARRAYS = 3;
uint256 internal constant PRIVATE_TO_PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1199;
uint256 internal constant PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2932;
uint256 internal constant VM_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2341;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 601;
uint256 internal constant PRIVATE_TO_PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1196;
uint256 internal constant PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2931;
uint256 internal constant VM_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2340;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 600;
uint256 internal constant CONSTANT_ROLLUP_DATA_LENGTH = 13;
uint256 internal constant BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH = 30;
uint256 internal constant BLOCK_ROOT_OR_BLOCK_MERGE_PUBLIC_INPUTS_LENGTH = 90;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl TailOutputComposer {
let mut output = KernelCircuitPublicInputs::empty();
output.rollup_validation_requests = source.validation_requests.for_rollup;
output.end = self.build_combined_accumulated_data();
output.gas_used = meter_gas_used(output.end);
output.constants =
CombinedConstantData::combine(source.constants, GlobalVariables::empty());
output.fee_payer = source.fee_payer;
Expand Down Expand Up @@ -65,7 +66,6 @@ impl TailOutputComposer {
0,
|len, l: ScopedLogHash| len + l.log_hash.length,
);
data.gas_used = meter_gas_used(data);
data
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ impl TailOutputValidator {

fn validate_gas_used(self) {
let gas_used = meter_gas_used(self.output.end);
assert(self.output.end.gas_used == gas_used, "incorrect metered gas used");
assert(self.output.gas_used == gas_used, "incorrect metered gas used");

let limits = self.previous_kernel.constants.tx_context.gas_settings.gas_limits;
assert(self.output.end.gas_used.within(limits), "The gas used exceeds the gas limits");
assert(self.output.gas_used.within(limits), "The gas used exceeds the gas limits");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ mod split_to_public;
use crate::components::{
private_kernel_circuit_public_inputs_composer::PrivateKernelCircuitPublicInputsComposer,
tail_to_public_output_composer::{
meter_gas_used::{meter_gas_used_non_revertible, meter_gas_used_revertible},
split_to_public::split_to_public,
meter_gas_used::meter_gas_used, split_to_public::split_to_public,
},
};
use dep::types::abis::{
kernel_circuit_public_inputs::{
PrivateKernelCircuitPublicInputs, PrivateToPublicKernelCircuitPublicInputs,
},
validation_requests::PublicValidationRequests,
use dep::types::abis::kernel_circuit_public_inputs::{
PrivateKernelCircuitPublicInputs, PrivateToPublicKernelCircuitPublicInputs,
};

pub struct TailToPublicOutputComposer {
Expand All @@ -31,21 +27,23 @@ impl TailToPublicOutputComposer {
pub unconstrained fn finish(self) -> PrivateToPublicKernelCircuitPublicInputs {
let source = self.output_composer.public_inputs;

let mut (non_revertible_accumulated_data, revertible_accumulated_data) =
let (non_revertible_accumulated_data, revertible_accumulated_data) =
split_to_public(source.end, source.min_revertible_side_effect_counter);

non_revertible_accumulated_data.gas_used =
meter_gas_used_non_revertible(non_revertible_accumulated_data);

revertible_accumulated_data.gas_used =
meter_gas_used_revertible(revertible_accumulated_data);
let gas_used = meter_gas_used(
non_revertible_accumulated_data,
revertible_accumulated_data,
source.public_teardown_call_request,
source.constants.tx_context.gas_settings.teardown_gas_limits,
);

let mut output = PrivateToPublicKernelCircuitPublicInputs {
constants: source.constants,
rollup_validation_requests: source.validation_requests.for_rollup(),
non_revertible_accumulated_data,
revertible_accumulated_data,
public_teardown_call_request: source.public_teardown_call_request,
gas_used,
fee_payer: source.fee_payer,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ use dep::types::{
accumulated_data::PrivateToPublicAccumulatedData,
gas::Gas,
log_hash::{LogHash, ScopedLogHash},
public_call_request::PublicCallRequest,
},
constants::{
DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE, FIXED_AVM_STARTUP_L2_GAS, L2_GAS_PER_LOG_BYTE,
L2_GAS_PER_NOTE_HASH, L2_GAS_PER_NULLIFIER,
},
traits::is_empty,
utils::arrays::array_length,
};

fn meter_gas_used(data: PrivateToPublicAccumulatedData) -> Gas {
fn meter_accumulated_data_gas_used(data: PrivateToPublicAccumulatedData) -> Gas {
let mut metered_da_bytes = 0;
let mut metered_l2_gas = 0;

Expand Down Expand Up @@ -45,10 +47,20 @@ fn meter_gas_used(data: PrivateToPublicAccumulatedData) -> Gas {
Gas::new(metered_da_bytes * DA_GAS_PER_BYTE, metered_l2_gas)
}

pub fn meter_gas_used_non_revertible(data: PrivateToPublicAccumulatedData) -> Gas {
meter_gas_used(data) + Gas::tx_overhead()
}
pub fn meter_gas_used(
non_revertible_data: PrivateToPublicAccumulatedData,
revertible_data: PrivateToPublicAccumulatedData,
public_teardown_call_request: PublicCallRequest,
teardown_gas_limits: Gas,
) -> Gas {
let teardown_gas = if is_empty(public_teardown_call_request) {
Gas::empty()
} else {
teardown_gas_limits
};

pub fn meter_gas_used_revertible(data: PrivateToPublicAccumulatedData) -> Gas {
meter_gas_used(data)
Gas::tx_overhead()
+ meter_accumulated_data_gas_used(non_revertible_data)
+ meter_accumulated_data_gas_used(revertible_data)
+ teardown_gas
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod tail_to_public_output_hints;

use crate::components::{
tail_to_public_output_composer::meter_gas_used::{
meter_gas_used_non_revertible, meter_gas_used_revertible,
},
tail_to_public_output_composer::meter_gas_used::meter_gas_used,
tail_to_public_output_validator::tail_to_public_output_hints::{
generate_tail_to_public_output_hints, TailToPublicOutputHints,
},
Expand All @@ -15,7 +13,7 @@ use dep::types::{
},
log_hash::{LogHash, NoteLogHash, ScopedEncryptedLogHash, ScopedLogHash},
note_hash::ScopedNoteHash,
nullifier::{Nullifier, ScopedNullifier},
nullifier::ScopedNullifier,
public_call_request::PublicCallRequest,
side_effect::Counted,
},
Expand Down Expand Up @@ -148,21 +146,14 @@ impl TailToPublicOutputValidator {
}

fn validate_gas_used(self) {
let gas_used = meter_gas_used_non_revertible(self.output.non_revertible_accumulated_data);
assert(
self.output.non_revertible_accumulated_data.gas_used == gas_used,
"incorrect metered non-revertible gas used",
);

let gas_used = meter_gas_used_revertible(self.output.revertible_accumulated_data);
assert(
self.output.revertible_accumulated_data.gas_used == gas_used,
"incorrect metered revertible gas used",
let gas_settings = self.output.constants.tx_context.gas_settings;
let gas_used = meter_gas_used(
self.output.non_revertible_accumulated_data,
self.output.revertible_accumulated_data,
self.output.public_teardown_call_request,
gas_settings.teardown_gas_limits,
);

let limits = self.previous_kernel.constants.tx_context.gas_settings.gas_limits;
let total_gas_used = self.output.non_revertible_accumulated_data.gas_used
+ self.output.revertible_accumulated_data.gas_used;
assert(total_gas_used.within(limits), "The gas used exceeds the gas limits");
assert_eq(self.output.gas_used, gas_used, "incorrect metered gas used");
assert(gas_used.within(gas_settings.gas_limits), "The gas used exceeds the gas limits");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ mod tests {
DA_GAS_PER_BYTE * DA_BYTES_PER_FIELD * 1,
L2_GAS_PER_NULLIFIER * 1,
);
assert_eq(public_inputs.end.gas_used, expected_gas_consumed);
assert_eq(public_inputs.gas_used, expected_gas_consumed);
}

#[test]
Expand All @@ -268,7 +268,7 @@ mod tests {
4 * DA_BYTES_PER_FIELD * DA_GAS_PER_BYTE,
1 * L2_GAS_PER_NULLIFIER,
),
public_inputs.end.gas_used,
public_inputs.gas_used,
);
}

Expand All @@ -290,7 +290,7 @@ mod tests {
(1 * DA_BYTES_PER_FIELD + 25) * DA_GAS_PER_BYTE,
1 * L2_GAS_PER_NULLIFIER + 25 * L2_GAS_PER_LOG_BYTE,
),
public_inputs.end.gas_used,
public_inputs.gas_used,
);
}

Expand Down
Loading
Loading