Skip to content

Commit

Permalink
feat: outgoing messages to any address (#4512)
Browse files Browse the repository at this point in the history
Fixes #4488

Also fixed there 2 commands broken on master.
  • Loading branch information
benesjan committed Feb 15, 2024
1 parent 6ec9f57 commit 4d0e8d3
Show file tree
Hide file tree
Showing 52 changed files with 1,170 additions and 925 deletions.
34 changes: 20 additions & 14 deletions l1-contracts/slither_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Summary
- [dead-code](#dead-code) (13 results) (Informational)
- [solc-version](#solc-version) (1 results) (Informational)
- [low-level-calls](#low-level-calls) (1 results) (Informational)
- [similar-names](#similar-names) (1 results) (Informational)
- [similar-names](#similar-names) (2 results) (Informational)
- [unused-state](#unused-state) (2 results) (Informational)
- [constable-states](#constable-states) (1 results) (Optimization)
- [pess-multiple-storage-read](#pess-multiple-storage-read) (2 results) (Optimization)
Expand Down Expand Up @@ -193,12 +193,12 @@ src/core/Rollup.sol#L27-L103


- [ ] ID-19
The following public functions could be turned into external in [Outbox](src/core/messagebridge/Outbox.sol#L21-L149) contract:
The following public functions could be turned into external in [Outbox](src/core/messagebridge/Outbox.sol#L21-L148) contract:
[Outbox.constructor(address)](src/core/messagebridge/Outbox.sol#L29-L31)
[Outbox.get(bytes32)](src/core/messagebridge/Outbox.sol#L78-L85)
[Outbox.contains(bytes32)](src/core/messagebridge/Outbox.sol#L92-L94)
[Outbox.get(bytes32)](src/core/messagebridge/Outbox.sol#L77-L84)
[Outbox.contains(bytes32)](src/core/messagebridge/Outbox.sol#L91-L93)

src/core/messagebridge/Outbox.sol#L21-L149
src/core/messagebridge/Outbox.sol#L21-L148


- [ ] ID-20
Expand Down Expand Up @@ -271,9 +271,9 @@ src/core/libraries/decoders/Decoder.sol#L401-L407


- [ ] ID-29
[Outbox._errNothingToConsume(bytes32)](src/core/messagebridge/Outbox.sol#L115-L117) is never used and should be removed
[Outbox._errNothingToConsume(bytes32)](src/core/messagebridge/Outbox.sol#L114-L116) is never used and should be removed

src/core/messagebridge/Outbox.sol#L115-L117
src/core/messagebridge/Outbox.sol#L114-L116


- [ ] ID-30
Expand Down Expand Up @@ -325,9 +325,9 @@ src/core/libraries/decoders/Decoder.sol#L132-L134


- [ ] ID-38
[Outbox._errIncompatibleEntryArguments(bytes32,uint64,uint64,uint32,uint32,uint32,uint32)](src/core/messagebridge/Outbox.sol#L130-L148) is never used and should be removed
[Outbox._errIncompatibleEntryArguments(bytes32,uint64,uint64,uint32,uint32,uint32,uint32)](src/core/messagebridge/Outbox.sol#L129-L147) is never used and should be removed

src/core/messagebridge/Outbox.sol#L130-L148
src/core/messagebridge/Outbox.sol#L129-L147


## solc-version
Expand All @@ -350,6 +350,12 @@ src/core/messagebridge/Inbox.sol#L148-L153
Impact: Informational
Confidence: Medium
- [ ] ID-41
Variable [Constants.L1_TO_L2_MESSAGE_LENGTH](src/core/libraries/ConstantsGen.sol#L80) is too similar to [Constants.L2_TO_L1_MESSAGE_LENGTH](src/core/libraries/ConstantsGen.sol#L81)

src/core/libraries/ConstantsGen.sol#L80


- [ ] ID-42
Variable [Rollup.AVAILABILITY_ORACLE](src/core/Rollup.sol#L30) is too similar to [Rollup.constructor(IRegistry,IAvailabilityOracle)._availabilityOracle](src/core/Rollup.sol#L39)

src/core/Rollup.sol#L30
Expand All @@ -358,13 +364,13 @@ src/core/Rollup.sol#L30
## unused-state
Impact: Informational
Confidence: High
- [ ] ID-42
- [ ] ID-43
[Decoder.END_TREES_BLOCK_HEADER_OFFSET](src/core/libraries/decoders/Decoder.sol#L103-L104) is never used in [Decoder](src/core/libraries/decoders/Decoder.sol#L72-L418)

src/core/libraries/decoders/Decoder.sol#L103-L104


- [ ] ID-43
- [ ] ID-44
[Decoder.BLOCK_HEADER_OFFSET](src/core/libraries/decoders/Decoder.sol#L107-L108) is never used in [Decoder](src/core/libraries/decoders/Decoder.sol#L72-L418)

src/core/libraries/decoders/Decoder.sol#L107-L108
Expand All @@ -373,7 +379,7 @@ src/core/libraries/decoders/Decoder.sol#L107-L108
## constable-states
Impact: Optimization
Confidence: High
- [ ] ID-44
- [ ] ID-45
[Rollup.lastWarpedBlockTs](src/core/Rollup.sol#L37) should be constant

src/core/Rollup.sol#L37
Expand All @@ -382,13 +388,13 @@ src/core/Rollup.sol#L37
## pess-multiple-storage-read
Impact: Optimization
Confidence: High
- [ ] ID-45
- [ ] ID-46
In a function [FrontierMerkle.root()](src/core/messagebridge/frontier_tree/Frontier.sol#L39-L72) variable [FrontierMerkle.DEPTH](src/core/messagebridge/frontier_tree/Frontier.sol#L8) is read multiple times

src/core/messagebridge/frontier_tree/Frontier.sol#L39-L72


- [ ] ID-46
- [ ] ID-47
In a function [FrontierMerkle.root()](src/core/messagebridge/frontier_tree/Frontier.sol#L39-L72) variable [FrontierMerkle.frontier](src/core/messagebridge/frontier_tree/Frontier.sol#L13) is read multiple times

src/core/messagebridge/frontier_tree/Frontier.sol#L39-L72
Expand Down
7 changes: 4 additions & 3 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ library Constants {
uint256 internal constant DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631;
uint256 internal constant L1_TO_L2_MESSAGE_LENGTH = 8;
uint256 internal constant L2_TO_L1_MESSAGE_LENGTH = 2;
uint256 internal constant L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH = 25;
uint256 internal constant MAX_NOTE_FIELDS_LENGTH = 20;
uint256 internal constant GET_NOTE_ORACLE_RETURN_LENGTH = 23;
Expand All @@ -90,11 +91,11 @@ library Constants {
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 = 207;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 212;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 209;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 214;
uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 2;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 187;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 189;
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
1 change: 1 addition & 0 deletions l1-contracts/src/core/libraries/DataStructures.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ library DataStructures {
* @param sender - The sender of the message
* @param recipient - The recipient of the message
* @param content - The content of the message (application specific) padded to bytes32 or hashed if larger.
* @dev Not to be confused with L2ToL1Message in Noir circuits
*/
struct L2ToL1Msg {
DataStructures.L2Actor sender;
Expand Down
1 change: 0 additions & 1 deletion l1-contracts/src/core/messagebridge/Outbox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ contract Outbox is IOutbox {

/**
* @notice Consumes an entry from the Outbox
* @dev Only meaningfully callable by portals, otherwise should never hit an entry
* @dev Emits the `MessageConsumed` event when consuming messages
* @param _message - The L2 to L1 message
* @return entryKey - The key of the entry removed
Expand Down
14 changes: 8 additions & 6 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use dep::protocol_types::{
RETURN_VALUES_LENGTH
},
contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest},
grumpkin_private_key::GrumpkinPrivateKey, hash::hash_args, header::Header, utils::reader::Reader
grumpkin_private_key::GrumpkinPrivateKey, hash::hash_args, header::Header,
messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader
};
use dep::std::option::Option;

Expand All @@ -52,7 +53,7 @@ struct PrivateContext {

private_call_stack_hashes : BoundedVec<Field, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL>,
public_call_stack_hashes : BoundedVec<Field, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL>,
new_l2_to_l1_msgs : BoundedVec<Field, MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,
new_l2_to_l1_msgs : BoundedVec<L2ToL1Message, MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,
// docs:end:private-context

// Header of a block whose state is used during private execution (not the block the transaction is included in).
Expand Down Expand Up @@ -80,7 +81,7 @@ impl PrivateContext {
historical_header: inputs.historical_header,
private_call_stack_hashes: BoundedVec::new(0),
public_call_stack_hashes: BoundedVec::new(0),
new_l2_to_l1_msgs: BoundedVec::new(0),
new_l2_to_l1_msgs: BoundedVec::new(L2ToL1Message::empty()),
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
// encrypted_logs_preimages: Vec::new(),
// unencrypted_logs_preimages: Vec::new(),
Expand Down Expand Up @@ -202,9 +203,10 @@ impl PrivateContext {
}

// docs:start:context_message_portal
pub fn message_portal(&mut self, content: Field) {
pub fn message_portal(&mut self, recipient: EthAddress, content: Field) {
// docs:end:context_message_portal
self.new_l2_to_l1_msgs.push(content);
let message = L2ToL1Message { recipient, content };
self.new_l2_to_l1_msgs.push(message);
}

// PrivateContextInputs must be temporarily passed in to prevent too many unknowns
Expand Down Expand Up @@ -388,7 +390,7 @@ impl PrivateContext {
public_call_stack_hashes: [0; MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL],
new_commitments: [SideEffect::empty(); MAX_NEW_COMMITMENTS_PER_CALL],
new_nullifiers: [SideEffectLinkedToNoteHash::empty(); MAX_NEW_NULLIFIERS_PER_CALL],
new_l2_to_l1_msgs: [0; MAX_NEW_L2_TO_L1_MSGS_PER_CALL],
new_l2_to_l1_msgs: [L2ToL1Message::empty(); MAX_NEW_L2_TO_L1_MSGS_PER_CALL],
unencrypted_logs_hash: [0; NUM_FIELDS_PER_SHA256],
unencrypted_log_preimages_length: 0,
historical_header: Header::empty(),
Expand Down
43 changes: 12 additions & 31 deletions noir-projects/aztec-nr/aztec/src/context/public_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use dep::protocol_types::{
RETURN_VALUES_LENGTH
},
contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest},
hash::hash_args, header::Header, utils::reader::Reader
hash::hash_args, header::Header, messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader
};

struct PublicContext {
Expand All @@ -35,7 +35,7 @@ struct PublicContext {
new_commitments: BoundedVec<SideEffect, MAX_NEW_COMMITMENTS_PER_CALL>,
new_nullifiers: BoundedVec<SideEffectLinkedToNoteHash, MAX_NEW_NULLIFIERS_PER_CALL>,

new_l2_to_l1_msgs: BoundedVec<Field, MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,
new_l2_to_l1_msgs: BoundedVec<L2ToL1Message, MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,

unencrypted_logs_hash: BoundedVec<Field, NUM_FIELDS_PER_SHA256>,
unencrypted_logs_preimages_length: Field,
Expand All @@ -60,7 +60,7 @@ impl PublicContext {
public_call_stack_hashes: BoundedVec::new(0),
new_commitments: BoundedVec::new(SideEffect::empty()),
new_nullifiers: BoundedVec::new(SideEffectLinkedToNoteHash::empty()),
new_l2_to_l1_msgs: BoundedVec::new(0),
new_l2_to_l1_msgs: BoundedVec::new(L2ToL1Message::empty()),
unencrypted_logs_hash: BoundedVec::new(0),
unencrypted_logs_preimages_length: 0,
historical_header: inputs.historical_header,
Expand Down Expand Up @@ -150,8 +150,9 @@ impl PublicContext {
self.side_effect_counter = self.side_effect_counter + 1;
}

pub fn message_portal(&mut self, content: Field) {
self.new_l2_to_l1_msgs.push(content);
pub fn message_portal(&mut self, recipient: EthAddress, content: Field) {
let message = L2ToL1Message { recipient, content };
self.new_l2_to_l1_msgs.push(message);
}

// PrivateContextInputs must be temporarily passed in to prevent too many unknowns
Expand Down Expand Up @@ -193,53 +194,33 @@ impl PublicContext {
) -> [Field; RETURN_VALUES_LENGTH] {
let args_hash = hash_args(args);
assert(args_hash == arguments::pack_arguments(args));
call_public_function_internal(
contract_address,
function_selector,
args_hash,
false
)
call_public_function_internal(contract_address, function_selector, args_hash, false)
}

pub fn call_public_function_static<ARGS_COUNT>(
_self: Self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args: [Field; ARGS_COUNT],
args: [Field; ARGS_COUNT]
) -> [Field; RETURN_VALUES_LENGTH] {
let args_hash = hash_args(args);
assert(args_hash == arguments::pack_arguments(args));
call_public_function_internal(
contract_address,
function_selector,
args_hash,
true
)
call_public_function_internal(contract_address, function_selector, args_hash, true)
}

pub fn call_public_function_no_args(
_self: Self,
contract_address: AztecAddress,
function_selector: FunctionSelector
) -> [Field; RETURN_VALUES_LENGTH] {
call_public_function_internal(
contract_address,
function_selector,
0,
false,
)
call_public_function_internal(contract_address, function_selector, 0, false)
}

pub fn call_public_function_no_args_static(
_self: Self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
function_selector: FunctionSelector
) -> [Field; RETURN_VALUES_LENGTH] {
call_public_function_internal(
contract_address,
function_selector,
0,
true,
)
call_public_function_internal(contract_address, function_selector, 0, true)
}
}
14 changes: 13 additions & 1 deletion noir-projects/noir-contracts/contracts/test_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,19 @@ contract Test {
let note = DummyNote::new(amount, secret_hash);

// Public oracle call to emit new commitment.
context.message_portal(note.get_commitment());
context.message_portal(context.this_portal_address(), note.get_commitment());
}

#[aztec(public)]
fn create_l2_to_l1_message_arbitrary_recipient_public(content: Field, recipient: EthAddress) {
// Public oracle call to emit new commitment.
context.message_portal(recipient, content);
}

#[aztec(private)]
fn create_l2_to_l1_message_arbitrary_recipient_private(content: Field, recipient: EthAddress) {
// Public oracle call to emit new commitment.
context.message_portal(recipient, content);
}

// Purely exists for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ contract TokenBridge {
) {
// Send an L2 to L1 message
let content = get_withdraw_content_hash(recipient, amount, callerOnL1);
context.message_portal(content);
context.message_portal(context.this_portal_address(), content);

// Burn tokens
Token::at(storage.token.read()).burn_public(context, context.msg_sender(), amount, nonce);
Expand Down Expand Up @@ -102,7 +102,7 @@ contract TokenBridge {
) {
// Send an L2 to L1 message
let content = get_withdraw_content_hash(recipient, amount, callerOnL1);
context.message_portal(content);
context.message_portal(context.this_portal_address(), content);

// docs:start:call_assert_token_is_same
// Assert that user provided token address is same as seen in storage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ contract Uniswap {
canceller_for_L1_to_L2_message,
caller_on_L1
);
context.message_portal(content_hash);
context.message_portal(context.this_portal_address(), content_hash);
}
// docs:end:swap_public

Expand Down Expand Up @@ -164,7 +164,7 @@ contract Uniswap {
canceller_for_L1_to_L2_message,
caller_on_L1
);
context.message_portal(content_hash);
context.message_portal(context.this_portal_address(), content_hash);
}
// docs:end:swap_private

Expand Down
2 changes: 1 addition & 1 deletion noir-projects/noir-protocol-circuits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build": "yarn clean && yarn noir:build",
"clean": "rm -rf ./dest src/target",
"noir:build": "cd src && ../../../noir/target/release/nargo compile --silence-warnings",
"test": "cd src && ../../../../noir/target/release/nargo test"
"test": "cd src && ../../../noir/target/release/nargo test"
},
"files": [
"dest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,16 @@ pub fn update_end_values(
public_inputs.end.public_call_stack.extend_from_bounded_vec(public_call_stack);

// new l2 to l1 messages
let portal_contract_address = private_call.portal_contract_address;
let new_l2_to_l1_msgs = private_call_public_inputs.new_l2_to_l1_msgs;
let mut new_l2_to_l1_msgs_to_insert : BoundedVec<Field, MAX_NEW_L2_TO_L1_MSGS_PER_CALL> = BoundedVec::new(0);
for i in 0..MAX_NEW_L2_TO_L1_MSGS_PER_CALL {
let msg_is_zero = new_l2_to_l1_msgs[i] == 0;
if msg_is_zero == false {
let msg = new_l2_to_l1_msgs[i];
if !is_empty(msg) {
let new_l2_to_l1_msgs = compute_l2_to_l1_hash(
storage_contract_address,
private_call_public_inputs.version,
portal_contract_address,
private_call_public_inputs.chain_id,
new_l2_to_l1_msgs[i]
msg
);
new_l2_to_l1_msgs_to_insert.push(new_l2_to_l1_msgs)
}
Expand Down
Loading

0 comments on commit 4d0e8d3

Please sign in to comment.