Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add totalFees to global variables #6439

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions barretenberg/cpp/pil/avm/constants.pil
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ namespace constants(256);
pol PORTAL_SELECTOR = 2;

// NOTE: constant expression evaluation does not seem to be supported yet in pil
// pol START_GLOBAL_VARIABLES = CALL_CONTEXT_LENGTH + HEADER_LENGTH = 6 + 22 = 28
// pol START_GLOBAL_VARIABLES = CALL_CONTEXT_LENGTH + HEADER_LENGTH = 6 + 23 = 29

// Global Variables
pol CHAIN_ID_SELECTOR = 28;
pol VERSION_SELECTOR = 29;
pol BLOCK_NUMBER_SELECTOR = 30;
pol TIMESTAMP_SELECTOR = 31;
pol COINBASE_SELECTOR = 32;
pol CHAIN_ID_SELECTOR = 29;
pol VERSION_SELECTOR = 30;
pol BLOCK_NUMBER_SELECTOR = 31;
pol TIMESTAMP_SELECTOR = 32;
pol COINBASE_SELECTOR = 33;

pol END_GLOBAL_VARIABLES = 28 + 8; // We only use the first 5 of 8 global variables for now
pol END_GLOBAL_VARIABLES = 28 + 10; // We only use the first 5 of 10 global variables for now

pol START_SIDE_EFFECT_COUNTER = 36;
pol START_SIDE_EFFECT_COUNTER = 38;

// Gas
pol FEE_PER_DA_GAS_SELECTOR = 37;
pol FEE_PER_L2_GAS_SELECTOR = 38;
pol FEE_PER_DA_GAS_SELECTOR = 39;
pol FEE_PER_L2_GAS_SELECTOR = 40;

pol TRANSACTION_FEE_SELECTOR = 39;
pol TRANSACTION_FEE_SELECTOR = 41;

