From 1c371d7a6c19c482ed94d4d01f81dca04ea2bc4f Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Fri, 15 Sep 2023 10:30:39 +0000 Subject: [PATCH] we live to try things out' --- .circleci/config.yml | 13 +++++++++++ .../end-to-end/src/cli_docs_sandbox.test.ts | 3 ++- .../end-to-end/src/e2e_token_bridge.test.ts | 14 ++++++++---- .../token_bridge_contract/src/main.nr | 22 +++++++++++++------ 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 89d875c64f18..476614635fb8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -737,6 +737,17 @@ jobs: name: "Test" command: cond_run_script end-to-end ./scripts/run_tests_local e2e_token_contract.test.ts + e2e-token-bridge-contract: + machine: + image: ubuntu-2004:202010-01 + resource_class: large + steps: + - *checkout + - *setup_env + - run: + name: "Test" + command: cond_run_script end-to-end ./scripts/run_tests_local e2e_token_bridge_contract.test.ts + e2e-private-token-contract: machine: image: ubuntu-2004:202010-01 @@ -1459,6 +1470,7 @@ workflows: - e2e-deploy-contract: *e2e_test - e2e-lending-contract: *e2e_test - e2e-token-contract: *e2e_test + - e2e-token-bridge-contract: *e2e_test - e2e-private-token-contract: *e2e_test - e2e-sandbox-example: *e2e_test - e2e-multi-transfer-contract: *e2e_test @@ -1494,6 +1506,7 @@ workflows: - e2e-deploy-contract - e2e-lending-contract - e2e-token-contract + - e2e-token-bridge-contract - e2e-private-token-contract - e2e-sandbox-example - e2e-multi-transfer-contract diff --git a/yarn-project/end-to-end/src/cli_docs_sandbox.test.ts b/yarn-project/end-to-end/src/cli_docs_sandbox.test.ts index 204e163fa53a..bd5f0e09fc39 100644 --- a/yarn-project/end-to-end/src/cli_docs_sandbox.test.ts +++ b/yarn-project/end-to-end/src/cli_docs_sandbox.test.ts @@ -77,7 +77,7 @@ describe('CLI docs sandbox', () => { logs.splice(0); }; - it('prints example contracts', async () => { + it.only('prints example contracts', async () => { const docs = ` // docs:start:example-contracts % aztec-cli example-contracts @@ -104,6 +104,7 @@ SchnorrAuthWitnessAccountContractAbi SchnorrHardcodedAccountContractAbi SchnorrSingleKeyAccountContractAbi TestContractAbi +TokenBridgeContractAbi TokenContractAbi UniswapContractAbi // docs:end:example-contracts diff --git a/yarn-project/end-to-end/src/e2e_token_bridge.test.ts b/yarn-project/end-to-end/src/e2e_token_bridge.test.ts index 7586e0335389..00874bad1f15 100644 --- a/yarn-project/end-to-end/src/e2e_token_bridge.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_bridge.test.ts @@ -71,7 +71,7 @@ describe('e2e_token_bridge_contract', () => { tokenPortal = contracts.tokenPortal; underlyingERC20 = contracts.underlyingERC20; logger(`Deployed and initialized token, portal and its bridge.`); - }, 60_000); + }, 65_000); afterEach(async () => { await aztecNode?.stop(); @@ -139,7 +139,9 @@ describe('e2e_token_bridge_contract', () => { // 3. Consume message on aztec and mint publicly logger('Consuming messages on L2'); - const tx = bridge.methods.mint_public(bridgeAmount, messageKey, secret, { address: ethAccount.toField() }).send(); + const tx = bridge.methods + .deposit_public(bridgeAmount, messageKey, secret, { address: ethAccount.toField() }) + .send(); const receipt = await tx.wait(); expect(receipt.status).toBe(TxStatus.MINED); const afterBalance = await token.methods.balance_of_public({ address: ownerAddress }).view(); @@ -188,7 +190,11 @@ describe('e2e_token_bridge_contract', () => { const secretHash = await computeMessageSecretHash(secret); // 1. Mint tokens on L1 - await mintTokensOnL1(l1TokenBalance); + // TODO (#2291): Because same owner is used across two tests, l1 balance already exists. This is why we have two separate cross chain tests. + // Separate them like before + if ((await underlyingERC20.read.balanceOf([ethAccount.toString()])) === 0n) { + await mintTokensOnL1(l1TokenBalance); + } // 2. Deposit tokens to the TokenPortal const messageKey = await depositTokensToPortal(bridgeAmount, secretHash); @@ -205,7 +211,7 @@ describe('e2e_token_bridge_contract', () => { // 3. Consume message on aztec and mint publicly logger('Consuming messages on L2'); - const tx = bridge.methods.mint(bridgeAmount, messageKey, secret, { address: ethAccount.toField() }).send(); + const tx = bridge.methods.deposit(bridgeAmount, messageKey, secret, { address: ethAccount.toField() }).send(); const receipt = await tx.wait(); expect(receipt.status).toBe(TxStatus.MINED); const txClaim = token.methods.redeem_shield({ address: ownerAddress }, bridgeAmount, secret).send(); diff --git a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr index 2d3610db9185..0399415907df 100644 --- a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr @@ -46,7 +46,7 @@ contract TokenBridge { // Consumes a L1->L2 message and calls the token contract to mint the appropriate amount publicly #[aztec(public)] - fn mint_public( + fn deposit_public( amount: Field, msg_key: Field, // L1 to L2 message key as derived from the inbox contract secret: Field, @@ -60,14 +60,17 @@ contract TokenBridge { context.consume_l1_to_l2_message(msg_key, content_hash, secret); // Mint token on L2 - Token::at(storage.token.read()).mint_public(context, context.msg_sender(), amount) + // If I call using the interface, for some reason, the kernel queues mint_public twice and mints double the tokens. + // So, for now, call the token contract directly + // Token::at(storage.token.read()).mint_public(context, context.msg_sender(), amount) + context.call_public_function(storage.token.read(), compute_selector("mint_public((Field),Field)"), [context.msg_sender(), amount])[0] } // Consumes a L1->L2 message and calls the token contract to mint the appropriate amount in private assets // User needs to call token.redeem_shield() to get the private assets // This method is public because it accesses public storage. For similar reasons, the corresponding call on the token is also public #[aztec(public)] - fn mint( + fn deposit( amount: Field, msg_key: Field, // L1 to L2 message key as derived from the inbox contract secret: Field, @@ -111,7 +114,7 @@ contract TokenBridge { // Requires `from` to give approval to the bridge to burn tokens on their behalf using witness signatures #[aztec(private)] fn withdraw( - token:AztecAddress, // can't read public storage in private, so pass the token and call an internal public fn to check if provided token is as expected. + token: AztecAddress, // can't read public storage in private, so pass the token and call an internal public fn to check if provided token is as expected. recipient: EthereumAddress, // ethereum address to withdraw to amount: Field, callerOnL1: EthereumAddress, // ethereum address that can call this function on the L1 portal (0x0 if anyone can call) @@ -119,8 +122,12 @@ contract TokenBridge { ) -> Field { // can't read public storage (`storage.token`) in private so let the user pass it in // later assert that this token address is as expected - let return_value = Token::at(token.address).burn(&mut context, context.msg_sender(), amount, nonce); - + // let return_value = Token::at(token.address).burn(&mut context, context.msg_sender(), amount, nonce); + let return_value = context.call_private_function( + token.address, + compute_selector("burn((Field),Field,Field)"), + [context.msg_sender(), amount, nonce] + ); let content = get_withdraw_content_hash(amount, recipient.address, callerOnL1.address); // Emit the l2 to l1 message context.message_portal(content); @@ -128,7 +135,8 @@ contract TokenBridge { // Assert that user provided token address is same as seen in storage. context.call_public_function(token.address, compute_selector("_assert_token_is_same(Field)"), [token.address]); - return_value + return_value[0] + } // /// Unconstrained ///