-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
278 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...-circuits/crates/private-kernel-lib/src/components/tail_output_composer/meter_gas_used.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use dep::types::{ | ||
abis::{ | ||
accumulated_data::combined_accumulated_data::CombinedAccumulatedData, gas::Gas, | ||
gas_settings::GasSettings | ||
}, | ||
constants::{DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE, L2_GAS_PER_NOTE_HASH, L2_GAS_PER_NULLIFIER, L2_GAS_PER_LOG_BYTE}, | ||
utils::arrays::array_length | ||
}; | ||
|
||
fn meter_gas_used(data: CombinedAccumulatedData, gas_settings: GasSettings) -> Gas { | ||
let mut metered_da_bytes = 0; | ||
let mut metered_l2_gas = 0; | ||
|
||
let num_note_hashes = array_length(data.note_hashes); | ||
metered_da_bytes += num_note_hashes * DA_BYTES_PER_FIELD; | ||
metered_l2_gas += num_note_hashes * L2_GAS_PER_NOTE_HASH; | ||
|
||
let num_nullifiers = array_length(data.nullifiers); | ||
metered_da_bytes += num_nullifiers * DA_BYTES_PER_FIELD; | ||
metered_l2_gas += num_nullifiers * L2_GAS_PER_NULLIFIER; | ||
|
||
let num_l2_to_l1_msgs = array_length(data.l2_to_l1_msgs); | ||
metered_da_bytes += num_l2_to_l1_msgs * 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 = gas_settings.teardown_gas_limits; | ||
Gas::new(metered_da_bytes * DA_GAS_PER_BYTE, metered_l2_gas) + Gas::tx_overhead() + teardown_gas | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 17 additions & 14 deletions
31
...crates/private-kernel-lib/src/components/tail_to_public_output_composer/meter_gas_used.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,50 @@ | ||
use dep::types::{ | ||
abis::{ | ||
accumulated_data::{public_accumulated_data_builder::PublicAccumulatedDataBuilder}, gas::Gas, | ||
accumulated_data::{public_accumulated_data_builder::PublicAccumulatedData}, gas::Gas, | ||
log_hash::{LogHash, ScopedLogHash} | ||
}, | ||
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 | ||
} | ||
}, | ||
utils::arrays::array_length | ||
}; | ||
|
||
fn meter_gas_used(data: PublicAccumulatedDataBuilder) -> Gas { | ||
fn meter_gas_used(data: PublicAccumulatedData) -> Gas { | ||
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; | ||
let num_note_hashes = array_length(data.note_hashes); | ||
metered_da_bytes += num_note_hashes * DA_BYTES_PER_FIELD; | ||
metered_l2_gas += num_note_hashes * 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; | ||
let num_nullifiers = array_length(data.nullifiers); | ||
metered_da_bytes += num_nullifiers * DA_BYTES_PER_FIELD; | ||
metered_l2_gas += num_nullifiers * L2_GAS_PER_NULLIFIER; | ||
|
||
metered_da_bytes += data.l2_to_l1_msgs.len() * DA_BYTES_PER_FIELD; | ||
metered_da_bytes += array_length(data.l2_to_l1_msgs) * DA_BYTES_PER_FIELD; | ||
|
||
let note_encrypted_log_preimages_length = data.note_encrypted_logs_hashes.storage.fold(0, |len, l: LogHash| len + l.length); | ||
let note_encrypted_log_preimages_length = data.note_encrypted_logs_hashes.fold(0, |len, l: LogHash| len + l.length); | ||
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); | ||
let encrypted_log_preimages_length = data.encrypted_logs_hashes.fold(0, |len, l: LogHash| len + l.length); | ||
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: ScopedLogHash| len + l.log_hash.length); | ||
let unencrypted_log_preimages_length = data.unencrypted_logs_hashes.fold(0, |len, l: ScopedLogHash| len + l.log_hash.length); | ||
metered_da_bytes += unencrypted_log_preimages_length as u32; | ||
metered_l2_gas += unencrypted_log_preimages_length as u32 * L2_GAS_PER_LOG_BYTE; | ||
|
||
metered_l2_gas += data.public_call_stack.len() * FIXED_AVM_STARTUP_L2_GAS; | ||
metered_l2_gas += array_length(data.public_call_stack) * FIXED_AVM_STARTUP_L2_GAS; | ||
|
||
Gas::new(metered_da_bytes * DA_GAS_PER_BYTE, metered_l2_gas) | ||
} | ||
|
||
pub fn meter_gas_used_non_revertible(data: PublicAccumulatedDataBuilder) -> Gas { | ||
pub fn meter_gas_used_non_revertible(data: PublicAccumulatedData) -> Gas { | ||
meter_gas_used(data) + Gas::tx_overhead() | ||
} | ||
|
||
pub fn meter_gas_used_revertible(data: PublicAccumulatedDataBuilder, teardown_gas: Gas) -> Gas { | ||
pub fn meter_gas_used_revertible(data: PublicAccumulatedData, teardown_gas: Gas) -> Gas { | ||
meter_gas_used(data) + Gas::new(teardown_gas.da_gas, teardown_gas.l2_gas) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...rcuits/crates/private-kernel-lib/src/tests/tail_output_composer_builder/meter_gas_used.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::components::tail_output_composer::meter_gas_used::meter_gas_used; | ||
use dep::types::{ | ||
abis::gas::Gas, | ||
constants::{DA_BYTES_PER_FIELD, DA_GAS_PER_BYTE, L2_GAS_PER_NOTE_HASH, L2_GAS_PER_NULLIFIER, L2_GAS_PER_LOG_BYTE}, | ||
tests::fixture_builder::FixtureBuilder | ||
}; | ||
|
||
fn new_builder() -> FixtureBuilder { | ||
let mut builder = FixtureBuilder::new(); | ||
builder.tx_context.gas_settings.teardown_gas_limits = Gas::new(12, 345); | ||
builder | ||
} | ||
|
||
#[test] | ||
fn meter_gas_used_empty_succeeds() { | ||
let builder = new_builder(); | ||
let data = builder.to_combined_accumulated_data(); | ||
let gas_settings = builder.tx_context.gas_settings; | ||
let gas = meter_gas_used(data, gas_settings); | ||
assert_eq(gas, Gas::tx_overhead() + gas_settings.teardown_gas_limits); | ||
} | ||
|
||
#[test] | ||
fn meter_gas_used_everything_succeeds() { | ||
let mut builder = new_builder(); | ||
let mut metered_da_bytes = 0; | ||
let mut computed_l2_gas = 0; | ||
|
||
builder.append_note_hashes(4); | ||
metered_da_bytes += 4 * DA_BYTES_PER_FIELD; | ||
computed_l2_gas += 4 * L2_GAS_PER_NOTE_HASH; | ||
|
||
builder.append_nullifiers(3); | ||
metered_da_bytes += 3 * DA_BYTES_PER_FIELD; | ||
computed_l2_gas += 3 * L2_GAS_PER_NULLIFIER; | ||
|
||
builder.append_l2_to_l1_msgs(1); | ||
metered_da_bytes += 1 * DA_BYTES_PER_FIELD; | ||
|
||
builder.add_note_encrypted_log_hash(1001, 12, 0); | ||
metered_da_bytes += 12; | ||
computed_l2_gas += 12 * L2_GAS_PER_LOG_BYTE; | ||
|
||
builder.add_note_encrypted_log_hash(1002, 8, 0); | ||
metered_da_bytes += 8; | ||
computed_l2_gas += 8 * L2_GAS_PER_LOG_BYTE; | ||
|
||
builder.add_note_encrypted_log_hash(1003, 20, 0); | ||
metered_da_bytes += 20; | ||
computed_l2_gas += 20 * L2_GAS_PER_LOG_BYTE; | ||
|
||
builder.add_encrypted_log_hash(2001, 2); | ||
metered_da_bytes += 2; | ||
computed_l2_gas += 2 * L2_GAS_PER_LOG_BYTE; | ||
|
||
builder.add_encrypted_log_hash(2002, 6); | ||
metered_da_bytes += 6; | ||
computed_l2_gas += 6 * L2_GAS_PER_LOG_BYTE; | ||
|
||
builder.add_unencrypted_log_hash(3001, 51); | ||
metered_da_bytes += 51; | ||
computed_l2_gas += 51 * L2_GAS_PER_LOG_BYTE; | ||
|
||
let data = builder.to_combined_accumulated_data(); | ||
let gas_settings = builder.tx_context.gas_settings; | ||
let gas = meter_gas_used(data, gas_settings); | ||
|
||
assert_eq( | ||
gas, Gas::new(metered_da_bytes * DA_GAS_PER_BYTE, computed_l2_gas) | ||
+ Gas::tx_overhead() | ||
+ gas_settings.teardown_gas_limits | ||
); | ||
} |
2 changes: 2 additions & 0 deletions
2
...src/tests/tail_output_composer_builder.nr → ...tests/tail_output_composer_builder/mod.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.