Skip to content

Commit

Permalink
reorder params in bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-kothari committed Oct 11, 2023
1 parent 900bee6 commit 74a21b8
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 38 deletions.
2 changes: 1 addition & 1 deletion yarn-project/acir-simulator/src/public/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ describe('ACIR public execution simulator', () => {
// Stub message key
const messageKey = Fr.random();
const args = encodeArguments(mintPublicArtifact, [
recipient.toField(),
bridgedAmount,
recipient.toField(),
canceller.toField(),
messageKey,
secret,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('e2e_cross_chain_messaging', () => {
await expect(
l2Bridge
.withWallet(user1Wallet)
.methods.exit_to_l1_private(ethAccount, l2Token.address, withdrawAmount, EthAddress.ZERO, nonce)
.methods.exit_to_l1_private(l2Token.address, withdrawAmount, ethAccount, EthAddress.ZERO, nonce)
.simulate(),
).rejects.toThrowError(`Unknown auth witness for message hash 0x${expectedBurnMessageHash.toString('hex')}`);
});
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('e2e_cross_chain_messaging', () => {
await expect(
l2Bridge
.withWallet(user2Wallet)
.methods.claim_public(ownerAddress, bridgeAmount, ethAccount, messageKey, secretForL2MessageConsumption)
.methods.claim_public(bridgeAmount, ownerAddress, ethAccount, messageKey, secretForL2MessageConsumption)
.simulate(),
).rejects.toThrowError(
"Failed to solve brillig function, reason: explicit trap hit in brillig 'l1_to_l2_message_data.message.content == content'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ describe('e2e_public_cross_chain_messaging', () => {
await expect(
l2Bridge
.withWallet(user2Wallet)
.methods.claim_public(user2Wallet.getAddress(), bridgeAmount, ownerEthAddress, messageKey, secret)
.methods.claim_public(bridgeAmount, user2Wallet.getAddress(), ownerEthAddress, messageKey, secret)
.simulate(),
).rejects.toThrow();

// user2 consumes owner's L1-> L2 message on bridge contract and mints public tokens on L2
logger("user2 consumes owner's message on L2 Publicly");
const tx = l2Bridge
.withWallet(user2Wallet)
.methods.claim_public(ownerAddress, bridgeAmount, ownerEthAddress, messageKey, secret)
.methods.claim_public(bridgeAmount, ownerAddress, ownerEthAddress, messageKey, secret)
.send();
const receipt = await tx.wait();
expect(receipt.status).toBe(TxStatus.MINED);
Expand All @@ -155,7 +155,7 @@ describe('e2e_public_cross_chain_messaging', () => {
await expect(
l2Bridge
.withWallet(ownerWallet)
.methods.exit_to_l1_public(ownerEthAddress, withdrawAmount, EthAddress.ZERO, nonce)
.methods.exit_to_l1_public(withdrawAmount, ownerEthAddress, EthAddress.ZERO, nonce)
.simulate(),
).rejects.toThrowError('Assertion failed: Message not authorized by account');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,23 @@ export class CrossChainTestHarness {
this.logger('Consuming messages on L2 Publicly');
// Call the mint tokens function on the Aztec.nr contract
const tx = this.l2Bridge.methods
.claim_public(this.ownerAddress, bridgeAmount, this.ethAccount, messageKey, secret)
.claim_public(bridgeAmount, this.ownerAddress, this.ethAccount, messageKey, secret)
.send();
const receipt = await tx.wait();
expect(receipt.status).toBe(TxStatus.MINED);
}

async withdrawPrivateFromAztecToL1(withdrawAmount: bigint, nonce: Fr = Fr.ZERO) {
const withdrawTx = this.l2Bridge.methods
.exit_to_l1_private(this.ethAccount, this.l2Token.address, withdrawAmount, EthAddress.ZERO, nonce)
.exit_to_l1_private(this.l2Token.address, withdrawAmount, this.ethAccount, EthAddress.ZERO, nonce)
.send();
const withdrawReceipt = await withdrawTx.wait();
expect(withdrawReceipt.status).toBe(TxStatus.MINED);
}

async withdrawPublicFromAztecToL1(withdrawAmount: bigint, nonce: Fr = Fr.ZERO) {
const withdrawTx = this.l2Bridge.methods
.exit_to_l1_public(this.ethAccount, withdrawAmount, EthAddress.ZERO, nonce)
.exit_to_l1_public(withdrawAmount, this.ethAccount, EthAddress.ZERO, nonce)
.send();
const withdrawReceipt = await withdrawTx.wait();
expect(withdrawReceipt.status).toBe(TxStatus.MINED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ impl TestPrivateContextInterface {
pub fn consume_mint_public_message(
self,
context: &mut PrivateContext,
to: Field,
amount: Field,
to: Field,
canceller: Field,
msg_key: Field,
secret: Field
) {
let mut serialized_args = [0; 5];
serialized_args[0] = to;
serialized_args[1] = amount;
serialized_args[0] = amount;
serialized_args[1] = to;
serialized_args[2] = canceller;
serialized_args[3] = msg_key;
serialized_args[4] = secret;
Expand Down Expand Up @@ -239,15 +239,15 @@ impl TestPublicContextInterface {
pub fn consume_mint_public_message(
self,
context: PublicContext,
to: Field,
amount: Field,
to: Field,
canceller: Field,
msg_key: Field,
secret: Field
) -> [Field; RETURN_VALUES_LENGTH] {
let mut serialized_args = [0; 5];
serialized_args[0] = to;
serialized_args[1] = amount;
serialized_args[0] = amount;
serialized_args[1] = to;
serialized_args[2] = canceller;
serialized_args[3] = msg_key;
serialized_args[4] = secret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ contract Test {

#[aztec(public)]
fn consume_mint_public_message(
to: Field,
amount: Field,
to: Field,
canceller: Field,
msg_key: Field,
secret: Field,
) {
let content_hash = get_mint_public_content_hash(to, amount, canceller);
let content_hash = get_mint_public_content_hash(amount, to, canceller);
// Consume message and emit nullifier
context.consume_l1_to_l2_message(msg_key, content_hash, secret);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::aztec::hash::{sha256_to_field};

// Computes a content hash of a deposit/mint_public message.
// Refer TokenPortal.sol for reference on L1.
fn get_mint_public_content_hash(owner_address: Field, amount: Field, canceller: Field) -> Field {
pub fn get_mint_public_content_hash(amount: Field, owner_address: Field, canceller: Field) -> Field {

let mut hash_bytes: [u8; 100] = [0; 100];
let amount_bytes = amount.to_be_bytes(32);
Expand All @@ -27,7 +27,7 @@ fn get_mint_public_content_hash(owner_address: Field, amount: Field, canceller:

// Computes a content hash of a deposit/mint_private message.
// Refer TokenPortal.sol for reference on L1.
fn get_mint_private_content_hash(amount: Field, secret_hash_for_redeeming_minted_notes: Field, canceller: Field) -> Field {
pub fn get_mint_private_content_hash(amount: Field, secret_hash_for_redeeming_minted_notes: Field, canceller: Field) -> Field {
let mut hash_bytes: [u8; 100] = [0; 100];
let amount_bytes = amount.to_be_bytes(32);
let secret_hash_bytes = secret_hash_for_redeeming_minted_notes.to_be_bytes(32);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ contract TokenBridge {
// Consumes a L1->L2 message and calls the token contract to mint the appropriate amount publicly
#[aztec(public)]
fn claim_public(
to: AztecAddress,
amount: Field,
to: AztecAddress,
canceller: EthereumAddress,
msg_key: Field,
secret: Field,
) -> Field {
let content_hash = get_mint_public_content_hash(to.address, amount, canceller.address);
let content_hash = get_mint_public_content_hash(amount, to.address, canceller.address);
// Consume message and emit nullifier
context.consume_l1_to_l2_message(msg_key, content_hash, secret);

Expand All @@ -71,13 +71,13 @@ contract TokenBridge {
// Requires `msg.sender` to give approval to the bridge to burn tokens on their behalf using witness signatures
#[aztec(public)]
fn exit_to_l1_public(
recipient: EthereumAddress, // ethereum address to withdraw to
amount: Field,
recipient: EthereumAddress, // ethereum address to withdraw to
callerOnL1: EthereumAddress, // ethereum address that can call this function on the L1 portal (0x0 if anyone can call)
nonce: Field, // nonce used in the approval message by `msg.sender` to let bridge burn their tokens on L2
) -> Field {
// Send an L2 to L1 message
let content = get_withdraw_content_hash(recipient.address, amount, callerOnL1.address);
let content = get_withdraw_content_hash(amount, recipient.address, callerOnL1.address);
context.message_portal(content);

// Burn tokens
Expand Down Expand Up @@ -119,14 +119,14 @@ contract TokenBridge {
// Requires `msg.sender` (caller of the method) to give approval to the bridge to burn tokens on their behalf using witness signatures
#[aztec(private)]
fn exit_to_l1_private(
recipient: EthereumAddress, // ethereum address to withdraw to
token: AztecAddress,
amount: Field,
recipient: EthereumAddress, // ethereum address to withdraw to
callerOnL1: EthereumAddress, // ethereum address that can call this function on the L1 portal (0x0 if anyone can call)
nonce: Field, // nonce used in the approval message by `msg.sender` to let bridge burn their tokens on L2
) -> Field {
// Send an L2 to L1 message
let content = get_withdraw_content_hash(recipient.address, amount, callerOnL1.address);
let content = get_withdraw_content_hash(amount, recipient.address, callerOnL1.address);
context.message_portal(content);

// 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 @@ -4,7 +4,7 @@ use dep::aztec::hash::{sha256_to_field};

// Computes a content hash of a deposit/mint_public message.
// Refer TokenPortal.sol for reference on L1.
pub fn get_mint_public_content_hash(owner_address: Field, amount: Field, canceller: Field) -> Field {
pub fn get_mint_public_content_hash(amount: Field, owner_address: Field, canceller: Field) -> Field {

let mut hash_bytes: [u8; 100] = [0; 100];
let amount_bytes = amount.to_be_bytes(32);
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn get_mint_private_content_hash(amount: Field, secret_hash_for_redeeming_mi
}

// Computes a content hash of a withdraw message.
pub fn get_withdraw_content_hash(recipient: Field, amount: Field, callerOnL1: Field) -> Field {
pub fn get_withdraw_content_hash(amount: Field, recipient: Field, callerOnL1: Field) -> Field {
// Compute the content hash
// Compute sha256(selector || amount || recipient)
// then convert to a single field element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,10 @@ impl TokenBridge {
AztecAddress::new(return_values[0])
}

pub fn claim_public(self: Self, context: PublicContext, to: Field, amount: Field, canceller: Field, msg_key: Field, secret: Field) {
pub fn exit_to_l1_public(self: Self, context: PublicContext, amount: Field, recipient: Field, callerOnL1: Field, nonce: Field) {
let _return_values = context.call_public_function(
self.address,
compute_selector("claim_public((Field),Field,Field,Field,Field)"),
[to, amount, canceller, msg_key, secret]
);
}

pub fn exit_to_l1_public(self: Self, context: PublicContext, recipient: Field, amount: Field, callerOnL1: Field, nonce: Field) {
let _return_values = context.call_public_function(
self.address,
compute_selector("exit_to_l1_public((Field),Field,(Field),Field)"),
compute_selector("exit_to_l1_public(Field,(Field),(Field),Field)"),
[recipient, amount, callerOnL1, nonce]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ contract Uniswap {
// this ensures the integrity of what the user originally intends to do on L1.
let input_asset_bridge_portal_address = get_portal_address(input_asset_bridge.address);
let output_asset_bridge_portal_address = get_portal_address(output_asset_bridge.address);
// ensure portal exists - else funds might be lost
assert(input_asset_bridge_portal_address != 0, "L1 portal address of input_asset's bridge is 0");
assert(output_asset_bridge_portal_address != 0, "L1 portal address of output_asset's bridge is 0");

Expand Down Expand Up @@ -160,6 +161,7 @@ contract Uniswap {
// this ensures the integrity of what the user originally intends to do on L1.
let input_asset_bridge_portal_address = get_portal_address(input_asset_bridge.address);
let output_asset_bridge_portal_address = get_portal_address(output_asset_bridge.address);
// ensure portal exists - else funds might be lost
assert(input_asset_bridge_portal_address != 0, "L1 portal address of input_asset's bridge is 0");
assert(output_asset_bridge_portal_address != 0, "L1 portal address of output_asset's bridge is 0");

Expand Down Expand Up @@ -219,9 +221,9 @@ contract Uniswap {
// Exit to L1 Uniswap Portal !
TokenBridge::at(token_bridge.address).exit_to_l1_public(
context,
context.this_portal_address(),
amount,
context.this_portal_address(),
context.this_portal_address(),
nonce_for_burn_approval,
);
}
Expand Down

0 comments on commit 74a21b8

Please sign in to comment.