Skip to content

Commit

Permalink
resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
just-mitch committed Jul 3, 2024
1 parent 6fc90db commit f2eb449
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use dep::types::{
log_hash::{ScopedEncryptedLogHash, NoteLogHash, ScopedLogHash}, note_hash::ScopedNoteHash,
nullifier::ScopedNullifier
},
constants::{DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE},
constants::{DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE, L2_GAS_PER_NOTE_HASH, L2_GAS_PER_NULLIFIER, L2_GAS_PER_LOG_BYTE},
hash::{compute_tx_logs_hash, compute_tx_note_logs_hash},
messaging::l2_to_l1_message::ScopedL2ToL1Message
};
Expand Down Expand Up @@ -50,20 +50,32 @@ impl TailOutputComposer {
}

fn meter_gas_used(self, data: CombinedAccumulatedData) -> Gas {
let mut metered_bytes = 0;
let mut metered_da_bytes = 0;
let mut metered_l2_gas = 0;

let data_builder = self.output_composer.public_inputs.end;
// IMPORTANT: Must use data_builder.__.len(), which is the the number of items pushed to the BoundedVec.
// Do not use data.__.len(), which is the array's max length.
metered_bytes += data_builder.note_hashes.len() * DA_BYTES_PER_FIELD;
metered_bytes += data_builder.nullifiers.len() * DA_BYTES_PER_FIELD;
metered_bytes += data_builder.l2_to_l1_msgs.len() * DA_BYTES_PER_FIELD;
metered_da_bytes += data_builder.note_hashes.len() * DA_BYTES_PER_FIELD;
metered_l2_gas += data_builder.note_hashes.len() * L2_GAS_PER_NOTE_HASH;

metered_bytes += data.note_encrypted_log_preimages_length as u32;
metered_bytes += data.encrypted_log_preimages_length as u32;
metered_bytes += data.unencrypted_log_preimages_length as u32;
metered_da_bytes += data_builder.nullifiers.len() * DA_BYTES_PER_FIELD;
metered_l2_gas += data_builder.nullifiers.len() * L2_GAS_PER_NULLIFIER;

metered_da_bytes += data_builder.l2_to_l1_msgs.len() * DA_BYTES_PER_FIELD;

metered_da_bytes += data.note_encrypted_log_preimages_length as u32;
metered_l2_gas += data.note_encrypted_log_preimages_length as u32 * L2_GAS_PER_LOG_BYTE;

metered_da_bytes += data.encrypted_log_preimages_length as u32;
metered_l2_gas += data.encrypted_log_preimages_length as u32 * L2_GAS_PER_LOG_BYTE;

metered_da_bytes += data.unencrypted_log_preimages_length as u32;
metered_l2_gas += data.unencrypted_log_preimages_length as u32 * L2_GAS_PER_LOG_BYTE;

let teardown_gas = self.output_composer.public_inputs.constants.tx_context.gas_settings.teardown_gas_limits;
Gas::new(metered_bytes * DA_GAS_PER_BYTE, 0) + Gas::tx_overhead() + teardown_gas
Gas::new(metered_da_bytes * DA_GAS_PER_BYTE, metered_l2_gas)
+ Gas::tx_overhead()
+ teardown_gas
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,39 @@ use dep::types::{
accumulated_data::{public_accumulated_data_builder::PublicAccumulatedDataBuilder}, gas::Gas,
log_hash::LogHash
},
constants::{DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE}
constants::{
DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE, FIXED_AVM_STARTUP_L2_GAS, L2_GAS_PER_NOTE_HASH,
L2_GAS_PER_NULLIFIER, L2_GAS_PER_LOG_BYTE
}
};

fn meter_gas_used(data: PublicAccumulatedDataBuilder) -> Gas {
let mut metered_bytes = 0;
metered_bytes += data.note_hashes.len() * DA_BYTES_PER_FIELD;
metered_bytes += data.nullifiers.len() * DA_BYTES_PER_FIELD;
metered_bytes += data.l2_to_l1_msgs.len() * DA_BYTES_PER_FIELD;
let mut metered_da_bytes = 0;
let mut metered_l2_gas = 0;

metered_da_bytes += data.note_hashes.len() * DA_BYTES_PER_FIELD;
metered_l2_gas += data.note_hashes.len() * L2_GAS_PER_NOTE_HASH;

metered_da_bytes += data.nullifiers.len() * DA_BYTES_PER_FIELD;
metered_l2_gas += data.nullifiers.len() * L2_GAS_PER_NULLIFIER;

metered_da_bytes += data.l2_to_l1_msgs.len() * DA_BYTES_PER_FIELD;

let note_encrypted_log_preimages_length = data.note_encrypted_logs_hashes.storage.fold(0, |len, l: LogHash| len + l.length);
metered_bytes += note_encrypted_log_preimages_length as u32;
metered_da_bytes += note_encrypted_log_preimages_length as u32;
metered_l2_gas += note_encrypted_log_preimages_length as u32 * L2_GAS_PER_LOG_BYTE;

let encrypted_log_preimages_length = data.encrypted_logs_hashes.storage.fold(0, |len, l: LogHash| len + l.length);
metered_bytes += encrypted_log_preimages_length as u32;
metered_da_bytes += encrypted_log_preimages_length as u32;
metered_l2_gas += encrypted_log_preimages_length as u32 * L2_GAS_PER_LOG_BYTE;

let unencrypted_log_preimages_length = data.unencrypted_logs_hashes.storage.fold(0, |len, l: LogHash| len + l.length);
metered_bytes += unencrypted_log_preimages_length as u32;
metered_da_bytes += unencrypted_log_preimages_length as u32;
metered_l2_gas += unencrypted_log_preimages_length as u32 * L2_GAS_PER_LOG_BYTE;

// TODO(gas): add AVM_STARTUP_L2_GAS
metered_l2_gas += data.public_call_stack.len() * FIXED_AVM_STARTUP_L2_GAS;

Gas::new(metered_bytes * DA_GAS_PER_BYTE, 0)
Gas::new(metered_da_bytes * DA_GAS_PER_BYTE, metered_l2_gas)
}

