Skip to content

Commit

Permalink
finished e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 20, 2024
1 parent 80541d7 commit d66e5a9
Showing 1 changed file with 75 additions and 67 deletions.
142 changes: 75 additions & 67 deletions yarn-project/end-to-end/src/e2e_public_cross_chain_messaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ describe('e2e_public_cross_chain_messaging', () => {
expect(await crossChainTestHarness.getL1BalanceOf(ethAccount)).toBe(l1TokenBalance - bridgeAmount);

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

// Perform an unrelated transaction on L2 to progress the rollup. Here we mint public tokens.
const unrelatedMintAmount = 99n;
await crossChainTestHarness.mintTokensPublicOnL2(unrelatedMintAmount);
await crossChainTestHarness.expectPublicBalanceOnL2(ownerAddress, unrelatedMintAmount);
const balanceBefore = unrelatedMintAmount;

// 3. Consume L1-> L2 message and mint public tokens on L2
// 3. Consume L1 -> L2 message and mint public tokens on L2
await crossChainTestHarness.consumeMessageOnAztecAndMintPublicly(bridgeAmount, messageKey, secret);
await crossChainTestHarness.expectPublicBalanceOnL2(ownerAddress, balanceBefore + bridgeAmount);
const afterBalance = balanceBefore + bridgeAmount;
Expand Down Expand Up @@ -250,70 +250,78 @@ describe('e2e_public_cross_chain_messaging', () => {

// Note: We register one portal address when deploying contract but that address is no-longer the only address
// allowed to send messages to the given contract. In the following test we'll test that it's really the case.
// it.each([true, false])(
// 'can send an L1 -> L2 message from a non-registered portal address consumed from private or public',
// async (isPrivate: boolean) => {
it.only('can send an L1 -> L2 message from a non-registered portal address consumed from private or public', async () => {
const testContract = await TestContract.deploy(user1Wallet).send().deployed();

const recipient = testContract.address.toString();

const secret = Fr.random();
const secretHash = computeMessageSecretHash(secret);

const content = Fr.random();
const fee = 100_0000n;
const deadline = 4294967295n;

const txHash = await inbox.write.sendL2Message(
[
{ actor: recipient as Hex, version: 1n },
deadline,
content.toString() as Hex,
secretHash.toString() as Hex,
] as const,
{ value: fee } as any,
);
it.each([true, false])(
'can send an L1 -> L2 message from a non-registered portal address consumed from private or public',
async (isPrivate: boolean) => {
const testContract = await TestContract.deploy(user1Wallet).send().deployed();

const abiItem = getAbiItem({
abi: InboxAbi,
name: 'MessageAdded',
});

const events = await crossChainTestHarness.publicClient.getLogs<typeof abiItem>({
address: getAddress(inbox.address.toString()),
event: abiItem,
fromBlock: 0n,
});

// We get the event just for the relevant transaction
const txEvents = events.filter(event => event.transactionHash === txHash);

// We check that exactly 1 MessageAdded event was emitted with the expected recipient
expect(txEvents.length).toBe(1);
expect(txEvents[0].args.recipient).toBe(recipient);

const sender = crossChainTestHarness.ethAccount;

// Why is called msgKey in one place and entryKey in another?
const msgKey = Fr.fromString(txEvents[0].args.entryKey!);

console.log('msgKey', msgKey);

// msg_key: FieldLike,
// content: FieldLike,
// secret: FieldLike,
// sender: EthAddressLike,

// We consume the L1 -> L2 message using the test contract
// if (isPrivate) {
// if (true) {
await testContract.methods
.consume_message_from_arbitrary_sender_private(msgKey, content, secret, sender)
.send()
.wait();
// } else {
// await testContract.methods.create_l2_to_l1_message_arbitrary_recipient_public(content, recipient).send().wait();
// }
}, 60_000);
const sender = crossChainTestHarness.ethAccount;
const recipient = testContract.address.toString();

const secret = Fr.random();
const secretHash = computeMessageSecretHash(secret);

// The following are arbitrary test values
const content = Fr.random();
const fee = 100_0000n;
const deadline = 4294967295n;

// We inject the message to Inbox
const txHash = await inbox.write.sendL2Message(
[
{ actor: recipient as Hex, version: 1n },
deadline,
content.toString() as Hex,
secretHash.toString() as Hex,
] as const,
{ value: fee } as any,
);

// We check that the message was correctly injected by checking the emitted event and we store the message key
// for later use
let msgKey!: Fr;
{
const abiItem = getAbiItem({
abi: InboxAbi,
name: 'MessageAdded',
});

const events = await crossChainTestHarness.publicClient.getLogs<typeof abiItem>({
address: getAddress(inbox.address.toString()),
event: abiItem,
fromBlock: 0n,
});

// We get the event just for the relevant transaction
const txEvents = events.filter(event => event.transactionHash === txHash);

// We check that exactly 1 MessageAdded event was emitted with the expected recipient
expect(txEvents.length).toBe(1);
expect(txEvents[0].args.recipient).toBe(recipient);

// TODO(#4678): Unify naming of message key/entry key
msgKey = Fr.fromString(txEvents[0].args.entryKey!);
}

// We wait for the archiver to process the message and we push a block for the message to be confirmed
{
await sleep(5000); // waiting 5 seconds.
await testContract.methods.get_this_portal_address().send().wait();
}

// Finally, e consume the L1 -> L2 message using the test contract either from private or public
if (isPrivate) {
await testContract.methods
.consume_message_from_arbitrary_sender_private(msgKey, content, secret, sender)
.send()
.wait();
} else {
await testContract.methods
.consume_message_from_arbitrary_sender_public(msgKey, content, secret, sender)
.send()
.wait();
}
},
60_000,
);
});

0 comments on commit d66e5a9

Please sign in to comment.