Original file line number Diff line number Diff line change
Expand Up @@ -915,63 +915,63 @@ template <typename FF_> class avm_mainImpl {
{
Avm_DECLARE_VIEWS(83);

auto tmp = (avm_main_sel_op_fee_per_da_gas * (avm_kernel_kernel_sel - FF(37)));
auto tmp = (avm_main_sel_op_fee_per_da_gas * (avm_kernel_kernel_sel - FF(39)));
tmp *= scaling_factor;
std::get<83>(evals) += tmp;
}
// Contribution 84
{
Avm_DECLARE_VIEWS(84);

auto tmp = (avm_main_sel_op_fee_per_l2_gas * (avm_kernel_kernel_sel - FF(38)));
auto tmp = (avm_main_sel_op_fee_per_l2_gas * (avm_kernel_kernel_sel - FF(40)));
tmp *= scaling_factor;
std::get<84>(evals) += tmp;
}
// Contribution 85
{
Avm_DECLARE_VIEWS(85);

auto tmp = (avm_main_sel_op_transaction_fee * (avm_kernel_kernel_sel - FF(39)));
auto tmp = (avm_main_sel_op_transaction_fee * (avm_kernel_kernel_sel - FF(41)));
tmp *= scaling_factor;
std::get<85>(evals) += tmp;
}
// Contribution 86
{
Avm_DECLARE_VIEWS(86);

auto tmp = (avm_main_sel_op_chain_id * (avm_kernel_kernel_sel - FF(28)));
auto tmp = (avm_main_sel_op_chain_id * (avm_kernel_kernel_sel - FF(29)));
tmp *= scaling_factor;
std::get<86>(evals) += tmp;
}
// Contribution 87
{
Avm_DECLARE_VIEWS(87);

auto tmp = (avm_main_sel_op_version * (avm_kernel_kernel_sel - FF(29)));
auto tmp = (avm_main_sel_op_version * (avm_kernel_kernel_sel - FF(30)));
tmp *= scaling_factor;
std::get<87>(evals) += tmp;
}
// Contribution 88
{
Avm_DECLARE_VIEWS(88);

auto tmp = (avm_main_sel_op_block_number * (avm_kernel_kernel_sel - FF(30)));
auto tmp = (avm_main_sel_op_block_number * (avm_kernel_kernel_sel - FF(31)));
tmp *= scaling_factor;
std::get<88>(evals) += tmp;
}
// Contribution 89
{
Avm_DECLARE_VIEWS(89);

auto tmp = (avm_main_sel_op_coinbase * (avm_kernel_kernel_sel - FF(32)));
auto tmp = (avm_main_sel_op_coinbase * (avm_kernel_kernel_sel - FF(33)));
tmp *= scaling_factor;
std::get<89>(evals) += tmp;
}
// Contribution 90
{
Avm_DECLARE_VIEWS(90);

auto tmp = (avm_main_sel_op_timestamp * (avm_kernel_kernel_sel - FF(31)));
auto tmp = (avm_main_sel_op_timestamp * (avm_kernel_kernel_sel - FF(32)));
tmp *= scaling_factor;
std::get<90>(evals) += tmp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const size_t CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 2;
const size_t ETH_ADDRESS_LENGTH = 1;
const size_t FUNCTION_DATA_LENGTH = 2;
const size_t FUNCTION_LEAF_PREIMAGE_LENGTH = 5;
const size_t GLOBAL_VARIABLES_LENGTH = 6 + GAS_FEES_LENGTH;
const size_t GLOBAL_VARIABLES_LENGTH = 6 + GAS_FEES_LENGTH + 1;
const size_t APPEND_ONLY_TREE_SNAPSHOT_LENGTH = 2;
const size_t L1_TO_L2_MESSAGE_LENGTH = 6;
const size_t L2_TO_L1_MESSAGE_LENGTH = 3;
Expand Down
6 changes: 5 additions & 1 deletion docs/docs/protocol-specs/gas-and-fees/kernel-tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Header --> GlobalVariables

class GlobalVariables {
+GasFees gas_fees
+Fr total_fees
}
GlobalVariables --> GasFees

Expand Down Expand Up @@ -397,6 +398,7 @@ PublicContextInputs --> Gas

class GlobalVariables {
+GasFees gas_fees
+Fr total_fees
}
GlobalVariables --> GasFees

Expand Down Expand Up @@ -538,10 +540,12 @@ The interplay between these two `revert_code`s is as follows:
| 1 | 1 | 3 |
| 2 or 3 | (any) | (unchanged) |

## Base Rollup Kernel Circuit
## Rollup Kernel Circuits

The base rollup kernel circuit takes in a `KernelData`, which contains a `KernelCircuitPublicInputs`, which it uses to compute the `transaction_fee`.

Additionally, it verifies that the max fees per gas specified by the user are greater than the current block's fees per gas. It also verifies the `constant_data.global_variables.gas_fees` are correct.

After the public data writes specific to this transaction have been processed, and a new tree root is produced, the kernel circuit injects an additional public data write based upon that root which deducts the transaction fee from the `fee_payer`'s balance.

The calculated trasaction fee is set as output on the base rollup as `aggregate_fees`. Each subsequent merge rollup circuit sums this value from both of its inputs. The root rollup circuit then uses this value to set the `total_fees` in the `Header`.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ The block header contains a `GlobalVariables`, which contains a `GasFees` object
- `feePerDaGas`: The fee in [FPA](./fee-payment-asset.md) per unit of DA gas consumed for transactions in the block.
- `feePerL2Gas`: The fee in FPA per unit of L2 gas consumed for transactions in the block.

`GlobalVariables` also includes
- a `totalFees` field, which is the total fees collected in the block in FPA.
- a `coinbase` field, which is the L1 address that receives the fees.

## Updating the `GasFees` Object

Presently, the `feePerDaGas` and `feePerL2Gas` are fixed at `1` FPA per unit of DA gas and L2 gas consumed, respectively.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Header {

class GlobalVariables {
+GasFees gasFees
+Fr totalFees
}

class GasFees {
Expand All @@ -97,6 +98,8 @@ A transaction cannot be executed if the `maxFeesPerGas` is less than the `feePer

The `feePerGas` is presently held constant at `1` for both dimensions, but may be updated in future protocol versions.

`totalFees` is the total fees contained in the block.

## Transaction Fee

The transaction fee is calculated as:
Expand Down
1 change: 1 addition & 0 deletions docs/docs/protocol-specs/rollup-circuits/base-rollup.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class GlobalVariables {
fee_recipient: Address
gas_fees.fees_per_da_gas: Fr
gas_fees.fees_per_l2_gas: Fr
total_fees: Fr
}

class ContentCommitment {
Expand Down
1 change: 1 addition & 0 deletions docs/docs/protocol-specs/rollup-circuits/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class GlobalVariables {
fee_recipient: Address
gas_fees.fees_per_da_gas: Fr
gas_fees.fees_per_l2_gas: Fr
total_fees: Fr
}

class ContentCommitment {
Expand Down
1 change: 1 addition & 0 deletions docs/docs/protocol-specs/rollup-circuits/merge-rollup.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class GlobalVariables {
fee_recipient: Address
gas_fees.fees_per_da_gas: Fr
gas_fees.fees_per_l2_gas: Fr
total_fees: Fr
}

class ConstantRollupData {
Expand Down
1 change: 1 addition & 0 deletions docs/docs/protocol-specs/rollup-circuits/root-rollup.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class GlobalVariables {
fee_recipient: Address
gas_fees.fees_per_da_gas: Fr
gas_fees.fees_per_l2_gas: Fr
total_fees: Fr
}

class ContentCommitment {
Expand Down
1 change: 1 addition & 0 deletions docs/docs/protocol-specs/state/archive.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class GlobalVariables {
fee_recipient: Address
gas_fees.fees_per_da_gas: Fr
gas_fees.fees_per_l2_gas: Fr
total_fees: Fr
}

class Header {
Expand Down
1 change: 1 addition & 0 deletions docs/docs/protocol-specs/state/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class GlobalVariables {
fee_recipient: Address
gas_fees.fees_per_da_gas: Fr
gas_fees.fees_per_l2_gas: Fr
total_fees: Fr
}

class ContentCommitment {
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ library Constants {
uint256 internal constant ETH_ADDRESS_LENGTH = 1;
uint256 internal constant FUNCTION_DATA_LENGTH = 2;
uint256 internal constant FUNCTION_LEAF_PREIMAGE_LENGTH = 5;
uint256 internal constant GLOBAL_VARIABLES_LENGTH = 6 + GAS_FEES_LENGTH;
uint256 internal constant GLOBAL_VARIABLES_LENGTH = 6 + GAS_FEES_LENGTH + 1;
uint256 internal constant APPEND_ONLY_TREE_SNAPSHOT_LENGTH = 2;
uint256 internal constant L1_TO_L2_MESSAGE_LENGTH = 6;
uint256 internal constant L2_TO_L1_MESSAGE_LENGTH = 3;
Expand Down
7 changes: 4 additions & 3 deletions l1-contracts/src/core/libraries/HeaderLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ pragma solidity >=0.8.18;

// Libraries
import {Errors} from "./Errors.sol";
import {Constants} from "./ConstantsGen.sol";
import {Hash} from "./Hash.sol";

/**
* @title Header Library
Expand Down Expand Up @@ -50,6 +48,7 @@ import {Hash} from "./Hash.sol";
* | 0x01c8 | 0x20 | feeRecipient
* | 0x01e8 | 0x20 | gasFees.feePerDaGas
* | 0x0208 | 0x20 | gasFees.feePerL2Gas
* | 0x0228 | 0x20 | totalFees
* | | | }
* | | | }
* | --- | --- | ---
Expand Down Expand Up @@ -86,6 +85,7 @@ library HeaderLib {
address coinbase;
bytes32 feeRecipient;
GasFees gasFees;
uint256 totalFees;
}

struct ContentCommitment {
Expand All @@ -102,7 +102,7 @@ library HeaderLib {
GlobalVariables globalVariables;
}

uint256 private constant HEADER_LENGTH = 0x228; // Header byte length
uint256 private constant HEADER_LENGTH = 0x248; // Header byte length

/**
* @notice Validates the header
Expand Down Expand Up @@ -189,6 +189,7 @@ library HeaderLib {
header.globalVariables.feeRecipient = bytes32(_header[0x01c8:0x01e8]);
header.globalVariables.gasFees.feePerDaGas = uint256(bytes32(_header[0x01e8:0x0208]));
header.globalVariables.gasFees.feePerL2Gas = uint256(bytes32(_header[0x0208:0x0228]));
header.globalVariables.totalFees = uint256(bytes32(_header[0x0228:0x0248]));

return header;
}
Expand Down
12 changes: 6 additions & 6 deletions l1-contracts/test/fixtures/empty_block_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"l2ToL1Messages": []
},
"block": {
"archive": "0x28db6a7d7f6d68e8eae12643769246709897c59c72fc94b5b6ce00655b245e42",
"archive": "0x0eae14b1cf1ec4c1de308458307f3e710c14ecba32f0a0d59289273401d3c7f7",
"body": "0x00000000",
"txsEffectsHash": "0x00f6922770c8d944eb7b03abaad695fa06ad1c395d3fbe216dfb508691d16a2f",
"decodedHeader": {
Expand All @@ -23,16 +23,16 @@
"chainId": 31337,
"timestamp": 0,
"version": 1,
"coinbase": "0x4fdc8e41d48477a1e7f9b7a5de5dc36546fdd89e",
"feeRecipient": "0x2aa0b5a2607f90e00553c29ce8bf75a8a453bc167be277fdcbe7c0ff284852fd",
"coinbase": "0xe75949a3b041f25a72023aacd4eafc63c80cfd17",
"feeRecipient": "0x27162d84578334dd9ed9fa3a2809e4861d07b669b7c2f074d59e64d61a847ecf",
"gasFees": {
"feePerDaGas": 0,
"feePerL2Gas": 0
}
},
"lastArchive": {
"nextAvailableLeafIndex": 1,
"root": "0x067a48e3140b6f15d71751ededfa0cccde3d436bb71aa7fec226b0bfe51dc5cf"
"root": "0x05b0b6df52f1d47d0406318558052c89a174fbc9d615def82b3cc9ccc1937db8"
},
"stateReference": {
"l1ToL2MessageTree": {
Expand All @@ -55,8 +55,8 @@
}
}
},
"header": "0x067a48e3140b6f15d71751ededfa0cccde3d436bb71aa7fec226b0bfe51dc5cf00000001000000000000000000000000000000000000000000000000000000000000000100f6922770c8d944eb7b03abaad695fa06ad1c395d3fbe216dfb508691d16a2f00089a9d421a82c4a25f7acbebe69e638d5b064fa8a60e018793dcb0be53752c0007638bb56b6dda2b64b8f76841114ac3a87a1820030e2e16772c4d294879c31864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000001016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000000800bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001000572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000800000000000000000000000000000000000000000000000000000000000007a690000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000004fdc8e41d48477a1e7f9b7a5de5dc36546fdd89e2aa0b5a2607f90e00553c29ce8bf75a8a453bc167be277fdcbe7c0ff284852fd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"publicInputsHash": "0x00f30a1da4a60be604e620ab3c0e4f960d0d47b647cde7c460e64a6c7e667e33",
"header": "0x05b0b6df52f1d47d0406318558052c89a174fbc9d615def82b3cc9ccc1937db800000001000000000000000000000000000000000000000000000000000000000000000100f6922770c8d944eb7b03abaad695fa06ad1c395d3fbe216dfb508691d16a2f00089a9d421a82c4a25f7acbebe69e638d5b064fa8a60e018793dcb0be53752c0007638bb56b6dda2b64b8f76841114ac3a87a1820030e2e16772c4d294879c31864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000001016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000000800bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001000572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000800000000000000000000000000000000000000000000000000000000000007a69000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000e75949a3b041f25a72023aacd4eafc63c80cfd1727162d84578334dd9ed9fa3a2809e4861d07b669b7c2f074d59e64d61a847ecf000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"publicInputsHash": "0x00953b88e136179ec39a68510c649f5aa1f9cb1b2ae541b327116c3a967f32e2",
"numTxs": 0
}
}
14 changes: 7 additions & 7 deletions l1-contracts/test/fixtures/empty_block_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"l2ToL1Messages": []
},
"block": {
"archive": "0x133f7fd0f4773b3d206b02f672a0fdd1e1a1ea9e562063c758ce4ddf6d92d079",
"archive": "0x19913c1d1ae0902cb33ef13d9419cdadba57fd3bfd33306cbe8beb48f46778a3",
"body": "0x00000000",
"txsEffectsHash": "0x00f6922770c8d944eb7b03abaad695fa06ad1c395d3fbe216dfb508691d16a2f",
"decodedHeader": {
Expand All @@ -21,18 +21,18 @@
"globalVariables": {
"blockNumber": 2,
"chainId": 31337,
"timestamp": 1715590033,
"timestamp": 1715882341,
"version": 1,
"coinbase": "0x4fdc8e41d48477a1e7f9b7a5de5dc36546fdd89e",
"feeRecipient": "0x2aa0b5a2607f90e00553c29ce8bf75a8a453bc167be277fdcbe7c0ff284852fd",
"coinbase": "0xe75949a3b041f25a72023aacd4eafc63c80cfd17",
"feeRecipient": "0x27162d84578334dd9ed9fa3a2809e4861d07b669b7c2f074d59e64d61a847ecf",
"gasFees": {
"feePerDaGas": 0,
"feePerL2Gas": 0
}
},
"lastArchive": {
"nextAvailableLeafIndex": 2,
"root": "0x28db6a7d7f6d68e8eae12643769246709897c59c72fc94b5b6ce00655b245e42"
"root": "0x0eae14b1cf1ec4c1de308458307f3e710c14ecba32f0a0d59289273401d3c7f7"
},
"stateReference": {
"l1ToL2MessageTree": {
Expand All @@ -55,8 +55,8 @@
}
}
},
"header": "0x28db6a7d7f6d68e8eae12643769246709897c59c72fc94b5b6ce00655b245e4200000002000000000000000000000000000000000000000000000000000000000000000100f6922770c8d944eb7b03abaad695fa06ad1c395d3fbe216dfb508691d16a2f00089a9d421a82c4a25f7acbebe69e638d5b064fa8a60e018793dcb0be53752c0007638bb56b6dda2b64b8f76841114ac3a87a1820030e2e16772c4d294879c31864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000002016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000001000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001800572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000c00000000000000000000000000000000000000000000000000000000000007a6900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000006641d3914fdc8e41d48477a1e7f9b7a5de5dc36546fdd89e2aa0b5a2607f90e00553c29ce8bf75a8a453bc167be277fdcbe7c0ff284852fd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"publicInputsHash": "0x00dacc45bae55cf4217a7a66cd1ff9c2fb65726a10c8d4835a8a7c7a9ac0f18c",
"header": "0x0eae14b1cf1ec4c1de308458307f3e710c14ecba32f0a0d59289273401d3c7f700000002000000000000000000000000000000000000000000000000000000000000000100f6922770c8d944eb7b03abaad695fa06ad1c395d3fbe216dfb508691d16a2f00089a9d421a82c4a25f7acbebe69e638d5b064fa8a60e018793dcb0be53752c0007638bb56b6dda2b64b8f76841114ac3a87a1820030e2e16772c4d294879c31864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000002016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000001000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001800572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000c00000000000000000000000000000000000000000000000000000000000007a69000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000066464965e75949a3b041f25a72023aacd4eafc63c80cfd1727162d84578334dd9ed9fa3a2809e4861d07b669b7c2f074d59e64d61a847ecf000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"publicInputsHash": "0x00d8e57e1de54e9d686e874e5870342ab7cb48f0c1210f16c7752ccc73f3a280",
"numTxs": 0
}
}
12 changes: 6 additions & 6 deletions l1-contracts/test/fixtures/mixed_block_0.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions l1-contracts/test/fixtures/mixed_block_1.json

Large diffs are not rendered by default.

Loading
Loading