Skip to content

Commit

Permalink
feat: Wire AVM gas used to public kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Apr 12, 2024
1 parent 5526b36 commit d2fef12
Show file tree
Hide file tree
Showing 79 changed files with 626 additions and 368 deletions.
6 changes: 3 additions & 3 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ library Constants {
uint256 internal constant GAS_SETTINGS_LENGTH = 10;
uint256 internal constant DIMENSION_GAS_SETTINGS_LENGTH = 3;
uint256 internal constant GAS_FEES_LENGTH = 3;
uint256 internal constant GAS_USED_LENGTH = 3;
uint256 internal constant GAS_LENGTH = 3;
uint256 internal constant CONTENT_COMMITMENT_LENGTH = 4;
uint256 internal constant CONTRACT_INSTANCE_LENGTH = 6;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
Expand All @@ -112,10 +112,10 @@ library Constants {
uint256 internal constant PARTIAL_STATE_REFERENCE_LENGTH = 6;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 221;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 218;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 209;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 212;
uint256 internal constant STATE_REFERENCE_LENGTH = 8;
uint256 internal constant TX_CONTEXT_DATA_LENGTH = 4;
uint256 internal constant TX_REQUEST_LENGTH = 8;
uint256 internal constant TX_REQUEST_LENGTH = 18;
uint256 internal constant ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH = 22;
uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674;
uint256 internal constant NOTE_HASHES_NUM_BYTES_PER_BASE_ROLLUP = 2048;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::context::globals::public_global_variables::PublicGlobalVariables;

use dep::protocol_types::{abis::call_context::CallContext, header::Header, traits::Empty};
use dep::protocol_types::{abis::call_context::CallContext, abis::gas::Gas, header::Header, traits::Empty};

// PublicContextInputs are expected to be provided to each public function
// docs:start:public-context-inputs
Expand All @@ -23,4 +23,4 @@ impl Empty for PublicContextInputs {
start_side_effect_counter: 0 as u32,
}
}
}
}
11 changes: 7 additions & 4 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use crate::{
};
use dep::protocol_types::{
abis::{
call_context::CallContext, function_data::FunctionData, function_selector::FunctionSelector,
max_block_number::MaxBlockNumber, nullifier_key_validation_request::NullifierKeyValidationRequest,
gas::Gas, call_context::CallContext, function_data::FunctionData,
function_selector::FunctionSelector, max_block_number::MaxBlockNumber,
nullifier_key_validation_request::NullifierKeyValidationRequest,
private_call_stack_item::PrivateCallStackItem,
private_circuit_public_inputs::PrivateCircuitPublicInputs,
public_call_stack_item::PublicCallStackItem,
Expand All @@ -28,7 +29,8 @@ use dep::protocol_types::{
},
contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest},
grumpkin_private_key::GrumpkinPrivateKey, header::Header,
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader, traits::{is_empty, Deserialize, Empty}
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader,
traits::{is_empty, Deserialize, Empty}
};

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
Expand Down Expand Up @@ -494,7 +496,8 @@ impl PrivateContext {
unencrypted_log_preimages_length: 0,
historical_header: Header::empty(),
prover_address: AztecAddress::zero(),
revert_code: 0
revert_code: 0,
gas_left: Gas::empty()
},
is_execution_request: true
};
Expand Down
3 changes: 2 additions & 1 deletion noir-projects/aztec-nr/aztec/src/context/public_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ impl PublicContext {
unencrypted_log_preimages_length,
historical_header: self.inputs.historical_header,
prover_address: self.prover_address,
revert_code: 0
revert_code: 0,
gas_left: self.inputs.call_context.gas_left
};
pub_circuit_pub_inputs
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use dep::aztec::context::PublicContext;
use dep::aztec::protocol_types::hash::sha256_to_field;

