Skip to content

Commit

Permalink
Merge 76daa78 into 2cad3e5
Browse files Browse the repository at this point in the history
  • Loading branch information
LeilaWang authored Dec 17, 2024
2 parents 2cad3e5 + 76daa78 commit 2850d56
Show file tree
Hide file tree
Showing 31 changed files with 416 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct GasSettings {
Gas gas_limits;
Gas teardown_gas_limits;
GasFees max_fees_per_gas;
GasFees max_priority_fees_per_gas;
};

inline void read(uint8_t const*& it, GasSettings& gas_settings)
Expand All @@ -46,6 +47,7 @@ inline void read(uint8_t const*& it, GasSettings& gas_settings)
read(it, gas_settings.gas_limits);
read(it, gas_settings.teardown_gas_limits);
read(it, gas_settings.max_fees_per_gas);
read(it, gas_settings.max_priority_fees_per_gas);
}

struct GlobalVariables {
Expand Down
12 changes: 10 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,16 @@ void AvmTraceBuilder::pay_fee()
{
auto clk = static_cast<uint32_t>(main_trace.size()) + 1;

auto tx_fee = (public_inputs.global_variables.gas_fees.fee_per_da_gas * public_inputs.end_gas_used.da_gas) +
(public_inputs.global_variables.gas_fees.fee_per_l2_gas * public_inputs.end_gas_used.l2_gas);
auto gas_settings = public_inputs.gas_settings;
auto gas_fees = public_inputs.global_variables.gas_fees;
auto priority_fee_per_da_gas = std::min(gas_settings.max_priority_fees_per_gas.fee_per_da_gas,
gas_settings.max_fees_per_gas.fee_per_da_gas - gas_fees.fee_per_da_gas);
auto priority_fee_per_l2_gas = std::min(gas_settings.max_priority_fees_per_gas.fee_per_l2_gas,
gas_settings.max_fees_per_gas.fee_per_l2_gas - gas_fees.fee_per_l2_gas);
auto total_fee_per_da_gas = gas_fees.fee_per_da_gas + priority_fee_per_da_gas;
auto total_fee_per_l2_gas = gas_fees.fee_per_l2_gas + priority_fee_per_l2_gas;
auto tx_fee = (total_fee_per_da_gas * public_inputs.end_gas_used.da_gas) +
(total_fee_per_l2_gas * public_inputs.end_gas_used.l2_gas);

if (public_inputs.fee_payer == 0) {
vinfo("No one is paying the fee of ", tx_fee);
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#define TOTAL_FEES_LENGTH 1
#define PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH 867
#define AVM_ACCUMULATED_DATA_LENGTH 320
#define AVM_CIRCUIT_PUBLIC_INPUTS_LENGTH 1009
#define AVM_CIRCUIT_PUBLIC_INPUTS_LENGTH 1011
#define AVM_VERIFICATION_KEY_LENGTH_IN_FIELDS 86
#define AVM_PROOF_LENGTH_IN_FIELDS 4155
#define AVM_PUBLIC_COLUMN_MAX_SIZE 1024
Expand Down
20 changes: 10 additions & 10 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,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 = 6;
uint256 internal constant GAS_SETTINGS_LENGTH = 8;
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 @@ -180,30 +180,30 @@ 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 = 8;
uint256 internal constant TX_REQUEST_LENGTH = 12;
uint256 internal constant TX_CONTEXT_LENGTH = 10;
uint256 internal constant TX_REQUEST_LENGTH = 14;
uint256 internal constant TOTAL_FEES_LENGTH = 1;
uint256 internal constant TOTAL_MANA_USED_LENGTH = 1;
uint256 internal constant BLOCK_HEADER_LENGTH = 25;
uint256 internal constant BLOCK_HEADER_LENGTH_BYTES = 648;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 739;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 741;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 867;
uint256 internal constant PRIVATE_CONTEXT_INPUTS_LENGTH = 38;
uint256 internal constant PRIVATE_CONTEXT_INPUTS_LENGTH = 40;
uint256 internal constant FEE_RECIPIENT_LENGTH = 2;
uint256 internal constant AGGREGATION_OBJECT_LENGTH = 16;
uint256 internal constant SCOPED_READ_REQUEST_LEN = 3;
uint256 internal constant PUBLIC_DATA_READ_LENGTH = 3;
uint256 internal constant PRIVATE_VALIDATION_REQUESTS_LENGTH = 772;
uint256 internal constant COMBINED_ACCUMULATED_DATA_LENGTH = 902;
uint256 internal constant TX_CONSTANT_DATA_LENGTH = 35;
uint256 internal constant COMBINED_CONSTANT_DATA_LENGTH = 44;
uint256 internal constant TX_CONSTANT_DATA_LENGTH = 37;
uint256 internal constant COMBINED_CONSTANT_DATA_LENGTH = 46;
uint256 internal constant PRIVATE_ACCUMULATED_DATA_LENGTH = 1412;
uint256 internal constant PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2226;
uint256 internal constant PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2229;
uint256 internal constant PRIVATE_TO_PUBLIC_ACCUMULATED_DATA_LENGTH = 900;
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 = 1845;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 958;
uint256 internal constant PRIVATE_TO_PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1847;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 960;
uint256 internal constant CONSTANT_ROLLUP_DATA_LENGTH = 13;
uint256 internal constant BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH = 52;
uint256 internal constant BLOCK_ROOT_OR_BLOCK_MERGE_PUBLIC_INPUTS_LENGTH = 986;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl TailOutputValidatorBuilder {
Gas::new(DEFAULT_GAS_LIMIT, DEFAULT_GAS_LIMIT),
Gas::new(DEFAULT_TEARDOWN_GAS_LIMIT, DEFAULT_TEARDOWN_GAS_LIMIT),
GasFees::new(10, 10),
GasFees::new(3, 3),
);

let mut output = FixtureBuilder::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) mod public_data_tree;

mod private_tube_data_validator;
mod public_tube_data_validator;
pub(crate) mod private_base_rollup_output_composer;
pub(crate) mod validate_tube_data;

pub(crate) use private_tube_data_validator::PrivateTubeDataValidator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use types::abis::{gas::Gas, gas_fees::GasFees, gas_settings::GasSettings};

pub fn compute_transaction_fee(
gas_fees: GasFees,
gas_settings: GasSettings,
gas_used: Gas,
) -> Field {
let max_priority_fees = gas_settings.max_priority_fees_per_gas;
let max_fees = gas_settings.max_fees_per_gas;
// max_fees are guaranteed to be greater than or equal to gas_fees, which is checked in tube_data_validator.
let priority_fees = GasFees::new(
dep::types::utils::field::min(
max_priority_fees.fee_per_da_gas,
max_fees.fee_per_da_gas - gas_fees.fee_per_da_gas,
),
dep::types::utils::field::min(
max_priority_fees.fee_per_l2_gas,
max_fees.fee_per_l2_gas - gas_fees.fee_per_l2_gas,
),
);

let total_fees = GasFees::new(
gas_fees.fee_per_da_gas + priority_fees.fee_per_da_gas,
gas_fees.fee_per_l2_gas + priority_fees.fee_per_l2_gas,
);

gas_used.compute_fee(total_fees)
}

mod tests {
use super::compute_transaction_fee;
use types::abis::{gas::Gas, gas_fees::GasFees, gas_settings::GasSettings};

struct TestBuilder {
gas_fees: GasFees,
gas_settings: GasSettings,
gas_used: Gas,
}

impl TestBuilder {
pub fn new() -> Self {
let gas_fees = GasFees::new(12, 34);

let mut gas_settings = GasSettings::empty();
gas_settings.max_fees_per_gas = GasFees::new(56, 78);
gas_settings.max_priority_fees_per_gas = GasFees::new(5, 7);

let gas_used = Gas::new(2, 3);

TestBuilder { gas_fees, gas_settings, gas_used }
}

pub fn compute(self) -> Field {
compute_transaction_fee(self.gas_fees, self.gas_settings, self.gas_used)
}
}

#[test]
fn compute_transaction_fee_default() {
let builder = TestBuilder::new();
let fee = builder.compute();

// Make sure the following value matches the one in `transaction_fee.test.ts` in `circuits.js`.
// Paying gas_fees + max_priority_fees.
let expected_fee = 2 * (12 + 5) + 3 * (34 + 7);
assert_eq(fee, expected_fee);
}

#[test]
fn compute_transaction_fee_zero_priority_fees() {
let mut builder = TestBuilder::new();

builder.gas_settings.max_priority_fees_per_gas = GasFees::empty();

let fee = builder.compute();

// Make sure the following value matches the one in `transaction_fee.test.ts` in `circuits.js`.
// Paying gas_fees only.
let expected_fee_empty_priority = 2 * 12 + 3 * 34;
assert_eq(fee, expected_fee_empty_priority);
}

#[test]
fn compute_transaction_fee_capped_max_fees() {
let mut builder = TestBuilder::new();

// Increase gas_fees so that gas_fees + max_priority_fees > max_fees.
builder.gas_fees = GasFees::new(53, 74);

let fee = builder.compute();

// Make sure the following value matches the one in `transaction_fee.test.ts` in `circuits.js`.
// max_fees were applied.
let expected_max_fee = 2 * 56 + 3 * 78;
assert_eq(fee, expected_max_fee);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod compute_transaction_fee;

pub use compute_transaction_fee::compute_transaction_fee;

// TODO: Move everything that composes the output of private base rollup to this component.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::{
components::{
archive::perform_archive_membership_check, constants::validate_combined_constant_data,
fees::compute_fee_payer_fee_juice_balance_leaf_slot,
nullifier_tree::nullifier_tree_batch_insert, PrivateTubeDataValidator,
nullifier_tree::nullifier_tree_batch_insert,
private_base_rollup_output_composer::compute_transaction_fee, PrivateTubeDataValidator,
public_data_tree::public_data_tree_insert,
},
state_diff_hints::PrivateBaseStateDiffHints,
Expand Down Expand Up @@ -47,12 +48,6 @@ pub struct PrivateBaseRollupInputs {
}

impl PrivateBaseRollupInputs {
fn compute_transaction_fee(self) -> Field {
let gas_fees = self.constants.global_variables.gas_fees;
let gas_used = self.tube_data.public_inputs.gas_used;
gas_used.compute_fee(gas_fees)
}

pub fn execute(self) -> BaseOrMergeRollupPublicInputs {
// TODO(#10311): There should be an empty base rollup.
// There's at least 1 nullifier for a tx. So gas_used won't be empty if the previous circuit is private_tail.
Expand All @@ -64,8 +59,6 @@ impl PrivateBaseRollupInputs {
tube_data_validator.validate_with_rollup_data(self.constants);
}

let transaction_fee = self.compute_transaction_fee();

validate_combined_constant_data(self.tube_data.public_inputs.constants, self.constants);

self.validate_kernel_start_state();
Expand Down Expand Up @@ -98,6 +91,12 @@ impl PrivateBaseRollupInputs {
let end_nullifier_tree_snapshot =
self.check_nullifier_tree_non_membership_and_insert_to_tree();

let transaction_fee = compute_transaction_fee(
self.constants.global_variables.gas_fees,
self.tube_data.public_inputs.constants.tx_context.gas_settings,
self.tube_data.public_inputs.gas_used,
);

// Write fee to public data tree
let fee_public_data_write = self.build_fee_public_data_write(transaction_fee);
let end_public_data_tree_snapshot =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ pub struct GasSettings {
pub gas_limits: Gas,
pub teardown_gas_limits: Gas,
pub max_fees_per_gas: GasFees,
pub max_priority_fees_per_gas: GasFees,
}

impl GasSettings {
pub fn new(gas_limits: Gas, teardown_gas_limits: Gas, max_fees_per_gas: GasFees) -> Self {
Self { gas_limits, teardown_gas_limits, max_fees_per_gas }
pub fn new(
gas_limits: Gas,
teardown_gas_limits: Gas,
max_fees_per_gas: GasFees,
max_priority_fees_per_gas: GasFees,
) -> Self {
Self { gas_limits, teardown_gas_limits, max_fees_per_gas, max_priority_fees_per_gas }
}
}

Expand All @@ -22,12 +28,18 @@ impl Eq for GasSettings {
(self.gas_limits == other.gas_limits)
& (self.teardown_gas_limits == other.teardown_gas_limits)
& (self.max_fees_per_gas == other.max_fees_per_gas)
& (self.max_priority_fees_per_gas == other.max_priority_fees_per_gas)
}
}

impl Empty for GasSettings {
fn empty() -> Self {
GasSettings::new(Gas::empty(), Gas::empty(), GasFees::empty())
GasSettings::new(
Gas::empty(),
Gas::empty(),
GasFees::empty(),
GasFees::empty(),
)
}
}

Expand All @@ -38,6 +50,7 @@ impl Serialize<GAS_SETTINGS_LENGTH> for GasSettings {
serialized.extend_from_array(self.gas_limits.serialize());
serialized.extend_from_array(self.teardown_gas_limits.serialize());
serialized.extend_from_array(self.max_fees_per_gas.serialize());
serialized.extend_from_array(self.max_priority_fees_per_gas.serialize());

serialized.storage()
}
Expand All @@ -50,6 +63,7 @@ impl Deserialize<GAS_SETTINGS_LENGTH> for GasSettings {
reader.read_struct(Gas::deserialize),
reader.read_struct(Gas::deserialize),
reader.read_struct(GasFees::deserialize),
reader.read_struct(GasFees::deserialize),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ pub global DEFAULT_TPK_M_Y: Field =
pub global AZTEC_ADDRESS_LENGTH: u32 = 1;
pub global GAS_FEES_LENGTH: u32 = 2;
pub global GAS_LENGTH: u32 = 2;
pub global GAS_SETTINGS_LENGTH: u32 = GAS_LENGTH * 2 + GAS_FEES_LENGTH;
pub global GAS_SETTINGS_LENGTH: u32 = GAS_LENGTH /* gas_limits */
+ GAS_LENGTH /* teardown_gas_limits */
+ GAS_FEES_LENGTH /* max_fees_per_gas */
+ GAS_FEES_LENGTH /* max_priority_fees_per_gas */;
pub global CALL_CONTEXT_LENGTH: u32 = 4;
pub global CONTENT_COMMITMENT_LENGTH: u32 = 4;
pub global CONTRACT_INSTANCE_LENGTH: u32 = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ mod tests {

#[test]
fn compute_hash() {
let gas_settings = GasSettings::new(Gas::new(2, 2), Gas::new(1, 1), GasFees::new(3, 3));
let gas_settings = GasSettings::new(
Gas::new(2, 2),
Gas::new(1, 1),
GasFees::new(4, 4),
GasFees::new(3, 3),
);
let tx_request = TxRequest {
origin: AztecAddress::from_field(1),
args_hash: 3,
Expand All @@ -105,7 +110,7 @@ mod tests {
};
// Value from tx_request.test.ts "compute hash" test
let test_data_tx_request_hash =
0x1b15ac8432c3c35718075a277d86f11263f057e2325afa208d6b19e37ff59a8d;
0x2d265ee0e3b9d206873a66527485afa3c6ebbfbaf451811666c9558a9f6e3d46;
assert(tx_request.hash() == test_data_tx_request_hash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ pub fn full_field_greater_than(lhs: Field, rhs: Field) -> bool {
rhs.lt(lhs)
}

pub fn min(f1: Field, f2: Field) -> Field {
if f1.lt(f2) {
f1
} else {
f2
}
}

#[test]
unconstrained fn bytes_field_test() {
// Tests correctness of field_from_bytes_32_trunc against existing methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export abstract class BaseContractInteraction {
const defaultFeeOptions = await this.getDefaultFeeOptions(request.fee);
const paymentMethod = defaultFeeOptions.paymentMethod;
const maxFeesPerGas = defaultFeeOptions.gasSettings.maxFeesPerGas;
const maxPriorityFeesPerGas = defaultFeeOptions.gasSettings.maxPriorityFeesPerGas;

let gasSettings = defaultFeeOptions.gasSettings;
if (request.fee?.estimateGas) {
Expand All @@ -132,7 +133,7 @@ export abstract class BaseContractInteraction {
simulationResult,
request.fee?.estimatedGasPadding,
);
gasSettings = GasSettings.from({ maxFeesPerGas, gasLimits, teardownGasLimits });
gasSettings = GasSettings.from({ maxFeesPerGas, maxPriorityFeesPerGas, gasLimits, teardownGasLimits });
this.log.verbose(
`Estimated gas limits for tx: DA=${gasLimits.daGas} L2=${gasLimits.l2Gas} teardownDA=${teardownGasLimits.daGas} teardownL2=${teardownGasLimits.l2Gas}`,
);
Expand Down
Loading

0 comments on commit 2850d56

Please sign in to comment.