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

fix(synchroniser): Store most recent globals hash in the synchroniser, rather than fetching from the latest block #1539

Merged
merged 13 commits into from
Aug 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "tx_context.hpp"

#include "aztec3/circuits/abis/constant_historic_block_data.hpp"
#include "aztec3/circuits/abis/historic_block_data.hpp"
#include "aztec3/utils/types/circuit_types.hpp"
#include "aztec3/utils/types/convert.hpp"
#include "aztec3/utils/types/native_types.hpp"
Expand All @@ -11,7 +11,7 @@

namespace aztec3::circuits::abis {

using aztec3::circuits::abis::ConstantHistoricBlockData;
using aztec3::circuits::abis::HistoricBlockData;
using aztec3::utils::types::CircuitTypes;
using aztec3::utils::types::NativeTypes;
using std::is_same;
Expand All @@ -20,7 +20,7 @@ template <typename NCT> struct CombinedConstantData {
using fr = typename NCT::fr;
using boolean = typename NCT::boolean;

ConstantHistoricBlockData<NCT> block_data{};
HistoricBlockData<NCT> block_data{};
TxContext<NCT> tx_context{};

// for serialization: update up with new fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using aztec3::utils::types::CircuitTypes;
using aztec3::utils::types::NativeTypes;
using std::is_same;

template <typename NCT> struct ConstantHistoricBlockData {
template <typename NCT> struct HistoricBlockData {
using fr = typename NCT::fr;
using boolean = typename NCT::boolean;

Expand All @@ -29,7 +29,7 @@ template <typename NCT> struct ConstantHistoricBlockData {

// Public data
fr public_data_tree_root = 0;
fr prev_global_variables_hash = 0;
fr global_variables_hash = 0;

// for serialization, update with new fields
MSGPACK_FIELDS(private_data_tree_root,
Expand All @@ -39,44 +39,44 @@ template <typename NCT> struct ConstantHistoricBlockData {
blocks_tree_root,
private_kernel_vk_tree_root,
public_data_tree_root,
prev_global_variables_hash);
global_variables_hash);

boolean operator==(ConstantHistoricBlockData<NCT> const& other) const
boolean operator==(HistoricBlockData<NCT> const& other) const
{
return private_data_tree_root == other.private_data_tree_root &&
nullifier_tree_root == other.nullifier_tree_root && contract_tree_root == other.contract_tree_root &&
l1_to_l2_messages_tree_root == other.l1_to_l2_messages_tree_root &&
blocks_tree_root == other.historic_block_root &&
private_kernel_vk_tree_root == other.private_kernel_vk_tree_root &&
public_data_tree_root == other.public_data_tree_root &&
prev_global_variables_hash == other.prev_global_variables_hash;
global_variables_hash == other.global_variables_hash;
};

template <typename Builder> ConstantHistoricBlockData<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
template <typename Builder> HistoricBlockData<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

// Capture the circuit builder:
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(builder, e); };

ConstantHistoricBlockData<CircuitTypes<Builder>> data = {
to_ct(private_data_tree_root), to_ct(nullifier_tree_root), to_ct(contract_tree_root),
to_ct(l1_to_l2_messages_tree_root), to_ct(blocks_tree_root), to_ct(private_kernel_vk_tree_root),
to_ct(public_data_tree_root), to_ct(prev_global_variables_hash),
HistoricBlockData<CircuitTypes<Builder>> data = {
to_ct(private_data_tree_root), to_ct(nullifier_tree_root), to_ct(contract_tree_root),
to_ct(l1_to_l2_messages_tree_root), to_ct(blocks_tree_root), to_ct(private_kernel_vk_tree_root),
to_ct(public_data_tree_root), to_ct(global_variables_hash),
};

return data;
};

template <typename Builder> ConstantHistoricBlockData<NativeTypes> to_native_type() const
template <typename Builder> HistoricBlockData<NativeTypes> to_native_type() const
{
static_assert(std::is_same<CircuitTypes<Builder>, NCT>::value);
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Builder>(e); };

ConstantHistoricBlockData<NativeTypes> data = {
to_nt(private_data_tree_root), to_nt(nullifier_tree_root), to_nt(contract_tree_root),
to_nt(l1_to_l2_messages_tree_root), to_nt(blocks_tree_root), to_nt(private_kernel_vk_tree_root),
to_nt(public_data_tree_root), to_nt(prev_global_variables_hash),
HistoricBlockData<NativeTypes> data = {
to_nt(private_data_tree_root), to_nt(nullifier_tree_root), to_nt(contract_tree_root),
to_nt(l1_to_l2_messages_tree_root), to_nt(blocks_tree_root), to_nt(private_kernel_vk_tree_root),
to_nt(public_data_tree_root), to_nt(global_variables_hash),
};

return data;
Expand All @@ -93,13 +93,13 @@ template <typename NCT> struct ConstantHistoricBlockData {
blocks_tree_root.set_public();
private_kernel_vk_tree_root.set_public();
public_data_tree_root.set_public();
prev_global_variables_hash.set_public();
global_variables_hash.set_public();
}


fr hash()
{
return compute_block_hash(prev_global_variables_hash,
return compute_block_hash(global_variables_hash,
private_data_tree_root,
nullifier_tree_root,
contract_tree_root,
Expand All @@ -108,8 +108,7 @@ template <typename NCT> struct ConstantHistoricBlockData {
}
};

template <typename NCT>
std::ostream& operator<<(std::ostream& os, ConstantHistoricBlockData<NCT> const& historic_tree_roots)
template <typename NCT> std::ostream& operator<<(std::ostream& os, HistoricBlockData<NCT> const& historic_tree_roots)
{
return os << "private_data_tree_root: " << historic_tree_roots.private_data_tree_root << "\n"
<< "nullifier_tree_root: " << historic_tree_roots.nullifier_tree_root << "\n"
Expand All @@ -118,7 +117,7 @@ std::ostream& operator<<(std::ostream& os, ConstantHistoricBlockData<NCT> const&
<< "blocks_tree_root: " << historic_tree_roots.blocks_tree_root << "\n"
<< "private_kernel_vk_tree_root: " << historic_tree_roots.private_kernel_vk_tree_root << "\n"
<< "public_data_tree_root: " << historic_tree_roots.public_data_tree_root << "\n"
<< "prev_global_variables_hash: " << historic_tree_roots.prev_global_variables_hash << "\n";
<< "prev_global_variables_hash: " << historic_tree_roots.global_variables_hash << "\n";
}

} // namespace aztec3::circuits::abis
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "init.hpp"

#include "aztec3/circuits/abis/combined_constant_data.hpp"
#include "aztec3/circuits/abis/constant_historic_block_data.hpp"
#include "aztec3/circuits/abis/historic_block_data.hpp"
#include "aztec3/circuits/abis/private_kernel/private_kernel_inputs_init.hpp"
#include "aztec3/utils/array.hpp"

Expand All @@ -11,7 +11,7 @@ namespace {
using NT = aztec3::utils::types::NativeTypes;

using aztec3::circuits::abis::CombinedConstantData;
using aztec3::circuits::abis::ConstantHistoricBlockData;
using aztec3::circuits::abis::HistoricBlockData;
using aztec3::circuits::abis::KernelCircuitPublicInputs;
using aztec3::circuits::abis::private_kernel::PrivateKernelInputsInit;
using aztec3::utils::array_push;
Expand All @@ -27,7 +27,7 @@ void initialise_end_values(PrivateKernelInputsInit<NT> const& private_inputs,
auto const& private_call_public_inputs = private_inputs.private_call.call_stack_item.public_inputs;
auto const constants = CombinedConstantData<NT>{
.block_data =
ConstantHistoricBlockData<NT>{
HistoricBlockData<NT>{
// TODO(dbanks12): remove historic root from app circuit public inputs and
// add it to PrivateCallData: https://github.com/AztecProtocol/aztec-packages/issues/778
// Then use this:
Expand All @@ -37,7 +37,7 @@ void initialise_end_values(PrivateKernelInputsInit<NT> const& private_inputs,
.contract_tree_root = private_call_public_inputs.historic_contract_tree_root,
.l1_to_l2_messages_tree_root = private_call_public_inputs.historic_l1_to_l2_messages_tree_root,
.public_data_tree_root = private_call_public_inputs.historic_public_data_tree_root,
.prev_global_variables_hash = private_call_public_inputs.historic_global_variables_hash,
.global_variables_hash = private_call_public_inputs.historic_global_variables_hash,
},
.tx_context = private_inputs.tx_request.tx_context,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include "aztec3/circuits/abis/call_stack_item.hpp"
#include "aztec3/circuits/abis/combined_accumulated_data.hpp"
#include "aztec3/circuits/abis/combined_constant_data.hpp"
#include "aztec3/circuits/abis/constant_historic_block_data.hpp"
#include "aztec3/circuits/abis/contract_deployment_data.hpp"
#include "aztec3/circuits/abis/function_data.hpp"
#include "aztec3/circuits/abis/historic_block_data.hpp"
#include "aztec3/circuits/abis/private_circuit_public_inputs.hpp"
#include "aztec3/circuits/abis/private_kernel/private_call_data.hpp"
#include "aztec3/circuits/abis/tx_context.hpp"
Expand All @@ -31,9 +31,9 @@ using aztec3::circuits::abis::CallContext;
using aztec3::circuits::abis::CallStackItem;
using aztec3::circuits::abis::CombinedAccumulatedData;
using aztec3::circuits::abis::CombinedConstantData;
using aztec3::circuits::abis::ConstantHistoricBlockData;
using aztec3::circuits::abis::ContractDeploymentData;
using aztec3::circuits::abis::FunctionData;
using aztec3::circuits::abis::HistoricBlockData;
using aztec3::circuits::abis::PrivateCircuitPublicInputs;
using aztec3::circuits::abis::PrivateTypes;
using aztec3::circuits::abis::TxContext;
Expand Down Expand Up @@ -484,7 +484,7 @@ PrivateKernelInputsInner<NT> do_private_call_get_kernel_inputs_inner(
mock_previous_kernel.public_inputs.end.private_call_stack = initial_kernel_private_call_stack;
mock_previous_kernel.public_inputs.constants = CombinedConstantData<NT>{
.block_data =
ConstantHistoricBlockData<NT>{
HistoricBlockData<NT>{
.private_data_tree_root = private_circuit_public_inputs.historic_private_data_tree_root,
.contract_tree_root = private_circuit_public_inputs.historic_contract_tree_root,
},
Expand Down
15 changes: 7 additions & 8 deletions circuits/cpp/src/aztec3/circuits/kernel/public/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include "aztec3/circuits/abis/call_stack_item.hpp"
#include "aztec3/circuits/abis/combined_accumulated_data.hpp"
#include "aztec3/circuits/abis/combined_constant_data.hpp"
#include "aztec3/circuits/abis/constant_historic_block_data.hpp"
#include "aztec3/circuits/abis/contract_deployment_data.hpp"
#include "aztec3/circuits/abis/function_data.hpp"
#include "aztec3/circuits/abis/historic_block_data.hpp"
#include "aztec3/circuits/abis/kernel_circuit_public_inputs.hpp"
#include "aztec3/circuits/abis/previous_kernel_data.hpp"
#include "aztec3/circuits/abis/private_circuit_public_inputs.hpp"
Expand All @@ -34,7 +34,7 @@ using aztec3::circuits::abis::CallContext;
using aztec3::circuits::abis::CallStackItem;
using aztec3::circuits::abis::CombinedAccumulatedData;
using aztec3::circuits::abis::CombinedConstantData;
using aztec3::circuits::abis::ConstantHistoricBlockData;
using aztec3::circuits::abis::HistoricBlockData;
using aztec3::circuits::abis::NewContractData;
using aztec3::circuits::abis::OptionallyRevealedData;
using aztec3::circuits::abis::PreviousKernelData;
Expand Down Expand Up @@ -358,19 +358,18 @@ PublicKernelInputs<NT> get_kernel_inputs_with_previous_kernel(NT::boolean privat
};

// TODO(914) Should this be unused?
[[maybe_unused]] ConstantHistoricBlockData<NT> const historic_tree_roots = {
[[maybe_unused]] HistoricBlockData<NT> const historic_tree_roots = {
.private_data_tree_root = 1000,
.contract_tree_root = 2000,
.l1_to_l2_messages_tree_root = 3000,
.private_kernel_vk_tree_root = 4000,
};

CombinedConstantData<NT> const end_constants = { .block_data =
ConstantHistoricBlockData<NT>{
.private_data_tree_root = ++seed,
.nullifier_tree_root = ++seed,
.contract_tree_root = ++seed,
.private_kernel_vk_tree_root = ++seed },
HistoricBlockData<NT>{ .private_data_tree_root = ++seed,
.nullifier_tree_root = ++seed,
.contract_tree_root = ++seed,
.private_kernel_vk_tree_root = ++seed },
.tx_context = TxContext<NT>{
.is_fee_payment_tx = false,
.is_rebate_payment_tx = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void perform_historical_blocks_tree_membership_checks(DummyBuilder& builder, Bas
auto l1_to_l2_data_tree_root = historic_block.l1_to_l2_messages_tree_root;
auto public_data_tree_root = historic_block.public_data_tree_root;

auto previous_block_hash = compute_block_hash<NT>(historic_block.prev_global_variables_hash,
auto previous_block_hash = compute_block_hash<NT>(historic_block.global_variables_hash,
private_data_tree_root,
nullifier_tree_root,
contract_tree_root,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ BaseRollupInputs base_rollup_inputs_from_kernels(std::array<KernelData, 2> kerne
kernel_data[i].public_inputs.constants.block_data.l1_to_l2_messages_tree_root = l1_to_l2_msg_tree.root();
kernel_data[i].public_inputs.constants.block_data.blocks_tree_root = historic_blocks_tree.root();
kernel_data[i].public_inputs.constants.block_data.public_data_tree_root = public_data_tree.root();
kernel_data[i].public_inputs.constants.block_data.prev_global_variables_hash = prev_global_variables_hash;
kernel_data[i].public_inputs.constants.block_data.global_variables_hash = prev_global_variables_hash;
}

// Then we collect all sibling paths for the reads in the left tx, and then apply the update requests while
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CircuitsWasm, ConstantHistoricBlockData, ReadRequestMembershipWitness, TxContext } from '@aztec/circuits.js';
import { CircuitsWasm, HistoricBlockData, ReadRequestMembershipWitness, TxContext } from '@aztec/circuits.js';
import { siloNullifier } from '@aztec/circuits.js/abis';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
Expand Down Expand Up @@ -35,7 +35,7 @@ export class ClientTxExecutionContext {
/** The tx context. */
public txContext: TxContext,
/** Data required to reconstruct the block hash, it contains historic roots. */
public constantHistoricBlockData: ConstantHistoricBlockData,
public historicBlockData: HistoricBlockData,
/** The cache of packed arguments */
public packedArgsCache: PackedArgsCache,
/** Pending notes created (and not nullified) up to current point in execution.
Expand All @@ -57,7 +57,7 @@ export class ClientTxExecutionContext {
this.db,
this.txNullifier,
this.txContext,
this.constantHistoricBlockData,
this.historicBlockData,
this.packedArgsCache,
this.pendingNotes,
this.pendingNullifiers,
Expand Down Expand Up @@ -199,7 +199,7 @@ export class ClientTxExecutionContext {
*/
public async getL1ToL2Message(msgKey: Fr): Promise<ACVMField[]> {
const messageInputs = await this.db.getL1ToL2Message(msgKey);
return toAcvmL1ToL2MessageLoadOracleInputs(messageInputs, this.constantHistoricBlockData.l1ToL2MessagesTreeRoot);
return toAcvmL1ToL2MessageLoadOracleInputs(messageInputs, this.historicBlockData.l1ToL2MessagesTreeRoot);
}

/**
Expand All @@ -216,7 +216,7 @@ export class ClientTxExecutionContext {
const commitmentInputs = await this.db.getCommitmentOracle(contractAddress, fromACVMField(commitment));
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1029): support pending commitments here
this.readRequestPartialWitnesses.push(ReadRequestMembershipWitness.empty(commitmentInputs.index));
return toAcvmCommitmentLoadOracleInputs(commitmentInputs, this.constantHistoricBlockData.privateDataTreeRoot);
return toAcvmCommitmentLoadOracleInputs(commitmentInputs, this.historicBlockData.privateDataTreeRoot);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion yarn-project/acir-simulator/src/client/db_oracle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PartialAddress, PrivateKey, PublicKey } from '@aztec/circuits.js';
import { HistoricBlockData, 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 @@ -119,4 +119,12 @@ export interface DBOracle extends CommitmentsDB {
* @returns A Promise that resolves to an EthAddress instance, representing the portal contract address.
*/
getPortalContractAddress(contractAddress: AztecAddress): Promise<EthAddress>;

/**
* Retrieve the databases view of the Historic Block Data object.
* This structure is fed into the circuits simulator and is used to prove against certain historic roots.
*
* @returns A Promise that resolves to a HistoricBlockData object.
*/
getHistoricBlockData(): Promise<HistoricBlockData>;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
CallContext,
CircuitsWasm,
ConstantHistoricBlockData,
ContractDeploymentData,
FieldsOf,
FunctionData,
HistoricBlockData,
L1_TO_L2_MSG_TREE_HEIGHT,
MAX_NEW_COMMITMENTS_PER_CALL,
PRIVATE_DATA_TREE_HEIGHT,
Expand Down Expand Up @@ -59,7 +59,8 @@ describe('Private Execution test suite', () => {
let circuitsWasm: CircuitsWasm;
let oracle: MockProxy<DBOracle>;
let acirSimulator: AcirSimulator;
let blockData = ConstantHistoricBlockData.empty();

let blockData = HistoricBlockData.empty();
let logger: DebugLogger;

const defaultContractAddress = AztecAddress.random();
Expand Down Expand Up @@ -110,7 +111,6 @@ describe('Private Execution test suite', () => {
abi,
functionData.isConstructor ? AztecAddress.ZERO : contractAddress,
portalContractAddress,
blockData,
);
};

Expand All @@ -130,7 +130,7 @@ describe('Private Execution test suite', () => {
const prevRoots = blockData.toBuffer();
const rootIndex = name === 'privateData' ? 0 : 32 * 3;
const newRoots = Buffer.concat([prevRoots.subarray(0, rootIndex), newRoot, prevRoots.subarray(rootIndex + 32)]);
blockData = ConstantHistoricBlockData.fromBuffer(newRoots);
blockData = HistoricBlockData.fromBuffer(newRoots);

return trees[name];
};
Expand All @@ -145,6 +145,7 @@ describe('Private Execution test suite', () => {
beforeEach(() => {
oracle = mock<DBOracle>();
oracle.getSecretKey.mockResolvedValue(ownerPk);
oracle.getHistoricBlockData.mockResolvedValue(blockData);

acirSimulator = new AcirSimulator(oracle);
});
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 @@ -224,7 +224,7 @@ export class PrivateFunctionExecution {
private writeInputs() {
const contractDeploymentData = this.context.txContext.contractDeploymentData ?? ContractDeploymentData.empty();

const blockData = this.context.constantHistoricBlockData;
const blockData = this.context.historicBlockData;

const fields = [
this.callContext.msgSender,
Expand All @@ -239,7 +239,7 @@ export class PrivateFunctionExecution {
blockData.contractTreeRoot,
blockData.l1ToL2MessagesTreeRoot,
blockData.blocksTreeRoot,
blockData.prevGlobalVariablesHash,
blockData.globalVariablesHash,
blockData.publicDataTreeRoot,

contractDeploymentData.deployerPublicKey.x,
Expand Down
Loading