Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: contract class must be registered before deployment #10949

Merged
merged 25 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,9 @@ contract ContractClassRegisterer {
LogHash { value: log_hash, counter, length: 40 + (N as Field) * 32 },
);
}

#[private]
fn assert_class_id_is_registered(contract_class_id: ContractClassId) {
context.push_nullifier_read_request(contract_class_id.to_field());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ type = "contract"

[dependencies]
aztec = { path = "../../../aztec-nr/aztec" }
contract_class_registerer = { path = "../contract_class_registerer_contract" }
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ contract ContractInstanceDeployer {
use dep::aztec::macros::{events::event, functions::private};
use dep::aztec::protocol_types::{
address::{AztecAddress, PartialAddress},
constants::DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE,
constants::{DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE, REGISTERER_CONTRACT_ADDRESS},
contract_class_id::ContractClassId,
public_keys::PublicKeys,
traits::Serialize,
utils::arrays::array_concat,
};
use dep::contract_class_registerer::ContractClassRegisterer;
use std::meta::derive;

#[derive(Serialize)]
Expand Down Expand Up @@ -85,7 +86,11 @@ contract ContractInstanceDeployer {
public_keys: PublicKeys,
universal_deploy: bool,
) {
// TODO(@spalladino): assert nullifier_exists silo(contract_class_id, ContractClassRegisterer)
// contract class must be registered to deploy an instance
ContractClassRegisterer::at(REGISTERER_CONTRACT_ADDRESS)
.assert_class_id_is_registered(contract_class_id)
.call(&mut context);

let deployer = if universal_deploy {
AztecAddress::zero()
} else {
Expand Down
14 changes: 12 additions & 2 deletions yarn-project/accounts/src/testing/create_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@ export async function createAccounts(

// Prepare deployments
const accountsAndDeployments = await Promise.all(
secrets.map(async secret => {
secrets.map(async (secret, index) => {
const signingKey = deriveSigningKey(secret);
const account = getSchnorrAccount(pxe, secret, signingKey);

// only register the contract class once
let skipClassRegistration = true;
if (index === 0) {
// for the first account, check if the contract class is already registered, otherwise we should register now
if (!(await pxe.isContractClassPubliclyRegistered(account.getInstance().contractClassId))) {
skipClassRegistration = false;
}
}

const deployMethod = await account.getDeployMethod();
const provenTx = await deployMethod.prove({
contractAddressSalt: account.salt,
skipClassRegistration: true,
skipClassRegistration,
skipPublicDeployment: true,
universalDeploy: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ describe('e2e_deploy_contract deploy method', () => {
await expect(TestContract.deploy(wallet).prove(opts)).rejects.toThrow(/no function calls needed/i);
});

it('refused to deploy a contract instance whose contract class is not yet registered', async () => {
const owner = wallet.getAddress();
const opts = { skipClassRegistration: true };
logger.debug(`Trying to deploy contract instance without registering its contract class`);
await expect(StatefulTestContract.deploy(wallet, owner, owner, 42).send(opts).wait()).rejects.toThrow(
/Cannot find the leaf for nullifier/,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need better error messages :-P

);
});

it('publicly deploys and calls a public contract in the same batched call', async () => {
const owner = wallet.getAddress();
// Create a contract instance and make the PXE aware of it
Expand Down
24 changes: 22 additions & 2 deletions yarn-project/end-to-end/src/e2e_prover/e2e_prover_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,28 @@ export class FullProverTest {
await this.snapshotManager.snapshot('2_accounts', addAccounts(2, this.logger), async ({ accountKeys }, { pxe }) => {
this.keys = accountKeys;
const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], SALT));
this.wallets = await Promise.all(accountManagers.map(a => a.getWallet()));
this.accounts = await pxe.getRegisteredAccounts();
this.wallets = (await Promise.all(accountManagers.map(a => a.getWallet()))).sort((aWallet, bWallet) => {
const a = aWallet.getAddress().toBigInt();
const b = bWallet.getAddress().toBigInt();
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
this.accounts = (await pxe.getRegisteredAccounts()).sort((aAccount, bAccount) => {
const a = aAccount.address.toBigInt();
const b = bAccount.address.toBigInt();
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
this.wallets.forEach((w, i) => this.logger.verbose(`Wallet ${i} address: ${w.getAddress()}`));
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pxe.getRegisteredAccounts wasn't returning the accounts in the same order as wallets which was causing test failures because there was an assumption that wallets[0] == accounts[0]


Expand Down
32 changes: 25 additions & 7 deletions yarn-project/end-to-end/src/fixtures/snapshot_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
type CompleteAddress,
type DeployL1Contracts,
Fr,
type FunctionCall,
GrumpkinScalar,
type Logger,
type PXE,
type Wallet,
getContractClassFromArtifact,
} from '@aztec/aztec.js';
import { deployInstance, registerContractClass } from '@aztec/aztec.js/deployment';
import { type BlobSinkServer, createBlobSinkServer } from '@aztec/blob-sink/server';
Expand Down Expand Up @@ -550,13 +552,22 @@ export const addAccounts =

logger.verbose('Simulating account deployment...');
const provenTxs = await Promise.all(
accountKeys.map(async ([secretKey, signPk]) => {
accountKeys.map(async ([secretKey, signPk], index) => {
const account = getSchnorrAccount(pxe, secretKey, signPk, 1);
const deployMethod = await account.getDeployMethod();

// only register the contract class once
let skipClassRegistration = true;
if (index === 0) {
// for the first account, check if the contract class is already registered, otherwise we should register now
if (!(await pxe.isContractClassPubliclyRegistered(account.getInstance().contractClassId))) {
skipClassRegistration = false;
}
}

const deployMethod = await account.getDeployMethod();
const provenTx = await deployMethod.prove({
contractAddressSalt: account.salt,
skipClassRegistration: true,
skipClassRegistration,
skipPublicDeployment: true,
universalDeploy: true,
});
Expand Down Expand Up @@ -589,9 +600,16 @@ export async function publicDeployAccounts(
) {
const accountAddressesToDeploy = accountsToDeploy.map(a => ('address' in a ? a.address : a));
const instances = await Promise.all(accountAddressesToDeploy.map(account => sender.getContractInstance(account)));
const batch = new BatchCall(sender, [
(await registerContractClass(sender, SchnorrAccountContractArtifact)).request(),
...instances.map(instance => deployInstance(sender, instance!).request()),
]);

const contractClass = getContractClassFromArtifact(SchnorrAccountContractArtifact);
const alreadyRegistered = await sender.isContractClassPubliclyRegistered(contractClass.id);

const calls: FunctionCall[] = [];
if (!alreadyRegistered) {
calls.push((await registerContractClass(sender, SchnorrAccountContractArtifact)).request());
}
calls.push(...instances.map(instance => deployInstance(sender, instance!).request()));

const batch = new BatchCall(sender, calls);
Comment on lines -592 to +613
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't batch a call to register the class if it is already registered

await batch.send().wait({ proven: waitUntilProven });
}
Loading