pub fn calculate_fee(_context: PublicContext) -> U128 {
// TODO(palla/gas-in-circuits): Use the transaction_fee injected into the context
U128::from_integer(1)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl PrivateKernelInitCircuitPrivateInputs {
public_inputs.constants = CombinedConstantData {
historical_header: self.private_call.call_stack_item.public_inputs.historical_header,
tx_context: self.tx_request.tx_context,
gas_settings: GasSettings::empty(), // TODO(palla/gas-in-circuits)
gas_settings: self.tx_request.gas_settings,
};
public_inputs.min_revertible_side_effect_counter = self.private_call.call_stack_item.public_inputs.min_revertible_side_effect_counter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ pub fn update_validation_requests(public_call: PublicCallData, circuit_outputs:
propagate_valid_public_data_reads(public_call, circuit_outputs);
}

pub fn update_revertible_gas_used(public_call: PublicCallData, circuit_outputs: &mut PublicKernelCircuitPublicInputsBuilder) {
circuit_outputs.end.gas_used = circuit_outputs.constants.gas_settings.get_gas_limits()
.sub(public_call.call_stack_item.public_inputs.gas_left)
.sub(circuit_outputs.end_non_revertible.gas_used);
}

pub fn update_non_revertible_gas_used(public_call: PublicCallData, circuit_outputs: &mut PublicKernelCircuitPublicInputsBuilder) {
circuit_outputs.end_non_revertible.gas_used = circuit_outputs.constants.gas_settings.get_gas_limits()
.sub(public_call.call_stack_item.public_inputs.gas_left)
.sub(circuit_outputs.end.gas_used);
}

pub fn update_public_end_non_revertible_values(
public_call: PublicCallData,
circuit_outputs: &mut PublicKernelCircuitPublicInputsBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ impl PublicKernelAppLogicCircuitPrivateInputs {

common::update_validation_requests(self.public_call, &mut public_inputs);

common::update_revertible_gas_used(self.public_call, &mut public_inputs);

if public_inputs.revert_code == 0 {
// Pops the item from the call stack and validates it against the current execution.
let call_request = public_inputs.end.public_call_stack.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ impl PublicKernelSetupCircuitPrivateInputs {
// validate the inputs unique to having a previous private kernel
self.validate_inputs();

common::update_non_revertible_gas_used(self.public_call, &mut public_inputs);

// Pops the item from the call stack and validates it against the current execution.
let call_request = public_inputs.end_non_revertible.public_call_stack.pop();
common::validate_call_against_request(self.public_call, call_request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ mod private_circuit_public_inputs;

mod gas_fees;
mod gas_settings;
mod gas_used;
mod gas;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
abis::{
accumulated_data::public_accumulated_data::PublicAccumulatedData,
public_data_update_request::PublicDataUpdateRequest,
side_effect::{SideEffect, SideEffectLinkedToNoteHash}, gas_used::GasUsed
side_effect::{SideEffect, SideEffectLinkedToNoteHash}, gas::Gas
},
constants::{
MAX_NEW_NOTE_HASHES_PER_TX, MAX_NEW_NULLIFIERS_PER_TX, MAX_NEW_L2_TO_L1_MSGS_PER_TX,
Expand All @@ -26,7 +26,7 @@ struct CombinedAccumulatedData {

public_data_update_requests: [PublicDataUpdateRequest; MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX],

gas_used: GasUsed,
gas_used: Gas,
}

impl CombinedAccumulatedData {
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Empty for CombinedAccumulatedData {
encrypted_log_preimages_length: 0,
unencrypted_log_preimages_length: 0,
public_data_update_requests: [PublicDataUpdateRequest::empty(); MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX],
gas_used: GasUsed::empty()
gas_used: Gas::empty()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{abis::{call_request::CallRequest, gas_used::GasUsed, side_effect::{SideEffect, SideEffectLinkedToNoteHash}}};
use crate::{abis::{call_request::CallRequest, gas::Gas, side_effect::{SideEffect, SideEffectLinkedToNoteHash}}};
use crate::constants::{
MAX_NEW_NOTE_HASHES_PER_TX, MAX_NEW_NULLIFIERS_PER_TX, MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, MAX_NEW_L2_TO_L1_MSGS_PER_TX
Expand All @@ -20,5 +20,5 @@ struct PrivateAccumulatedData {
private_call_stack: [CallRequest; MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX],
public_call_stack: [CallRequest; MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX],

gas_used: GasUsed,
gas_used: Gas,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
abis::{
gas_used::GasUsed,
gas::Gas,
accumulated_data::{
combined_accumulated_data::CombinedAccumulatedData,
private_accumulated_data::PrivateAccumulatedData, public_accumulated_data::PublicAccumulatedData,
Expand Down Expand Up @@ -33,7 +33,7 @@ struct PrivateAccumulatedDataBuilder {
private_call_stack: BoundedVec<CallRequest, MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX>,
public_call_stack: BoundedVec<CallRequest, MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX>,

gas_used: GasUsed
gas_used: Gas
}

impl PrivateAccumulatedDataBuilder {
Expand Down Expand Up @@ -204,7 +204,7 @@ impl Empty for PrivateAccumulatedDataBuilder {
unencrypted_log_preimages_length: 0,
private_call_stack: BoundedVec::new(),
public_call_stack: BoundedVec::new(),
gas_used: GasUsed::empty(),
gas_used: Gas::empty(),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
abis::{
call_request::CallRequest, public_data_update_request::PublicDataUpdateRequest, gas_used::GasUsed,
call_request::CallRequest, public_data_update_request::PublicDataUpdateRequest, gas::Gas,
side_effect::{SideEffect, SideEffectLinkedToNoteHash}
},
constants::{
Expand All @@ -27,7 +27,7 @@ struct PublicAccumulatedData {

public_call_stack: [CallRequest; MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX],

gas_used: GasUsed,
gas_used: Gas,
}

impl Empty for PublicAccumulatedData {
Expand All @@ -42,7 +42,7 @@ impl Empty for PublicAccumulatedData {
unencrypted_log_preimages_length: 0,
public_data_update_requests: [PublicDataUpdateRequest::empty(); MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX],
public_call_stack: [CallRequest::empty(); MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX],
gas_used: GasUsed::empty(),
gas_used: Gas::empty(),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
abis::{
gas_used::GasUsed, accumulated_data::public_accumulated_data::PublicAccumulatedData,
gas::Gas, accumulated_data::public_accumulated_data::PublicAccumulatedData,
call_request::CallRequest, public_data_update_request::PublicDataUpdateRequest,
side_effect::{SideEffect, SideEffectLinkedToNoteHash}
},
Expand Down Expand Up @@ -28,7 +28,7 @@ struct PublicAccumulatedDataBuilder {

public_call_stack: BoundedVec<CallRequest, MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX>,

gas_used: GasUsed,
gas_used: Gas,
}

impl PublicAccumulatedDataBuilder {
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Empty for PublicAccumulatedDataBuilder {
unencrypted_log_preimages_length: 0,
public_data_update_requests: BoundedVec::new(),
public_call_stack: BoundedVec::new(),
gas_used: GasUsed::empty(),
gas_used: Gas::empty(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use crate::{
abis::function_selector::FunctionSelector, address::{EthAddress, AztecAddress},
constants::{CALL_CONTEXT_LENGTH, GENERATOR_INDEX__CALL_CONTEXT}, hash::pedersen_hash,
traits::{Deserialize, Hash, Serialize, Empty}, abis::side_effect::Ordered,
abis::gas_settings::GasSettings, utils::reader::Reader
abis::{gas_settings::GasSettings, gas::Gas}, utils::reader::Reader
};

// docs:start:call-context
struct CallContext {
msg_sender : AztecAddress,
storage_contract_address : AztecAddress,
portal_contract_address : EthAddress,

function_selector : FunctionSelector,
gas_left: Gas,

is_delegate_call : bool,
is_static_call : bool,
Expand Down Expand Up @@ -53,6 +53,7 @@ impl Serialize<CALL_CONTEXT_LENGTH> for CallContext {
serialized.push(self.storage_contract_address.to_field());
serialized.push(self.portal_contract_address.to_field());
serialized.push(self.function_selector.to_field());
serialized.extend_from_array(self.gas_left.serialize());
serialized.push(self.is_delegate_call as Field);
serialized.push(self.is_static_call as Field);
serialized.push(self.side_effect_counter as Field);
Expand All @@ -71,6 +72,7 @@ impl Deserialize<CALL_CONTEXT_LENGTH> for CallContext {
storage_contract_address: AztecAddress::from_field(reader.read()),
portal_contract_address: EthAddress::from_field(reader.read()),
function_selector: FunctionSelector::from_field(reader.read()),
gas_left: reader.read_struct(Gas::deserialize),
is_delegate_call: reader.read() as bool,
is_static_call: reader.read() as bool,
side_effect_counter: reader.read() as u32,
Expand All @@ -87,6 +89,7 @@ impl Empty for CallContext {
storage_contract_address: AztecAddress::empty(),
portal_contract_address: EthAddress::empty(),
function_selector: FunctionSelector::empty(),
gas_left: Gas::empty(),
is_delegate_call: false,
is_static_call: false,
side_effect_counter: 0,
Expand Down
58 changes: 58 additions & 0 deletions noir-projects/noir-protocol-circuits/crates/types/src/abis/gas.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::{
abis::function_selector::FunctionSelector, address::{EthAddress, AztecAddress},
constants::GAS_LENGTH, hash::pedersen_hash, traits::{Deserialize, Hash, Serialize, Empty},
abis::side_effect::Ordered, utils::reader::Reader
};

struct Gas {
da_gas: Field,
l1_gas: Field,
l2_gas: Field,
}

impl Gas {
fn new(da_gas: Field, l1_gas: Field, l2_gas: Field) -> Self {
Self { da_gas, l1_gas, l2_gas }
}

fn add(self, other: Gas) -> Self {
Gas::new(
self.da_gas + other.da_gas,
self.l1_gas + other.l1_gas,
self.l2_gas + other.l2_gas
)
}

fn sub(self, other: Gas) -> Self {
Gas::new(
self.da_gas - other.da_gas,
self.l1_gas - other.l1_gas,
self.l2_gas - other.l2_gas
)
}
}

impl Serialize<GAS_LENGTH> for Gas {
fn serialize(self) -> [Field; GAS_LENGTH] {
[self.da_gas, self.l1_gas, self.l2_gas]
}
}

impl Deserialize<GAS_LENGTH> for Gas {
fn deserialize(serialized: [Field; GAS_LENGTH]) -> Gas {
Gas::new(serialized[0], serialized[1], serialized[2])
}
}

impl Eq for Gas {
fn eq(self, other : Gas) -> bool {
(self.da_gas == other.da_gas) & (self.l1_gas == other.l1_gas) & (self.l2_gas == other.l2_gas)
}
}

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

Loading

0 comments on commit d2fef12

Please sign in to comment.