Skip to content

Commit

Permalink
refactor: Reorganize gas fields in structs (#5828)
Browse files Browse the repository at this point in the history
Move gas stuff around! More info in [this internal
doc](https://hackmd.io/v1EJ-qG3SzGpFg7WO9QlOw).

Suggest reviewing per commit.

- Removes all gas fields (`gasFee`, `gasSettings`, `transactionFee`)
from `CallContext`
- Removes unused flags `isFeePayment` and `isRebate` from `TxContext`
- Removes `PrivateGlobalVariables`
- Adds `gasSettings` to `TxContext`, removing them from
`CombinedConstantData`, `TxRequest`, and `TxExecutionRequest`
- Adds `gasLeft` and `transactionFee` to `PublicContextInputs`
- Adds `startGasLeft`, `endGasLeft`, and `transactionFee` to
`PublicCircuitPublicInputs`, removing `gasFee`
- Adds `TxContext` to `PrivateContextInputs` and
`PrivateCircuitPublicInputs`, removing `chainId` and `version`
- Refactors `GasSettings` so it depends on `Gas` and `GasFees`, removing
`DimensionGasFees`
  • Loading branch information
spalladino authored Apr 19, 2024
1 parent a0a9668 commit e26d342
Show file tree
Hide file tree
Showing 92 changed files with 608 additions and 787 deletions.
8 changes: 6 additions & 2 deletions docs/docs/developers/contracts/references/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ For developers coming from solidity, this concept will be similar to how the glo

## Private Global Variables

#include_code private-global-variables /noir-projects/aztec-nr/aztec/src/context/globals/private_global_variables.nr rust
#include_code tx-context /noir-projects/noir-protocol-circuits/crates/types/src/transaction/tx_context.nr rust

The private global variables contain:
The private global variables are equal to the transaction context and contain:

### Chain Id

Expand All @@ -31,6 +31,10 @@ The version number indicates which Aztec hardfork you are on. The Genesis block
context.version();
```

### Gas Settings

The gas limits set by the user for the transaction, the max fee per gas, and the inclusion fee.

## Public Global Variables

#include_code global-variables /noir-projects/noir-protocol-circuits/crates/types/src/abis/global_variables.nr rust
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ In the public context this header is set by sequencer (sequencer executes public

#include_code header /noir-projects/noir-protocol-circuits/crates/types/src/header.nr rust

### Private Global Variables
### Transaction Context

In the private execution context, we only have access to a subset of the total global variables, we are restricted to those which can be reliably proven by the kernel circuits.
The private context provides access to the transaction context as well, which are user-defined values for the transaction in general that stay constant throughout its execution.

#include_code private-global-variables /noir-projects/aztec-nr/aztec/src/context/globals/private_global_variables.nr rust
#include_code tx-context /noir-projects/noir-protocol-circuits/crates/types/src/transaction/tx_context.nr rust

### Args Hash

Expand Down
19 changes: 7 additions & 12 deletions docs/docs/protocol-specs/circuits/private-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,13 @@ After generating a proof for a private function circuit, that proof (and associa

### `CallContext`

| Field | Type | Description |
| -------------------------- | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `msg_sender` | `AztecAddress` | Address of the caller contract. |
| `storage_contract_address` | `AztecAddress` | Address of the contract against which all state changes will be stored. (It is not called `contract_address`, because in the context of delegate calls, that would be an ambiguous name.) |
| `portal_contract_address` | `AztecAddress` | Address of the portal contract to the storage contract. |
| `is_delegate_call` | `bool` | A flag indicating whether the call is a [delegate call](../calls/delegate-calls.md). |
| `is_static_call` | `bool` | A flag indicating whether the call is a [static call](../calls/static-calls.md). |
| `gas_settings` | [`GasSettings`](#gassettings) | User-defined limits and max fees per each gas dimension for the transaction. |
| `gas_left.da_gas` | `u32` | How much DA gas is available for this call. |
| `gas_left.l1_gas` | `u32` | How much L1 gas is available for this call. |
| `gas_left.l2_gas` | `u32` | How much L2 gas is available for this call. |
| `transaction_fee` | `field` | Accumulated transaction fee, only set during teardown phase. |
| Field | Type | Description |
| -------------------------- | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `msg_sender` | `AztecAddress` | Address of the caller contract. |
| `storage_contract_address` | `AztecAddress` | Address of the contract against which all state changes will be stored. (It is not called `contract_address`, because in the context of delegate calls, that would be an ambiguous name.) |
| `portal_contract_address` | `AztecAddress` | Address of the portal contract to the storage contract. |
| `is_delegate_call` | `bool` | A flag indicating whether the call is a [delegate call](../calls/delegate-calls.md). |
| `is_static_call` | `bool` | A flag indicating whether the call is a [static call](../calls/static-calls.md). |

### `GasSettings`

Expand Down
3 changes: 0 additions & 3 deletions docs/docs/protocol-specs/circuits/private-kernel-initial.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,6 @@ class CallContext {
portal_contract_address: AztecAddress
is_delegate_call: bool
is_static_call: bool
gas_left: Gas
gas_settings: GasSettings
transaction_fee: field
}
CallContext ..> CallerContext : call_context
Expand Down
3 changes: 0 additions & 3 deletions docs/docs/protocol-specs/circuits/private-kernel-inner.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,6 @@ class CallContext {
portal_contract_address: AztecAddress
is_delegate_call: bool
is_static_call: bool
gas_left: Gas
gas_settings: GasSettings
transaction_fee: field
}
CallContext ..> CallerContext : call_context

Expand Down
26 changes: 3 additions & 23 deletions docs/docs/protocol-specs/rollup-circuits/base-rollup.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@ class PublicDataRead {
value: Fr
}
class NewContractData {
function_tree_root: Fr
address: Address
portal: EthAddress
}
class CombinedAccumulatedData {
aggregation_object: AggregationObject
read_requests: List~Fr~
Expand All @@ -139,7 +133,6 @@ class CombinedAccumulatedData {
nullified_note_hashes: List~Fr~
l2_to_l1_messages: List~Fr~
contracts: List~NewContractData~
public_update_requests: List~PublicDataUpdateRequest~
public_reads: List~PublicDataRead~
logs: Logs
Expand All @@ -153,37 +146,24 @@ class CombinedAccumulatedData {
gas_used.l1_gas: u32
gas_used.l2_gas: u32
}
CombinedAccumulatedData *-- "m" NewContractData: contracts
CombinedAccumulatedData *-- "m" PublicDataUpdateRequest: public_update_requests
CombinedAccumulatedData *-- "m" PublicDataRead: public_reads
CombinedAccumulatedData *-- Logs : logs
class ContractDeploymentData {
deployer_public_key: Point
constructor_vk_hash: Fr
constructor_args_hash: Fr
function_tree_root: Fr
salt: Fr
portal_address: Fr
}
class TxContext {
fee_context: FeeContext
is_contract_deployment: bool
chain_id: Fr
version: Fr
contract_deployment_data: ContractDeploymentData
gas_settings: GasSettings
}
TxContext *-- ContractDeploymentData: contract_deployment_data
TxContext *-- GasSettings : gas_settings
class CombinedConstantData {
historical_header: Header
tx_context: TxContext
gas_settings: GasSettings
}
CombinedConstantData *-- Header : historical_header
CombinedConstantData *-- TxContext : tx_context
CombinedConstantData *-- GasSettings : gas_settings
class GasSettings {
da.gas_limit: u32
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/protocol-specs/rollup-circuits/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,19 @@ CombinedAccumulatedData *-- "m" PublicDataRead: public_reads
CombinedAccumulatedData *-- Logs : logs
class TxContext {
fee_context: FeeContext
chain_id: Fr
version: Fr
gas_settings: GasSettings
}
TxContext *-- GasSettings : gas_settings
class CombinedConstantData {
historical_header: Header
tx_context: TxContext
gas_settings: GasSettings
}
CombinedConstantData *-- Header : historical_header
CombinedConstantData *-- TxContext : tx_context
CombinedConstantData *-- GasSettings : gas_settings
class GasSettings {
da.gas_limit: u32
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/protocol-specs/transactions/local-execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ A transaction execution request has the following structure. Note that, since Az
| `origin` | `AztecAddress` | Address of the contract where the transaction is initiated. |
| `functionSelector` | u32 | Selector (identifier) of the function to be called as entrypoint in the origin contract. |
| `argsHash` | `Field` | Hash of the arguments to be used for calling the entrypoint function. |
| `txContext` | `TxContext` | Includes chain id, and protocol version. |
| `txContext` | `TxContext` | Includes chain id, protocol version, and gas settings. |
| `packedArguments` | `PackedValues[]` | Preimages for argument hashes. When executing a function call with the hash of the arguments, the PXE will look for the preimage of that hash in this list, and expand the arguments to execute the call. |
| `authWitnesses` | `AuthWitness[]` | Authorization witnesses. When authorizing an action identified by a hash, the PXE will look for the authorization witness identified by that hash and provide that value to the account contract. |

Expand Down
1 change: 0 additions & 1 deletion docs/docs/protocol-specs/transactions/tx-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ Output of the last iteration of the private kernel circuit. Includes _accumulate
| unencryptedLogsHash | Field[] | Accumulated unencrypted logs hash from all the previous kernel iterations. |
| encryptedLogPreimagesLength | Field | Total accumulated length of the encrypted log preimages emitted in all the previous kernel iterations. |
| unencryptedLogPreimagesLength | Field | Total accumulated length of the unencrypted log preimages emitted in all the previous kernel iterations. |
| newContracts | NewContractData[] | All the new contracts deployed in this transaction. |
| maxBlockNum | Field | Maximum block number (inclusive) for inclusion of this transaction in a block. |

**Block header**
Expand Down
19 changes: 11 additions & 8 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ library Constants {
0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631;
uint256 internal constant DEPLOYER_CONTRACT_ADDRESS =
0x1f47133752dfcd9604f2d89c631797a84ed207c1c51d08533226dafcc8bd8548;
uint256 internal constant DEFAULT_GAS_LIMIT = 1_000_000_000;
uint256 internal constant DEFAULT_TEARDOWN_GAS_LIMIT = 100_000_000;
uint256 internal constant DEFAULT_MAX_FEE_PER_GAS = 10;
uint256 internal constant DEFAULT_INCLUSION_FEE = 0;
uint256 internal constant AZTEC_ADDRESS_LENGTH = 1;
uint256 internal constant DIMENSION_GAS_SETTINGS_LENGTH = 3;
uint256 internal constant GAS_FEES_LENGTH = 3;
uint256 internal constant GAS_LENGTH = 3;
uint256 internal constant GAS_SETTINGS_LENGTH = 1 + 3 * DIMENSION_GAS_SETTINGS_LENGTH;
uint256 internal constant CALL_CONTEXT_LENGTH = 8 + GAS_SETTINGS_LENGTH + GAS_LENGTH;
uint256 internal constant GAS_SETTINGS_LENGTH =
GAS_LENGTH * 2 + GAS_FEES_LENGTH /* inclusion_fee */ + 1;
uint256 internal constant CALL_CONTEXT_LENGTH = 7;
uint256 internal constant CONTENT_COMMITMENT_LENGTH = 4;
uint256 internal constant CONTRACT_INSTANCE_LENGTH = 6;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
Expand All @@ -116,9 +120,8 @@ library Constants {
uint256 internal constant SIDE_EFFECT_LINKED_TO_NOTE_HASH_LENGTH = 3;
uint256 internal constant STATE_REFERENCE_LENGTH =
APPEND_ONLY_TREE_SNAPSHOT_LENGTH + PARTIAL_STATE_REFERENCE_LENGTH;
uint256 internal constant TX_CONTEXT_DATA_LENGTH = 4;
uint256 internal constant TX_REQUEST_LENGTH =
2 + TX_CONTEXT_DATA_LENGTH + FUNCTION_DATA_LENGTH + GAS_SETTINGS_LENGTH;
uint256 internal constant TX_CONTEXT_LENGTH = 2 + GAS_SETTINGS_LENGTH;
uint256 internal constant TX_REQUEST_LENGTH = 2 + TX_CONTEXT_LENGTH + FUNCTION_DATA_LENGTH;
uint256 internal constant HEADER_LENGTH = APPEND_ONLY_TREE_SNAPSHOT_LENGTH
+ CONTENT_COMMITMENT_LENGTH + STATE_REFERENCE_LENGTH + GLOBAL_VARIABLES_LENGTH;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = CALL_CONTEXT_LENGTH + 3
Expand All @@ -130,7 +133,7 @@ library Constants {
+ MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL + MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL
+ (L2_TO_L1_MESSAGE_LENGTH * MAX_NEW_L2_TO_L1_MSGS_PER_CALL) + 2
+ (SIDE_EFFECT_LENGTH * MAX_ENCRYPTED_LOGS_PER_CALL)
+ (SIDE_EFFECT_LENGTH * MAX_UNENCRYPTED_LOGS_PER_CALL) + 2 + HEADER_LENGTH + 2;
+ (SIDE_EFFECT_LENGTH * MAX_UNENCRYPTED_LOGS_PER_CALL) + 2 + HEADER_LENGTH + TX_CONTEXT_LENGTH;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = CALL_CONTEXT_LENGTH + 2
+ (READ_REQUEST_LENGTH * MAX_NULLIFIER_READ_REQUESTS_PER_CALL)
+ (READ_REQUEST_LENGTH * MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL)
Expand All @@ -140,7 +143,7 @@ library Constants {
+ (SIDE_EFFECT_LINKED_TO_NOTE_HASH_LENGTH * MAX_NEW_NULLIFIERS_PER_CALL)
+ (L2_TO_L1_MESSAGE_LENGTH * MAX_NEW_L2_TO_L1_MSGS_PER_CALL) + 2
+ (SIDE_EFFECT_LENGTH * MAX_UNENCRYPTED_LOGS_PER_CALL) + 1 + HEADER_LENGTH + AZTEC_ADDRESS_LENGTH /* revert_code */
+ 1 + GAS_LENGTH;
+ 1 + 2 * GAS_LENGTH /* transaction_fee */ + 1;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH =
AZTEC_ADDRESS_LENGTH + FUNCTION_DATA_LENGTH + PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH;
uint256 internal constant ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_LENGTH =
Expand Down
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/aztec/src/context/globals.nr
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
mod private_global_variables;
mod public_global_variables;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use dep::protocol_types::{abis::call_context::CallContext, header::Header, traits::Empty};
use crate::context::globals::private_global_variables::PrivateGlobalVariables;

use dep::protocol_types::{
transaction::tx_context::TxContext, abis::{call_context::CallContext, gas_settings::GasSettings},
header::Header, traits::Empty
};

// PrivateContextInputs are expected to be provided to each private function
// docs:start:private-context-inputs
struct PrivateContextInputs {
call_context : CallContext,
historical_header: Header,
private_global_variables: PrivateGlobalVariables,
tx_context: TxContext,
start_side_effect_counter: u32,
}
// docs:end:private-context-inputs
Expand All @@ -17,7 +18,7 @@ impl Empty for PrivateContextInputs {
PrivateContextInputs {
call_context : CallContext::empty(),
historical_header: Header::empty(),
private_global_variables: PrivateGlobalVariables::empty(),
tx_context: TxContext::empty(),
start_side_effect_counter: 0 as u32,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ struct PublicContextInputs {
historical_header: Header,

public_global_variables: PublicGlobalVariables,

start_side_effect_counter: u32,
gas_left: Gas,
transaction_fee: Field,
}
// docs:end:public-context-inputs

Expand All @@ -21,6 +22,8 @@ impl Empty for PublicContextInputs {
historical_header: Header::empty(),
public_global_variables: PublicGlobalVariables::empty(),
start_side_effect_counter: 0 as u32,
gas_left: Gas::empty(),
transaction_fee: 0,
}
}
}
11 changes: 6 additions & 5 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ impl ContextInterface for PrivateContext {
}

fn chain_id(self) -> Field {
self.inputs.private_global_variables.chain_id
self.inputs.tx_context.chain_id
}

fn version(self) -> Field {
self.inputs.private_global_variables.version
self.inputs.tx_context.version
}

fn selector(self) -> FunctionSelector {
Expand Down Expand Up @@ -190,8 +190,7 @@ impl PrivateContext {
encrypted_log_preimages_length,
unencrypted_log_preimages_length,
historical_header: self.historical_header,
chain_id: self.inputs.private_global_variables.chain_id,
version: self.inputs.private_global_variables.version
tx_context: self.inputs.tx_context
};

priv_circuit_pub_inputs
Expand Down Expand Up @@ -500,7 +499,9 @@ impl PrivateContext {
historical_header: Header::empty(),
prover_address: AztecAddress::zero(),
revert_code: 0,
gas_left: Gas::empty()
start_gas_left: Gas::empty(),
end_gas_left: Gas::empty(),
transaction_fee: 0
},
is_execution_request: true
};
Expand Down
4 changes: 3 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 @@ -169,7 +169,9 @@ impl PublicContext {
historical_header: self.inputs.historical_header,
prover_address: self.prover_address,
revert_code: 0,
gas_left: self.inputs.call_context.gas_left
start_gas_left: self.inputs.gas_left,
end_gas_left: self.inputs.gas_left, // AVM should decrease this value
transaction_fee: self.inputs.transaction_fee
};
pub_circuit_pub_inputs
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use dep::types::{
MAX_NULLIFIER_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL
},
hash::{
compute_l2_to_l1_hash, pedersen_hash, private_functions_root_from_siblings,
silo_note_hash, silo_nullifier, stdlib_recursion_verification_key_compress_native_vk,
compute_l2_to_l1_hash, pedersen_hash, private_functions_root_from_siblings, silo_note_hash,
silo_nullifier, stdlib_recursion_verification_key_compress_native_vk
},
merkle_tree::check_membership,
utils::{arrays::{array_length, array_to_bounded_vec, validate_array}},
Expand Down Expand Up @@ -280,15 +280,15 @@ pub fn update_end_values(
if !is_empty(msg) {
let new_l2_to_l1_msgs = compute_l2_to_l1_hash(
storage_contract_address,
private_call_public_inputs.version,
private_call_public_inputs.chain_id,
private_call_public_inputs.tx_context.version,
private_call_public_inputs.tx_context.chain_id,
msg
);
new_l2_to_l1_msgs_to_insert.push(new_l2_to_l1_msgs)
}
}
public_inputs.end.new_l2_to_l1_msgs.extend_from_bounded_vec(new_l2_to_l1_msgs_to_insert);

// logs hashes
// See the following thread if not clear:
// https://discourse.aztec.network/t/proposal-forcing-the-sequencer-to-actually-submit-data-to-l1/426
Expand Down
Loading

0 comments on commit e26d342

Please sign in to comment.