Skip to content

Commit

Permalink
feat: updating global vars with fees (#4421)
Browse files Browse the repository at this point in the history
Fixes #3824
  • Loading branch information
benesjan committed Feb 7, 2024
1 parent fabeac8 commit 34109eb
Show file tree
Hide file tree
Showing 53 changed files with 1,296 additions and 978 deletions.
74 changes: 32 additions & 42 deletions l1-contracts/slither_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Summary
- [low-level-calls](#low-level-calls) (1 results) (Informational)
- [similar-names](#similar-names) (1 results) (Informational)
- [unused-state](#unused-state) (2 results) (Informational)
- [pess-magic-number](#pess-magic-number) (1 results) (Informational)
- [constable-states](#constable-states) (1 results) (Optimization)
## pess-unprotected-setter
Impact: High
Expand All @@ -27,9 +26,9 @@ src/core/Rollup.sol#L54-L94
Impact: Medium
Confidence: Medium
- [ ] ID-1
[HeaderLib.decode(bytes).header](src/core/libraries/HeaderLib.sol#L133) is a local variable never initialized
[HeaderLib.decode(bytes).header](src/core/libraries/HeaderLib.sol#L139) is a local variable never initialized

src/core/libraries/HeaderLib.sol#L133
src/core/libraries/HeaderLib.sol#L139


## unused-return
Expand Down Expand Up @@ -59,43 +58,45 @@ src/core/libraries/decoders/Decoder.sol#L415-L417


- [ ] ID-5
Dubious typecast in [HeaderLib.decode(bytes)](src/core/libraries/HeaderLib.sol#L134-L175):
bytes => bytes32 casting occurs in [header.lastArchive = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L142-L144)
bytes => bytes4 casting occurs in [header.lastArchive = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L142-L144)
bytes => bytes32 casting occurs in [header.bodyHash = bytes32(_header)](src/core/libraries/HeaderLib.sol#L147)
bytes => bytes32 casting occurs in [header.stateReference.l1ToL2MessageTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L150-L152)
bytes => bytes4 casting occurs in [header.stateReference.l1ToL2MessageTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L150-L152)
bytes => bytes32 casting occurs in [header.stateReference.partialStateReference.noteHashTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L153-L155)
bytes => bytes4 casting occurs in [header.stateReference.partialStateReference.noteHashTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L153-L155)
bytes => bytes32 casting occurs in [header.stateReference.partialStateReference.nullifierTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L156-L158)
bytes => bytes4 casting occurs in [header.stateReference.partialStateReference.nullifierTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L156-L158)
bytes => bytes32 casting occurs in [header.stateReference.partialStateReference.contractTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L159-L161)
bytes => bytes4 casting occurs in [header.stateReference.partialStateReference.contractTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L159-L161)
bytes => bytes32 casting occurs in [header.stateReference.partialStateReference.publicDataTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L162-L164)
bytes => bytes4 casting occurs in [header.stateReference.partialStateReference.publicDataTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L162-L164)
bytes => bytes32 casting occurs in [header.globalVariables.chainId = uint256(bytes32(_header))](src/core/libraries/HeaderLib.sol#L167)
bytes => bytes32 casting occurs in [header.globalVariables.version = uint256(bytes32(_header))](src/core/libraries/HeaderLib.sol#L168)
bytes => bytes32 casting occurs in [header.globalVariables.blockNumber = uint256(bytes32(_header))](src/core/libraries/HeaderLib.sol#L169)
bytes => bytes32 casting occurs in [header.globalVariables.timestamp = uint256(bytes32(_header))](src/core/libraries/HeaderLib.sol#L170)
bytes => bytes20 casting occurs in [header.globalVariables.coinbase = address(bytes20(_header))](src/core/libraries/HeaderLib.sol#L171)
bytes => bytes32 casting occurs in [header.globalVariables.feeRecipient = bytes32(_header)](src/core/libraries/HeaderLib.sol#L172)

src/core/libraries/HeaderLib.sol#L134-L175


- [ ] ID-6
Dubious typecast in [Outbox.sendL1Messages(bytes32[])](src/core/messagebridge/Outbox.sol#L38-L46):
uint256 => uint32 casting occurs in [version = uint32(REGISTRY.getVersionFor(msg.sender))](src/core/messagebridge/Outbox.sol#L40)

src/core/messagebridge/Outbox.sol#L38-L46


- [ ] ID-6
- [ ] ID-7
Dubious typecast in [Inbox.sendL2Message(DataStructures.L2Actor,uint32,bytes32,bytes32)](src/core/messagebridge/Inbox.sol#L45-L91):
uint256 => uint64 casting occurs in [fee = uint64(msg.value)](src/core/messagebridge/Inbox.sol#L64)
uint256 => uint32 casting occurs in [entries.insert(key,fee,uint32(_recipient.version),_deadline,_errIncompatibleEntryArguments)](src/core/messagebridge/Inbox.sol#L76)

src/core/messagebridge/Inbox.sol#L45-L91


- [ ] ID-7
Dubious typecast in [HeaderLib.decode(bytes)](src/core/libraries/HeaderLib.sol#L128-L167):
bytes => bytes32 casting occurs in [header.lastArchive = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L136-L138)
bytes => bytes4 casting occurs in [header.lastArchive = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L136-L138)
bytes => bytes32 casting occurs in [header.bodyHash = bytes32(_header)](src/core/libraries/HeaderLib.sol#L141)
bytes => bytes32 casting occurs in [header.stateReference.l1ToL2MessageTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L144-L146)
bytes => bytes4 casting occurs in [header.stateReference.l1ToL2MessageTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L144-L146)
bytes => bytes32 casting occurs in [header.stateReference.partialStateReference.noteHashTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L147-L149)
bytes => bytes4 casting occurs in [header.stateReference.partialStateReference.noteHashTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L147-L149)
bytes => bytes32 casting occurs in [header.stateReference.partialStateReference.nullifierTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L150-L152)
bytes => bytes4 casting occurs in [header.stateReference.partialStateReference.nullifierTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L150-L152)
bytes => bytes32 casting occurs in [header.stateReference.partialStateReference.contractTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L153-L155)
bytes => bytes4 casting occurs in [header.stateReference.partialStateReference.contractTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L153-L155)
bytes => bytes32 casting occurs in [header.stateReference.partialStateReference.publicDataTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L156-L158)
bytes => bytes4 casting occurs in [header.stateReference.partialStateReference.publicDataTree = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L156-L158)
bytes => bytes32 casting occurs in [header.globalVariables.chainId = uint256(bytes32(_header))](src/core/libraries/HeaderLib.sol#L161)
bytes => bytes32 casting occurs in [header.globalVariables.version = uint256(bytes32(_header))](src/core/libraries/HeaderLib.sol#L162)
bytes => bytes32 casting occurs in [header.globalVariables.blockNumber = uint256(bytes32(_header))](src/core/libraries/HeaderLib.sol#L163)
bytes => bytes32 casting occurs in [header.globalVariables.timestamp = uint256(bytes32(_header))](src/core/libraries/HeaderLib.sol#L164)

src/core/libraries/HeaderLib.sol#L128-L167


- [ ] ID-8
Dubious typecast in [MessagesDecoder.read4(bytes,uint256)](src/core/libraries/decoders/MessagesDecoder.sol#L110-L112):
bytes => bytes4 casting occurs in [uint256(uint32(bytes4(_data)))](src/core/libraries/decoders/MessagesDecoder.sol#L111)
Expand Down Expand Up @@ -151,11 +152,11 @@ src/core/messagebridge/Inbox.sol#L45-L91


- [ ] ID-14
[HeaderLib.validate(HeaderLib.Header,uint256,uint256,bytes32)](src/core/libraries/HeaderLib.sol#L91-L121) uses timestamp for comparisons
[HeaderLib.validate(HeaderLib.Header,uint256,uint256,bytes32)](src/core/libraries/HeaderLib.sol#L97-L127) uses timestamp for comparisons
Dangerous comparisons:
- [_header.globalVariables.timestamp > block.timestamp](src/core/libraries/HeaderLib.sol#L105)
- [_header.globalVariables.timestamp > block.timestamp](src/core/libraries/HeaderLib.sol#L111)

src/core/libraries/HeaderLib.sol#L91-L121
src/core/libraries/HeaderLib.sol#L97-L127


- [ ] ID-15
Expand Down Expand Up @@ -361,21 +362,10 @@ src/core/libraries/decoders/Decoder.sol#L103-L104
src/core/libraries/decoders/Decoder.sol#L107-L108


## pess-magic-number
Impact: Informational
Confidence: High
- [ ] ID-43
Magic number 376 is used multiple times in:
[_header.length != 376](src/core/libraries/HeaderLib.sol#L129)
[Errors.HeaderLib__InvalidHeaderSize(376,_header.length)](src/core/libraries/HeaderLib.sol#L130)

src/core/libraries/HeaderLib.sol#L129


## constable-states
Impact: Optimization
Confidence: High
- [ ] ID-44
- [ ] ID-43
[Rollup.lastWarpedBlockTs](src/core/Rollup.sol#L37) should be constant

src/core/Rollup.sol#L37
Expand Down
10 changes: 5 additions & 5 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ library Constants {
uint256 internal constant MAX_NOTES_PER_PAGE = 10;
uint256 internal constant VIEW_NOTE_ORACLE_RETURN_LENGTH = 212;
uint256 internal constant CALL_CONTEXT_LENGTH = 8;
uint256 internal constant GLOBAL_VARIABLES_LENGTH = 4;
uint256 internal constant GLOBAL_VARIABLES_LENGTH = 6;
uint256 internal constant PARTIAL_STATE_REFERENCE_LENGTH = 8;
uint256 internal constant STATE_REFERENCE_LENGTH = 10;
uint256 internal constant HEADER_LENGTH = 18;
uint256 internal constant HEADER_LENGTH = 20;
uint256 internal constant FUNCTION_DATA_LENGTH = 4;
uint256 internal constant CONTRACT_DEPLOYMENT_DATA_LENGTH = 6;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 205;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 210;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 207;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 212;
uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 203;
uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674;
uint256 internal constant COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
uint256 internal constant NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
Expand Down
12 changes: 10 additions & 2 deletions l1-contracts/src/core/libraries/HeaderLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import {Hash} from "./Hash.sol";
* | 0x0118 | 0x20 | version
* | 0x0138 | 0x20 | blockNumber
* | 0x0158 | 0x20 | timestamp
* | 0x0178 | 0x14 | coinbase
* | 0x018c | 0x20 | feeRecipient
* | | | }
* | | | }
* | --- | --- | ---
Expand Down Expand Up @@ -72,6 +74,8 @@ library HeaderLib {
uint256 version;
uint256 blockNumber;
uint256 timestamp;
address coinbase;
bytes32 feeRecipient;
}

struct Header {
Expand All @@ -81,6 +85,8 @@ library HeaderLib {
GlobalVariables globalVariables;
}

uint256 private constant HEADER_LENGTH = 0x1ac; // Header byte length

/**
* @notice Validates the header
* @param _header - The decoded header
Expand Down Expand Up @@ -126,8 +132,8 @@ library HeaderLib {
* @return The decoded header
*/
function decode(bytes calldata _header) internal pure returns (Header memory) {
if (_header.length != 376) {
revert Errors.HeaderLib__InvalidHeaderSize(376, _header.length);
if (_header.length != HEADER_LENGTH) {
revert Errors.HeaderLib__InvalidHeaderSize(HEADER_LENGTH, _header.length);
}

Header memory header;
Expand Down Expand Up @@ -162,6 +168,8 @@ library HeaderLib {
header.globalVariables.version = uint256(bytes32(_header[0x0118:0x0138]));
header.globalVariables.blockNumber = uint256(bytes32(_header[0x0138:0x0158]));
header.globalVariables.timestamp = uint256(bytes32(_header[0x0158:0x0178]));
header.globalVariables.coinbase = address(bytes20(_header[0x0178:0x018c]));
header.globalVariables.feeRecipient = bytes32(_header[0x018c:HEADER_LENGTH]);

return header;
}
Expand Down
2 changes: 2 additions & 0 deletions l1-contracts/test/decoders/Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ contract DecoderBase is Test {
struct GlobalVariables {
uint256 blockNumber;
uint256 chainId;
address coinbase;
bytes32 feeRecipient;
uint256 timestamp;
uint256 version;
}
Expand Down
4 changes: 4 additions & 0 deletions l1-contracts/test/decoders/Decoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ contract DecoderTest is DecoderBase {
assertEq(header.globalVariables.chainId, globalVariables.chainId, "Invalid chain Id");
assertEq(header.globalVariables.timestamp, globalVariables.timestamp, "Invalid timestamp");
assertEq(header.globalVariables.version, globalVariables.version, "Invalid version");
assertEq(header.globalVariables.coinbase, globalVariables.coinbase, "Invalid coinbase");
assertEq(
header.globalVariables.feeRecipient, globalVariables.feeRecipient, "Invalid feeRecipient"
);
}

// StateReference
Expand Down
12 changes: 7 additions & 5 deletions l1-contracts/test/fixtures/empty_block_0.json

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions l1-contracts/test/fixtures/empty_block_1.json

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions l1-contracts/test/fixtures/mixed_block_0.json

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions l1-contracts/test/fixtures/mixed_block_1.json

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import {
ARCHIVE_HEIGHT,
CONTRACT_TREE_HEIGHT,
EthAddress,
Fr,
Header,
L1_TO_L2_MSG_TREE_HEIGHT,
Expand Down Expand Up @@ -550,7 +551,16 @@ export class AztecNodeService implements AztecNode {
public async simulatePublicCalls(tx: Tx) {
this.log.info(`Simulating tx ${await tx.getTxHash()}`);
const blockNumber = (await this.blockSource.getBlockNumber()) + 1;
const newGlobalVariables = await this.globalVariableBuilder.buildGlobalVariables(new Fr(blockNumber));

// If sequencer is not initialized, we just set these values to zero for simulation.
const coinbase = this.sequencer?.coinbase || EthAddress.ZERO;
const feeRecipient = this.sequencer?.feeRecipient || AztecAddress.ZERO;

const newGlobalVariables = await this.globalVariableBuilder.buildGlobalVariables(
new Fr(blockNumber),
coinbase,
feeRecipient,
);
const prevHeader = (await this.blockSource.getBlock(-1))?.header;

// Instantiate merkle trees so uncommitted updates by this simulation are local to it.
Expand Down
16 changes: 13 additions & 3 deletions yarn-project/aztec-nr/aztec/src/abi.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use dep::protocol_types::{
private_circuit_public_inputs::PrivateCircuitPublicInputs,
public_circuit_public_inputs::PublicCircuitPublicInputs,
},
address::{AztecAddress, EthAddress},
contrakt::deployment_data::ContractDeploymentData,
hash::hash_args,
traits::{Hash, Serialize},
Expand All @@ -29,12 +30,21 @@ struct PublicGlobalVariables {
version: Field,
block_number: Field,
timestamp: Field,
coinbase: EthAddress,
fee_recipient: AztecAddress,
}
// docs:end:public-global-variables

impl Serialize<4> for PublicGlobalVariables {
fn serialize(self) -> [Field; 4] {
[self.chain_id, self.version, self.block_number, self.timestamp]
impl Serialize<6> for PublicGlobalVariables {
fn serialize(self) -> [Field; 6] {
[
self.chain_id,
self.version,
self.block_number,
self.timestamp,
self.coinbase.to_field(),
self.fee_recipient.to_field(),
]
}
}

Expand Down
8 changes: 8 additions & 0 deletions yarn-project/aztec-nr/aztec/src/context/public.nr
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ impl PublicContext {
self.inputs.public_global_variables.timestamp
}

pub fn coinbase(self) -> EthAddress {
self.inputs.public_global_variables.coinbase
}

pub fn fee_recipient(self) -> AztecAddress {
self.inputs.public_global_variables.fee_recipient
}

pub fn finish(self) -> PublicCircuitPublicInputs {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
let unencrypted_logs_hash = [0; NUM_FIELDS_PER_SHA256];
Expand Down
1 change: 0 additions & 1 deletion yarn-project/aztec-nr/aztec/src/messaging.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod l1_to_l2_message_getter_data;

use l1_to_l2_message_getter_data::make_l1_to_l2_message_getter_data;

use crate::abi::PublicContextInputs;
use crate::oracle::get_l1_to_l2_message::get_l1_to_l2_message_call;

use dep::std::merkle::compute_merkle_root;
Expand Down
7 changes: 1 addition & 6 deletions yarn-project/aztec-nr/aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::abi::PublicContextInputs;
use crate::context::{
PrivateContext,
PublicContext,
Expand Down Expand Up @@ -35,11 +34,7 @@ pub fn create_note<Note, N>(
}
}

pub fn create_note_hash_from_public<Note, N>(
context: &mut PublicContext,
storage_slot: Field,
note: &mut Note
) where Note: NoteInterface<N> {
pub fn create_note_hash_from_public<Note, N>(context: &mut PublicContext, storage_slot: Field, note: &mut Note) where Note: NoteInterface<N> {
let contract_address = (*context).this_address();

let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true };
Expand Down
1 change: 0 additions & 1 deletion yarn-project/aztec-nr/aztec/src/state_vars/set.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use dep::std::option::Option;
use crate::abi::PublicContextInputs;
use dep::protocol_types::{
constants::{MAX_NOTES_PER_PAGE, MAX_READ_REQUESTS_PER_CALL},
abis::side_effect::{SideEffect, SideEffectLinkedToNoteHash},
Expand Down
18 changes: 9 additions & 9 deletions yarn-project/circuit-types/src/interfaces/configs.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { AztecAddress, EthAddress } from '@aztec/circuits.js';

/**
* The sequencer configuration.
*/
export interface SequencerConfig {
/**
* The number of ms to wait between polling for pending txs.
*/
/** The number of ms to wait between polling for pending txs. */
transactionPollingIntervalMS?: number;
/**
* The maximum number of txs to include in a block.
*/
/** The maximum number of txs to include in a block. */
maxTxsPerBlock?: number;
/**
* The minimum number of txs to include in a block.
*/
/** The minimum number of txs to include in a block. */
minTxsPerBlock?: number;
/** Recipient of block reward. */
coinbase?: EthAddress;
/** Address to receive fees. */
feeRecipient?: AztecAddress;
}
Loading

0 comments on commit 34109eb

Please sign in to comment.