-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(e2e): Test multiple decryption keys on the same tx #1110
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
37e5785
chore(e2e): Adds new escrow contract and e2e test
spalladino 57dc270
Add missing await isSync
spalladino 4ee522b
More logging!
spalladino 60240cd
Fix escrow contract and tests
spalladino 7b4a2b4
Delete artifacts
spalladino 87e4fc0
Make latest noir happy
spalladino 10fa8d8
Increase timeouts
spalladino fd162db
Update syntax to latest noir lib
spalladino d7f488f
Increase timeout even more
spalladino 2e28e70
Format
spalladino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
yarn-project/end-to-end/src/e2e_escrow_contract.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { AztecNodeService } from '@aztec/aztec-node'; | ||
import { AztecRPCServer } from '@aztec/aztec-rpc'; | ||
import { AztecAddress, SentTx, Wallet, generatePublicKey } from '@aztec/aztec.js'; | ||
import { Fr, PrivateKey, TxContext, getContractDeploymentInfo } from '@aztec/circuits.js'; | ||
import { generateFunctionSelector } from '@aztec/foundation/abi'; | ||
import { toBufferBE } from '@aztec/foundation/bigint-buffer'; | ||
import { DebugLogger } from '@aztec/foundation/log'; | ||
import { retryUntil } from '@aztec/foundation/retry'; | ||
import { EscrowContractAbi, ZkTokenContractAbi } from '@aztec/noir-contracts/artifacts'; | ||
import { EscrowContract, ZkTokenContract } from '@aztec/noir-contracts/types'; | ||
import { AztecRPC, PublicKey } from '@aztec/types'; | ||
|
||
import { setup } from './utils.js'; | ||
|
||
describe('e2e_escrow_contract', () => { | ||
let aztecNode: AztecNodeService | undefined; | ||
let aztecRpcServer: AztecRPC; | ||
let wallet: Wallet; | ||
let accounts: AztecAddress[]; | ||
let logger: DebugLogger; | ||
|
||
let zkTokenContract: ZkTokenContract; | ||
let escrowContract: EscrowContract; | ||
let owner: AztecAddress; | ||
let recipient: AztecAddress; | ||
|
||
let escrowPrivateKey: PrivateKey; | ||
let escrowPublicKey: PublicKey; | ||
|
||
beforeAll(() => { | ||
// Validate transfer selector. If this fails, then make sure to change it in the escrow contract. | ||
const transferAbi = ZkTokenContractAbi.functions.find(f => f.name === 'transfer')!; | ||
const transferSelector = generateFunctionSelector(transferAbi.name, transferAbi.parameters); | ||
expect(transferSelector).toEqual(toBufferBE(0xdcd4c318n, 4)); | ||
}); | ||
|
||
beforeEach(async () => { | ||
// Setup environment | ||
({ aztecNode, aztecRpcServer, accounts, wallet, logger } = await setup(2)); | ||
[owner, recipient] = accounts; | ||
|
||
// Generate private key for escrow contract, register key in rpc server, and deploy | ||
// Note that we need to register it first if we want to emit an encrypted note for it in the constructor | ||
// TODO: We need a nicer interface for deploying contracts! | ||
escrowPrivateKey = PrivateKey.random(); | ||
escrowPublicKey = await generatePublicKey(escrowPrivateKey); | ||
const salt = Fr.random(); | ||
const deployInfo = await getContractDeploymentInfo(EscrowContractAbi, [owner], salt, escrowPublicKey); | ||
await aztecRpcServer.addAccount(escrowPrivateKey, deployInfo.address, deployInfo.partialAddress); | ||
const escrowDeployTx = EscrowContract.deployWithPublicKey(aztecRpcServer, escrowPublicKey, owner); | ||
await escrowDeployTx.send({ contractAddressSalt: salt }).wait(); | ||
escrowContract = new EscrowContract(escrowDeployTx.completeContractAddress!, wallet); | ||
logger(`Escrow contract deployed at ${escrowContract.address}`); | ||
|
||
// Deploy ZK token contract and mint funds for the escrow contract | ||
zkTokenContract = await ZkTokenContract.deploy(aztecRpcServer, 100n, escrowContract.address) | ||
.send() | ||
.wait() | ||
.then(r => new ZkTokenContract(r.contractAddress!, wallet)); | ||
logger(`Token contract deployed at ${zkTokenContract.address}`); | ||
}, 100_000); | ||
|
||
afterEach(async () => { | ||
await aztecNode?.stop(); | ||
if (aztecRpcServer instanceof AztecRPCServer) await aztecRpcServer.stop(); | ||
}, 30_000); | ||
|
||
const expectBalance = async (who: AztecAddress, expectedBalance: bigint) => { | ||
const [balance] = await zkTokenContract.methods.getBalance(who).view({ from: who }); | ||
logger(`Account ${who} balance: ${balance}`); | ||
expect(balance).toBe(expectedBalance); | ||
}; | ||
|
||
it('withdraws funds from the escrow contract', async () => { | ||
await expectBalance(owner, 0n); | ||
await expectBalance(recipient, 0n); | ||
await expectBalance(escrowContract.address, 100n); | ||
|
||
logger(`Withdrawing funds from token contract to ${recipient}`); | ||
await escrowContract.methods.withdraw(zkTokenContract.address, 30, recipient).send().wait(); | ||
|
||
await expectBalance(owner, 0n); | ||
await expectBalance(recipient, 30n); | ||
await expectBalance(escrowContract.address, 70n); | ||
}, 60_000); | ||
|
||
it('refuses to withdraw funds as a non-owner', async () => { | ||
await expect( | ||
escrowContract.methods.withdraw(zkTokenContract.address, 30, recipient).simulate({ origin: recipient }), | ||
).rejects.toThrowError(); | ||
}, 60_000); | ||
|
||
it('moves funds using multiple keys on the same tx (#1010)', async () => { | ||
logger(`Minting funds in token contract to ${owner}`); | ||
await zkTokenContract.methods.mint(50, owner).send().wait(); | ||
await expectBalance(owner, 50n); | ||
|
||
const actions = [ | ||
zkTokenContract.methods.transfer(10, owner, recipient).request(), | ||
escrowContract.methods.withdraw(zkTokenContract.address, 20, recipient).request(), | ||
]; | ||
|
||
// TODO: We need a nicer interface for batch actions | ||
const nodeInfo = await wallet.getNodeInfo(); | ||
const txContext = TxContext.empty(new Fr(nodeInfo.chainId), new Fr(nodeInfo.version)); | ||
const txRequest = await wallet.createAuthenticatedTxRequest(actions, txContext); | ||
logger(`Executing batch transfer from ${wallet.getAddress()}`); | ||
const tx = await wallet.simulateTx(txRequest); | ||
const sentTx = new SentTx(aztecRpcServer, wallet.sendTx(tx)); | ||
await sentTx.isMined(); | ||
|
||
await retryUntil(() => aztecRpcServer.isAccountSynchronised(recipient), 'account sync', 30); | ||
await expectBalance(recipient, 30n); | ||
}, 90_000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
yarn-project/noir-contracts/src/contracts/escrow_contract/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "escrow-contract" | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] | ||
aztec = { path = "../../../../noir-libs/noir-aztec" } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd've expected this test to fail with a
cannot satisfy constraints
, but I got this error instead. For context, I'm running anensure_note_exists
for a note which does not exist. Is it possible to catch this somehow? cc @LeilaWang