Skip to content

Commit

Permalink
feat: add key registry to deployment (e2e & sandbox) (#5875)
Browse files Browse the repository at this point in the history
Resolves #5611
  • Loading branch information
sklppy88 authored Apr 25, 2024
1 parent 12bfc15 commit 0881cd3
Show file tree
Hide file tree
Showing 24 changed files with 194 additions and 45 deletions.
4 changes: 3 additions & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ library Constants {
uint256 internal constant DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631;
uint256 internal constant DEPLOYER_CONTRACT_ADDRESS =
0x1236d27f14d2934fd666beff34a0b4b746949f5d51a149eb67f908eb95092f54;
0x0097949bb96834550868230a1b6cc242d1f662f7c52946245e4e73da1b8b2165;
uint256 internal constant DEFAULT_GAS_LIMIT = 1_000_000_000;
uint256 internal constant DEFAULT_TEARDOWN_GAS_LIMIT = 100_000_000;
uint256 internal constant DEFAULT_MAX_FEE_PER_GAS = 10;
uint256 internal constant DEFAULT_INCLUSION_FEE = 0;
uint256 internal constant CANONICAL_KEY_REGISTRY_ADDRESS =
0x1585e564a60e6ec974bc151b62705292ebfc75c33341986a47fd9749cedb567e;
uint256 internal constant AZTEC_ADDRESS_LENGTH = 1;
uint256 internal constant GAS_FEES_LENGTH = 3;
uint256 internal constant GAS_LENGTH = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ contract KeyRegistry {
use dep::authwit::auth::assert_current_call_valid_authwit_public;

use dep::aztec::{
context::gas::GasOpts,
state_vars::{
SharedMutable,
Map
},
protocol_types::{
abis::function_selector::FunctionSelector,
contract_class_id::ContractClassId,
address::{
AztecAddress,
EthAddress,
PublicKeysHash,
PartialAddress,
},
Expand All @@ -21,10 +17,6 @@ contract KeyRegistry {
GENERATOR_INDEX__PUBLIC_KEYS_HASH
},
hash::poseidon2_hash,
traits::{
Serialize,
Deserialize,
}
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,16 @@ global REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_MAGIC_VALUE = 0xe7af8166354
// CONTRACT INSTANCE CONSTANTS
// sha224sum 'struct ContractInstanceDeployed'
global DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE = 0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631;
global DEPLOYER_CONTRACT_ADDRESS = 0x1236d27f14d2934fd666beff34a0b4b746949f5d51a149eb67f908eb95092f54;
global DEPLOYER_CONTRACT_ADDRESS = 0x0097949bb96834550868230a1b6cc242d1f662f7c52946245e4e73da1b8b2165;

// GAS DEFAULTS
global DEFAULT_GAS_LIMIT: u32 = 1_000_000_000;
global DEFAULT_TEARDOWN_GAS_LIMIT: u32 = 100_000_000;
global DEFAULT_MAX_FEE_PER_GAS: Field = 10;
global DEFAULT_INCLUSION_FEE: Field = 0;

global CANONICAL_KEY_REGISTRY_ADDRESS = 0x1585e564a60e6ec974bc151b62705292ebfc75c33341986a47fd9749cedb567e;

// LENGTH OF STRUCTS SERIALIZED TO FIELDS
global AZTEC_ADDRESS_LENGTH = 1;
global GAS_FEES_LENGTH: u64 = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ struct ContractData {
global default_contract = ContractData {
contract_address_salt: 0x000000000000000000000000000000000000000000000000000000000000ddd5,
artifact_hash: 0x0000000000000000000000000000000000000000000000000000000000003039,
public_bytecode_commitment: 0x129a3438653fe147133b2c274757920e37896305e7664c8c1eb380be3efd5fed,
public_bytecode_commitment: 0x0000000000000000000000000000000000000000000000000000000000000005,
private_functions_root: 0x19a3cc0b714976fb35d58b684ba36e86f82bac8b87517904a2727e5113fb4cba,
address: AztecAddress { inner: 0x2d941148ee5adeece35991d32acbcf4200742991c61990dee965bedf729d21a9 },
partial_address: PartialAddress { inner: 0x23a6933a485200a8d34b9929d61868c9635793f878d67ce86a1b1355c0ab0d47 },
contract_class_id: ContractClassId { inner: 0x0ce2a998337b1e6da1ac1d802a8bb9e10b7d705d210e61efb9642855009814a6 },
address: AztecAddress { inner: 0x24e2561e4216c843ff11bf77d4f8a68247e537980273ce54b09b505f7352f6bb },
partial_address: PartialAddress { inner: 0x27ab8475fe4647b48ffc4df7a6cc42bf1125f000ff113d9a0d6a11b952626761 },
contract_class_id: ContractClassId { inner: 0x29f4bda24f38507064a90f7505dc0381c9d83c97271c5c2e92a4261d300861bf },
public_keys_hash: PublicKeysHash { inner: 0x000000000000000000000000000000000000000000000000000000000000b26e },
salted_initialization_hash: SaltedInitializationHash { inner: 0x0b095458845137ebf1e6061c8c0ba1d907241a3b56dc1d3e73d2fea78f04a036 },
salted_initialization_hash: SaltedInitializationHash { inner: 0x25765504545d2cdaaa6544eb24bc78a3e20384452f2525669f196a1a42f45906 },
deployer: AztecAddress { inner: 0x0000000000000000000000000000000000000000000000000000000000000000 }
};

// taken from __snapshots__/noir_test_gen.test.ts.snap
global parent_contract = ContractData {
contract_address_salt: 0x0000000000000000000000000000000000000000000000000000000000001618,
artifact_hash: 0x00000000000000000000000000000000000000000000000000000000000004bc,
public_bytecode_commitment: 0x1435ed970b275bebf95de3df53f23f3d2e97c9b54cf442bb03a3fa17a0ee3cd7,
public_bytecode_commitment: 0x0000000000000000000000000000000000000000000000000000000000000005,
private_functions_root: 0x2c1c949cb226995de94b7b8b5aeaab440739f2dfeb06d358441f60932cf243a7,
address: AztecAddress { inner: 0x24692d7dbb532557c7466e8782d1fe99077e4787570414bd1a5e8fa5300caad8 },
partial_address: PartialAddress { inner: 0x127bbd73a3cf497fb2d85342571695d894985b449a9343eec55485e9cbc514f8 },
contract_class_id: ContractClassId { inner: 0x1f1f963a350e2c883cc6730c19fc5d5b47a40694d805cbb0720fa76fe295df90 },
contract_class_id: ContractClassId { inner: 0x037a09515a79a2b8ebe5139dae1ab7c433523ac1fd5631836890df2148df51c7 },
public_keys_hash: PublicKeysHash { inner: 0x00000000000000000000000000000000000000000000000000000000000011c1 },
salted_initialization_hash: SaltedInitializationHash { inner: 0x04643e65513869350552499ed3412df59540dffe3cd698203deee8900b53bcec },
deployer: AztecAddress { inner: 0x0000000000000000000000000000000000000000000000000000000000000000 }
Expand Down
42 changes: 41 additions & 1 deletion yarn-project/aztec/src/sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env -S node --no-warnings
import { type AztecNodeConfig, AztecNodeService, getConfigEnvVars } from '@aztec/aztec-node';
import { type AztecAddress, SignerlessWallet, type Wallet } from '@aztec/aztec.js';
import { AztecAddress, SignerlessWallet, type Wallet } from '@aztec/aztec.js';
import { DefaultMultiCallEntrypoint } from '@aztec/aztec.js/entrypoint';
import { type AztecNode } from '@aztec/circuit-types';
import { CANONICAL_KEY_REGISTRY_ADDRESS } from '@aztec/circuits.js';
import {
type DeployL1Contracts,
type L1ContractAddresses,
Expand Down Expand Up @@ -30,7 +31,9 @@ import {
RollupBytecode,
} from '@aztec/l1-artifacts';
import { GasTokenContract } from '@aztec/noir-contracts.js/GasToken';
import { KeyRegistryContract } from '@aztec/noir-contracts.js/KeyRegistry';
import { getCanonicalGasToken } from '@aztec/protocol-contracts/gas-token';
import { getCanonicalKeyRegistry } from '@aztec/protocol-contracts/key-registry';
import { type PXEServiceConfig, createPXEService, getPXEServiceConfig } from '@aztec/pxe';

import {
Expand Down Expand Up @@ -185,6 +188,39 @@ async function deployCanonicalL2GasToken(deployer: Wallet, l1ContractAddresses:
logger.info(`Deployed Gas Token on L2 at ${canonicalGasToken.address}`);
}

/**
* Deploys the key registry on L2.
*/
async function deployCanonicalKeyRegistry(deployer: Wallet) {
const canonicalKeyRegistry = getCanonicalKeyRegistry();

// We check to see if there exists a contract at the canonical Key Registry address with the same contract class id as we expect. This means that
// the key registry has already been deployed to the correct address.
if (
(await deployer.getContractInstance(canonicalKeyRegistry.address))?.contractClassId.equals(
canonicalKeyRegistry.contractClass.id,
) &&
(await deployer.isContractClassPubliclyRegistered(canonicalKeyRegistry.contractClass.id))
) {
return;
}

const keyRegistry = await KeyRegistryContract.deploy(deployer)
.send({ contractAddressSalt: canonicalKeyRegistry.instance.salt, universalDeploy: true })
.deployed();

if (
!keyRegistry.address.equals(canonicalKeyRegistry.address) ||
!keyRegistry.address.equals(AztecAddress.fromBigInt(CANONICAL_KEY_REGISTRY_ADDRESS))
) {
throw new Error(
`Deployed Key Registry address ${keyRegistry.address} does not match expected address ${canonicalKeyRegistry.address}, or they both do not equal CANONICAL_KEY_REGISTRY_ADDRESS`,
);
}

logger.info(`Deployed Key Registry on L2 at ${canonicalKeyRegistry.address}`);
}

/** Sandbox settings. */
export type SandboxConfig = AztecNodeConfig & {
/** Mnemonic used to derive the L1 deployer private key.*/
Expand Down Expand Up @@ -213,6 +249,10 @@ export async function createSandbox(config: Partial<SandboxConfig> = {}) {
const node = await createAztecNode(aztecNodeConfig);
const pxe = await createAztecPXE(node);

await deployCanonicalKeyRegistry(
new SignerlessWallet(pxe, new DefaultMultiCallEntrypoint(aztecNodeConfig.chainId, aztecNodeConfig.version)),
);

if (config.enableGas) {
await deployCanonicalL2GasToken(
new SignerlessWallet(pxe, new DefaultMultiCallEntrypoint(aztecNodeConfig.chainId, aztecNodeConfig.version)),
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ export const REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_MAGIC_VALUE =
0xe7af816635466f128568edb04c9fa024f6c87fb9010fdbffa68b3d99n;
export const DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631n;
export const DEPLOYER_CONTRACT_ADDRESS = 0x1236d27f14d2934fd666beff34a0b4b746949f5d51a149eb67f908eb95092f54n;
export const DEPLOYER_CONTRACT_ADDRESS = 0x0097949bb96834550868230a1b6cc242d1f662f7c52946245e4e73da1b8b2165n;
export const DEFAULT_GAS_LIMIT = 1_000_000_000;
export const DEFAULT_TEARDOWN_GAS_LIMIT = 100_000_000;
export const DEFAULT_MAX_FEE_PER_GAS = 10;
export const DEFAULT_INCLUSION_FEE = 0;
export const CANONICAL_KEY_REGISTRY_ADDRESS = 0x1585e564a60e6ec974bc151b62705292ebfc75c33341986a47fd9749cedb567en;
export const AZTEC_ADDRESS_LENGTH = 1;
export const GAS_FEES_LENGTH = 3;
export const GAS_LENGTH = 3;
Expand Down
12 changes: 11 additions & 1 deletion yarn-project/circuits.js/src/contract/artifact_hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,18 @@ export function computeArtifactHashPreimage(artifact: ContractArtifact) {
}

export function computeArtifactMetadataHash(artifact: ContractArtifact) {
// TODO(@spalladino): Should we use the sorted event selectors instead? They'd need to be unique for that.
// TODO: #6021: Should we use the sorted event selectors instead? They'd need to be unique for that.
// Response - The output selectors need to be sorted, because if not noir makes no guarantees on the order of outputs for some reason

const metadata = { name: artifact.name, outputs: artifact.outputs };

// This is a temporary workaround for the Key Registry
// TODO: #6021 We need to make sure the artifact is deterministic from any specific compiler run. This relates to selectors not being sorted and being
// apparently random in the order they appear after compiled w/ nargo. We can try to sort this upon loading an artifact.
if (artifact.name === 'KeyRegistry') {
return sha256Fr(Buffer.from(JSON.stringify({ name: artifact.name }), 'utf-8'));
}

return sha256Fr(Buffer.from(JSON.stringify(metadata), 'utf-8'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('ContractClass', () => {
};

expect(computeContractClassId(contractClass).toString()).toMatchInlineSnapshot(
`"0x2f4c56801b35e01081aeb1b2bd07eba0f8d55de625ec1e957347eedaea1669bb"`,
`"0x0fd34f4f2d6d6a7fc61d8fb8e0c9a411354856fa86c568e4c9e0935b367dc69d"`,
);
});
});
Expand Down
9 changes: 5 additions & 4 deletions yarn-project/circuits.js/src/contract/contract_class_id.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pedersenHash, sha256 } from '@aztec/foundation/crypto';
import { pedersenHash } from '@aztec/foundation/crypto';
import { Fr } from '@aztec/foundation/fields';
import { type ContractClass } from '@aztec/types/contracts';

Expand Down Expand Up @@ -56,7 +56,8 @@ export type ContractClassIdPreimage = {
publicBytecodeCommitment: Fr;
};

// TODO(@spalladino): Replace with actual implementation
export function computePublicBytecodeCommitment(bytecode: Buffer) {
return Fr.fromBufferReduce(sha256(bytecode));
// TODO(#5860): Replace with actual implementation
// Changed to work with canonical contracts that may have non-deterministic noir compiles and we want to keep the address constant
export function computePublicBytecodeCommitment(_bytecode: Buffer) {
return new Fr(5);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ describe('ContractClassRegisteredEvent', () => {
);
expect(event.artifactHash.toString()).toEqual('0x072dce903b1a299d6820eeed695480fe9ec46658b1101885816aed6dd86037f0');
expect(event.packedPublicBytecode.length).toEqual(27090);
// TODO: #5860
expect(computePublicBytecodeCommitment(event.packedPublicBytecode).toString()).toEqual(
'0x1d5c54998c08cee8ad4a8af5740f2e844fe6db3a5bb4b6382a48b2daeabeee3f',
'0x0000000000000000000000000000000000000000000000000000000000000005',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
const BLOCK_SIZE = BENCHMARK_HISTORY_BLOCK_SIZE;
const CHAIN_LENGTHS = BENCHMARK_HISTORY_CHAIN_LENGTHS;
const MAX_CHAIN_LENGTH = CHAIN_LENGTHS[CHAIN_LENGTHS.length - 1];
const SETUP_BLOCK_COUNT = 2; // deploy account + deploy contract
const SETUP_BLOCK_COUNT = 3; // deploy protocol contracts + deploy account + deploy contract

describe('benchmarks/process_history', () => {
let context: EndToEndContext;
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_counter_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('e2e_counter_contract', () => {
counterContract = await CounterContract.deploy(wallet, 0, owner).send().deployed();

logger.info(`Counter contract deployed at ${counterContract.address}`);
}, 25_000);
}, 45_000);

afterAll(() => teardown());

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_encryption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('e2e_encryption', () => {
beforeAll(async () => {
({ teardown, wallet } = await setup());
contract = await TestContract.deploy(wallet).send().deployed();
}, 25_000);
}, 45_000);

afterAll(() => teardown());

Expand Down
5 changes: 4 additions & 1 deletion yarn-project/end-to-end/src/e2e_key_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type AccountWallet, AztecAddress, Fr, type PXE } from '@aztec/aztec.js'
import { GeneratorIndex } from '@aztec/circuits.js';
import { poseidon2Hash } from '@aztec/foundation/crypto';
import { KeyRegistryContract, TestContract } from '@aztec/noir-contracts.js';
import { getCanonicalKeyRegistryAddress } from '@aztec/protocol-contracts/key-registry';

import { jest } from '@jest/globals';

Expand All @@ -11,6 +12,7 @@ const TIMEOUT = 100_000;

describe('SharedMutablePrivateGetter', () => {
let keyRegistry: KeyRegistryContract;

let testContract: TestContract;
let pxe: PXE;
jest.setTimeout(TIMEOUT);
Expand All @@ -21,8 +23,9 @@ describe('SharedMutablePrivateGetter', () => {

beforeAll(async () => {
({ teardown, pxe, wallets } = await setup(2));
keyRegistry = await KeyRegistryContract.at(getCanonicalKeyRegistryAddress(), wallets[0]);

testContract = await TestContract.deploy(wallets[0]).send().deployed();
keyRegistry = await KeyRegistryContract.deploy(wallets[0]).send().deployed();

await publicDeployAccounts(wallets[0], wallets.slice(0, 2));
}, 120_000);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_max_block_number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('e2e_max_block_number', () => {
beforeAll(async () => {
({ teardown, wallet, pxe } = await setup());
contract = await TestContract.deploy(wallet).send().deployed();
}, 25_000);
}, 45_000);

afterAll(() => teardown());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('e2e_voting_contract', () => {
votingContract = await EasyPrivateVotingContract.deploy(wallet, owner).send().deployed();

logger.info(`Counter contract deployed at ${votingContract.address}`);
}, 25_000);
}, 45_000);

afterAll(() => teardown());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('e2e_public_cross_chain_messaging', () => {
user1Wallet = wallets[0];
user2Wallet = wallets[1];
await publicDeployAccounts(wallets[0], wallets.slice(0, 2));
}, 30_000);
}, 45_000);

beforeEach(async () => {
crossChainTestHarness = await CrossChainTestHarness.new(
Expand Down
Loading

0 comments on commit 0881cd3

Please sign in to comment.