Skip to content

Commit

Permalink
chore(uniswap_tests): test edge cases around uniswap flow (#2620)
Browse files Browse the repository at this point in the history
Fix #2493. Part of #2167 
* Adds test cases for both public, private flow
* Found an edge case that wasn't tested in l1<>l2 tests so added those
too
* Also rename `swap` to `swap_private`

Runtime currently is ~5 mins
  • Loading branch information
rahul-kothari authored Oct 6, 2023
1 parent 30c7935 commit 7a58fe9
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 27 deletions.
4 changes: 2 additions & 2 deletions yarn-project/canary/src/uniswap_trade_on_l1_from_l2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ describe('uniswap_trade_on_l1_from_l2', () => {
const [secretForRedeemingDai, secretHashForRedeemingDai] = await generateClaimSecret();

const withdrawReceipt = await uniswapL2Contract.methods
.swap(
.swap_private(
wethL2Contract.address,
wethL2Bridge.address,
wethAmountToBridge,
Expand All @@ -372,7 +372,7 @@ describe('uniswap_trade_on_l1_from_l2', () => {
// ensure that uniswap contract didn't eat the funds.
await expectPublicBalanceOnL2(uniswapL2Contract.address, 0n, wethL2Contract);

// 6. Consume L2 to L1 message by calling uniswapPortal.swap()
// 6. Consume L2 to L1 message by calling uniswapPortal.swap_private()
logger('Execute withdraw and swap on the uniswapPortal!');
const daiL1BalanceOfPortalBeforeSwap = await daiContract.read.balanceOf([daiTokenPortalAddress.toString()]);
const swapArgs = [
Expand Down
33 changes: 33 additions & 0 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 @@ -209,4 +209,37 @@ describe('e2e_cross_chain_messaging', () => {
.simulate(),
).rejects.toThrowError(`Unknown auth witness for message hash 0x${expectedBurnMessageHash.toString('hex')}`);
});

it("Can't claim funds publicly if they were deposited privately", async () => {
// 1. Mint tokens on L1
const bridgeAmount = 100n;
await crossChainTestHarness.mintTokensOnL1(bridgeAmount);

// 2. Deposit tokens to the TokenPortal privately
const [secretForL2MessageConsumption, secretHashForL2MessageConsumption] =
await crossChainTestHarness.generateClaimSecret();

const messageKey = await crossChainTestHarness.sendTokensToPortalPrivate(
bridgeAmount,
secretHashForL2MessageConsumption,
Fr.random(),
);
expect(await crossChainTestHarness.getL1BalanceOf(ethAccount)).toBe(0n);

// Wait for the archiver to process the message
await delay(5000); /// waiting 5 seconds.

// Perform an unrelated transaction on L2 to progress the rollup. Here we mint public tokens.
await crossChainTestHarness.performL2Transfer(0n);

// 3. Consume L1-> L2 message and try to mint publicly on L2 - should fail
await expect(
l2Bridge
.withWallet(user2Wallet)
.methods.claim_public(ownerAddress, bridgeAmount, 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'",
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,26 @@ describe('e2e_public_cross_chain_messaging', () => {
.simulate(),
).rejects.toThrowError('Assertion failed: Message not authorized by account');
});

it("can't claim funds privately which were intended for public deposit from the token portal", async () => {
const bridgeAmount = 100n;
const [secret, secretHash] = await crossChainTestHarness.generateClaimSecret();

await crossChainTestHarness.mintTokensOnL1(bridgeAmount);
const messageKey = await crossChainTestHarness.sendTokensToPortalPublic(bridgeAmount, secretHash);
expect(await crossChainTestHarness.getL1BalanceOf(ownerEthAddress)).toBe(0n);

// Wait for the archiver to process the message
await delay(5000); /// waiting 5 seconds.

// Perform an unrelated transaction on L2 to progress the rollup. Here we mint public tokens.
await crossChainTestHarness.performL2Transfer(0n);

await expect(
l2Bridge
.withWallet(user2Wallet)
.methods.claim_private(bridgeAmount, secretHash, ownerEthAddress, messageKey, secret)
.simulate(),
).rejects.toThrowError("Cannot satisfy constraint 'l1_to_l2_message_data.message.content == content");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ export class CrossChainTestHarness {
expect(receipt.status).toBe(TxStatus.MINED);
}

async mintTokensPrivateOnL2(amount: bigint, secretHash: Fr) {
const tx = this.l2Token.methods.mint_private(amount, secretHash).send();
const receipt = await tx.wait();
expect(receipt.status).toBe(TxStatus.MINED);
await this.addPendingShieldNoteToPXE(amount, secretHash, receipt.txHash);
}

async performL2Transfer(transferAmount: bigint) {
// send a transfer tx to force through rollup with the message included
const transferTx = this.l2Token.methods.transfer_public(this.ownerAddress, this.receiver, transferAmount, 0).send();
Expand Down
Loading

0 comments on commit 7a58fe9

Please sign in to comment.