From d6ea91e7a066e0bc10d4579908d42a6f1df0d237 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Mon, 27 Mar 2023 20:40:50 +0000 Subject: [PATCH 01/23] WIP --- bootstrap.sh | 1 + .../archiver/src/archiver/archiver.ts | 2 +- .../aztec-node/src/aztec-node/fixtures.ts | 21 --- .../src/aztec_rpc_server/aztec_rpc_server.ts | 8 +- .../create_aztec_rpc_server.ts | 5 +- yarn-project/aztec-rpc/src/circuits.ts | 157 ++--------------- .../aztec-rpc/src/key_store/test_key_store.ts | 6 +- .../src/synchroniser/synchroniser.ts | 3 +- .../aztec.js/src/contract/send_method.ts | 7 + yarn-project/aztec.js/src/contract/sent_tx.ts | 1 + .../contract_deployer/constructor_method.ts | 1 + yarn-project/end-to-end/package.json | 1 + .../end-to-end/src/create_aztec_node.ts | 23 ++- .../end-to-end/src/create_aztec_rpc_client.ts | 6 +- .../end-to-end/src/deploy_l1_contracts.ts | 23 +++ .../src/e2e_deploy_contract.test.ts | 58 +++++- .../src/e2e_zk_token_contract.test.ts | 166 +++++++++--------- yarn-project/kernel-prover/package.json | 1 + yarn-project/kernel-prover/src/circuits.ts | 29 ++- yarn-project/kernel-prover/src/index.ts | 68 ++++++- .../src/sequencer/block_builder.ts | 9 +- yarn-project/yarn.lock | 1 + 22 files changed, 311 insertions(+), 286 deletions(-) create mode 100644 yarn-project/end-to-end/src/deploy_l1_contracts.ts diff --git a/bootstrap.sh b/bootstrap.sh index e6d05b76c2c3..3c8260e9d5b8 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -61,6 +61,7 @@ PROJECTS=( "yarn-project/kernel-prover:yarn build" "yarn-project/aztec-rpc:yarn build" "yarn-project/aztec.js:yarn build" + "yarn-project/noir-contracts:yarn build" ) for E in "${PROJECTS[@]}"; do diff --git a/yarn-project/archiver/src/archiver/archiver.ts b/yarn-project/archiver/src/archiver/archiver.ts index cf33a70cba34..8959e4de63e3 100644 --- a/yarn-project/archiver/src/archiver/archiver.ts +++ b/yarn-project/archiver/src/archiver/archiver.ts @@ -226,7 +226,7 @@ export class Archiver implements L2BlockSource { if (from > this.l2Blocks.length) { return Promise.resolve([]); } - const startIndex = from - 1; + const startIndex = from - INITIAL_ROLLUP_ID; const endIndex = startIndex + take; return Promise.resolve(this.l2Blocks.slice(startIndex, endIndex)); } diff --git a/yarn-project/aztec-node/src/aztec-node/fixtures.ts b/yarn-project/aztec-node/src/aztec-node/fixtures.ts index aa89512c8541..40eb1a3eae78 100644 --- a/yarn-project/aztec-node/src/aztec-node/fixtures.ts +++ b/yarn-project/aztec-node/src/aztec-node/fixtures.ts @@ -28,27 +28,6 @@ import { randomBytes } from '@aztec/foundation'; import { Rollup, Yeeter } from '@aztec/l1-contracts'; import { Tx } from '@aztec/p2p'; -// REFACTOR: Move deployment logic to l1-contracts package, and refactor it out of other integration tests (archiver, sequencer) -export const deployRollupContract = async (provider: WalletProvider, ethRpc: EthereumRpc) => { - const deployAccount = provider.getAccount(0); - const contract = new Rollup(ethRpc, undefined, { from: deployAccount, gas: 1000000 }); - await contract.deploy().send().getReceipt(); - return contract.address; -}; - -export const deployYeeterContract = async (provider: WalletProvider, ethRpc: EthereumRpc) => { - const deployAccount = provider.getAccount(0); - const contract = new Yeeter(ethRpc, undefined, { from: deployAccount, gas: 1000000 }); - await contract.deploy().send().getReceipt(); - return contract.address; -}; - -export const createProvider = (host: string, mnemonic: string, accounts: number) => { - const walletProvider = WalletProvider.fromHost(host); - walletProvider.addAccountsFromMnemonic(mnemonic, accounts); - return walletProvider; -}; - // REFACTOR: Use @aztec/circuit.js/factories where possible export const createCircuitEthAddress = () => { return new CircuitEthAddress(randomBytes(20)); diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts index 08debd47d077..d820293e6b50 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts @@ -13,8 +13,7 @@ import { OldTreeRoots, Signature, TxContext, - TxRequest, -} from '../circuits.js'; +} from '@aztec/circuits.js'; import { ContractDao, ContractDataSource } from '../contract_data_source/index.js'; import { KeyStore } from '../key_store/index.js'; import { ContractAbi } from '../noir.js'; @@ -80,9 +79,11 @@ export class AztecRPCServer implements AztecRPCClient { const contractAddress = generateContractAddress(from, contractAddressSalt, args); await this.db.addContract(contractAddress, portalContract, abi, false); + console.log(`Function data ${functionData.isContructor}`); + return new TxRequest( from, - AztecAddress.ZERO, // to + contractAddress, functionData, args, txContext, @@ -133,6 +134,7 @@ export class AztecRPCServer implements AztecRPCClient { ); } else { contractAddress = txRequest.to; + console.log(`to is not zero ${contractAddress.buffer.toString('hex')}`); } const contract = await this.db.getContract(contractAddress); diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/create_aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/create_aztec_rpc_server.ts index 06c0ea900081..451796b31e01 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/create_aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/create_aztec_rpc_server.ts @@ -24,7 +24,10 @@ export async function createAztecRPCServer( ) { keyStore = keyStore || new TestKeyStore(); db = db || new MemoryDB(); - synchroniser = synchroniser || new Synchroniser(aztecNode, db); + if (!synchroniser) { + synchroniser = new Synchroniser(aztecNode, db); + synchroniser.start(); + } acirSimulator = acirSimulator || new AcirSimulator(); kernelProver = kernelProver || new KernelProver(); diff --git a/yarn-project/aztec-rpc/src/circuits.ts b/yarn-project/aztec-rpc/src/circuits.ts index 7360897095fc..8fab19cde731 100644 --- a/yarn-project/aztec-rpc/src/circuits.ts +++ b/yarn-project/aztec-rpc/src/circuits.ts @@ -1,84 +1,5 @@ -import { randomBytes } from './foundation.js'; - -export class Fr { - public static ZERO = new Fr(Buffer.alloc(32)); - - public static random() { - return new Fr(randomBytes(32)); - } - - constructor(public readonly buffer: Buffer) {} -} - -export class EthAddress { - public static ZERO = new EthAddress(Buffer.alloc(20)); - - public static random() { - return new EthAddress(randomBytes(20)); - } - - public static fromString(address: string) { - return new EthAddress(Buffer.from(address.replace(/^0x/i, ''), 'hex')); - } - - constructor(public readonly buffer: Buffer) {} -} - -export class AztecAddress { - public static SIZE = 64; - - public static ZERO = new AztecAddress(Buffer.alloc(AztecAddress.SIZE)); - - public static random() { - return new AztecAddress(randomBytes(AztecAddress.SIZE)); - } - - constructor(public readonly buffer: Buffer) {} - - public equals(rhs: AztecAddress) { - return this.buffer.equals(rhs.buffer); - } -} - -export class Signature { - public static SIZE = 64; - - public static random() { - return new EthAddress(randomBytes(Signature.SIZE)); - } - - constructor(public readonly buffer: Buffer) {} -} - -export interface FunctionData { - functionSelector: Buffer; - isSecret: boolean; - isContructor: boolean; -} - -/** - * Contract deployment data in a TxContext - * cpp/src/aztec3/circuits/abis/contract_deployment_data.hpp - */ -export class ContractDeploymentData { - public static EMPTY = new ContractDeploymentData(Fr.ZERO, Fr.ZERO, Fr.ZERO, Fr.ZERO); - - constructor( - public constructorVkHash: Fr, - public functionTreeRoot: Fr, - public contractAddressSalt: Fr, - public portalContractAddress: EthAddress, - ) {} -} - -export class TxContext { - constructor( - public readonly isFeePaymentTx: boolean, - public readonly isRebatePaymentTx: boolean, - public readonly isContractDeploymentTx: boolean, - public readonly contractDeploymentData: ContractDeploymentData, - ) {} -} +import { AztecAddress, EthAddress, Fr, FunctionData, TxContext } from '@aztec/circuits.js'; +import { randomBytes } from '@aztec/foundation'; export class TxRequest { constructor( @@ -92,21 +13,25 @@ export class TxRequest { ) {} toBuffer() { - return Buffer.alloc(0); + const buf = Buffer.alloc(4); + buf.writeUInt32BE(this.functionData.functionSelector); + return Buffer.concat([ + buf, + Buffer.concat(this.args.map(x => x.toBuffer())), + this.nonce.toBuffer(), + this.chainId.toBuffer(), + ]); } } -export class PreviousKernelData {} +export class Signature { + public static SIZE = 64; -export class PrivateCallData {} + public static random() { + return new EthAddress(randomBytes(Signature.SIZE)); + } -export class KernelPrivateInputs { - constructor( - public readonly txRequest: TxRequest, - public readonly signature: Signature, - public readonly previousKernelData: PreviousKernelData, - public readonly privateCallData: PrivateCallData, - ) {} + constructor(public readonly buffer: Buffer) {} } export function generateContractAddress( @@ -115,53 +40,5 @@ export function generateContractAddress( args: Fr[], // functionLeaves: Fr[], ) { - return AztecAddress.random(); -} - -export class OldTreeRoots { - constructor( - public privateDataTreeRoot: Fr, - public nullifierTreeRoot: Fr, - public contractTreeRoot: Fr, - public privateKernelVkTreeRoot: Fr, // future enhancement - ) {} -} - -export class ConstantData { - constructor(public oldTreeRoots: OldTreeRoots, public txContext: TxContext) {} -} - -export class AggregationObject {} - -export class NewContractData { - constructor( - public readonly contractAddress: AztecAddress, - public readonly portalContractAddress: EthAddress, - public readonly functionTreeRoot: Fr, - ) {} -} - -export class OptionallyRevealedData {} - -export class AccumulatedTxData { - constructor( - public aggregationObject: AggregationObject, // Contains the aggregated proof of all previous kernel iterations - - public privateCallCount: Fr, - - public newCommitments: Fr[], - public newNullifiers: Fr[], - - public privateCallStack: Fr[], - public publicCallStack: Fr[], - public l1MsgStack: Fr[], - - public newContracts: NewContractData[], - - public optionallyRevealedData: OptionallyRevealedData[], - ) {} -} - -export class PrivateKernelPublicInputs { - constructor(public end: AccumulatedTxData, public constants: ConstantData, public isPrivateKernel: true) {} + return new Fr(randomBytes(32)); } diff --git a/yarn-project/aztec-rpc/src/key_store/test_key_store.ts b/yarn-project/aztec-rpc/src/key_store/test_key_store.ts index a95423a7013d..8a4fd4c94560 100644 --- a/yarn-project/aztec-rpc/src/key_store/test_key_store.ts +++ b/yarn-project/aztec-rpc/src/key_store/test_key_store.ts @@ -1,4 +1,4 @@ -import { TxRequest } from '../circuits.js'; +import { TxRequest, AztecAddress } from '../circuits.js'; import { ConstantKeyPair, KeyPair } from './key_pair.js'; import { KeyStore } from './key_store.js'; @@ -24,7 +24,9 @@ export class TestKeyStore implements KeyStore { } signTxRequest(txRequest: TxRequest) { - const account = this.accounts.find(a => a.getPublicKey().equals(txRequest.from)); + const account = txRequest.from.equals(AztecAddress.ZERO) + ? this.accounts[0] + : this.accounts.find(a => a.getPublicKey().equals(txRequest.from)); if (!account) { throw new Error('Unknown account.'); } diff --git a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts index 4976a8fa2031..4f93a4cf4dc7 100644 --- a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts +++ b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts @@ -14,7 +14,7 @@ export class Synchroniser { constructor(private node: AztecNode, private db: Database) {} - public start(from = 0, take = 1, retryInterval = 10000) { + public start(from = 1, take = 1, retryInterval = 10000) { if (this.running) { return; } @@ -35,6 +35,7 @@ export class Synchroniser { .map(b => b.newContractData.map(d => d.aztecAddress)) .flat() .map(fr => new AztecAddress(fr.toBuffer())); + console.log(`address ${contractAddresses[0].buffer.toString('hex')}`); await this.db.confirmContractsDeployed(contractAddresses); from += blocks.length; diff --git a/yarn-project/aztec.js/src/contract/send_method.ts b/yarn-project/aztec.js/src/contract/send_method.ts index c3e094552dee..3f8ead941c5b 100644 --- a/yarn-project/aztec.js/src/contract/send_method.ts +++ b/yarn-project/aztec.js/src/contract/send_method.ts @@ -26,6 +26,7 @@ export class SendMethod { public async request(options: SendMethodOptions = {}) { const { from } = { ...this.defaultOptions, ...options }; + console.log(`send address ${this.contractAddress.buffer.toString('hex')}`); this.txRequest = await this.arc.createTxRequest( this.entry.encodeABI(), this.entry.encodeParameters(this.args).map(p => new Fr(p)), @@ -36,6 +37,7 @@ export class SendMethod { } public async sign(options: SendMethodOptions = {}) { + console.log(`signing ${this.txRequest}`); if (!this.txRequest) { await this.request(options); } @@ -60,6 +62,11 @@ export class SendMethod { } else { promise = (async () => { await this.create(options); + console.log( + `final address ${this.tx!.data.end.newContracts[0].contractAddress.toString()}, is const ${ + this.txRequest?.functionData.isContructor + }`, + ); return this.arc.sendTx(this.tx!); })(); } diff --git a/yarn-project/aztec.js/src/contract/sent_tx.ts b/yarn-project/aztec.js/src/contract/sent_tx.ts index ad1dbce0bea8..44d99f29a4a8 100644 --- a/yarn-project/aztec.js/src/contract/sent_tx.ts +++ b/yarn-project/aztec.js/src/contract/sent_tx.ts @@ -16,6 +16,7 @@ export class SentTx { } const txHash = await this.getTxHash(); + console.log('tx hash'); this.receipt = await retryUntil(() => this.arc.getTxReceipt(txHash), 'getReceipt', timeout, interval); return this.receipt; } diff --git a/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts b/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts index 033c148c67c9..c9036ff4abcc 100644 --- a/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts +++ b/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts @@ -33,6 +33,7 @@ export class ConstructorMethod extends SendMethod { contractAddressSalt || Fr.random(), from || AztecAddress.ZERO, ); + console.log(`contract address ${this.txRequest.to.buffer.toString('hex')}`); return this.txRequest; } diff --git a/yarn-project/end-to-end/package.json b/yarn-project/end-to-end/package.json index 48f0261d1995..424af5235716 100644 --- a/yarn-project/end-to-end/package.json +++ b/yarn-project/end-to-end/package.json @@ -25,6 +25,7 @@ "dependencies": { "@aztec/aztec-node": "workspace:^", "@aztec/aztec.js": "workspace:^", + "@aztec/ethereum.js": "workspace:^", "@aztec/noir-contracts": "workspace:^", "@types/jest": "^29.4.0", "jest": "^28.1.3", diff --git a/yarn-project/end-to-end/src/create_aztec_node.ts b/yarn-project/end-to-end/src/create_aztec_node.ts index 4444d3a667ae..7b395bdc9e64 100644 --- a/yarn-project/end-to-end/src/create_aztec_node.ts +++ b/yarn-project/end-to-end/src/create_aztec_node.ts @@ -1,19 +1,18 @@ import { AztecNode } from '@aztec/aztec-node'; -import { EthAddress } from '@aztec/aztec.js'; +import { EthAddress } from '@aztec/ethereum.js/eth_address'; -const { - ETHEREUM_HOST = 'http://localhost:8545', - ROLLUP_ADDRESS = '0x5FbDB2315678afecb367f032d93F642f64180aa3', - YEETER_ADDRESS = '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512', -} = process.env; - -export async function createAztecNode() { +export async function createAztecNode( + rollupContract: EthAddress, + yeeterContract: EthAddress, + rpcUrl: string, + publisherPrivateKey: Buffer, +) { return await AztecNode.createAndSync({ - rpcUrl: ETHEREUM_HOST, - rollupContract: EthAddress.fromString(ROLLUP_ADDRESS) as any, - yeeterContract: EthAddress.fromString(YEETER_ADDRESS) as any, + rpcUrl, + rollupContract, + yeeterContract, retryIntervalMs: 10000, - publisherPrivateKey: Buffer.alloc(64), + publisherPrivateKey: publisherPrivateKey, requiredConfirmations: 1, transactionPollingInterval: 1000, }); diff --git a/yarn-project/end-to-end/src/create_aztec_rpc_client.ts b/yarn-project/end-to-end/src/create_aztec_rpc_client.ts index 5729472d966e..e3ade5aa636f 100644 --- a/yarn-project/end-to-end/src/create_aztec_rpc_client.ts +++ b/yarn-project/end-to-end/src/create_aztec_rpc_client.ts @@ -1,10 +1,8 @@ import { AztecNode } from '@aztec/aztec-node'; import { createAztecRPCServer } from '@aztec/aztec.js'; -import { createAztecNode } from './create_aztec_node.js'; -export async function createAztecRPCClient(numberOfAccounts = 1, aztecNode?: AztecNode) { - const node = aztecNode || (await createAztecNode()); - const arc = await createAztecRPCServer(node); +export async function createAztecRPCClient(numberOfAccounts = 1, aztecNode: AztecNode) { + const arc = await createAztecRPCServer(aztecNode); for (let i = 0; i < numberOfAccounts; ++i) { await arc.addAccount(); diff --git a/yarn-project/end-to-end/src/deploy_l1_contracts.ts b/yarn-project/end-to-end/src/deploy_l1_contracts.ts new file mode 100644 index 000000000000..8a9c57edca36 --- /dev/null +++ b/yarn-project/end-to-end/src/deploy_l1_contracts.ts @@ -0,0 +1,23 @@ +import { EthereumRpc } from '@aztec/ethereum.js/eth_rpc'; +import { WalletProvider } from '@aztec/ethereum.js/provider'; +import { Rollup, Yeeter } from '@aztec/l1-contracts'; + +export const deployRollupContract = async (provider: WalletProvider, ethRpc: EthereumRpc) => { + const deployAccount = provider.getAccount(0); + const contract = new Rollup(ethRpc, undefined, { from: deployAccount, gas: 1000000 }); + await contract.deploy().send().getReceipt(); + return contract.address; +}; + +export const deployYeeterContract = async (provider: WalletProvider, ethRpc: EthereumRpc) => { + const deployAccount = provider.getAccount(0); + const contract = new Yeeter(ethRpc, undefined, { from: deployAccount, gas: 1000000 }); + await contract.deploy().send().getReceipt(); + return contract.address; +}; + +export const createProvider = (host: string, mnemonic: string, accounts: number) => { + const walletProvider = WalletProvider.fromHost(host); + walletProvider.addAccountsFromMnemonic(mnemonic, accounts); + return walletProvider; +}; diff --git a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts index bdc57008293a..42283792faa5 100644 --- a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts @@ -1,24 +1,74 @@ +import { AztecNode, AztecNodeConfig } from '@aztec/aztec-node'; import { AztecAddress, AztecRPCClient, ContractDeployer, Fr } from '@aztec/aztec.js'; +import { EthAddress } from '@aztec/ethereum.js/eth_address'; +import { EthereumRpc } from '@aztec/ethereum.js/eth_rpc'; +import { WalletProvider } from '@aztec/ethereum.js/provider'; +import { createDebugLogger } from '@aztec/foundation'; import { TestContractAbi } from '@aztec/noir-contracts/examples'; import { createAztecRPCClient } from './create_aztec_rpc_client.js'; +import { createProvider, deployRollupContract, deployYeeterContract } from './deploy_l1_contracts.js'; + +const ETHEREUM_HOST = 'http://localhost:8545'; +const MNEMONIC = 'test test test test test test test test test test test junk'; + +const createAztecNode = async ( + rollupContract: EthAddress, + yeeterContract: EthAddress, + rpcUrl: string, + publisherPrivateKey: Buffer, +) => { + const config: AztecNodeConfig = { + rollupContract, + yeeterContract, + rpcUrl, + publisherPrivateKey, + retryIntervalMs: 1000, + requiredConfirmations: 1, + transactionPollingInterval: 1000, + }; + return await AztecNode.createAndSync(config); +}; + +const logger = createDebugLogger('aztec:e2e_test'); describe('e2e_deploy_contract', () => { + let provider: WalletProvider; + let node: AztecNode; let arc: AztecRPCClient; + let rollupAddress: EthAddress; + let yeeterAddress: EthAddress; let accounts: AztecAddress[]; const abi = TestContractAbi; + beforeAll(async () => { + provider = createProvider(ETHEREUM_HOST, MNEMONIC, 1); + const ethRpc = new EthereumRpc(provider); + logger('deploying contracts...'); + rollupAddress = await deployRollupContract(provider, ethRpc); + yeeterAddress = await deployYeeterContract(provider, ethRpc); + logger('deployed contracts...'); + }); + beforeEach(async () => { - arc = await createAztecRPCClient(1); + node = await createAztecNode(rollupAddress, yeeterAddress, ETHEREUM_HOST, provider.getPrivateKey(0)!); + arc = await createAztecRPCClient(1, node); accounts = await arc.getAccounts(); }); + afterEach(async () => { + await node.stop(); + }); + /** * Milestone 1.1 * https://hackmd.io/ouVCnacHQRq2o1oRc5ksNA#Interfaces-and-Responsibilities */ - it.skip('should deploy a contract', async () => { + it('should deploy a contract', async () => { const deployer = new ContractDeployer(abi, arc); - const receipt = await deployer.deploy().send().getReceipt(); + const tx = deployer.deploy().send(); + logger(`Tx sent!`); + const receipt = await tx.getReceipt(); + logger(`Receipt received`); expect(receipt).toEqual( expect.objectContaining({ from: accounts[0], @@ -32,7 +82,7 @@ describe('e2e_deploy_contract', () => { const constructor = abi.functions.find(f => f.name === 'constructor')!; const bytecode = await arc.getCode(contractAddress); expect(bytecode).toEqual(constructor.bytecode); - }); + }, 30_000); /** * Milestone 1.2 diff --git a/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts b/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts index bfc7d00773f5..ca3b47603923 100644 --- a/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts @@ -1,83 +1,83 @@ -import { AztecAddress, AztecRPCClient, Contract, ContractDeployer, Fr } from '@aztec/aztec.js'; -import { ZkTokenContractAbi } from '@aztec/noir-contracts/examples'; -import { createAztecRPCClient } from './create_aztec_rpc_client.js'; - -describe('e2e_zk_token_contract', () => { - let arc: AztecRPCClient; - let accounts: AztecAddress[]; - let contract: Contract; - - const expectStorageSlot = async (accountIdx: number, expectedBalance: bigint) => { - // We only generate 1 note in each test. Balance is the first field of the only note. - // TBD - how to calculate storage slot? - const storageSlot = Fr.ZERO; - const [[balance]] = await arc.getStorageAt(contract.address, storageSlot); - console.log(`Account ${accountIdx} balance: ${balance}`); - expect(balance).toBe(expectedBalance); - }; - - const expectBalance = async (accountIdx: number, expectedBalance: bigint) => { - const balance = await contract.methods.getBalance().call({ from: accounts[accountIdx] }); - console.log(`Account ${accountIdx} balance: ${balance}`); - expect(balance).toBe(expectedBalance); - }; - - const deployContract = async (initialBalance = 0n) => { - const deployer = new ContractDeployer(ZkTokenContractAbi, arc); - const receipt = await deployer.deploy(initialBalance).send().getReceipt(); - return new Contract(receipt.contractAddress!, ZkTokenContractAbi, arc); - }; - - beforeEach(async () => { - arc = await createAztecRPCClient(2); - accounts = await arc.getAccounts(); - }); - - /** - * Milestone 1.3 - * https://hackmd.io/AG5rb9DyTRu3y7mBptWauA - */ - it.skip('should deploy zk token contract with initial token minted to the account', async () => { - const initialBalance = 987n; - await deployContract(initialBalance); - await expectStorageSlot(0, initialBalance); - await expectStorageSlot(1, 0n); - }); - - /** - * Milestone 1.4 - */ - it.skip('should call mint and increase balance', async () => { - const mintAmount = 65n; - - await deployContract(); - - await expectStorageSlot(0, 0n); - await expectStorageSlot(1, 0n); - - const receipt = await contract.methods.mint(mintAmount).send({ from: accounts[1] }).getReceipt(); - expect(receipt.status).toBe(true); - - await expectStorageSlot(0, 0n); - await expectStorageSlot(1, mintAmount); - }); - - /** - * Milestone 1.5 - */ - it.skip('should call transfer and increase balance of another account', async () => { - const initialBalance = 987n; - const transferAmount = 654n; - - await deployContract(initialBalance); - - await expectBalance(0, initialBalance); - await expectBalance(1, 0n); - - const receipt = await contract.methods.transfer(accounts[1]).send({ from: accounts[0] }).getReceipt(); - expect(receipt.status).toBe(true); - - await expectBalance(0, initialBalance - transferAmount); - await expectBalance(1, transferAmount); - }); -}); +// import { AztecAddress, AztecRPCClient, Contract, ContractDeployer, Fr } from '@aztec/aztec.js'; +// import { ZkTokenContractAbi } from '@aztec/noir-contracts/examples'; +// import { createAztecRPCClient } from './create_aztec_rpc_client.js'; + +// describe('e2e_zk_token_contract', () => { +// let arc: AztecRPCClient; +// let accounts: AztecAddress[]; +// let contract: Contract; + +// const expectStorageSlot = async (accountIdx: number, expectedBalance: bigint) => { +// // We only generate 1 note in each test. Balance is the first field of the only note. +// // TBD - how to calculate storage slot? +// const storageSlot = Fr.ZERO; +// const [[balance]] = await arc.getStorageAt(contract.address, storageSlot); +// console.log(`Account ${accountIdx} balance: ${balance}`); +// expect(balance).toBe(expectedBalance); +// }; + +// const expectBalance = async (accountIdx: number, expectedBalance: bigint) => { +// const balance = await contract.methods.getBalance().call({ from: accounts[accountIdx] }); +// console.log(`Account ${accountIdx} balance: ${balance}`); +// expect(balance).toBe(expectedBalance); +// }; + +// const deployContract = async (initialBalance = 0n) => { +// const deployer = new ContractDeployer(ZkTokenContractAbi, arc); +// const receipt = await deployer.deploy(initialBalance).send().getReceipt(); +// return new Contract(receipt.contractAddress!, ZkTokenContractAbi, arc); +// }; + +// beforeEach(async () => { +// arc = await createAztecRPCClient(2); +// accounts = await arc.getAccounts(); +// }); + +// /** +// * Milestone 1.3 +// * https://hackmd.io/AG5rb9DyTRu3y7mBptWauA +// */ +// it.skip('should deploy zk token contract with initial token minted to the account', async () => { +// const initialBalance = 987n; +// await deployContract(initialBalance); +// await expectStorageSlot(0, initialBalance); +// await expectStorageSlot(1, 0n); +// }); + +// /** +// * Milestone 1.4 +// */ +// it.skip('should call mint and increase balance', async () => { +// const mintAmount = 65n; + +// await deployContract(); + +// await expectStorageSlot(0, 0n); +// await expectStorageSlot(1, 0n); + +// const receipt = await contract.methods.mint(mintAmount).send({ from: accounts[1] }).getReceipt(); +// expect(receipt.status).toBe(true); + +// await expectStorageSlot(0, 0n); +// await expectStorageSlot(1, mintAmount); +// }); + +// /** +// * Milestone 1.5 +// */ +// it.skip('should call transfer and increase balance of another account', async () => { +// const initialBalance = 987n; +// const transferAmount = 654n; + +// await deployContract(initialBalance); + +// await expectBalance(0, initialBalance); +// await expectBalance(1, 0n); + +// const receipt = await contract.methods.transfer(accounts[1]).send({ from: accounts[0] }).getReceipt(); +// expect(receipt.status).toBe(true); + +// await expectBalance(0, initialBalance - transferAmount); +// await expectBalance(1, transferAmount); +// }); +// }); diff --git a/yarn-project/kernel-prover/package.json b/yarn-project/kernel-prover/package.json index d5a11bba87e9..0af5858f8c08 100644 --- a/yarn-project/kernel-prover/package.json +++ b/yarn-project/kernel-prover/package.json @@ -31,6 +31,7 @@ "dependencies": { "@aztec/acir-simulator": "workspace:^", "@aztec/circuits.js": "workspace:^", + "@aztec/foundation": "workspace:^", "tslib": "^2.4.0" }, "devDependencies": { diff --git a/yarn-project/kernel-prover/src/circuits.ts b/yarn-project/kernel-prover/src/circuits.ts index 5bbd42b113f3..d6653cdb186a 100644 --- a/yarn-project/kernel-prover/src/circuits.ts +++ b/yarn-project/kernel-prover/src/circuits.ts @@ -1,7 +1,6 @@ -// See aztec3/constants.hpp -// Copied here for prototyping purposes - -import { AztecAddress, Fr, FunctionData, TxContext } from '@aztec/circuits.js'; +import { AztecAddress, EthAddress, Fr, FunctionData, TxContext } from '@aztec/circuits.js'; +import { randomBytes } from '@aztec/foundation'; +//import { randomBytes } from '@aztec/foundation'; export class TxRequest { constructor( @@ -15,12 +14,32 @@ export class TxRequest { ) {} toBuffer() { - return Buffer.alloc(0); + const buf = Buffer.alloc(4); + buf.writeUInt32BE(this.functionData.functionSelector); + return Buffer.concat([ + buf, + Buffer.concat(this.args.map(x => x.toBuffer())), + this.nonce.toBuffer(), + this.chainId.toBuffer(), + ]); } } export class Signature { public static SIZE = 64; + public static random() { + return new EthAddress(randomBytes(Signature.SIZE)); + } + constructor(public readonly buffer: Buffer) {} } + +// export function generateContractAddress( +// deployerAddress: AztecAddress, +// salt: Fr, +// args: Fr[], +// // functionLeaves: Fr[], +// ) { +// return new Fr(randomBytes(32)); +// } diff --git a/yarn-project/kernel-prover/src/index.ts b/yarn-project/kernel-prover/src/index.ts index 79318c291e61..e027e0fdc86f 100644 --- a/yarn-project/kernel-prover/src/index.ts +++ b/yarn-project/kernel-prover/src/index.ts @@ -1,13 +1,27 @@ import { ExecutionResult } from '@aztec/acir-simulator'; import { AccumulatedData, + AffineElement, AggregationObject, ConstantData, + EMITTED_EVENTS_LENGTH, + EthAddress, + Fq, Fr, + FunctionData, + KERNEL_L1_MSG_STACK_LENGTH, + KERNEL_NEW_COMMITMENTS_LENGTH, + KERNEL_NEW_CONTRACTS_LENGTH, + KERNEL_NEW_NULLIFIERS_LENGTH, + KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH, + KERNEL_PRIVATE_CALL_STACK_LENGTH, + KERNEL_PUBLIC_CALL_STACK_LENGTH, NewContractData, OldTreeRoots, + OptionallyRevealedData, PrivateKernelPublicInputs, } from '@aztec/circuits.js'; +import { randomBytes } from 'crypto'; import { Signature, TxRequest } from './circuits.js'; export class KernelProver { prove( @@ -17,7 +31,16 @@ export class KernelProver { oldRoots: OldTreeRoots, ): Promise<{ publicInputs: PrivateKernelPublicInputs; proof: Buffer }> { // TODO: implement this + const createRandomFields = (num: number) => { + return Array(num) + .fill(0) + .map(() => new Fr(randomBytes(32))); + }; + const createRandomContractData = () => { + return new NewContractData(new Fr(randomBytes(32)), new EthAddress(randomBytes(20)), createRandomFields(1)[0]); + }; const newContracts = []; + console.log(`is contructor ${txRequest.functionData.isConstructor} request to ${txRequest.to.toString()}`); if (txRequest.functionData.isConstructor) { newContracts.push( new NewContractData( @@ -27,16 +50,45 @@ export class KernelProver { ), ); } + newContracts.push( + ...Array(KERNEL_NEW_CONTRACTS_LENGTH - newContracts.length) + .fill(0) + .map(() => createRandomContractData()), + ); + + const aggregationObject = new AggregationObject( + new AffineElement(new Fq(0), new Fq(0)), + new AffineElement(new Fq(0), new Fq(0)), + [], + [], + false, + ); + const createOptionallyRevealedData = () => { + const optionallyRevealedData = new OptionallyRevealedData( + createRandomFields(1)[0], + new FunctionData(1, true, true), + createRandomFields(EMITTED_EVENTS_LENGTH), + createRandomFields(1)[0], + new EthAddress(randomBytes(20)), + true, + true, + true, + true, + ); + return optionallyRevealedData; + }; const accumulatedTxData = new AccumulatedData( - AggregationObject.fromBuffer(Buffer.alloc(0)), // TODO - Fix this. - new Fr(Buffer.from([1])), - [], // newCommitments - [], // newNullifiers - [], // privateCallStack - [], // publicCallStack - [], // l1MsgStack + aggregationObject, + new Fr(0), + createRandomFields(KERNEL_NEW_COMMITMENTS_LENGTH), + createRandomFields(KERNEL_NEW_NULLIFIERS_LENGTH), + createRandomFields(KERNEL_PRIVATE_CALL_STACK_LENGTH), + createRandomFields(KERNEL_PUBLIC_CALL_STACK_LENGTH), + createRandomFields(KERNEL_L1_MSG_STACK_LENGTH), newContracts, - [], // optionallyRevealedData + Array(KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH) + .fill(0) + .map(() => createOptionallyRevealedData()), ); const publicInputs = new PrivateKernelPublicInputs( diff --git a/yarn-project/sequencer-client/src/sequencer/block_builder.ts b/yarn-project/sequencer-client/src/sequencer/block_builder.ts index ec4315f596fd..222820a7580a 100644 --- a/yarn-project/sequencer-client/src/sequencer/block_builder.ts +++ b/yarn-project/sequencer-client/src/sequencer/block_builder.ts @@ -9,6 +9,7 @@ import { } from '@aztec/circuits.js'; import { MerkleTreeId, MerkleTreeOperations } from '@aztec/world-state'; import { Tx } from '@aztec/p2p'; +import { createDebugLogger } from '@aztec/foundation'; const mapContractData = (n: NewContractData) => { const contractData = new ContractData(n.contractAddress, n.portalContractAddress); @@ -20,7 +21,12 @@ export class BlockBuilder { private nullifierTreeLeaves: Buffer[] = []; private contractTreeLeaves: Buffer[] = []; - constructor(private db: MerkleTreeOperations, private nextRollupId: number, private tx: Tx) { + constructor( + private db: MerkleTreeOperations, + private nextRollupId: number, + private tx: Tx, + private log = createDebugLogger('aztec:block_builder'), + ) { this.dataTreeLeaves = tx.data.end.newCommitments.map((x: Fr) => x.toBuffer()); this.nullifierTreeLeaves = tx.data.end.newNullifiers.map((x: Fr) => x.toBuffer()); this.contractTreeLeaves = tx.data.end.newContracts.map((x: NewContractData) => x.functionTreeRoot.toBuffer()); @@ -46,6 +52,7 @@ export class BlockBuilder { const endTreeOfHistoricContractTreeRootsSnapshot = await this.getTreeSnapshot( MerkleTreeId.CONTRACT_TREE_ROOTS_TREE, ); + this.log(`contract address ${this.tx.data.end.newContracts[0].contractAddress.toString()}`); const l2block = L2Block.fromFields({ number: this.nextRollupId, diff --git a/yarn-project/yarn.lock b/yarn-project/yarn.lock index 6997ebad9bbc..c2e24925878f 100644 --- a/yarn-project/yarn.lock +++ b/yarn-project/yarn.lock @@ -196,6 +196,7 @@ __metadata: dependencies: "@aztec/aztec-node": "workspace:^" "@aztec/aztec.js": "workspace:^" + "@aztec/ethereum.js": "workspace:^" "@aztec/noir-contracts": "workspace:^" "@jest/globals": ^29.4.3 "@rushstack/eslint-patch": ^1.1.4 From 524f389deaf6450f54920043258cb3a2d292e7bf Mon Sep 17 00:00:00 2001 From: sirasistant Date: Mon, 27 Mar 2023 21:46:58 +0000 Subject: [PATCH 02/23] refactor: remove some circuits.ts boilerplate --- yarn-project/acir-simulator/src/acvm.ts | 2 +- yarn-project/acir-simulator/src/circuits.ts | 208 +----------------- yarn-project/acir-simulator/src/db_oracle.ts | 2 +- yarn-project/acir-simulator/src/simulator.ts | 12 +- .../src/account_state/account_state.ts | 2 +- .../src/aztec_rpc_client/aztec_rpc_client.ts | 3 +- .../src/aztec_rpc_server/aztec_rpc_server.ts | 59 ++--- yarn-project/aztec-rpc/src/circuits.ts | 6 + .../src/contract_data_source/contract_dao.ts | 5 +- .../contract_data_source.ts | 2 +- .../memory_contract_data_source.ts | 4 +- yarn-project/aztec-rpc/src/database/tx_dao.ts | 2 +- .../aztec-rpc/src/key_store/key_pair.ts | 5 +- .../aztec-rpc/src/key_store/key_store.ts | 3 +- .../aztec-rpc/src/key_store/test_key_store.ts | 6 +- .../src/synchroniser/synchroniser.ts | 8 +- yarn-project/aztec-rpc/src/tx/tx_receipt.ts | 2 +- .../aztec.js/src/contract/call_method.ts | 3 +- .../aztec.js/src/contract/contract.ts | 3 +- .../aztec.js/src/contract/send_method.ts | 12 +- .../contract_deployer/constructor_method.ts | 13 +- .../contract_deployer.test.ts | 27 +-- yarn-project/kernel-prover/src/circuits.ts | 9 - 23 files changed, 93 insertions(+), 305 deletions(-) diff --git a/yarn-project/acir-simulator/src/acvm.ts b/yarn-project/acir-simulator/src/acvm.ts index 2c2f5daabe24..a80dc2d88eec 100644 --- a/yarn-project/acir-simulator/src/acvm.ts +++ b/yarn-project/acir-simulator/src/acvm.ts @@ -1,4 +1,4 @@ -import { AztecAddress, CallContext, ContractDeploymentData } from './circuits.js'; +import { AztecAddress, CallContext, ContractDeploymentData } from '@aztec/circuits.js'; import { NoteLoadOracleInputs } from './db_oracle.js'; export interface ACIRCallback { diff --git a/yarn-project/acir-simulator/src/circuits.ts b/yarn-project/acir-simulator/src/circuits.ts index 917b55cc08a2..4f307c4dcb6b 100644 --- a/yarn-project/acir-simulator/src/circuits.ts +++ b/yarn-project/acir-simulator/src/circuits.ts @@ -1,152 +1,4 @@ -// See aztec3/constants.hpp -// Copied here for prototyping purposes -// In future: structured serialization? -export const ARGS_LENGTH = 8; -export const RETURN_VALUES_LENGTH = 4; -export const EMITTED_EVENTS_LENGTH = 4; - -export const NEW_COMMITMENTS_LENGTH = 4; -export const NEW_NULLIFIERS_LENGTH = 4; - -export const STATE_TRANSITIONS_LENGTH = 4; -export const STATE_READS_LENGTH = 4; - -export const PRIVATE_CALL_STACK_LENGTH = 4; -export const PUBLIC_CALL_STACK_LENGTH = 4; -export const L1_MSG_STACK_LENGTH = 2; - -export const KERNEL_NEW_COMMITMENTS_LENGTH = 16; -export const KERNEL_NEW_NULLIFIERS_LENGTH = 16; -export const KERNEL_NEW_CONTRACTS_LENGTH = 8; -export const KERNEL_PRIVATE_CALL_STACK_LENGTH = 8; -export const KERNEL_PUBLIC_CALL_STACK_LENGTH = 8; -export const KERNEL_L1_MSG_STACK_LENGTH = 4; -export const KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH = 4; - -export const VK_TREE_HEIGHT = 3; -export const CONTRACT_TREE_HEIGHT = 4; -export const PRIVATE_DATA_TREE_HEIGHT = 8; -export const NULLIFIER_TREE_HEIGHT = 8; - -export const PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT = 8; -export const CONTRACT_TREE_ROOTS_TREE_HEIGHT = 8; - -export const FUNCTION_SELECTOR_NUM_BYTES = 31; - -/** - * Assert a member is a certain length. - * @param obj - An object. - * @param member - A member string. - * @param length - The length. - */ -export function assertLength( - obj: T, - member: F, - length: number, -) { - if (obj[member].length !== length) { - throw new Error(`Expected ${member} to have length ${length}! Was: ${obj[member].length}`); - } -} - -export class Fr { - public static ZERO = new Fr(Buffer.alloc(32)); - - constructor(public readonly buffer: Buffer) {} -} - -export class AztecAddress { - public static SIZE = 64; - - public static ZERO = new AztecAddress(Buffer.alloc(AztecAddress.SIZE)); - - constructor(public readonly buffer: Buffer) {} - - public equals(rhs: AztecAddress) { - return this.buffer.equals(rhs.buffer); - } -} - -export class EthAddress { - public static ZERO = new EthAddress(Buffer.alloc(20)); - - constructor(public readonly buffer: Buffer) {} -} - -/** - * Call context. - * @see abis/call_context.hpp - */ -export class CallContext { - constructor( - public msgSender: AztecAddress, - public storageContractAddress: AztecAddress, - public portalContractAddress: EthAddress, - public isDelegateCall: boolean, - public isStaticCall: boolean, - public isContractDeployment: boolean, - ) {} -} - -/** - * Contract deployment data in a @TxContext. - * cpp/src/aztec3/circuits/abis/contract_deployment_data.hpp - */ -export class ContractDeploymentData { - constructor( - public constructorVkHash: Fr, - public functionTreeRoot: Fr, - public contractAddressSalt: Fr, - public portalContractAddress: EthAddress, - ) {} -} - -/** - * Public inputs to a private circuit. - * @see abis/private_circuit_public_inputs.hpp. - */ -export class PrivateCircuitPublicInputs { - constructor( - // NOTE: Must have same order as CPP. - public callContext: CallContext, - public args: Fr[], - public returnValues: Fr[], - public emittedEvents: Fr[], - public newCommitments: Fr[], - public newNullifiers: Fr[], - public privateCallStack: Fr[], - public publicCallStack: Fr[], - public l1MsgStack: Fr[], - public historicPrivateDataTreeRoot: Fr, - public historicPrivateNullifierTreeRoot: Fr, - public historicContractTreeRoot: Fr, - public contractDeploymentData: ContractDeploymentData, - ) { - assertLength(this, 'args', ARGS_LENGTH); - assertLength(this, 'returnValues', RETURN_VALUES_LENGTH); - assertLength(this, 'emittedEvents', EMITTED_EVENTS_LENGTH); - assertLength(this, 'newCommitments', NEW_COMMITMENTS_LENGTH); - assertLength(this, 'newNullifiers', NEW_NULLIFIERS_LENGTH); - assertLength(this, 'privateCallStack', PRIVATE_CALL_STACK_LENGTH); - assertLength(this, 'publicCallStack', PUBLIC_CALL_STACK_LENGTH); - assertLength(this, 'l1MsgStack', L1_MSG_STACK_LENGTH); - } -} - -export class TxContext { - constructor( - public readonly isFeePaymentTx: boolean, - public readonly isRebatePaymentTx: boolean, - public readonly isContractDeploymentTx: boolean, - public readonly contractDeploymentData: ContractDeploymentData, - ) {} -} - -export interface FunctionData { - functionSelector: Buffer; - isSecret: boolean; - isContructor: boolean; -} +import { AztecAddress, Fr, FunctionData, PrivateCircuitPublicInputs, TxContext } from '@aztec/circuits.js'; export class TxRequest { constructor( @@ -167,63 +19,7 @@ export class TxRequest { export class PrivateCallStackItem { constructor( public readonly contractAddress: AztecAddress, - public readonly functionSelector: Buffer, + public readonly functionSelector: number, public readonly publicInputs: PrivateCircuitPublicInputs, ) {} } - -export class OldTreeRoots { - constructor( - public privateDataTreeRoot: Fr, - public nullifierTreeRoot: Fr, - public contractTreeRoot: Fr, - public privateKernelVkTreeRoot: Fr, // future enhancement - ) {} -} - -export class ConstantData { - constructor(public oldTreeRoots: OldTreeRoots, public txContext: TxContext) {} -} - -export class AggregationObject {} - -export class NewContractData { - constructor( - public readonly contractAddress: AztecAddress, - public readonly portalContractAddress: EthAddress, - public readonly functionTreeRoot: Fr, - ) {} -} - -export class OptionallyRevealedData {} - -export class AccumulatedTxData { - constructor( - public aggregationObject: AggregationObject, // Contains the aggregated proof of all previous kernel iterations - - public privateCallCount: Fr, - - public newCommitments: Fr[], - public newNullifiers: Fr[], - - public privateCallStack: Fr[], - public publicCallStack: Fr[], - public l1MsgStack: Fr[], - - public newContracts: NewContractData[], - - public optionallyRevealedData: OptionallyRevealedData[], - ) { - assertLength(this, 'newCommitments', KERNEL_NEW_COMMITMENTS_LENGTH); - assertLength(this, 'newNullifiers', KERNEL_NEW_NULLIFIERS_LENGTH); - assertLength(this, 'privateCallStack', KERNEL_PRIVATE_CALL_STACK_LENGTH); - assertLength(this, 'publicCallStack', KERNEL_PUBLIC_CALL_STACK_LENGTH); - assertLength(this, 'l1MsgStack', KERNEL_L1_MSG_STACK_LENGTH); - assertLength(this, 'newContracts', KERNEL_NEW_CONTRACTS_LENGTH); - assertLength(this, 'optionallyRevealedData', KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH); - } -} - -export class PrivateKernelPublicInputs { - constructor(public end: AccumulatedTxData, public constants: ConstantData, public isPrivateKernel: true) {} -} diff --git a/yarn-project/acir-simulator/src/db_oracle.ts b/yarn-project/acir-simulator/src/db_oracle.ts index ae006a01f950..dbf7424a3948 100644 --- a/yarn-project/acir-simulator/src/db_oracle.ts +++ b/yarn-project/acir-simulator/src/db_oracle.ts @@ -1,4 +1,4 @@ -import { AztecAddress, EthAddress } from './circuits.js'; +import { AztecAddress, EthAddress } from '@aztec/circuits.js'; export interface NoteLoadOracleInputs { note: Buffer; diff --git a/yarn-project/acir-simulator/src/simulator.ts b/yarn-project/acir-simulator/src/simulator.ts index 0716d462c694..f51e84e8ecd4 100644 --- a/yarn-project/acir-simulator/src/simulator.ts +++ b/yarn-project/acir-simulator/src/simulator.ts @@ -1,12 +1,6 @@ import { ExecutionPreimages } from './acvm.js'; -import { - CallContext, - PrivateCircuitPublicInputs, - TxRequest, - EthAddress, - PrivateCallStackItem, - OldTreeRoots, -} from './circuits.js'; +import { CallContext, PrivateCircuitPublicInputs, EthAddress, OldTreeRoots } from '@aztec/circuits.js'; +import { PrivateCallStackItem, TxRequest } from './circuits.js'; export interface ExecutionResult { // Needed for prover @@ -36,7 +30,7 @@ export class AcirSimulator { portalContractAddress, false, false, - request.functionData.isContructor, + request.functionData.isConstructor, ); const publicInputs = new PrivateCircuitPublicInputs( diff --git a/yarn-project/aztec-rpc/src/account_state/account_state.ts b/yarn-project/aztec-rpc/src/account_state/account_state.ts index 8e5e97e27f91..3eeb8aaa5e26 100644 --- a/yarn-project/aztec-rpc/src/account_state/account_state.ts +++ b/yarn-project/aztec-rpc/src/account_state/account_state.ts @@ -1,4 +1,4 @@ -import { AztecAddress } from '../circuits.js'; +import { AztecAddress } from '@aztec/circuits.js'; import { Database } from '../database/index.js'; import { TxHash } from '../tx/index.js'; diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_client/aztec_rpc_client.ts b/yarn-project/aztec-rpc/src/aztec_rpc_client/aztec_rpc_client.ts index 2298494e2faf..b8b1f7fd8af0 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_client/aztec_rpc_client.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_client/aztec_rpc_client.ts @@ -1,5 +1,6 @@ +import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js'; import { Tx } from '@aztec/p2p'; -import { AztecAddress, EthAddress, Fr, Signature, TxRequest } from '../circuits.js'; +import { Signature, TxRequest } from '../circuits.js'; import { ContractAbi } from '../noir.js'; import { TxHash, TxReceipt } from '../tx/index.js'; diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts index d820293e6b50..1e286957841e 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts @@ -9,9 +9,8 @@ import { ContractDeploymentData, EthAddress, Fr, - generateContractAddress, + FunctionData, OldTreeRoots, - Signature, TxContext, } from '@aztec/circuits.js'; import { ContractDao, ContractDataSource } from '../contract_data_source/index.js'; @@ -19,6 +18,8 @@ import { KeyStore } from '../key_store/index.js'; import { ContractAbi } from '../noir.js'; import { Synchroniser } from '../synchroniser/index.js'; import { TxHash } from '../tx/index.js'; +import { generateContractAddress, selectorToNumber, Signature, TxRequest, ZERO_FR } from '../circuits.js'; +import { randomBytes } from '@aztec/foundation'; export class AztecRPCServer implements AztecRPCClient { constructor( @@ -60,26 +61,26 @@ export class AztecRPCServer implements AztecRPCClient { throw new Error('Cannot find constructor in the ABI.'); } - const functionData = { - functionSelector: generateFunctionSelector(constructorAbi.name, constructorAbi.parameters), - isSecret: true, - isContructor: true, - }; + const functionData = new FunctionData( + selectorToNumber(generateFunctionSelector(constructorAbi.name, constructorAbi.parameters)), + true, + true, + ); - const constructorVkHash = Fr.ZERO; - const functionTreeRoot = Fr.ZERO; + const constructorVkHash = ZERO_FR; + const functionTreeRoot = ZERO_FR; const contractDeploymentData = new ContractDeploymentData( constructorVkHash, functionTreeRoot, contractAddressSalt, portalContract, ); - const txContext = new TxContext(false, false, false, contractDeploymentData); + const txContext = new TxContext(false, false, true, contractDeploymentData); const contractAddress = generateContractAddress(from, contractAddressSalt, args); await this.db.addContract(contractAddress, portalContract, abi, false); - console.log(`Function data ${functionData.isContructor}`); + console.log(`Function data ${functionData.isConstructor}`); return new TxRequest( from, @@ -87,8 +88,8 @@ export class AztecRPCServer implements AztecRPCClient { functionData, args, txContext, - Fr.random(), // nonce - Fr.ZERO, // chainId + new Fr(randomBytes(Fr.SIZE_IN_BYTES)), // nonce + ZERO_FR, // chainId ); } @@ -100,13 +101,18 @@ export class AztecRPCServer implements AztecRPCClient { const functionDao = this.findFunction(contract, functionSelector); - const functionData = { - functionSelector, - isSecret: functionDao.isSecret, - isContructor: false, - }; + const functionData = new FunctionData( + functionSelector.readUint32BE(), + functionDao.isSecret as any, // TODO: remove as any + false as any, // TODO: remove as any + ); - const txContext = new TxContext(false, false, false, ContractDeploymentData.EMPTY); + const txContext = new TxContext( + false, + false, + true, + new ContractDeploymentData(ZERO_FR, ZERO_FR, ZERO_FR, new EthAddress(Buffer.alloc(EthAddress.SIZE_IN_BYTES))), + ); return new TxRequest( from, @@ -114,8 +120,8 @@ export class AztecRPCServer implements AztecRPCClient { functionData, args, txContext, - Fr.random(), // nonce - Fr.ZERO, // chainId + new Fr(randomBytes(Fr.SIZE_IN_BYTES)), // nonce + ZERO_FR, // chainId ); } @@ -126,7 +132,7 @@ export class AztecRPCServer implements AztecRPCClient { public async createTx(txRequest: TxRequest, signature: Signature) { let contractAddress; - if (txRequest.to.equals(AztecAddress.ZERO)) { + if (txRequest.to.toBuffer().equals(ZERO_FR.toBuffer())) { contractAddress = generateContractAddress( txRequest.from, txRequest.txContext.contractDeploymentData.contractAddressSalt, @@ -134,7 +140,7 @@ export class AztecRPCServer implements AztecRPCClient { ); } else { contractAddress = txRequest.to; - console.log(`to is not zero ${contractAddress.buffer.toString('hex')}`); + console.log(`to is not zero ${contractAddress.toBuffer().toString('hex')}`); } const contract = await this.db.getContract(contractAddress); @@ -143,9 +149,12 @@ export class AztecRPCServer implements AztecRPCClient { throw new Error('Unknown contract.'); } - const functionDao = this.findFunction(contract, txRequest.functionData.functionSelector); + const selector = Buffer.alloc(4); + selector.writeUint32BE(txRequest.functionData.functionSelector); + + const functionDao = this.findFunction(contract, selector); - const oldRoots = new OldTreeRoots(Fr.ZERO, Fr.ZERO, Fr.ZERO, Fr.ZERO); // TODO - get old roots from the database/node + const oldRoots = new OldTreeRoots(ZERO_FR, ZERO_FR, ZERO_FR, ZERO_FR); // TODO - get old roots from the database/node const executionResult = await this.acirSimulator.run( txRequest, Buffer.from(functionDao.bytecode, 'base64'), diff --git a/yarn-project/aztec-rpc/src/circuits.ts b/yarn-project/aztec-rpc/src/circuits.ts index 8fab19cde731..54d24605ad07 100644 --- a/yarn-project/aztec-rpc/src/circuits.ts +++ b/yarn-project/aztec-rpc/src/circuits.ts @@ -1,6 +1,8 @@ import { AztecAddress, EthAddress, Fr, FunctionData, TxContext } from '@aztec/circuits.js'; import { randomBytes } from '@aztec/foundation'; +export const ZERO_FR = new Fr(Buffer.alloc(32)); + export class TxRequest { constructor( public readonly from: AztecAddress, @@ -42,3 +44,7 @@ export function generateContractAddress( ) { return new Fr(randomBytes(32)); } + +export function selectorToNumber(selector: Buffer) { + return selector.readUInt32BE(); +} diff --git a/yarn-project/aztec-rpc/src/contract_data_source/contract_dao.ts b/yarn-project/aztec-rpc/src/contract_data_source/contract_dao.ts index ae3f167c3985..dfc4b1f5a677 100644 --- a/yarn-project/aztec-rpc/src/contract_data_source/contract_dao.ts +++ b/yarn-project/aztec-rpc/src/contract_data_source/contract_dao.ts @@ -1,5 +1,5 @@ import { generateFunctionSelector } from '../abi_coder/index.js'; -import { AztecAddress, EthAddress } from '../circuits.js'; +import { AztecAddress, EthAddress } from '@aztec/circuits.js'; import { ContractAbi, FunctionAbi } from '../noir.js'; export interface ContractFunctionDao extends FunctionAbi { @@ -14,10 +14,9 @@ export interface ContractDao extends ContractAbi { } export function functionAbiToFunctionDao(abi: FunctionAbi) { - const selector = generateFunctionSelector(abi.name, abi.parameters); return { ...abi, - selector, + selector: generateFunctionSelector(abi.name, abi.parameters), }; } diff --git a/yarn-project/aztec-rpc/src/contract_data_source/contract_data_source.ts b/yarn-project/aztec-rpc/src/contract_data_source/contract_data_source.ts index 917884b8dd05..db336b2a16d7 100644 --- a/yarn-project/aztec-rpc/src/contract_data_source/contract_data_source.ts +++ b/yarn-project/aztec-rpc/src/contract_data_source/contract_data_source.ts @@ -1,4 +1,4 @@ -import { AztecAddress, EthAddress } from '../circuits.js'; +import { AztecAddress, EthAddress } from '@aztec/circuits.js'; import { ContractAbi } from '../noir.js'; import { ContractDao } from './contract_dao.js'; diff --git a/yarn-project/aztec-rpc/src/contract_data_source/memory_contract_data_source.ts b/yarn-project/aztec-rpc/src/contract_data_source/memory_contract_data_source.ts index e342908b14bc..f1783e109941 100644 --- a/yarn-project/aztec-rpc/src/contract_data_source/memory_contract_data_source.ts +++ b/yarn-project/aztec-rpc/src/contract_data_source/memory_contract_data_source.ts @@ -1,4 +1,4 @@ -import { AztecAddress, EthAddress } from '../circuits.js'; +import { AztecAddress, EthAddress } from '@aztec/circuits.js'; import { ContractAbi } from '../noir.js'; import { contractAbiToContractDao, ContractDao } from './contract_dao.js'; import { ContractDataSource } from './contract_data_source.js'; @@ -26,7 +26,7 @@ export class MemoryContractDataSource implements ContractDataSource { } public getContract(address: AztecAddress) { - return Promise.resolve(this.contracts.find(c => c.address.equals(address))); + return Promise.resolve(this.contracts.find(c => c.address.toBuffer().equals(address.toBuffer()))); } public async getCode(contractAddress: AztecAddress, functionSelector: Buffer) { diff --git a/yarn-project/aztec-rpc/src/database/tx_dao.ts b/yarn-project/aztec-rpc/src/database/tx_dao.ts index 30d0b77b5624..c8547a622d7a 100644 --- a/yarn-project/aztec-rpc/src/database/tx_dao.ts +++ b/yarn-project/aztec-rpc/src/database/tx_dao.ts @@ -1,4 +1,4 @@ -import { AztecAddress } from '../circuits.js'; +import { AztecAddress } from '@aztec/circuits.js'; import { TxHash } from '../tx/index.js'; export class TxDao { diff --git a/yarn-project/aztec-rpc/src/key_store/key_pair.ts b/yarn-project/aztec-rpc/src/key_store/key_pair.ts index c0c7c9077a3b..08d024aa80c1 100644 --- a/yarn-project/aztec-rpc/src/key_store/key_pair.ts +++ b/yarn-project/aztec-rpc/src/key_store/key_pair.ts @@ -1,4 +1,5 @@ -import { AztecAddress, Signature } from '../circuits.js'; +import { AztecAddress, Fr } from '@aztec/circuits.js'; +import { Signature } from '../circuits.js'; import { randomBytes } from '../foundation.js'; export interface KeyPair { @@ -10,7 +11,7 @@ export interface KeyPair { export class ConstantKeyPair implements KeyPair { public static random() { const privateKey = randomBytes(32); - const publicKey = AztecAddress.random(); + const publicKey = new Fr(randomBytes(Fr.SIZE_IN_BYTES)); return new ConstantKeyPair(publicKey, privateKey); } diff --git a/yarn-project/aztec-rpc/src/key_store/key_store.ts b/yarn-project/aztec-rpc/src/key_store/key_store.ts index e9fd0a684311..61a825410d59 100644 --- a/yarn-project/aztec-rpc/src/key_store/key_store.ts +++ b/yarn-project/aztec-rpc/src/key_store/key_store.ts @@ -1,4 +1,5 @@ -import { AztecAddress, Signature, TxRequest } from '../circuits.js'; +import { AztecAddress } from '@aztec/circuits.js'; +import { Signature, TxRequest } from '../circuits.js'; export interface KeyStore { addAccount(): Promise; diff --git a/yarn-project/aztec-rpc/src/key_store/test_key_store.ts b/yarn-project/aztec-rpc/src/key_store/test_key_store.ts index 8a4fd4c94560..c314b9696a23 100644 --- a/yarn-project/aztec-rpc/src/key_store/test_key_store.ts +++ b/yarn-project/aztec-rpc/src/key_store/test_key_store.ts @@ -1,4 +1,4 @@ -import { TxRequest, AztecAddress } from '../circuits.js'; +import { TxRequest, ZERO_FR } from '../circuits.js'; import { ConstantKeyPair, KeyPair } from './key_pair.js'; import { KeyStore } from './key_store.js'; @@ -24,9 +24,9 @@ export class TestKeyStore implements KeyStore { } signTxRequest(txRequest: TxRequest) { - const account = txRequest.from.equals(AztecAddress.ZERO) + const account = txRequest.from.toBuffer().equals(ZERO_FR.toBuffer()) ? this.accounts[0] - : this.accounts.find(a => a.getPublicKey().equals(txRequest.from)); + : this.accounts.find(a => a.getPublicKey().toBuffer().equals(txRequest.from.toBuffer())); if (!account) { throw new Error('Unknown account.'); } diff --git a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts index 4f93a4cf4dc7..298a26cc7062 100644 --- a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts +++ b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts @@ -1,7 +1,7 @@ import { AztecNode } from '@aztec/aztec-node'; import { InterruptableSleep } from '@aztec/foundation'; import { AccountState } from '../account_state/index.js'; -import { AztecAddress, EthAddress } from '../circuits.js'; +import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js'; import { Database } from '../database/index.js'; import { ContractAbi } from '../noir.js'; import { TxHash } from '../tx/index.js'; @@ -34,8 +34,8 @@ export class Synchroniser { const contractAddresses = blocks .map(b => b.newContractData.map(d => d.aztecAddress)) .flat() - .map(fr => new AztecAddress(fr.toBuffer())); - console.log(`address ${contractAddresses[0].buffer.toString('hex')}`); + .map(fr => new Fr(fr.toBuffer())); + console.log(`address ${contractAddresses[0].toBuffer().toString('hex')}`); await this.db.confirmContractsDeployed(contractAddresses); from += blocks.length; @@ -61,7 +61,7 @@ export class Synchroniser { } public getAccount(account: AztecAddress) { - return this.accountStates.find(as => as.publicKey.equals(account)); + return this.accountStates.find(as => as.publicKey.toBuffer().equals(account.toBuffer())); } public getAccounts() { diff --git a/yarn-project/aztec-rpc/src/tx/tx_receipt.ts b/yarn-project/aztec-rpc/src/tx/tx_receipt.ts index fb0fe8fb1cee..52197026166f 100644 --- a/yarn-project/aztec-rpc/src/tx/tx_receipt.ts +++ b/yarn-project/aztec-rpc/src/tx/tx_receipt.ts @@ -1,4 +1,4 @@ -import { AztecAddress } from '../circuits.js'; +import { AztecAddress } from '@aztec/circuits.js'; import { TxHash } from './tx_hash.js'; export interface TxReceipt { diff --git a/yarn-project/aztec.js/src/contract/call_method.ts b/yarn-project/aztec.js/src/contract/call_method.ts index cf2b550a6da9..5d5ca68df910 100644 --- a/yarn-project/aztec.js/src/contract/call_method.ts +++ b/yarn-project/aztec.js/src/contract/call_method.ts @@ -1,4 +1,5 @@ -import { AztecAddress, AztecRPCClient } from '@aztec/aztec-rpc'; +import { AztecRPCClient } from '@aztec/aztec-rpc'; +import { AztecAddress } from '@aztec/circuits.js'; import { ContractFunction } from './contract_function.js'; export interface CallMethodOptions { diff --git a/yarn-project/aztec.js/src/contract/contract.ts b/yarn-project/aztec.js/src/contract/contract.ts index 12df2ae042f5..81d4fb70e297 100644 --- a/yarn-project/aztec.js/src/contract/contract.ts +++ b/yarn-project/aztec.js/src/contract/contract.ts @@ -1,4 +1,5 @@ -import { AztecAddress, AztecRPCClient, ContractAbi } from '@aztec/aztec-rpc'; +import { AztecRPCClient, ContractAbi } from '@aztec/aztec-rpc'; +import { AztecAddress } from '@aztec/circuits.js'; import { CallMethodOptions } from './call_method.js'; import { SendMethodOptions } from './send_method.js'; import { SentTx } from './sent_tx.js'; diff --git a/yarn-project/aztec.js/src/contract/send_method.ts b/yarn-project/aztec.js/src/contract/send_method.ts index 3f8ead941c5b..d46e00512c81 100644 --- a/yarn-project/aztec.js/src/contract/send_method.ts +++ b/yarn-project/aztec.js/src/contract/send_method.ts @@ -1,4 +1,5 @@ -import { AztecAddress, AztecRPCClient, Fr, Signature, Tx, TxHash, TxRequest } from '@aztec/aztec-rpc'; +import { AztecRPCClient, Signature, Tx, TxHash, TxRequest, ZERO_FR } from '@aztec/aztec-rpc'; +import { AztecAddress, Fr } from '@aztec/circuits.js'; import { ContractFunction } from './contract_function.js'; import { SentTx } from './sent_tx.js'; @@ -26,12 +27,12 @@ export class SendMethod { public async request(options: SendMethodOptions = {}) { const { from } = { ...this.defaultOptions, ...options }; - console.log(`send address ${this.contractAddress.buffer.toString('hex')}`); + console.log(`send address ${this.contractAddress.toBuffer().toString('hex')}`); this.txRequest = await this.arc.createTxRequest( this.entry.encodeABI(), this.entry.encodeParameters(this.args).map(p => new Fr(p)), this.contractAddress, - from || AztecAddress.ZERO, + from || ZERO_FR, ); return this.txRequest; } @@ -62,11 +63,6 @@ export class SendMethod { } else { promise = (async () => { await this.create(options); - console.log( - `final address ${this.tx!.data.end.newContracts[0].contractAddress.toString()}, is const ${ - this.txRequest?.functionData.isContructor - }`, - ); return this.arc.sendTx(this.tx!); })(); } diff --git a/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts b/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts index c9036ff4abcc..aa8d6735644d 100644 --- a/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts +++ b/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts @@ -1,4 +1,6 @@ -import { AztecAddress, AztecRPCClient, ContractAbi, EthAddress, Fr } from '@aztec/aztec-rpc'; +import { AztecRPCClient, ContractAbi, ZERO_FR } from '@aztec/aztec-rpc'; +import { EthAddress, Fr } from '@aztec/circuits.js'; +import { randomBytes } from '@aztec/foundation'; import { ContractFunction, SendMethod, SendMethodOptions } from '../contract/index.js'; export interface ConstructorOptions extends SendMethodOptions { @@ -21,7 +23,7 @@ export class ConstructorMethod extends SendMethod { throw new Error('Cannot find constructor in the ABI.'); } - super(arc, AztecAddress.ZERO, new ContractFunction(constructorAbi), args, defaultOptions); + super(arc, ZERO_FR, new ContractFunction(constructorAbi), args, defaultOptions); } public async request(options: ConstructorOptions = {}) { @@ -29,11 +31,10 @@ export class ConstructorMethod extends SendMethod { this.txRequest = await this.arc.createDeploymentTxRequest( this.abi, this.entry.encodeParameters(this.args).map(p => new Fr(p)), - portalContract || EthAddress.ZERO, - contractAddressSalt || Fr.random(), - from || AztecAddress.ZERO, + portalContract || new EthAddress(Buffer.alloc(EthAddress.SIZE_IN_BYTES)), + contractAddressSalt || new Fr(randomBytes(Fr.SIZE_IN_BYTES)), + from || ZERO_FR, ); - console.log(`contract address ${this.txRequest.to.buffer.toString('hex')}`); return this.txRequest; } diff --git a/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts b/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts index d744dd4fbe76..82144e89b460 100644 --- a/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts +++ b/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts @@ -1,17 +1,8 @@ import { mock } from 'jest-mock-extended'; -import { - AztecAddress, - AztecRPCClient, - ContractAbi, - EthAddress, - Fr, - Signature, - Tx, - TxHash, - TxReceipt, - TxRequest, -} from '@aztec/aztec-rpc'; +import { AztecRPCClient, ContractAbi, Signature, Tx, TxHash, TxReceipt, TxRequest, ZERO_FR } from '@aztec/aztec-rpc'; import { ContractDeployer } from './contract_deployer.js'; +import { EthAddress, Fr } from '@aztec/circuits.js'; +import { randomBytes } from 'crypto'; describe('Contract Deployer', () => { let arc: ReturnType>; @@ -29,9 +20,9 @@ describe('Contract Deployer', () => { ], }; - const portalContract = EthAddress.random(); - const contractAddressSalt = Fr.random(); - const account = AztecAddress.random(); + const portalContract = new EthAddress(randomBytes(EthAddress.SIZE_IN_BYTES)); + const contractAddressSalt = new Fr(randomBytes(Fr.SIZE_IN_BYTES)); + const account = new Fr(randomBytes(Fr.SIZE_IN_BYTES)); const mockTxRequest = { type: 'TxRequest' } as any as TxRequest; const mockSignature = { type: 'Signature' } as any as Signature; @@ -103,12 +94,12 @@ describe('Contract Deployer', () => { expect(arc.createDeploymentTxRequest).toHaveBeenCalledWith( abi, [], - EthAddress.ZERO, // portalContract + new EthAddress(Buffer.alloc(EthAddress.SIZE_IN_BYTES)), // portalContract expect.anything(), // contractAddressSalt - AztecAddress.ZERO, // account + ZERO_FR, // account ); const defaultContractAddressSalt = arc.createDeploymentTxRequest.mock.calls[0][3]; expect(defaultContractAddressSalt).not.toEqual(contractAddressSalt); - expect(defaultContractAddressSalt).not.toEqual(Fr.ZERO); + expect(defaultContractAddressSalt).not.toEqual(ZERO_FR); }); }); diff --git a/yarn-project/kernel-prover/src/circuits.ts b/yarn-project/kernel-prover/src/circuits.ts index d6653cdb186a..2601ea59fbdf 100644 --- a/yarn-project/kernel-prover/src/circuits.ts +++ b/yarn-project/kernel-prover/src/circuits.ts @@ -34,12 +34,3 @@ export class Signature { constructor(public readonly buffer: Buffer) {} } - -// export function generateContractAddress( -// deployerAddress: AztecAddress, -// salt: Fr, -// args: Fr[], -// // functionLeaves: Fr[], -// ) { -// return new Fr(randomBytes(32)); -// } From 8a58c322ec5bc7cf77485a7aed39bd7d13dca706 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 14:05:03 +0000 Subject: [PATCH 03/23] Fixed first end to end test --- yarn-project/acir-simulator/src/simulator.ts | 38 ++++++++--- yarn-project/aztec-rpc/package.json | 1 + .../src/aztec_rpc_server/aztec_rpc_server.ts | 41 +++++++++--- .../create_aztec_rpc_server.ts | 9 +-- yarn-project/aztec-rpc/src/circuits.ts | 2 +- .../aztec-rpc/src/database/database.ts | 1 + .../aztec-rpc/src/database/memory_db.ts | 10 +++ yarn-project/aztec-rpc/src/database/tx_dao.ts | 4 +- yarn-project/aztec-rpc/src/index.ts | 3 + .../aztec-rpc/src/key_store/test_key_store.ts | 4 +- .../src/synchroniser/synchroniser.ts | 66 +++++++++++++++---- yarn-project/aztec-rpc/src/tx/tx_receipt.ts | 4 +- .../aztec.js/src/contract/send_method.ts | 2 - yarn-project/aztec.js/src/contract/sent_tx.ts | 1 - .../end-to-end/src/create_aztec_rpc_client.ts | 2 +- .../src/e2e_deploy_contract.test.ts | 28 ++++---- yarn-project/kernel-prover/src/index.ts | 1 - 17 files changed, 153 insertions(+), 64 deletions(-) diff --git a/yarn-project/acir-simulator/src/simulator.ts b/yarn-project/acir-simulator/src/simulator.ts index f51e84e8ecd4..172c34b37253 100644 --- a/yarn-project/acir-simulator/src/simulator.ts +++ b/yarn-project/acir-simulator/src/simulator.ts @@ -1,5 +1,19 @@ import { ExecutionPreimages } from './acvm.js'; -import { CallContext, PrivateCircuitPublicInputs, EthAddress, OldTreeRoots } from '@aztec/circuits.js'; +import { + CallContext, + PrivateCircuitPublicInputs, + EthAddress, + OldTreeRoots, + Fr, + ARGS_LENGTH, + EMITTED_EVENTS_LENGTH, + NEW_COMMITMENTS_LENGTH, + NEW_NULLIFIERS_LENGTH, + PRIVATE_CALL_STACK_LENGTH, + PUBLIC_CALL_STACK_LENGTH, + L1_MSG_STACK_LENGTH, + RETURN_VALUES_LENGTH, +} from '@aztec/circuits.js'; import { PrivateCallStackItem, TxRequest } from './circuits.js'; export interface ExecutionResult { @@ -33,16 +47,22 @@ export class AcirSimulator { request.functionData.isConstructor, ); + const randomFields = (num: number) => { + return Array(num) + .fill(0) + .map(() => new Fr(0)); + }; + const publicInputs = new PrivateCircuitPublicInputs( callContext, - request.args, - [], // returnValues, - [], // emittedEvents, - [], // newCommitments, - [], // newNullifiers, - [], // privateCallStack, - [], // publicCallStack, - [], // l1MsgStack, + request.args.concat(randomFields(ARGS_LENGTH - request.args.length)), + randomFields(RETURN_VALUES_LENGTH), // returnValues, + randomFields(EMITTED_EVENTS_LENGTH), // emittedEvents, + randomFields(NEW_COMMITMENTS_LENGTH), // newCommitments, + randomFields(NEW_NULLIFIERS_LENGTH), // newNullifiers, + randomFields(PRIVATE_CALL_STACK_LENGTH), // privateCallStack, + randomFields(PUBLIC_CALL_STACK_LENGTH), // publicCallStack, + randomFields(L1_MSG_STACK_LENGTH), // l1MsgStack, oldRoots.privateDataTreeRoot, oldRoots.nullifierTreeRoot, oldRoots.contractTreeRoot, diff --git a/yarn-project/aztec-rpc/package.json b/yarn-project/aztec-rpc/package.json index d359e6d3730b..d731a77a6a72 100644 --- a/yarn-project/aztec-rpc/package.json +++ b/yarn-project/aztec-rpc/package.json @@ -33,6 +33,7 @@ "@aztec/aztec-node": "workspace:^", "@aztec/foundation": "workspace:^", "@aztec/kernel-prover": "workspace:^", + "aztec/circuits.js": "workspace:^", "@aztec/p2p": "workspace:^", "sha3": "^2.1.4", "tslib": "^2.4.0" diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts index 1e286957841e..fd063e6b81a9 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts @@ -19,20 +19,31 @@ import { ContractAbi } from '../noir.js'; import { Synchroniser } from '../synchroniser/index.js'; import { TxHash } from '../tx/index.js'; import { generateContractAddress, selectorToNumber, Signature, TxRequest, ZERO_FR } from '../circuits.js'; -import { randomBytes } from '@aztec/foundation'; +import { createDebugLogger, createLogger, randomBytes } from '@aztec/foundation'; +import { Database } from '../database/database.js'; +import { TxDao } from '../database/tx_dao.js'; export class AztecRPCServer implements AztecRPCClient { + private synchroniser: Synchroniser; constructor( private keyStore: KeyStore, - private synchroniser: Synchroniser, private acirSimulator: AcirSimulator, private kernelProver: KernelProver, private node: AztecNode, - private db: ContractDataSource, - ) {} + private db: Database, + private log = createDebugLogger('aztec:rpc_server'), + ) { + this.synchroniser = new Synchroniser(node, db); + this.synchroniser.start(); + } + + public async stop() { + await this.synchroniser.stop(); + } public async addAccount() { const accountPublicKey = await this.keyStore.addAccount(); + this.log(`adding account ${accountPublicKey.toString()}`); await this.synchroniser.addAccount(accountPublicKey); return accountPublicKey; } @@ -77,13 +88,13 @@ export class AztecRPCServer implements AztecRPCClient { ); const txContext = new TxContext(false, false, true, contractDeploymentData); - const contractAddress = generateContractAddress(from, contractAddressSalt, args); - await this.db.addContract(contractAddress, portalContract, abi, false); + const fromAddress = from.toBuffer().equals(ZERO_FR.toBuffer()) ? (await this.keyStore.getAccounts())[0] : from; - console.log(`Function data ${functionData.isConstructor}`); + const contractAddress = generateContractAddress(fromAddress, contractAddressSalt, args); + await this.db.addContract(contractAddress, portalContract, abi, false); return new TxRequest( - from, + fromAddress, contractAddress, functionData, args, @@ -140,7 +151,6 @@ export class AztecRPCServer implements AztecRPCClient { ); } else { contractAddress = txRequest.to; - console.log(`to is not zero ${contractAddress.toBuffer().toString('hex')}`); } const contract = await this.db.getContract(contractAddress); @@ -167,8 +177,19 @@ export class AztecRPCServer implements AztecRPCClient { executionResult, oldRoots as any, // TODO - remove `as any` ); + const tx = new Tx(publicInputs); + const dao: TxDao = new TxDao( + new TxHash(tx.txId), + undefined, + undefined, + txRequest.from, + undefined, + txRequest.to, + '', + ); + await this.db.addOrUpdateTx(dao); // TODO I think the TX should include all the data from the publicInputs + proof - return new Tx(publicInputs); + return tx; } public async sendTx(tx: Tx) { diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/create_aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/create_aztec_rpc_server.ts index 451796b31e01..4527834ea170 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/create_aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/create_aztec_rpc_server.ts @@ -3,7 +3,6 @@ import { AztecNode } from '@aztec/aztec-node'; import { KernelProver } from '@aztec/kernel-prover'; import { MemoryDB } from '../database/index.js'; import { KeyStore, TestKeyStore } from '../key_store/index.js'; -import { Synchroniser } from '../synchroniser/index.js'; import { AztecRPCServer } from './aztec_rpc_server.js'; export async function createAztecRPCServer( @@ -11,25 +10,19 @@ export async function createAztecRPCServer( { keyStore, db, - synchroniser, acirSimulator, kernelProver, }: { keyStore?: KeyStore; db?: MemoryDB; - synchroniser?: Synchroniser; acirSimulator?: AcirSimulator; kernelProver?: KernelProver; } = {}, ) { keyStore = keyStore || new TestKeyStore(); db = db || new MemoryDB(); - if (!synchroniser) { - synchroniser = new Synchroniser(aztecNode, db); - synchroniser.start(); - } acirSimulator = acirSimulator || new AcirSimulator(); kernelProver = kernelProver || new KernelProver(); - return await Promise.resolve(new AztecRPCServer(keyStore, synchroniser, acirSimulator, kernelProver, aztecNode, db)); + return await Promise.resolve(new AztecRPCServer(keyStore, acirSimulator, kernelProver, aztecNode, db)); } diff --git a/yarn-project/aztec-rpc/src/circuits.ts b/yarn-project/aztec-rpc/src/circuits.ts index 54d24605ad07..7970fa474e30 100644 --- a/yarn-project/aztec-rpc/src/circuits.ts +++ b/yarn-project/aztec-rpc/src/circuits.ts @@ -30,7 +30,7 @@ export class Signature { public static SIZE = 64; public static random() { - return new EthAddress(randomBytes(Signature.SIZE)); + return new Signature(randomBytes(Signature.SIZE)); } constructor(public readonly buffer: Buffer) {} diff --git a/yarn-project/aztec-rpc/src/database/database.ts b/yarn-project/aztec-rpc/src/database/database.ts index 3e74a51a55fa..038212ee1123 100644 --- a/yarn-project/aztec-rpc/src/database/database.ts +++ b/yarn-project/aztec-rpc/src/database/database.ts @@ -4,4 +4,5 @@ import { TxDao } from './tx_dao.js'; export interface Database extends ContractDataSource { getTx(txHash: TxHash): Promise; + addOrUpdateTx(tx: TxDao): Promise; } diff --git a/yarn-project/aztec-rpc/src/database/memory_db.ts b/yarn-project/aztec-rpc/src/database/memory_db.ts index cc2215964d08..c89fe6e16d01 100644 --- a/yarn-project/aztec-rpc/src/database/memory_db.ts +++ b/yarn-project/aztec-rpc/src/database/memory_db.ts @@ -9,4 +9,14 @@ export class MemoryDB extends MemoryContractDataSource implements Database { public getTx(txHash: TxHash) { return Promise.resolve(this.txs.find(tx => tx.txHash.equals(txHash))); } + + public addOrUpdateTx(tx: TxDao): Promise { + const index = this.txs.findIndex(t => t.txHash.equals(tx.txHash)); + if (index === -1) { + this.txs.push(tx); + } else { + this.txs[index] = tx; + } + return Promise.resolve(); + } } diff --git a/yarn-project/aztec-rpc/src/database/tx_dao.ts b/yarn-project/aztec-rpc/src/database/tx_dao.ts index c8547a622d7a..24f1082b6231 100644 --- a/yarn-project/aztec-rpc/src/database/tx_dao.ts +++ b/yarn-project/aztec-rpc/src/database/tx_dao.ts @@ -4,8 +4,8 @@ import { TxHash } from '../tx/index.js'; export class TxDao { constructor( public readonly txHash: TxHash, - public readonly blockHash: Buffer, - public readonly blockNumber: number, + public blockHash: Buffer | undefined, + public blockNumber: number | undefined, public readonly from: AztecAddress, public readonly to: AztecAddress | undefined, public readonly contractAddress: AztecAddress | undefined, diff --git a/yarn-project/aztec-rpc/src/index.ts b/yarn-project/aztec-rpc/src/index.ts index 74f073486776..8dcb6c42c258 100644 --- a/yarn-project/aztec-rpc/src/index.ts +++ b/yarn-project/aztec-rpc/src/index.ts @@ -7,3 +7,6 @@ export { Tx } from '@aztec/p2p'; // TODO - only export necessary stuffs export * from './circuits.js'; export * from './noir.js'; + +export { AztecAddress } from '@aztec/circuits.js'; +export { Fr } from '@aztec/circuits.js'; diff --git a/yarn-project/aztec-rpc/src/key_store/test_key_store.ts b/yarn-project/aztec-rpc/src/key_store/test_key_store.ts index c314b9696a23..120d426579f7 100644 --- a/yarn-project/aztec-rpc/src/key_store/test_key_store.ts +++ b/yarn-project/aztec-rpc/src/key_store/test_key_store.ts @@ -5,9 +5,7 @@ import { KeyStore } from './key_store.js'; export class TestKeyStore implements KeyStore { private accounts: KeyPair[] = []; - constructor() { - this.accounts.push(ConstantKeyPair.random()); - } + constructor() {} public addAccount() { const keyPair = ConstantKeyPair.random(); diff --git a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts index 298a26cc7062..09da3012240c 100644 --- a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts +++ b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts @@ -1,10 +1,19 @@ import { AztecNode } from '@aztec/aztec-node'; -import { InterruptableSleep } from '@aztec/foundation'; +import { createDebugLogger, InterruptableSleep } from '@aztec/foundation'; import { AccountState } from '../account_state/index.js'; -import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js'; -import { Database } from '../database/index.js'; +import { + AztecAddress, + EthAddress, + Fr, + KERNEL_NEW_COMMITMENTS_LENGTH, + KERNEL_NEW_CONTRACTS_LENGTH, + KERNEL_NEW_NULLIFIERS_LENGTH, +} from '@aztec/circuits.js'; +import { Database, TxDao } from '../database/index.js'; import { ContractAbi } from '../noir.js'; import { TxHash } from '../tx/index.js'; +import { L2Block } from '@aztec/archiver'; +import { keccak256 } from '../foundation.js'; export class Synchroniser { private runningPromise?: Promise; @@ -12,9 +21,9 @@ export class Synchroniser { private interruptableSleep = new InterruptableSleep(); private running = false; - constructor(private node: AztecNode, private db: Database) {} + constructor(private node: AztecNode, private db: Database, private log = createDebugLogger('aztec:synchroniser')) {} - public start(from = 1, take = 1, retryInterval = 10000) { + public start(from = 1, take = 1, retryInterval = 1000) { if (this.running) { return; } @@ -31,12 +40,7 @@ export class Synchroniser { continue; } - const contractAddresses = blocks - .map(b => b.newContractData.map(d => d.aztecAddress)) - .flat() - .map(fr => new Fr(fr.toBuffer())); - console.log(`address ${contractAddresses[0].toBuffer().toString('hex')}`); - await this.db.confirmContractsDeployed(contractAddresses); + await this.decodeBlocks(blocks); from += blocks.length; } catch (err) { @@ -47,12 +51,14 @@ export class Synchroniser { }; this.runningPromise = run(); + this.log('Started'); } public async stop() { this.running = false; this.interruptableSleep.interrupt(); await this.runningPromise; + this.log('Stopped'); } public async addAccount(account: AztecAddress) { @@ -77,6 +83,9 @@ export class Synchroniser { if (!tx) { return; } + if (!tx.blockHash) { + return; + } const account = this.getAccount(tx.from); if (!account) { @@ -94,4 +103,39 @@ export class Synchroniser { status: !tx.error, }; } + + private async decodeBlocks(l2Blocks: L2Block[]) { + for (const block of l2Blocks) { + let i = 0; + const numTxs = Math.floor(block.newCommitments.length / KERNEL_NEW_COMMITMENTS_LENGTH); + while (i < numTxs) { + const dataToHash = Buffer.concat( + [ + block.newCommitments + .slice( + i * KERNEL_NEW_COMMITMENTS_LENGTH, + i * KERNEL_NEW_COMMITMENTS_LENGTH + KERNEL_NEW_COMMITMENTS_LENGTH, + ) + .map(x => x.toBuffer()), + block.newNullifiers + .slice(i * KERNEL_NEW_NULLIFIERS_LENGTH, i * KERNEL_NEW_NULLIFIERS_LENGTH + KERNEL_NEW_NULLIFIERS_LENGTH) + .map(x => x.toBuffer()), + block.newContracts + .slice(i * KERNEL_NEW_CONTRACTS_LENGTH, i * KERNEL_NEW_CONTRACTS_LENGTH + KERNEL_NEW_CONTRACTS_LENGTH) + .map(x => x.toBuffer()), + ].flat(), + ); + const txDao: TxDao | undefined = await this.db.getTx(new TxHash(keccak256(dataToHash))); + if (txDao !== undefined) { + txDao.blockHash = keccak256(block.encode()); + txDao.blockNumber = block.number; + await this.db.addOrUpdateTx(txDao); + } + i++; + } + const contractAddresses = block.newContractData.map(d => d.aztecAddress).flat(); + await this.db.confirmContractsDeployed(contractAddresses); + this.log(`Synched block ${block.number}`); + } + } } diff --git a/yarn-project/aztec-rpc/src/tx/tx_receipt.ts b/yarn-project/aztec-rpc/src/tx/tx_receipt.ts index 52197026166f..24fc94ece7c5 100644 --- a/yarn-project/aztec-rpc/src/tx/tx_receipt.ts +++ b/yarn-project/aztec-rpc/src/tx/tx_receipt.ts @@ -4,8 +4,8 @@ import { TxHash } from './tx_hash.js'; export interface TxReceipt { txHash: TxHash; // txIndex: number; - blockHash: Buffer; - blockNumber: number; + blockHash: Buffer | undefined; + blockNumber: number | undefined; from: AztecAddress; to?: AztecAddress; contractAddress?: AztecAddress; diff --git a/yarn-project/aztec.js/src/contract/send_method.ts b/yarn-project/aztec.js/src/contract/send_method.ts index d46e00512c81..91792420cbe7 100644 --- a/yarn-project/aztec.js/src/contract/send_method.ts +++ b/yarn-project/aztec.js/src/contract/send_method.ts @@ -27,7 +27,6 @@ export class SendMethod { public async request(options: SendMethodOptions = {}) { const { from } = { ...this.defaultOptions, ...options }; - console.log(`send address ${this.contractAddress.toBuffer().toString('hex')}`); this.txRequest = await this.arc.createTxRequest( this.entry.encodeABI(), this.entry.encodeParameters(this.args).map(p => new Fr(p)), @@ -38,7 +37,6 @@ export class SendMethod { } public async sign(options: SendMethodOptions = {}) { - console.log(`signing ${this.txRequest}`); if (!this.txRequest) { await this.request(options); } diff --git a/yarn-project/aztec.js/src/contract/sent_tx.ts b/yarn-project/aztec.js/src/contract/sent_tx.ts index 44d99f29a4a8..ad1dbce0bea8 100644 --- a/yarn-project/aztec.js/src/contract/sent_tx.ts +++ b/yarn-project/aztec.js/src/contract/sent_tx.ts @@ -16,7 +16,6 @@ export class SentTx { } const txHash = await this.getTxHash(); - console.log('tx hash'); this.receipt = await retryUntil(() => this.arc.getTxReceipt(txHash), 'getReceipt', timeout, interval); return this.receipt; } diff --git a/yarn-project/end-to-end/src/create_aztec_rpc_client.ts b/yarn-project/end-to-end/src/create_aztec_rpc_client.ts index e3ade5aa636f..ce1edbce1e62 100644 --- a/yarn-project/end-to-end/src/create_aztec_rpc_client.ts +++ b/yarn-project/end-to-end/src/create_aztec_rpc_client.ts @@ -1,7 +1,7 @@ import { AztecNode } from '@aztec/aztec-node'; import { createAztecRPCServer } from '@aztec/aztec.js'; -export async function createAztecRPCClient(numberOfAccounts = 1, aztecNode: AztecNode) { +export async function createAztecRpcServer(numberOfAccounts = 1, aztecNode: AztecNode) { const arc = await createAztecRPCServer(aztecNode); for (let i = 0; i < numberOfAccounts; ++i) { diff --git a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts index 42283792faa5..059bea4f8e35 100644 --- a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts @@ -1,11 +1,11 @@ import { AztecNode, AztecNodeConfig } from '@aztec/aztec-node'; -import { AztecAddress, AztecRPCClient, ContractDeployer, Fr } from '@aztec/aztec.js'; +import { AztecAddress, AztecRPCServer, ContractDeployer, Fr } from '@aztec/aztec.js'; import { EthAddress } from '@aztec/ethereum.js/eth_address'; import { EthereumRpc } from '@aztec/ethereum.js/eth_rpc'; import { WalletProvider } from '@aztec/ethereum.js/provider'; -import { createDebugLogger } from '@aztec/foundation'; +import { createDebugLogger, randomBytes } from '@aztec/foundation'; import { TestContractAbi } from '@aztec/noir-contracts/examples'; -import { createAztecRPCClient } from './create_aztec_rpc_client.js'; +import { createAztecRpcServer } from './create_aztec_rpc_client.js'; import { createProvider, deployRollupContract, deployYeeterContract } from './deploy_l1_contracts.js'; const ETHEREUM_HOST = 'http://localhost:8545'; @@ -25,6 +25,7 @@ const createAztecNode = async ( retryIntervalMs: 1000, requiredConfirmations: 1, transactionPollingInterval: 1000, + archiverPollingInterval: 1000, }; return await AztecNode.createAndSync(config); }; @@ -34,7 +35,7 @@ const logger = createDebugLogger('aztec:e2e_test'); describe('e2e_deploy_contract', () => { let provider: WalletProvider; let node: AztecNode; - let arc: AztecRPCClient; + let aztecRpcServer: AztecRPCServer; let rollupAddress: EthAddress; let yeeterAddress: EthAddress; let accounts: AztecAddress[]; @@ -43,20 +44,21 @@ describe('e2e_deploy_contract', () => { beforeAll(async () => { provider = createProvider(ETHEREUM_HOST, MNEMONIC, 1); const ethRpc = new EthereumRpc(provider); - logger('deploying contracts...'); + logger('Deploying contracts...'); rollupAddress = await deployRollupContract(provider, ethRpc); yeeterAddress = await deployYeeterContract(provider, ethRpc); - logger('deployed contracts...'); + logger('Deployed contracts...'); }); beforeEach(async () => { node = await createAztecNode(rollupAddress, yeeterAddress, ETHEREUM_HOST, provider.getPrivateKey(0)!); - arc = await createAztecRPCClient(1, node); - accounts = await arc.getAccounts(); + aztecRpcServer = await createAztecRpcServer(1, node); + accounts = await aztecRpcServer.getAccounts(); }); afterEach(async () => { await node.stop(); + await aztecRpcServer.stop(); }); /** @@ -64,11 +66,10 @@ describe('e2e_deploy_contract', () => { * https://hackmd.io/ouVCnacHQRq2o1oRc5ksNA#Interfaces-and-Responsibilities */ it('should deploy a contract', async () => { - const deployer = new ContractDeployer(abi, arc); + const deployer = new ContractDeployer(abi, aztecRpcServer); const tx = deployer.deploy().send(); logger(`Tx sent!`); const receipt = await tx.getReceipt(); - logger(`Receipt received`); expect(receipt).toEqual( expect.objectContaining({ from: accounts[0], @@ -77,10 +78,11 @@ describe('e2e_deploy_contract', () => { error: '', }), ); + logger(`Receipt received`); const contractAddress = receipt.contractAddress!; const constructor = abi.functions.find(f => f.name === 'constructor')!; - const bytecode = await arc.getCode(contractAddress); + const bytecode = await aztecRpcServer.getCode(contractAddress); expect(bytecode).toEqual(constructor.bytecode); }, 30_000); @@ -89,8 +91,8 @@ describe('e2e_deploy_contract', () => { * https://hackmd.io/-a5DjEfHTLaMBR49qy6QkA */ it.skip('should not deploy a contract with the same salt twice', async () => { - const contractAddressSalt = Fr.random(); - const deployer = new ContractDeployer(abi, arc, { contractAddressSalt }); + const contractAddressSalt = new Fr(randomBytes(32)); + const deployer = new ContractDeployer(abi, aztecRpcServer, { contractAddressSalt }); { const receipt = await deployer.deploy().send().getReceipt(); diff --git a/yarn-project/kernel-prover/src/index.ts b/yarn-project/kernel-prover/src/index.ts index e027e0fdc86f..26debc3d3a0c 100644 --- a/yarn-project/kernel-prover/src/index.ts +++ b/yarn-project/kernel-prover/src/index.ts @@ -40,7 +40,6 @@ export class KernelProver { return new NewContractData(new Fr(randomBytes(32)), new EthAddress(randomBytes(20)), createRandomFields(1)[0]); }; const newContracts = []; - console.log(`is contructor ${txRequest.functionData.isConstructor} request to ${txRequest.to.toString()}`); if (txRequest.functionData.isConstructor) { newContracts.push( new NewContractData( From 1321f5ea92b97eed07d8d51c06d2bdc75aa01500 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 14:33:19 +0000 Subject: [PATCH 04/23] Fixed merge --- yarn-project/yarn.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/yarn-project/yarn.lock b/yarn-project/yarn.lock index 740962183e91..0b31d2e9bbbc 100644 --- a/yarn-project/yarn.lock +++ b/yarn-project/yarn.lock @@ -333,6 +333,7 @@ __metadata: dependencies: "@aztec/acir-simulator": "workspace:^" "@aztec/circuits.js": "workspace:^" + "@aztec/foundation": "workspace:^" "@jest/globals": ^29.4.3 "@rushstack/eslint-patch": ^1.1.4 "@types/jest": ^29.4.0 From cf66bda94792efc015550246ef75397a18b60c71 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 14:46:15 +0000 Subject: [PATCH 05/23] Merge fix --- yarn-project/aztec-rpc/package.json | 1 - .../aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/yarn-project/aztec-rpc/package.json b/yarn-project/aztec-rpc/package.json index d731a77a6a72..d359e6d3730b 100644 --- a/yarn-project/aztec-rpc/package.json +++ b/yarn-project/aztec-rpc/package.json @@ -33,7 +33,6 @@ "@aztec/aztec-node": "workspace:^", "@aztec/foundation": "workspace:^", "@aztec/kernel-prover": "workspace:^", - "aztec/circuits.js": "workspace:^", "@aztec/p2p": "workspace:^", "sha3": "^2.1.4", "tslib": "^2.4.0" diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts index 647dd79c4f30..170e9922635b 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts @@ -178,7 +178,7 @@ export class AztecRPCServer implements AztecRPCClient { executionResult, oldRoots as any, // TODO - remove `as any` ); - const tx = new Tx(publicInputs); + const tx = new Tx(publicInputs, new UInt8Vector(Buffer.alloc(0))); const dao: TxDao = new TxDao( new TxHash(tx.txId), undefined, @@ -190,7 +190,7 @@ export class AztecRPCServer implements AztecRPCClient { ); await this.db.addOrUpdateTx(dao); // TODO I think the TX should include all the data from the publicInputs + proof - return new Tx(publicInputs, new UInt8Vector(Buffer.alloc(0))); + return tx; } public async sendTx(tx: Tx) { From 138a21de9e175c48069bc0823a153010e72f9aea Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 14:55:24 +0000 Subject: [PATCH 06/23] Merge fix --- yarn-project/acir-simulator/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/yarn-project/acir-simulator/package.json b/yarn-project/acir-simulator/package.json index f16e40391455..170a065e2d7b 100644 --- a/yarn-project/acir-simulator/package.json +++ b/yarn-project/acir-simulator/package.json @@ -29,6 +29,7 @@ "rootDir": "./src" }, "dependencies": { + "@aztec/circuits.js": "workspace:^", "tslib": "^2.4.0" }, "devDependencies": { From 22c55b29b87373e19c79155b5bb120d577a25664 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 15:01:34 +0000 Subject: [PATCH 07/23] Merge fix --- yarn-project/acir-simulator/tsconfig.dest.json | 7 ++++++- yarn-project/acir-simulator/tsconfig.json | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/yarn-project/acir-simulator/tsconfig.dest.json b/yarn-project/acir-simulator/tsconfig.dest.json index 965aaa1c433e..924fe6549e6a 100644 --- a/yarn-project/acir-simulator/tsconfig.dest.json +++ b/yarn-project/acir-simulator/tsconfig.dest.json @@ -1,4 +1,9 @@ { "extends": ".", - "exclude": ["**/*.test.*", "**/fixtures/*"] + "exclude": ["**/*.test.*", "**/fixtures/*"], + "references": [ + { + "path": "../circuits.js/tsconfig.dest.json" + } + ] } diff --git a/yarn-project/acir-simulator/tsconfig.json b/yarn-project/acir-simulator/tsconfig.json index f67ddec9fd6b..687c3e5874f2 100644 --- a/yarn-project/acir-simulator/tsconfig.json +++ b/yarn-project/acir-simulator/tsconfig.json @@ -5,5 +5,10 @@ "rootDir": "src", "tsBuildInfoFile": ".tsbuildinfo" }, + "references": [ + { + "path": "../circuits.js/tsconfig.dest.json" + } + ], "include": ["src"] } From 8ef373e9218358692d33f9277ea9b2842cd5e64e Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 28 Mar 2023 15:14:52 +0000 Subject: [PATCH 08/23] refactor: remove duplicated foundation classes --- yarn-project/acir-simulator/src/circuits.ts | 25 ------------- yarn-project/acir-simulator/src/db_oracle.ts | 10 +++++- yarn-project/acir-simulator/src/simulator.ts | 3 +- yarn-project/aztec-rpc/package.json | 1 - .../src/aztec_rpc_client/aztec_rpc_client.ts | 4 +-- .../src/aztec_rpc_server/aztec_rpc_server.ts | 18 +++++----- yarn-project/aztec-rpc/src/circuits.ts | 29 ++------------- yarn-project/aztec-rpc/src/index.ts | 3 +- .../aztec-rpc/src/key_store/key_pair.ts | 2 +- .../aztec-rpc/src/key_store/key_store.ts | 4 +-- .../aztec-rpc/src/key_store/test_key_store.ts | 3 +- .../src/synchroniser/synchroniser.ts | 3 +- .../aztec.js/src/contract/send_method.ts | 4 +-- .../contract_deployer/constructor_method.ts | 8 ++--- .../contract_deployer.test.ts | 4 +-- .../foundation/src/aztec-address/index.ts | 2 +- yarn-project/kernel-prover/src/circuits.ts | 36 ------------------- yarn-project/kernel-prover/src/index.ts | 8 +++-- yarn-project/sequencer-client/src/deps/tx.ts | 3 +- .../circuit_powered_block_builder.ts | 4 ++- 20 files changed, 51 insertions(+), 123 deletions(-) delete mode 100644 yarn-project/acir-simulator/src/circuits.ts delete mode 100644 yarn-project/kernel-prover/src/circuits.ts diff --git a/yarn-project/acir-simulator/src/circuits.ts b/yarn-project/acir-simulator/src/circuits.ts deleted file mode 100644 index 4f307c4dcb6b..000000000000 --- a/yarn-project/acir-simulator/src/circuits.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { AztecAddress, Fr, FunctionData, PrivateCircuitPublicInputs, TxContext } from '@aztec/circuits.js'; - -export class TxRequest { - constructor( - public readonly from: AztecAddress, - public readonly to: AztecAddress, - public readonly functionData: FunctionData, - public readonly args: Fr[], - public readonly txContext: TxContext, - public readonly nonce: Fr, - public readonly chainId: Fr, - ) {} - - toBuffer() { - return Buffer.alloc(0); - } -} - -export class PrivateCallStackItem { - constructor( - public readonly contractAddress: AztecAddress, - public readonly functionSelector: number, - public readonly publicInputs: PrivateCircuitPublicInputs, - ) {} -} diff --git a/yarn-project/acir-simulator/src/db_oracle.ts b/yarn-project/acir-simulator/src/db_oracle.ts index dbf7424a3948..521664e2c400 100644 --- a/yarn-project/acir-simulator/src/db_oracle.ts +++ b/yarn-project/acir-simulator/src/db_oracle.ts @@ -1,4 +1,4 @@ -import { AztecAddress, EthAddress } from '@aztec/circuits.js'; +import { AztecAddress, EthAddress, PrivateCircuitPublicInputs } from '@aztec/circuits.js'; export interface NoteLoadOracleInputs { note: Buffer; @@ -14,3 +14,11 @@ export interface DBOracle { getProvingKey(contractAddress: AztecAddress, functionSelector: string): Promise; getPortalContractAddress(contractAddress: AztecAddress): Promise; } + +export class PrivateCallStackItem { + constructor( + public readonly contractAddress: AztecAddress, + public readonly functionSelector: number, + public readonly publicInputs: PrivateCircuitPublicInputs, + ) {} +} diff --git a/yarn-project/acir-simulator/src/simulator.ts b/yarn-project/acir-simulator/src/simulator.ts index 172c34b37253..ed3cdc159c0d 100644 --- a/yarn-project/acir-simulator/src/simulator.ts +++ b/yarn-project/acir-simulator/src/simulator.ts @@ -13,8 +13,9 @@ import { PUBLIC_CALL_STACK_LENGTH, L1_MSG_STACK_LENGTH, RETURN_VALUES_LENGTH, + TxRequest, } from '@aztec/circuits.js'; -import { PrivateCallStackItem, TxRequest } from './circuits.js'; +import { PrivateCallStackItem } from './db_oracle.js'; export interface ExecutionResult { // Needed for prover diff --git a/yarn-project/aztec-rpc/package.json b/yarn-project/aztec-rpc/package.json index d731a77a6a72..d359e6d3730b 100644 --- a/yarn-project/aztec-rpc/package.json +++ b/yarn-project/aztec-rpc/package.json @@ -33,7 +33,6 @@ "@aztec/aztec-node": "workspace:^", "@aztec/foundation": "workspace:^", "@aztec/kernel-prover": "workspace:^", - "aztec/circuits.js": "workspace:^", "@aztec/p2p": "workspace:^", "sha3": "^2.1.4", "tslib": "^2.4.0" diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_client/aztec_rpc_client.ts b/yarn-project/aztec-rpc/src/aztec_rpc_client/aztec_rpc_client.ts index b8b1f7fd8af0..8e0191724372 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_client/aztec_rpc_client.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_client/aztec_rpc_client.ts @@ -1,6 +1,6 @@ -import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js'; +import { AztecAddress, EthAddress, Fr, TxRequest } from '@aztec/circuits.js'; import { Tx } from '@aztec/p2p'; -import { Signature, TxRequest } from '../circuits.js'; +import { Signature } from '../circuits.js'; import { ContractAbi } from '../noir.js'; import { TxHash, TxReceipt } from '../tx/index.js'; diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts index 647dd79c4f30..4e57516ab5de 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts @@ -1,6 +1,6 @@ import { AcirSimulator } from '@aztec/acir-simulator'; import { AztecNode } from '@aztec/aztec-node'; -import { UInt8Vector } from '@aztec/circuits.js'; +import { TxRequest, UInt8Vector } from '@aztec/circuits.js'; import { KernelProver } from '@aztec/kernel-prover'; import { Tx } from '@aztec/p2p'; import { generateFunctionSelector } from '../abi_coder/index.js'; @@ -14,13 +14,13 @@ import { OldTreeRoots, TxContext, } from '@aztec/circuits.js'; -import { ContractDao, ContractDataSource } from '../contract_data_source/index.js'; +import { ContractDao } from '../contract_data_source/index.js'; import { KeyStore } from '../key_store/index.js'; import { ContractAbi } from '../noir.js'; import { Synchroniser } from '../synchroniser/index.js'; import { TxHash } from '../tx/index.js'; -import { generateContractAddress, selectorToNumber, Signature, TxRequest, ZERO_FR } from '../circuits.js'; -import { createDebugLogger, createLogger, randomBytes } from '@aztec/foundation'; +import { generateContractAddress, selectorToNumber, Signature, ZERO_FR } from '../circuits.js'; +import { createDebugLogger, randomBytes } from '@aztec/foundation'; import { Database } from '../database/database.js'; import { TxDao } from '../database/tx_dao.js'; @@ -99,8 +99,8 @@ export class AztecRPCServer implements AztecRPCClient { contractAddress, functionData, args, - txContext, new Fr(randomBytes(Fr.SIZE_IN_BYTES)), // nonce + txContext, ZERO_FR, // chainId ); } @@ -131,8 +131,8 @@ export class AztecRPCServer implements AztecRPCClient { to, functionData, args, - txContext, new Fr(randomBytes(Fr.SIZE_IN_BYTES)), // nonce + txContext, ZERO_FR, // chainId ); } @@ -178,7 +178,7 @@ export class AztecRPCServer implements AztecRPCClient { executionResult, oldRoots as any, // TODO - remove `as any` ); - const tx = new Tx(publicInputs); + const tx = new Tx(publicInputs, new UInt8Vector(Buffer.alloc(0))); const dao: TxDao = new TxDao( new TxHash(tx.txId), undefined, @@ -189,8 +189,8 @@ export class AztecRPCServer implements AztecRPCClient { '', ); await this.db.addOrUpdateTx(dao); - // TODO I think the TX should include all the data from the publicInputs + proof - return new Tx(publicInputs, new UInt8Vector(Buffer.alloc(0))); + + return tx; } public async sendTx(tx: Tx) { diff --git a/yarn-project/aztec-rpc/src/circuits.ts b/yarn-project/aztec-rpc/src/circuits.ts index 7970fa474e30..7ad35d3c635a 100644 --- a/yarn-project/aztec-rpc/src/circuits.ts +++ b/yarn-project/aztec-rpc/src/circuits.ts @@ -1,30 +1,7 @@ -import { AztecAddress, EthAddress, Fr, FunctionData, TxContext } from '@aztec/circuits.js'; +import { AztecAddress, Fr } from '@aztec/circuits.js'; import { randomBytes } from '@aztec/foundation'; -export const ZERO_FR = new Fr(Buffer.alloc(32)); - -export class TxRequest { - constructor( - public readonly from: AztecAddress, - public readonly to: AztecAddress, - public readonly functionData: FunctionData, - public readonly args: Fr[], - public readonly txContext: TxContext, - public readonly nonce: Fr, - public readonly chainId: Fr, - ) {} - - toBuffer() { - const buf = Buffer.alloc(4); - buf.writeUInt32BE(this.functionData.functionSelector); - return Buffer.concat([ - buf, - Buffer.concat(this.args.map(x => x.toBuffer())), - this.nonce.toBuffer(), - this.chainId.toBuffer(), - ]); - } -} +export const ZERO_FR = new Fr(Buffer.alloc(Fr.SIZE_IN_BYTES)); export class Signature { public static SIZE = 64; @@ -42,7 +19,7 @@ export function generateContractAddress( args: Fr[], // functionLeaves: Fr[], ) { - return new Fr(randomBytes(32)); + return AztecAddress.random(); } export function selectorToNumber(selector: Buffer) { diff --git a/yarn-project/aztec-rpc/src/index.ts b/yarn-project/aztec-rpc/src/index.ts index 8dcb6c42c258..2e6a9769e159 100644 --- a/yarn-project/aztec-rpc/src/index.ts +++ b/yarn-project/aztec-rpc/src/index.ts @@ -8,5 +8,4 @@ export { Tx } from '@aztec/p2p'; export * from './circuits.js'; export * from './noir.js'; -export { AztecAddress } from '@aztec/circuits.js'; -export { Fr } from '@aztec/circuits.js'; +export { Fr, TxRequest, AztecAddress, EthAddress } from '@aztec/circuits.js'; diff --git a/yarn-project/aztec-rpc/src/key_store/key_pair.ts b/yarn-project/aztec-rpc/src/key_store/key_pair.ts index 08d024aa80c1..a31df0b33b69 100644 --- a/yarn-project/aztec-rpc/src/key_store/key_pair.ts +++ b/yarn-project/aztec-rpc/src/key_store/key_pair.ts @@ -11,7 +11,7 @@ export interface KeyPair { export class ConstantKeyPair implements KeyPair { public static random() { const privateKey = randomBytes(32); - const publicKey = new Fr(randomBytes(Fr.SIZE_IN_BYTES)); + const publicKey = AztecAddress.random(); return new ConstantKeyPair(publicKey, privateKey); } diff --git a/yarn-project/aztec-rpc/src/key_store/key_store.ts b/yarn-project/aztec-rpc/src/key_store/key_store.ts index 61a825410d59..d3b15f1f8d34 100644 --- a/yarn-project/aztec-rpc/src/key_store/key_store.ts +++ b/yarn-project/aztec-rpc/src/key_store/key_store.ts @@ -1,5 +1,5 @@ -import { AztecAddress } from '@aztec/circuits.js'; -import { Signature, TxRequest } from '../circuits.js'; +import { AztecAddress, TxRequest } from '@aztec/circuits.js'; +import { Signature } from '../circuits.js'; export interface KeyStore { addAccount(): Promise; diff --git a/yarn-project/aztec-rpc/src/key_store/test_key_store.ts b/yarn-project/aztec-rpc/src/key_store/test_key_store.ts index 120d426579f7..3aa3d8a366fa 100644 --- a/yarn-project/aztec-rpc/src/key_store/test_key_store.ts +++ b/yarn-project/aztec-rpc/src/key_store/test_key_store.ts @@ -1,4 +1,5 @@ -import { TxRequest, ZERO_FR } from '../circuits.js'; +import { TxRequest } from '@aztec/circuits.js'; +import { ZERO_FR } from '../circuits.js'; import { ConstantKeyPair, KeyPair } from './key_pair.js'; import { KeyStore } from './key_store.js'; diff --git a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts index 09da3012240c..7ba16449b6ce 100644 --- a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts +++ b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts @@ -4,7 +4,6 @@ import { AccountState } from '../account_state/index.js'; import { AztecAddress, EthAddress, - Fr, KERNEL_NEW_COMMITMENTS_LENGTH, KERNEL_NEW_CONTRACTS_LENGTH, KERNEL_NEW_NULLIFIERS_LENGTH, @@ -133,7 +132,7 @@ export class Synchroniser { } i++; } - const contractAddresses = block.newContractData.map(d => d.aztecAddress).flat(); + const contractAddresses = block.newContractData.map(d => new AztecAddress(d.aztecAddress.toBuffer())).flat(); await this.db.confirmContractsDeployed(contractAddresses); this.log(`Synched block ${block.number}`); } diff --git a/yarn-project/aztec.js/src/contract/send_method.ts b/yarn-project/aztec.js/src/contract/send_method.ts index 91792420cbe7..89c3ece625e7 100644 --- a/yarn-project/aztec.js/src/contract/send_method.ts +++ b/yarn-project/aztec.js/src/contract/send_method.ts @@ -1,4 +1,4 @@ -import { AztecRPCClient, Signature, Tx, TxHash, TxRequest, ZERO_FR } from '@aztec/aztec-rpc'; +import { AztecRPCClient, Signature, Tx, TxHash, TxRequest } from '@aztec/aztec-rpc'; import { AztecAddress, Fr } from '@aztec/circuits.js'; import { ContractFunction } from './contract_function.js'; import { SentTx } from './sent_tx.js'; @@ -31,7 +31,7 @@ export class SendMethod { this.entry.encodeABI(), this.entry.encodeParameters(this.args).map(p => new Fr(p)), this.contractAddress, - from || ZERO_FR, + from || AztecAddress.ZERO, ); return this.txRequest; } diff --git a/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts b/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts index aa8d6735644d..454e3e0329db 100644 --- a/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts +++ b/yarn-project/aztec.js/src/contract_deployer/constructor_method.ts @@ -1,6 +1,6 @@ -import { AztecRPCClient, ContractAbi, ZERO_FR } from '@aztec/aztec-rpc'; +import { AztecRPCClient, ContractAbi } from '@aztec/aztec-rpc'; import { EthAddress, Fr } from '@aztec/circuits.js'; -import { randomBytes } from '@aztec/foundation'; +import { AztecAddress, randomBytes } from '@aztec/foundation'; import { ContractFunction, SendMethod, SendMethodOptions } from '../contract/index.js'; export interface ConstructorOptions extends SendMethodOptions { @@ -23,7 +23,7 @@ export class ConstructorMethod extends SendMethod { throw new Error('Cannot find constructor in the ABI.'); } - super(arc, ZERO_FR, new ContractFunction(constructorAbi), args, defaultOptions); + super(arc, AztecAddress.ZERO, new ContractFunction(constructorAbi), args, defaultOptions); } public async request(options: ConstructorOptions = {}) { @@ -33,7 +33,7 @@ export class ConstructorMethod extends SendMethod { this.entry.encodeParameters(this.args).map(p => new Fr(p)), portalContract || new EthAddress(Buffer.alloc(EthAddress.SIZE_IN_BYTES)), contractAddressSalt || new Fr(randomBytes(Fr.SIZE_IN_BYTES)), - from || ZERO_FR, + from || AztecAddress.ZERO, ); return this.txRequest; } diff --git a/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts b/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts index 82144e89b460..3d2f5e474b32 100644 --- a/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts +++ b/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts @@ -1,7 +1,7 @@ import { mock } from 'jest-mock-extended'; import { AztecRPCClient, ContractAbi, Signature, Tx, TxHash, TxReceipt, TxRequest, ZERO_FR } from '@aztec/aztec-rpc'; import { ContractDeployer } from './contract_deployer.js'; -import { EthAddress, Fr } from '@aztec/circuits.js'; +import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js'; import { randomBytes } from 'crypto'; describe('Contract Deployer', () => { @@ -22,7 +22,7 @@ describe('Contract Deployer', () => { const portalContract = new EthAddress(randomBytes(EthAddress.SIZE_IN_BYTES)); const contractAddressSalt = new Fr(randomBytes(Fr.SIZE_IN_BYTES)); - const account = new Fr(randomBytes(Fr.SIZE_IN_BYTES)); + const account = AztecAddress.random(); const mockTxRequest = { type: 'TxRequest' } as any as TxRequest; const mockSignature = { type: 'Signature' } as any as Signature; diff --git a/yarn-project/foundation/src/aztec-address/index.ts b/yarn-project/foundation/src/aztec-address/index.ts index 3ec354669638..a37258ab1cc8 100644 --- a/yarn-project/foundation/src/aztec-address/index.ts +++ b/yarn-project/foundation/src/aztec-address/index.ts @@ -15,7 +15,7 @@ export class AztecAddress { } public static random() { - return new AztecAddress(randomBytes(64)); + return new AztecAddress(randomBytes(32)); } public equals(rhs: AztecAddress) { diff --git a/yarn-project/kernel-prover/src/circuits.ts b/yarn-project/kernel-prover/src/circuits.ts deleted file mode 100644 index 2601ea59fbdf..000000000000 --- a/yarn-project/kernel-prover/src/circuits.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { AztecAddress, EthAddress, Fr, FunctionData, TxContext } from '@aztec/circuits.js'; -import { randomBytes } from '@aztec/foundation'; -//import { randomBytes } from '@aztec/foundation'; - -export class TxRequest { - constructor( - public readonly from: AztecAddress, - public readonly to: AztecAddress, - public readonly functionData: FunctionData, - public readonly args: Fr[], - public readonly txContext: TxContext, - public readonly nonce: Fr, - public readonly chainId: Fr, - ) {} - - toBuffer() { - const buf = Buffer.alloc(4); - buf.writeUInt32BE(this.functionData.functionSelector); - return Buffer.concat([ - buf, - Buffer.concat(this.args.map(x => x.toBuffer())), - this.nonce.toBuffer(), - this.chainId.toBuffer(), - ]); - } -} - -export class Signature { - public static SIZE = 64; - - public static random() { - return new EthAddress(randomBytes(Signature.SIZE)); - } - - constructor(public readonly buffer: Buffer) {} -} diff --git a/yarn-project/kernel-prover/src/index.ts b/yarn-project/kernel-prover/src/index.ts index 26debc3d3a0c..996bb87393f7 100644 --- a/yarn-project/kernel-prover/src/index.ts +++ b/yarn-project/kernel-prover/src/index.ts @@ -3,6 +3,7 @@ import { AccumulatedData, AffineElement, AggregationObject, + AztecAddress, ConstantData, EMITTED_EVENTS_LENGTH, EthAddress, @@ -20,13 +21,14 @@ import { OldTreeRoots, OptionallyRevealedData, PrivateKernelPublicInputs, + TxRequest, } from '@aztec/circuits.js'; import { randomBytes } from 'crypto'; -import { Signature, TxRequest } from './circuits.js'; + export class KernelProver { prove( txRequest: TxRequest, - txSignature: Signature, + txSignature: unknown, executionResult: ExecutionResult, oldRoots: OldTreeRoots, ): Promise<{ publicInputs: PrivateKernelPublicInputs; proof: Buffer }> { @@ -37,7 +39,7 @@ export class KernelProver { .map(() => new Fr(randomBytes(32))); }; const createRandomContractData = () => { - return new NewContractData(new Fr(randomBytes(32)), new EthAddress(randomBytes(20)), createRandomFields(1)[0]); + return new NewContractData(AztecAddress.random(), new EthAddress(randomBytes(20)), createRandomFields(1)[0]); }; const newContracts = []; if (txRequest.functionData.isConstructor) { diff --git a/yarn-project/sequencer-client/src/deps/tx.ts b/yarn-project/sequencer-client/src/deps/tx.ts index c899adf4c71b..5cf22c4b4d2f 100644 --- a/yarn-project/sequencer-client/src/deps/tx.ts +++ b/yarn-project/sequencer-client/src/deps/tx.ts @@ -1,6 +1,7 @@ import { AffineElement, AggregationObject, + AztecAddress, ConstantData, ContractDeploymentData, EMITTED_EVENTS_LENGTH, @@ -39,7 +40,7 @@ function makeEmptyEthAddress() { } export function makeEmptyNewContractData(): NewContractData { - return new NewContractData(frZero(), makeEmptyEthAddress(), frZero()); + return new NewContractData(AztecAddress.ZERO, makeEmptyEthAddress(), frZero()); } export function makeEmptyAggregationObject(): AggregationObject { diff --git a/yarn-project/sequencer-client/src/sequencer/circuit_powered_block_builder.ts b/yarn-project/sequencer-client/src/sequencer/circuit_powered_block_builder.ts index f63d2b79a165..265b3743a268 100644 --- a/yarn-project/sequencer-client/src/sequencer/circuit_powered_block_builder.ts +++ b/yarn-project/sequencer-client/src/sequencer/circuit_powered_block_builder.ts @@ -87,7 +87,9 @@ export class CircuitPoweredBlockBuilder { newCommitments: tx.data.end.newCommitments, newNullifiers: tx.data.end.newNullifiers, newContracts: tx.data.end.newContracts.map(x => x.functionTreeRoot), - newContractData: tx.data.end.newContracts.map(n => new ContractData(n.contractAddress, n.portalContractAddress)), + newContractData: tx.data.end.newContracts.map( + n => new ContractData(new Fr(n.contractAddress.toBuffer()), n.portalContractAddress), + ), }); return l2block; } From 3d3e390a0c7251657900270b26930b3873a7f39a Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 15:29:40 +0000 Subject: [PATCH 09/23] More test fixes --- .../src/aztec_rpc_server/aztec_rpc_server.ts | 2 +- .../end-to-end/src/create_aztec_node.ts | 20 +- .../src/e2e_deploy_contract.test.ts | 22 +- .../src/e2e_zk_token_contract.test.ts | 197 ++++++++++-------- 4 files changed, 128 insertions(+), 113 deletions(-) diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts index 170e9922635b..248463f3645b 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts @@ -54,7 +54,7 @@ export class AztecRPCServer implements AztecRPCClient { } public getStorageAt(contract: AztecAddress, storageSlot: Fr) { - return Promise.resolve(); + return Promise.resolve([[0]]); } public getCode(contract: AztecAddress, functionSelector?: Buffer) { diff --git a/yarn-project/end-to-end/src/create_aztec_node.ts b/yarn-project/end-to-end/src/create_aztec_node.ts index 7b395bdc9e64..23fd642c23f6 100644 --- a/yarn-project/end-to-end/src/create_aztec_node.ts +++ b/yarn-project/end-to-end/src/create_aztec_node.ts @@ -1,19 +1,21 @@ -import { AztecNode } from '@aztec/aztec-node'; +import { AztecNode, AztecNodeConfig } from '@aztec/aztec-node'; import { EthAddress } from '@aztec/ethereum.js/eth_address'; -export async function createAztecNode( +export const createAztecNode = async ( rollupContract: EthAddress, yeeterContract: EthAddress, rpcUrl: string, publisherPrivateKey: Buffer, -) { - return await AztecNode.createAndSync({ - rpcUrl, +) => { + const config: AztecNodeConfig = { rollupContract, yeeterContract, - retryIntervalMs: 10000, - publisherPrivateKey: publisherPrivateKey, + rpcUrl, + publisherPrivateKey, + retryIntervalMs: 1000, requiredConfirmations: 1, transactionPollingInterval: 1000, - }); -} + archiverPollingInterval: 1000, + }; + return await AztecNode.createAndSync(config); +}; diff --git a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts index 059bea4f8e35..7857fc3e9015 100644 --- a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts @@ -5,32 +5,14 @@ import { EthereumRpc } from '@aztec/ethereum.js/eth_rpc'; import { WalletProvider } from '@aztec/ethereum.js/provider'; import { createDebugLogger, randomBytes } from '@aztec/foundation'; import { TestContractAbi } from '@aztec/noir-contracts/examples'; +import { createAztecNode } from './create_aztec_node.js'; import { createAztecRpcServer } from './create_aztec_rpc_client.js'; import { createProvider, deployRollupContract, deployYeeterContract } from './deploy_l1_contracts.js'; const ETHEREUM_HOST = 'http://localhost:8545'; const MNEMONIC = 'test test test test test test test test test test test junk'; -const createAztecNode = async ( - rollupContract: EthAddress, - yeeterContract: EthAddress, - rpcUrl: string, - publisherPrivateKey: Buffer, -) => { - const config: AztecNodeConfig = { - rollupContract, - yeeterContract, - rpcUrl, - publisherPrivateKey, - retryIntervalMs: 1000, - requiredConfirmations: 1, - transactionPollingInterval: 1000, - archiverPollingInterval: 1000, - }; - return await AztecNode.createAndSync(config); -}; - -const logger = createDebugLogger('aztec:e2e_test'); +const logger = createDebugLogger('aztec:e2e_deploy_contract'); describe('e2e_deploy_contract', () => { let provider: WalletProvider; diff --git a/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts b/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts index ca3b47603923..24d68143d729 100644 --- a/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts @@ -1,83 +1,114 @@ -// import { AztecAddress, AztecRPCClient, Contract, ContractDeployer, Fr } from '@aztec/aztec.js'; -// import { ZkTokenContractAbi } from '@aztec/noir-contracts/examples'; -// import { createAztecRPCClient } from './create_aztec_rpc_client.js'; - -// describe('e2e_zk_token_contract', () => { -// let arc: AztecRPCClient; -// let accounts: AztecAddress[]; -// let contract: Contract; - -// const expectStorageSlot = async (accountIdx: number, expectedBalance: bigint) => { -// // We only generate 1 note in each test. Balance is the first field of the only note. -// // TBD - how to calculate storage slot? -// const storageSlot = Fr.ZERO; -// const [[balance]] = await arc.getStorageAt(contract.address, storageSlot); -// console.log(`Account ${accountIdx} balance: ${balance}`); -// expect(balance).toBe(expectedBalance); -// }; - -// const expectBalance = async (accountIdx: number, expectedBalance: bigint) => { -// const balance = await contract.methods.getBalance().call({ from: accounts[accountIdx] }); -// console.log(`Account ${accountIdx} balance: ${balance}`); -// expect(balance).toBe(expectedBalance); -// }; - -// const deployContract = async (initialBalance = 0n) => { -// const deployer = new ContractDeployer(ZkTokenContractAbi, arc); -// const receipt = await deployer.deploy(initialBalance).send().getReceipt(); -// return new Contract(receipt.contractAddress!, ZkTokenContractAbi, arc); -// }; - -// beforeEach(async () => { -// arc = await createAztecRPCClient(2); -// accounts = await arc.getAccounts(); -// }); - -// /** -// * Milestone 1.3 -// * https://hackmd.io/AG5rb9DyTRu3y7mBptWauA -// */ -// it.skip('should deploy zk token contract with initial token minted to the account', async () => { -// const initialBalance = 987n; -// await deployContract(initialBalance); -// await expectStorageSlot(0, initialBalance); -// await expectStorageSlot(1, 0n); -// }); - -// /** -// * Milestone 1.4 -// */ -// it.skip('should call mint and increase balance', async () => { -// const mintAmount = 65n; - -// await deployContract(); - -// await expectStorageSlot(0, 0n); -// await expectStorageSlot(1, 0n); - -// const receipt = await contract.methods.mint(mintAmount).send({ from: accounts[1] }).getReceipt(); -// expect(receipt.status).toBe(true); - -// await expectStorageSlot(0, 0n); -// await expectStorageSlot(1, mintAmount); -// }); - -// /** -// * Milestone 1.5 -// */ -// it.skip('should call transfer and increase balance of another account', async () => { -// const initialBalance = 987n; -// const transferAmount = 654n; - -// await deployContract(initialBalance); - -// await expectBalance(0, initialBalance); -// await expectBalance(1, 0n); - -// const receipt = await contract.methods.transfer(accounts[1]).send({ from: accounts[0] }).getReceipt(); -// expect(receipt.status).toBe(true); - -// await expectBalance(0, initialBalance - transferAmount); -// await expectBalance(1, transferAmount); -// }); -// }); +import { AztecNode } from '@aztec/aztec-node'; +import { AztecAddress, AztecRPCServer, Contract, ContractDeployer, Fr, ZERO_FR } from '@aztec/aztec.js'; +import { EthAddress } from '@aztec/ethereum.js/eth_address'; +import { EthereumRpc } from '@aztec/ethereum.js/eth_rpc'; +import { WalletProvider } from '@aztec/ethereum.js/provider'; +import { createDebugLogger } from '@aztec/foundation'; +import { ZkTokenContractAbi } from '@aztec/noir-contracts/examples'; +import { createAztecNode } from './create_aztec_node.js'; +import { createAztecRpcServer } from './create_aztec_rpc_client.js'; +import { createProvider, deployRollupContract, deployYeeterContract } from './deploy_l1_contracts.js'; + +const ETHEREUM_HOST = 'http://localhost:8545'; +const MNEMONIC = 'test test test test test test test test test test test junk'; + +const logger = createDebugLogger('aztec:e2e_zk_token_contract'); + +describe('e2e_zk_token_contract', () => { + let provider: WalletProvider; + let node: AztecNode; + let aztecRpcServer: AztecRPCServer; + let rollupAddress: EthAddress; + let yeeterAddress: EthAddress; + let accounts: AztecAddress[]; + let contract: Contract; + + beforeAll(async () => { + provider = createProvider(ETHEREUM_HOST, MNEMONIC, 1); + const ethRpc = new EthereumRpc(provider); + logger('Deploying contracts...'); + rollupAddress = await deployRollupContract(provider, ethRpc); + yeeterAddress = await deployYeeterContract(provider, ethRpc); + logger('Deployed contracts...'); + }); + + beforeEach(async () => { + node = await createAztecNode(rollupAddress, yeeterAddress, ETHEREUM_HOST, provider.getPrivateKey(0)!); + aztecRpcServer = await createAztecRpcServer(1, node); + accounts = await aztecRpcServer.getAccounts(); + }); + + afterEach(async () => { + await node.stop(); + await aztecRpcServer.stop(); + }); + + const expectStorageSlot = async (accountIdx: number, expectedBalance: bigint) => { + // We only generate 1 note in each test. Balance is the first field of the only note. + // TBD - how to calculate storage slot? + const storageSlot = ZERO_FR; + const [[balance]] = await aztecRpcServer.getStorageAt(contract.address, storageSlot); + logger(`Account ${accountIdx} balance: ${balance}`); + expect(balance).toBe(expectedBalance); + }; + + const expectBalance = async (accountIdx: number, expectedBalance: bigint) => { + const balance = await contract.methods.getBalance().call({ from: accounts[accountIdx] }); + logger(`Account ${accountIdx} balance: ${balance}`); + expect(balance).toBe(expectedBalance); + }; + + const deployContract = async (initialBalance = 0n) => { + const deployer = new ContractDeployer(ZkTokenContractAbi, aztecRpcServer); + const receipt = await deployer.deploy(initialBalance).send().getReceipt(); + return new Contract(receipt.contractAddress!, ZkTokenContractAbi, aztecRpcServer); + }; + + /** + * Milestone 1.3 + * https://hackmd.io/AG5rb9DyTRu3y7mBptWauA + */ + it.skip('should deploy zk token contract with initial token minted to the account', async () => { + const initialBalance = 987n; + await deployContract(initialBalance); + await expectStorageSlot(0, initialBalance); + await expectStorageSlot(1, 0n); + }); + + /** + * Milestone 1.4 + */ + it.skip('should call mint and increase balance', async () => { + const mintAmount = 65n; + + await deployContract(); + + await expectStorageSlot(0, 0n); + await expectStorageSlot(1, 0n); + + const receipt = await contract.methods.mint(mintAmount).send({ from: accounts[1] }).getReceipt(); + expect(receipt.status).toBe(true); + + await expectStorageSlot(0, 0n); + await expectStorageSlot(1, mintAmount); + }); + + /** + * Milestone 1.5 + */ + it.skip('should call transfer and increase balance of another account', async () => { + const initialBalance = 987n; + const transferAmount = 654n; + + await deployContract(initialBalance); + + await expectBalance(0, initialBalance); + await expectBalance(1, 0n); + + const receipt = await contract.methods.transfer(accounts[1]).send({ from: accounts[0] }).getReceipt(); + expect(receipt.status).toBe(true); + + await expectBalance(0, initialBalance - transferAmount); + await expectBalance(1, transferAmount); + }); +}); From 333daaf3e5876131c5a6f542e82493599e6d1ed7 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 17:54:31 +0000 Subject: [PATCH 10/23] Fix build attempt --- yarn-project/circuits.js/tsconfig.dest.json | 7 ++++++- yarn-project/circuits.js/tsconfig.json | 7 ++++++- .../end-to-end/jest.integration.config.json | 13 +++++++++++++ yarn-project/end-to-end/package.json | 5 ++++- yarn-project/end-to-end/scripts/start_e2e.sh | 2 +- yarn-project/yarn.lock | 2 ++ 6 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 yarn-project/end-to-end/jest.integration.config.json diff --git a/yarn-project/circuits.js/tsconfig.dest.json b/yarn-project/circuits.js/tsconfig.dest.json index 08a9c1ff256e..2baca8002657 100644 --- a/yarn-project/circuits.js/tsconfig.dest.json +++ b/yarn-project/circuits.js/tsconfig.dest.json @@ -1,4 +1,9 @@ { "extends": "./tsconfig.json", - "exclude": ["**/*.test.*", "**/fixtures/*"] + "exclude": ["**/*.test.*", "**/fixtures/*"], + "references": [ + { + "path": "../foundation/tsconfig.dest.json" + } + ] } diff --git a/yarn-project/circuits.js/tsconfig.json b/yarn-project/circuits.js/tsconfig.json index 41d839cd823e..7feb7e3a6265 100644 --- a/yarn-project/circuits.js/tsconfig.json +++ b/yarn-project/circuits.js/tsconfig.json @@ -18,5 +18,10 @@ "rootDir": "src", "tsBuildInfoFile": ".tsbuildinfo" }, - "include": ["src"] + "include": ["src"], + "references": [ + { + "path": "../foundation/tsconfig.dest.json" + } + ] } diff --git a/yarn-project/end-to-end/jest.integration.config.json b/yarn-project/end-to-end/jest.integration.config.json new file mode 100644 index 000000000000..d9dc9cf2dbb9 --- /dev/null +++ b/yarn-project/end-to-end/jest.integration.config.json @@ -0,0 +1,13 @@ +{ + "preset": "ts-jest/presets/default-esm", + "globals": { + "ts-jest": { + "useESM": true + } + }, + "moduleNameMapper": { + "^(\\.{1,2}/.*)\\.js$": "$1" + }, + "testRegex": "./src/.*\\.test\\.ts$", + "rootDir": "./src" +} diff --git a/yarn-project/end-to-end/package.json b/yarn-project/end-to-end/package.json index 424af5235716..e6d9863a59df 100644 --- a/yarn-project/end-to-end/package.json +++ b/yarn-project/end-to-end/package.json @@ -7,7 +7,9 @@ "build": "yarn clean && tsc -b tsconfig.json", "clean": "rm -rf ./dest .tsbuildinfo", "formatting": "run -T prettier --check ./src && run -T eslint --max-warnings 0 ./src", - "test": "./scripts/start_e2e.sh" + "test": "./scripts/start_e2e.sh", + "test:integration": "concurrently -k -s first -c reset,dim -n test,anvil \"yarn test:integration:run\" \"anvil\"", + "test:integration:run": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --no-cache --runInBand --config jest.integration.config.json" }, "jest": { "preset": "ts-jest/presets/default-esm", @@ -37,6 +39,7 @@ "@jest/globals": "^29.4.3", "@rushstack/eslint-patch": "^1.1.4", "@types/node": "^18.7.23", + "concurrently": "^7.6.0", "ts-node": "^10.9.1" } } diff --git a/yarn-project/end-to-end/scripts/start_e2e.sh b/yarn-project/end-to-end/scripts/start_e2e.sh index ef6959c4e38b..617982ba7e80 100755 --- a/yarn-project/end-to-end/scripts/start_e2e.sh +++ b/yarn-project/end-to-end/scripts/start_e2e.sh @@ -2,4 +2,4 @@ set -eu export NODE_NO_WARNINGS=1 -node ${NODE_ARGS-} --openssl-legacy-provider --experimental-vm-modules $(yarn bin jest) --no-cache --runInBand $1 \ No newline at end of file +node ${NODE_ARGS-} --openssl-legacy-provider --experimental-vm-modules $(yarn bin jest) --no-cache --runInBand --passWithNoTests $@ \ No newline at end of file diff --git a/yarn-project/yarn.lock b/yarn-project/yarn.lock index 0b31d2e9bbbc..053e52bbbb32 100644 --- a/yarn-project/yarn.lock +++ b/yarn-project/yarn.lock @@ -19,6 +19,7 @@ __metadata: version: 0.0.0-use.local resolution: "@aztec/acir-simulator@workspace:acir-simulator" dependencies: + "@aztec/circuits.js": "workspace:^" "@jest/globals": ^29.4.3 "@noir-lang/noir-source-resolver": ^1.1.0 "@noir-lang/noir_wasm": 0.3.2-29b1f7df @@ -227,6 +228,7 @@ __metadata: "@rushstack/eslint-patch": ^1.1.4 "@types/jest": ^29.4.0 "@types/node": ^18.7.23 + concurrently: ^7.6.0 jest: ^28.1.3 ts-jest: ^28.0.7 ts-node: ^10.9.1 From 28b80485826e188c0c720b350269002bb9597edd Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 17:59:33 +0000 Subject: [PATCH 11/23] Attempt build fix --- yarn-project/end-to-end/src/e2e_deploy_contract.test.ts | 2 +- yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts index 7857fc3e9015..ff45a795027c 100644 --- a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts @@ -1,4 +1,4 @@ -import { AztecNode, AztecNodeConfig } from '@aztec/aztec-node'; +import { AztecNode } from '@aztec/aztec-node'; import { AztecAddress, AztecRPCServer, ContractDeployer, Fr } from '@aztec/aztec.js'; import { EthAddress } from '@aztec/ethereum.js/eth_address'; import { EthereumRpc } from '@aztec/ethereum.js/eth_rpc'; diff --git a/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts b/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts index 24d68143d729..18b38c0afda3 100644 --- a/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_zk_token_contract.test.ts @@ -1,5 +1,5 @@ import { AztecNode } from '@aztec/aztec-node'; -import { AztecAddress, AztecRPCServer, Contract, ContractDeployer, Fr, ZERO_FR } from '@aztec/aztec.js'; +import { AztecAddress, AztecRPCServer, Contract, ContractDeployer, ZERO_FR } from '@aztec/aztec.js'; import { EthAddress } from '@aztec/ethereum.js/eth_address'; import { EthereumRpc } from '@aztec/ethereum.js/eth_rpc'; import { WalletProvider } from '@aztec/ethereum.js/provider'; From 6514017f094243d444e19764828217455a6563ee Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 18:50:15 +0000 Subject: [PATCH 12/23] Attempt test fix --- yarn-project/end-to-end/scripts/docker-compose.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/yarn-project/end-to-end/scripts/docker-compose.yml b/yarn-project/end-to-end/scripts/docker-compose.yml index 025e24c74dfb..fd62c537a633 100644 --- a/yarn-project/end-to-end/scripts/docker-compose.yml +++ b/yarn-project/end-to-end/scripts/docker-compose.yml @@ -1,5 +1,11 @@ version: '3' services: + fork: + image: ghcr.io/foundry-rs/foundry:nightly-a44aa13cfc23491ba32aaedc093e9488c1a6db43 + entrypoint: 'anvil -p 8545 --host 0.0.0.0 --chain-id 1337' + ports: + - '8545:8545' + end-to-end: image: aztecprotocol/end-to-end:latest command: ${TEST:-./src/e2e_deploy_contract.test.ts} From 86b49ed030695e2d9ca5ca88390c4218d020c7de Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 18:56:49 +0000 Subject: [PATCH 13/23] Attempt fix --- yarn-project/end-to-end/scripts/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/end-to-end/scripts/docker-compose.yml b/yarn-project/end-to-end/scripts/docker-compose.yml index fd62c537a633..641a3b1582be 100644 --- a/yarn-project/end-to-end/scripts/docker-compose.yml +++ b/yarn-project/end-to-end/scripts/docker-compose.yml @@ -2,7 +2,7 @@ version: '3' services: fork: image: ghcr.io/foundry-rs/foundry:nightly-a44aa13cfc23491ba32aaedc093e9488c1a6db43 - entrypoint: 'anvil -p 8545 --host 0.0.0.0 --chain-id 1337' + entrypoint: 'anvil -p 8545 --host 127.0.0.1 --chain-id 1337' ports: - '8545:8545' From d517d31495ed1a90cfa0575945573ebf803d34c8 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 19:04:23 +0000 Subject: [PATCH 14/23] More fixes --- yarn-project/end-to-end/scripts/docker-compose.yml | 2 ++ yarn-project/end-to-end/src/e2e_deploy_contract.test.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/yarn-project/end-to-end/scripts/docker-compose.yml b/yarn-project/end-to-end/scripts/docker-compose.yml index 641a3b1582be..912ab4f2dafc 100644 --- a/yarn-project/end-to-end/scripts/docker-compose.yml +++ b/yarn-project/end-to-end/scripts/docker-compose.yml @@ -8,4 +8,6 @@ services: end-to-end: image: aztecprotocol/end-to-end:latest + environment: + ETHEREUM_HOST: http://fork:8545 command: ${TEST:-./src/e2e_deploy_contract.test.ts} diff --git a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts index ff45a795027c..2bc7732379f5 100644 --- a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts @@ -9,7 +9,7 @@ import { createAztecNode } from './create_aztec_node.js'; import { createAztecRpcServer } from './create_aztec_rpc_client.js'; import { createProvider, deployRollupContract, deployYeeterContract } from './deploy_l1_contracts.js'; -const ETHEREUM_HOST = 'http://localhost:8545'; +const { ETHEREUM_HOST = 'http://localhost:8545' } = process.env; const MNEMONIC = 'test test test test test test test test test test test junk'; const logger = createDebugLogger('aztec:e2e_deploy_contract'); From 046f51699730687f2006175b9234764c31a5fc19 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 19:12:47 +0000 Subject: [PATCH 15/23] Fix attempt --- yarn-project/end-to-end/scripts/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/end-to-end/scripts/docker-compose.yml b/yarn-project/end-to-end/scripts/docker-compose.yml index 912ab4f2dafc..7888d14116e9 100644 --- a/yarn-project/end-to-end/scripts/docker-compose.yml +++ b/yarn-project/end-to-end/scripts/docker-compose.yml @@ -2,7 +2,7 @@ version: '3' services: fork: image: ghcr.io/foundry-rs/foundry:nightly-a44aa13cfc23491ba32aaedc093e9488c1a6db43 - entrypoint: 'anvil -p 8545 --host 127.0.0.1 --chain-id 1337' + entrypoint: 'anvil -p 8545 --host 0.0.0.0 --chain-id 1337' ports: - '8545:8545' From 21d29ef5f4960f640d760cb94913f34684117034 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 19:14:45 +0000 Subject: [PATCH 16/23] TxRequest fix --- .../aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts index 8bdbec596ef7..66d727c938ff 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts @@ -1,6 +1,6 @@ import { AcirSimulator } from '@aztec/acir-simulator'; import { AztecNode } from '@aztec/aztec-node'; -import { TxRequest, UInt8Vector } from '@aztec/circuits.js'; +import { ARGS_LENGTH, TxRequest, UInt8Vector } from '@aztec/circuits.js'; import { KernelProver } from '@aztec/kernel-prover'; import { Tx } from '@aztec/p2p'; import { generateFunctionSelector } from '../abi_coder/index.js'; @@ -94,11 +94,17 @@ export class AztecRPCServer implements AztecRPCClient { const contractAddress = generateContractAddress(fromAddress, contractAddressSalt, args); await this.db.addContract(contractAddress, portalContract, abi, false); + const txRequestArgs = args.concat( + Array(ARGS_LENGTH - args.length) + .fill(0) + .map(() => new Fr(0)), + ); + return new TxRequest( fromAddress, contractAddress, functionData, - args, + txRequestArgs, new Fr(randomBytes(Fr.SIZE_IN_BYTES)), // nonce txContext, ZERO_FR, // chainId From f2681dc23be6f5261143ae2d08b305a798a571cd Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 19:27:17 +0000 Subject: [PATCH 17/23] Merge fixes --- yarn-project/aztec.js/package.json | 2 +- yarn-project/circuits.js/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/yarn-project/aztec.js/package.json b/yarn-project/aztec.js/package.json index c0ac7b65354c..eef2a1985592 100644 --- a/yarn-project/aztec.js/package.json +++ b/yarn-project/aztec.js/package.json @@ -45,4 +45,4 @@ "ts-node": "^10.9.1", "typescript": "^4.9.5" } -} \ No newline at end of file +} diff --git a/yarn-project/circuits.js/package.json b/yarn-project/circuits.js/package.json index 307dfb750a53..806b14990498 100644 --- a/yarn-project/circuits.js/package.json +++ b/yarn-project/circuits.js/package.json @@ -55,4 +55,4 @@ "ts-node": "^10.9.1", "typescript": "^4.9.5" } -} \ No newline at end of file +} From 038a5c219a3a282c1bb086be346146853b0a5462 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 19:33:17 +0000 Subject: [PATCH 18/23] Merge fixes --- .../aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts index 2ad0a1284da2..b07deef2ff6a 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts @@ -9,7 +9,6 @@ import { AztecAddress, ContractDeploymentData, EthAddress, - Fr, FunctionData, OldTreeRoots, TxContext, @@ -19,7 +18,7 @@ import { KeyStore } from '../key_store/index.js'; import { ContractAbi } from '../noir.js'; import { Synchroniser } from '../synchroniser/index.js'; import { generateContractAddress, selectorToNumber, Signature } from '../circuits.js'; -import { createDebugLogger, randomBytes } from '@aztec/foundation'; +import { createDebugLogger, randomBytes, Fr } from '@aztec/foundation'; import { Database } from '../database/database.js'; import { TxDao } from '../database/tx_dao.js'; @@ -106,7 +105,7 @@ export class AztecRPCServer implements AztecRPCClient { txRequestArgs, new Fr(randomBytes(Fr.SIZE_IN_BYTES)), // nonce txContext, - ZERO_FR, // chainId + Fr.ZERO, // chainId ); } From beddf9df7c543d9d42b6fe0c6507f1747ed04837 Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 19:35:34 +0000 Subject: [PATCH 19/23] Merge fix --- yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts index 7d9cceca28eb..9e1fc032ff3b 100644 --- a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts +++ b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts @@ -12,6 +12,7 @@ import { import { Database, TxDao } from '../database/index.js'; import { ContractAbi } from '../noir.js'; import { L2Block } from '@aztec/l2-block'; +import { keccak } from '@aztec/foundation'; export class Synchroniser { private runningPromise?: Promise; @@ -123,9 +124,9 @@ export class Synchroniser { .map(x => x.toBuffer()), ].flat(), ); - const txDao: TxDao | undefined = await this.db.getTx(new TxHash(keccak256(dataToHash))); + const txDao: TxDao | undefined = await this.db.getTx(new TxHash(keccak(dataToHash))); if (txDao !== undefined) { - txDao.blockHash = keccak256(block.encode()); + txDao.blockHash = keccak(block.encode()); txDao.blockNumber = block.number; await this.db.addOrUpdateTx(txDao); } From 82cb057a0b8de60daebe4e2a354ad34fbaf7e30d Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 19:44:20 +0000 Subject: [PATCH 20/23] Merge fix --- .../src/aztec_rpc_server/aztec_rpc_server.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts index b07deef2ff6a..b5d509ae9a5c 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts @@ -17,7 +17,7 @@ import { ContractDao } from '../contract_data_source/index.js'; import { KeyStore } from '../key_store/index.js'; import { ContractAbi } from '../noir.js'; import { Synchroniser } from '../synchroniser/index.js'; -import { generateContractAddress, selectorToNumber, Signature } from '../circuits.js'; +import { generateContractAddress, selectorToNumber, Signature, ZERO_FR } from '../circuits.js'; import { createDebugLogger, randomBytes, Fr } from '@aztec/foundation'; import { Database } from '../database/database.js'; import { TxDao } from '../database/tx_dao.js'; @@ -77,8 +77,8 @@ export class AztecRPCServer implements AztecRPCClient { true, ); - const constructorVkHash = Fr.ZERO; - const functionTreeRoot = Fr.ZERO; + const constructorVkHash = ZERO_FR; + const functionTreeRoot = ZERO_FR; const contractDeploymentData = new ContractDeploymentData( constructorVkHash, functionTreeRoot, @@ -87,7 +87,7 @@ export class AztecRPCServer implements AztecRPCClient { ); const txContext = new TxContext(false, false, true, contractDeploymentData); - const fromAddress = from.toBuffer().equals(Fr.ZERO.toBuffer()) ? (await this.keyStore.getAccounts())[0] : from; + const fromAddress = from.toBuffer().equals(ZERO_FR.toBuffer()) ? (await this.keyStore.getAccounts())[0] : from; const contractAddress = generateContractAddress(fromAddress, contractAddressSalt, args); await this.db.addContract(contractAddress, portalContract, abi, false); @@ -105,7 +105,7 @@ export class AztecRPCServer implements AztecRPCClient { txRequestArgs, new Fr(randomBytes(Fr.SIZE_IN_BYTES)), // nonce txContext, - Fr.ZERO, // chainId + ZERO_FR, // chainId ); } @@ -127,7 +127,7 @@ export class AztecRPCServer implements AztecRPCClient { false, false, true, - new ContractDeploymentData(Fr.ZERO, Fr.ZERO, Fr.ZERO, new EthAddress(Buffer.alloc(EthAddress.SIZE_IN_BYTES))), + new ContractDeploymentData(ZERO_FR, ZERO_FR, ZERO_FR, new EthAddress(Buffer.alloc(EthAddress.SIZE_IN_BYTES))), ); return new TxRequest( @@ -137,7 +137,7 @@ export class AztecRPCServer implements AztecRPCClient { args, new Fr(randomBytes(Fr.SIZE_IN_BYTES)), // nonce txContext, - Fr.ZERO, // chainId + ZERO_FR, // chainId ); } @@ -148,7 +148,7 @@ export class AztecRPCServer implements AztecRPCClient { public async createTx(txRequest: TxRequest, signature: Signature) { let contractAddress; - if (txRequest.to.toBuffer().equals(Fr.ZERO.toBuffer())) { + if (txRequest.to.toBuffer().equals(ZERO_FR.toBuffer())) { contractAddress = generateContractAddress( txRequest.from, txRequest.txContext.contractDeploymentData.contractAddressSalt, @@ -169,7 +169,7 @@ export class AztecRPCServer implements AztecRPCClient { const functionDao = this.findFunction(contract, selector); - const oldRoots = new OldTreeRoots(Fr.ZERO, Fr.ZERO, Fr.ZERO, Fr.ZERO); // TODO - get old roots from the database/node + const oldRoots = new OldTreeRoots(ZERO_FR, ZERO_FR, ZERO_FR, ZERO_FR); // TODO - get old roots from the database/node const executionResult = await this.acirSimulator.run( txRequest, Buffer.from(functionDao.bytecode, 'base64'), From e1e061f8cec0ddddd6c70b401272ed220eed5f4c Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 19:48:34 +0000 Subject: [PATCH 21/23] Merge fix --- .../aztec.js/src/contract_deployer/contract_deployer.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts b/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts index e5b6f05ebc56..0172372e5603 100644 --- a/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts +++ b/yarn-project/aztec.js/src/contract_deployer/contract_deployer.test.ts @@ -97,10 +97,10 @@ describe('Contract Deployer', () => { [], new EthAddress(Buffer.alloc(EthAddress.SIZE_IN_BYTES)), // portalContract expect.anything(), // contractAddressSalt - ZERO_FR, // account + new Fr(0), // account ); const defaultContractAddressSalt = arc.createDeploymentTxRequest.mock.calls[0][3]; expect(defaultContractAddressSalt).not.toEqual(contractAddressSalt); - expect(defaultContractAddressSalt).not.toEqual(ZERO_FR); + expect(defaultContractAddressSalt).not.toEqual(new Fr(0)); }); }); From dc7855674aa5166b2379da0a7ccfa1fdf30c70cf Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 19:54:09 +0000 Subject: [PATCH 22/23] Enable test debug --- yarn-project/end-to-end/scripts/docker-compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/yarn-project/end-to-end/scripts/docker-compose.yml b/yarn-project/end-to-end/scripts/docker-compose.yml index 7888d14116e9..4419208947b0 100644 --- a/yarn-project/end-to-end/scripts/docker-compose.yml +++ b/yarn-project/end-to-end/scripts/docker-compose.yml @@ -9,5 +9,6 @@ services: end-to-end: image: aztecprotocol/end-to-end:latest environment: + DEBUG: 'aztec:*' ETHEREUM_HOST: http://fork:8545 command: ${TEST:-./src/e2e_deploy_contract.test.ts} From f7c467fc15206958a413640df679613fa353296d Mon Sep 17 00:00:00 2001 From: PhilWindle Date: Tue, 28 Mar 2023 19:59:31 +0000 Subject: [PATCH 23/23] Logging --- yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts index 9e1fc032ff3b..48dd11f6e03c 100644 --- a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts +++ b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts @@ -20,7 +20,11 @@ export class Synchroniser { private interruptableSleep = new InterruptableSleep(); private running = false; - constructor(private node: AztecNode, private db: Database, private log = createDebugLogger('aztec:synchroniser')) {} + constructor( + private node: AztecNode, + private db: Database, + private log = createDebugLogger('aztec:aztec_rps_synchroniser'), + ) {} public start(from = 1, take = 1, retryInterval = 1000) { if (this.running) {