Skip to content

Commit

Permalink
refactor: Aztec RPC interface cleanup (#1423)
Browse files Browse the repository at this point in the history
1. Fixes
[#1344](#1344),
2. renamed partial contract address as partial address,
3. Sean's CI fix.
  • Loading branch information
benesjan authored Aug 10, 2023
1 parent cf1f111 commit 1a6168a
Show file tree
Hide file tree
Showing 69 changed files with 389 additions and 401 deletions.
1 change: 1 addition & 0 deletions build_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"^l1-contracts/",
"^yarn-project/l1-artifacts/",
"^yarn-project/noir-contracts/",
"^yarn-project/noir-compiler/",
"^yarn-project/yarn-project-base/",
"^yarn-project/yarn.lock"
],
Expand Down
22 changes: 12 additions & 10 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace {

using aztec3::circuits::compute_constructor_hash;
using aztec3::circuits::compute_contract_address;
using aztec3::circuits::compute_partial_contract_address;
using aztec3::circuits::compute_partial_address;
using aztec3::circuits::abis::CallStackItem;
using aztec3::circuits::abis::ConstantsPacker;
using aztec3::circuits::abis::FunctionData;
Expand Down Expand Up @@ -330,14 +330,14 @@ WASM_EXPORT void abis__compute_contract_address(uint8_t const* point_data_buf,
}

/**
* @brief Compute a contract address from its partial contract address
* @brief Compute a contract address from deployer public key and partial address.
* This is a WASM-export that can be called from Typescript.
*
* @details Computes a contract address by hashing the deployers public key along with the previously computed partial
* address Return the serialized results in the `output` buffer.
*
* @param point_data_buf point data struct as a buffer of bytes
* @param partial_address_data_buf partial contract address
* @param partial_address_data_buf partial address
* @param output buffer that will contain the output. The serialized contract address.
*/
WASM_EXPORT void abis__compute_contract_address_from_partial(uint8_t const* point_data_buf,
Expand All @@ -357,21 +357,23 @@ WASM_EXPORT void abis__compute_contract_address_from_partial(uint8_t const* poin
}

/**
* @brief Compute a partial contract address
* @brief Compute a partial address
* This is a WASM-export that can be called from Typescript.
*
* @details Computes a partial contract address by hashing the salt, functio tree root and constructor hash
* @details Computes a partial address by hashing the salt, function tree root and constructor hash
* Return the serialized results in the `output` buffer.
*
* @param contract_address_salt_buf salt value for the contract address
* @param function_tree_root_buf root value of the contract's function tree
* @param constructor_hash_buf the hash of the contract constructor's verification key
* @param output buffer that will contain the output. The serialized contract address.
* See the link bellow for more details:
* https://github.com/AztecProtocol/aztec-packages/blob/janb/rpc-interface-cleanup/docs/docs/concepts/foundation/accounts/keys.md#addresses-partial-addresses-and-public-keys
*/
WASM_EXPORT void abis__compute_partial_contract_address(uint8_t const* contract_address_salt_buf,
uint8_t const* function_tree_root_buf,
uint8_t const* constructor_hash_buf,
uint8_t* output)
WASM_EXPORT void abis__compute_partial_address(uint8_t const* contract_address_salt_buf,
uint8_t const* function_tree_root_buf,
uint8_t const* constructor_hash_buf,
uint8_t* output)
{
NT::fr contract_address_salt;
NT::fr function_tree_root;
Expand All @@ -381,7 +383,7 @@ WASM_EXPORT void abis__compute_partial_contract_address(uint8_t const* contract_
read(function_tree_root_buf, function_tree_root);
read(constructor_hash_buf, constructor_hash);
NT::fr const partial_address =
compute_partial_contract_address<NT>(contract_address_salt, function_tree_root, constructor_hash);
compute_partial_address<NT>(contract_address_salt, function_tree_root, constructor_hash);

NT::fr::serialize_to_buffer(partial_address, output);
}
Expand Down
8 changes: 4 additions & 4 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ WASM_EXPORT void abis__compute_contract_address(uint8_t const* point_data_buf,
uint8_t const* constructor_hash_buf,
uint8_t* output);

WASM_EXPORT void abis__compute_partial_contract_address(uint8_t const* contract_address_salt_buf,
uint8_t const* function_tree_root_buf,
uint8_t const* constructor_hash_buf,
uint8_t* output);
WASM_EXPORT void abis__compute_partial_address(uint8_t const* contract_address_salt_buf,
uint8_t const* function_tree_root_buf,
uint8_t const* constructor_hash_buf,
uint8_t* output);

CBIND_DECL(abis__compute_commitment_nonce);
CBIND_DECL(abis__compute_unique_commitment);
Expand Down
7 changes: 3 additions & 4 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ template <size_t NUM_BYTES> std::string bytes_to_hex_str(std::array<uint8_t, NUM

namespace aztec3::circuits::abis {

TEST(abi_tests, compute_partial_contract_address)
TEST(abi_tests, compute_partial_address)
{
auto const contract_address_salt = NT::fr(3);
auto const function_tree_root = NT::fr(4);
auto const constructor_hash = NT::fr(5);
NT::fr const expected =
compute_partial_contract_address<NT>(contract_address_salt, function_tree_root, constructor_hash);
NT::fr const expected = compute_partial_address<NT>(contract_address_salt, function_tree_root, constructor_hash);

std::array<uint8_t, sizeof(NT::fr)> output = { 0 };
std::vector<uint8_t> salt_buf;
Expand All @@ -64,7 +63,7 @@ TEST(abi_tests, compute_partial_contract_address)
write(salt_buf, contract_address_salt);
write(function_tree_root_buf, function_tree_root);
write(constructor_hash_buf, constructor_hash);
abis__compute_partial_contract_address(
abis__compute_partial_address(
salt_buf.data(), function_tree_root_buf.data(), constructor_hash_buf.data(), output.data());

// Convert buffer to `fr` for comparison to in-test calculated hash
Expand Down
4 changes: 2 additions & 2 deletions circuits/cpp/src/aztec3/circuits/abis/packers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ struct GeneratorIndexPacker {
int PUBLIC_DATA_LEAF = GeneratorIndex::PUBLIC_DATA_LEAF;
int SIGNED_TX_REQUEST = GeneratorIndex::SIGNED_TX_REQUEST;
int GLOBAL_VARIABLES = GeneratorIndex::GLOBAL_VARIABLES;
int PARTIAL_CONTRACT_ADDRESS = GeneratorIndex::PARTIAL_CONTRACT_ADDRESS;
int PARTIAL_ADDRESS = GeneratorIndex::PARTIAL_ADDRESS;
int TX_REQUEST = GeneratorIndex::TX_REQUEST;
int SIGNATURE_PAYLOAD = GeneratorIndex::SIGNATURE_PAYLOAD;
int VK = GeneratorIndex::VK;
Expand Down Expand Up @@ -162,7 +162,7 @@ struct GeneratorIndexPacker {
PUBLIC_DATA_LEAF,
SIGNED_TX_REQUEST,
GLOBAL_VARIABLES,
PARTIAL_CONTRACT_ADDRESS,
PARTIAL_ADDRESS,
TX_REQUEST,
SIGNATURE_PAYLOAD,
VK,
Expand Down
10 changes: 5 additions & 5 deletions circuits/cpp/src/aztec3/circuits/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ template <typename NCT> typename NCT::fr compute_constructor_hash(FunctionData<N
return NCT::compress(inputs, aztec3::GeneratorIndex::CONSTRUCTOR);
}

template <typename NCT> typename NCT::fr compute_partial_contract_address(typename NCT::fr contract_address_salt,
typename NCT::fr function_tree_root,
typename NCT::fr constructor_hash)
template <typename NCT> typename NCT::fr compute_partial_address(typename NCT::fr contract_address_salt,
typename NCT::fr function_tree_root,
typename NCT::fr constructor_hash)
{
using fr = typename NCT::fr;
std::vector<fr> const inputs = {
fr(0), fr(0), contract_address_salt, function_tree_root, constructor_hash,
};
return NCT::hash(inputs, aztec3::GeneratorIndex::PARTIAL_CONTRACT_ADDRESS);
return NCT::hash(inputs, aztec3::GeneratorIndex::PARTIAL_ADDRESS);
}

template <typename NCT>
Expand All @@ -81,7 +81,7 @@ template <typename NCT> typename NCT::address compute_contract_address(Point<NCT
using fr = typename NCT::fr;

const fr partial_address =
compute_partial_contract_address<NCT>(contract_address_salt, function_tree_root, constructor_hash);
compute_partial_address<NCT>(contract_address_salt, function_tree_root, constructor_hash);

return compute_contract_address_from_partial(point, partial_address);
}
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ enum GeneratorIndex {
PUBLIC_DATA_LEAF, // Size = ? (unused) // TODO what's the expected size? Assuming ≤ 8
SIGNED_TX_REQUEST, // Size = 7
GLOBAL_VARIABLES, // Size = 4
PARTIAL_CONTRACT_ADDRESS, // Size = 7
PARTIAL_ADDRESS, // Size = 7
BLOCK_HASH, // Size = 6
/**
* Indices with size ≤ 16
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/acir-simulator/src/client/db_oracle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PartialContractAddress, PrivateKey, PublicKey } from '@aztec/circuits.js';
import { PartialAddress, PrivateKey, PublicKey } from '@aztec/circuits.js';
import { FunctionAbi } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
Expand Down Expand Up @@ -74,9 +74,9 @@ export interface DBOracle extends CommitmentsDB {
/**
* Retrieve the public key associated to a given address.
* @param address - Address to fetch the pubkey for.
* @returns A public key and the corresponding partial contract address, such that the hash of the two resolves to the input address.
* @returns A public key and the corresponding partial address, such that the hash of the two resolves to the input address.
*/
getPublicKey(address: AztecAddress): Promise<[PublicKey, PartialContractAddress]>;
getPublicKey(address: AztecAddress): Promise<[PublicKey, PartialAddress]>;

/**
* Retrieve the secret key associated with a specific public key.
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/acir-simulator/src/client/private_execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class PrivateFunctionExecution {
getSecretKey: ([ownerX], [ownerY]) => this.context.getSecretKey(this.contractAddress, ownerX, ownerY),
getPublicKey: async ([acvmAddress]) => {
const address = frToAztecAddress(fromACVMField(acvmAddress));
const [pubKey, partialContractAddress] = await this.context.db.getPublicKey(address);
return [pubKey.x, pubKey.y, partialContractAddress].map(toACVMField);
const [pubKey, partialAddress] = await this.context.db.getPublicKey(address);
return [pubKey.x, pubKey.y, partialAddress].map(toACVMField);
},
getNotes: ([slot], sortBy, sortOrder, [limit], [offset], [returnSize]) =>
this.context.getNotes(this.contractAddress, slot, sortBy, sortOrder, +limit, +offset, +returnSize),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export class UnconstrainedFunctionExecution {
getSecretKey: ([ownerX], [ownerY]) => this.context.getSecretKey(this.contractAddress, ownerX, ownerY),
getPublicKey: async ([acvmAddress]) => {
const address = frToAztecAddress(fromACVMField(acvmAddress));
const [pubKey, partialContractAddress] = await this.context.db.getPublicKey(address);
return [pubKey.x, pubKey.y, partialContractAddress].map(toACVMField);
const [pubKey, partialAddress] = await this.context.db.getPublicKey(address);
return [pubKey.x, pubKey.y, partialAddress].map(toACVMField);
},
getNotes: ([slot], sortBy, sortOrder, [limit], [offset], [returnSize]) =>
this.context.getNotes(this.contractAddress, slot, sortBy, sortOrder, +limit, +offset, +returnSize),
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/archiver/src/archiver/archiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sleep } from '@aztec/foundation/sleep';
import { ContractDeploymentEmitterAbi, InboxAbi, RollupAbi } from '@aztec/l1-artifacts';
import {
ContractData,
ContractPublicData,
ContractDataAndBytecode,
EncodedContractFunction,
L2Block,
L2BlockL2Logs,
Expand Down Expand Up @@ -158,7 +158,7 @@ function makeContractDeploymentEvent(l1BlockNum: bigint, l2Block: L2Block) {
// const contractData = ContractData.random();
const aztecAddress = AztecAddress.random();
const portalAddress = EthAddress.random();
const contractData = new ContractPublicData(new ContractData(aztecAddress, portalAddress), [
const contractData = new ContractDataAndBytecode(new ContractData(aztecAddress, portalAddress), [
EncodedContractFunction.random(),
EncodedContractFunction.random(),
]);
Expand Down
31 changes: 15 additions & 16 deletions yarn-project/archiver/src/archiver/archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import { RunningPromise } from '@aztec/foundation/running-promise';
import {
ContractData,
ContractDataAndBytecode,
ContractDataSource,
ContractPublicData,
EncodedContractFunction,
INITIAL_L2_BLOCK_NUM,
L1ToL2Message,
Expand Down Expand Up @@ -221,9 +221,9 @@ export class Archiver implements L2BlockSource, L2LogsSource, ContractDataSource
// store contracts for which we have retrieved L2 blocks
const lastKnownL2BlockNum = retrievedBlocks.retrievedData[retrievedBlocks.retrievedData.length - 1].number;
retrievedContracts.retrievedData.forEach(async ([contracts, l2BlockNum], index) => {
this.log(`Retrieved contract public data for l2 block number: ${index}`);
this.log(`Retrieved contract data and bytecode for l2 block number: ${index}`);
if (l2BlockNum <= lastKnownL2BlockNum) {
await this.store.addL2ContractPublicData(contracts, l2BlockNum);
await this.store.addContractDataAndBytecode(contracts, l2BlockNum);
}
});

Expand Down Expand Up @@ -291,37 +291,37 @@ export class Archiver implements L2BlockSource, L2LogsSource, ContractDataSource
* @param contractAddress - The contract data address.
* @returns The contract data.
*/
public getL2ContractPublicData(contractAddress: AztecAddress): Promise<ContractPublicData | undefined> {
return this.store.getL2ContractPublicData(contractAddress);
public getContractDataAndBytecode(contractAddress: AztecAddress): Promise<ContractDataAndBytecode | undefined> {
return this.store.getContractDataAndBytecode(contractAddress);
}

/**
* Lookup all contract data in an L2 block.
* @param blockNum - The block number to get all contract data from.
* @returns All new contract data in the block (if found).
*/
public getL2ContractPublicDataInBlock(blockNum: number): Promise<ContractPublicData[]> {
return this.store.getL2ContractPublicDataInBlock(blockNum);
public getContractDataAndBytecodeInBlock(blockNum: number): Promise<ContractDataAndBytecode[]> {
return this.store.getContractDataAndBytecodeInBlock(blockNum);
}

/**
* Lookup the L2 contract info for this contract.
* Lookup the contract data for this contract.
* Contains contract address & the ethereum portal address.
* @param contractAddress - The contract data address.
* @returns ContractData with the portal address (if we didn't throw an error).
*/
public getL2ContractInfo(contractAddress: AztecAddress): Promise<ContractData | undefined> {
return this.store.getL2ContractInfo(contractAddress);
public getContractData(contractAddress: AztecAddress): Promise<ContractData | undefined> {
return this.store.getContractData(contractAddress);
}

/**
* Lookup the L2 contract info inside a block.
* Lookup the L2 contract data inside a block.
* Contains contract address & the ethereum portal address.
* @param l2BlockNum - The L2 block number to get the contract data from.
* @returns ContractData with the portal address (if we didn't throw an error).
*/
public getL2ContractInfoInBlock(l2BlockNum: number): Promise<ContractData[] | undefined> {
return this.store.getL2ContractInfoInBlock(l2BlockNum);
public getContractDataInBlock(l2BlockNum: number): Promise<ContractData[] | undefined> {
return this.store.getContractDataInBlock(l2BlockNum);
}

/**
Expand All @@ -334,9 +334,8 @@ export class Archiver implements L2BlockSource, L2LogsSource, ContractDataSource
contractAddress: AztecAddress,
functionSelector: Buffer,
): Promise<EncodedContractFunction | undefined> {
const contractData = await this.getL2ContractPublicData(contractAddress);
const result = contractData?.publicFunctions?.find(fn => fn.functionSelector.equals(functionSelector));
return result;
const contractData = await this.getContractDataAndBytecode(contractAddress);
return contractData?.getPublicFunction(functionSelector);
}

/**
Expand Down
Loading

0 comments on commit 1a6168a

Please sign in to comment.