pub fn meter_gas_used_non_revertible(data: PublicAccumulatedDataBuilder) -> Gas {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ impl PrivateKernelTailToPublicCircuitPrivateInputs {

mod tests {
use crate::private_kernel_tail_to_public::PrivateKernelTailToPublicCircuitPrivateInputs;
use dep::types::constants::{DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE, GENERATOR_INDEX__TSK_M};
use dep::types::constants::{
DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE, GENERATOR_INDEX__TSK_M, L2_GAS_PER_LOG_BYTE,
L2_GAS_PER_NOTE_HASH, L2_GAS_PER_NULLIFIER, FIXED_AVM_STARTUP_L2_GAS
};
use dep::types::{
abis::{
kernel_circuit_public_inputs::PublicKernelCircuitPublicInputs, gas::Gas,
note_hash::{NoteHash, ScopedNoteHash}, nullifier::{Nullifier, ScopedNullifier},
log_hash::{LogHash, NoteLogHash}
},
address::AztecAddress, hash::{silo_note_hash, silo_nullifier},
address::{AztecAddress, EthAddress}, hash::{silo_note_hash, silo_nullifier},
tests::fixture_builder::FixtureBuilder, utils::{arrays::array_eq}, grumpkin_point::GrumpkinPoint
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::components::tail_to_public_output_composer::meter_gas_used::{meter_gas_used_non_revertible, meter_gas_used_revertible};
use dep::types::{
abis::gas::Gas, constants::{DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE},
abis::gas::Gas,
constants::{
DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE, FIXED_AVM_STARTUP_L2_GAS, L2_GAS_PER_NOTE_HASH,
L2_GAS_PER_NULLIFIER, L2_GAS_PER_LOG_BYTE
},
tests::fixture_builder::FixtureBuilder
};

Expand Down Expand Up @@ -36,8 +40,12 @@ fn meter_gas_used_non_revertible_everything_succeeds() {
+ 2 + 6 // encrypted_log_hash
+ 51; // unencrypted_log_hash
let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE;
let computed_l2_gas = 4 * L2_GAS_PER_NOTE_HASH
+ 3 * L2_GAS_PER_NULLIFIER
+ total_log_length * L2_GAS_PER_LOG_BYTE
+ 2 * FIXED_AVM_STARTUP_L2_GAS;

assert_eq(gas, Gas::new(computed_da_gas, 0) + Gas::tx_overhead());
assert_eq(gas, Gas::new(computed_da_gas, computed_l2_gas) + Gas::tx_overhead());
}

#[test]
Expand Down Expand Up @@ -74,6 +82,10 @@ fn meter_gas_used_revertible_everything_succeeds() {
+ 2 + 6 // encrypted_log_hash
+ 51; // unencrypted_log_hash
let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE;
let computed_l2_gas = 4 * L2_GAS_PER_NOTE_HASH
+ 3 * L2_GAS_PER_NULLIFIER
+ total_log_length * L2_GAS_PER_LOG_BYTE
+ 2 * FIXED_AVM_STARTUP_L2_GAS;

assert_eq(gas, Gas::new(computed_da_gas, 0) + teardown_gas);
assert_eq(gas, Gas::new(computed_da_gas, computed_l2_gas) + teardown_gas);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::tests::tail_to_public_output_composer_builder::TailToPublicOutputComposerBuilder;
use dep::types::{
abis::gas::Gas, constants::{DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE},
abis::gas::Gas,
constants::{
DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE, FIXED_AVM_STARTUP_L2_GAS, L2_GAS_PER_NOTE_HASH,
L2_GAS_PER_NULLIFIER, L2_GAS_PER_LOG_BYTE
},
tests::utils::{assert_array_eq, swap_items}
};

Expand Down Expand Up @@ -132,13 +136,23 @@ fn tail_to_public_output_composer_succeeds() {
+ 2 // encrypted_log_hash
+ 51; // unencrypted_log_hash
let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE;
assert_eq(output.end_non_revertible.gas_used, Gas::new(computed_da_gas, 0) + Gas::tx_overhead());
let computed_l2_gas = 4 * L2_GAS_PER_NOTE_HASH
+ 3 * L2_GAS_PER_NULLIFIER
+ total_log_length * L2_GAS_PER_LOG_BYTE
+ 2 * FIXED_AVM_STARTUP_L2_GAS;
assert_eq(
output.end_non_revertible.gas_used, Gas::new(computed_da_gas, computed_l2_gas) + Gas::tx_overhead()
);

// Gas: revertible
let total_num_side_effects = 2 + 1 + 1;
let total_log_length = 20 // note_encrypted_log_hash
+ 6 + 24 // encrypted_log_hash
+ 4; // unencrypted_log_hash
let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE;
assert_eq(output.end.gas_used, Gas::new(computed_da_gas, 0) + teardown_gas);
let computed_l2_gas = 2 * L2_GAS_PER_NOTE_HASH
+ 1 * L2_GAS_PER_NULLIFIER
+ total_log_length * L2_GAS_PER_LOG_BYTE
+ 3 * FIXED_AVM_STARTUP_L2_GAS;
assert_eq(output.end.gas_used, Gas::new(computed_da_gas, computed_l2_gas) + teardown_gas);
}
Loading

0 comments on commit f2eb449

Please sign in to comment.