Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 6, 2023
1 parent ca36822 commit e8099cc
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 57 deletions.
4 changes: 2 additions & 2 deletions circuits/cpp/src/aztec3/circuits/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,10 @@ template <typename NCT> typename NCT::fr compute_public_data_tree_value(typename
* @param storage_slot The storage slot to which the inserted element belongs
* @return The index for insertion into the public data tree
*/
template <typename NCT> typename NCT::fr compute_public_data_tree_index(typename NCT::fr const& contract_address,
template <typename NCT> typename NCT::fr compute_public_data_tree_index(typename NCT::address const& contract_address,
typename NCT::fr const& storage_slot)
{
return NCT::compress({ contract_address, storage_slot }, GeneratorIndex::PUBLIC_LEAF_INDEX);
return NCT::compress({ contract_address.to_field(), storage_slot }, GeneratorIndex::PUBLIC_LEAF_INDEX);
}

template <typename NCT> typename NCT::fr compute_l2_to_l1_hash(typename NCT::address const& contract_address,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/acir-simulator/src/public/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function contractStorageReadToPublicDataRead(
contractAddress: AztecAddress,
): PublicDataRead {
return new PublicDataRead(
computePublicDataTreeIndex(wasm, contractAddress.toField(), read.storageSlot),
computePublicDataTreeIndex(wasm, contractAddress, read.storageSlot),
computePublicDataTreeValue(wasm, read.currentValue),
read.sideEffectCounter!,
);
Expand All @@ -141,7 +141,7 @@ function contractStorageUpdateRequestToPublicDataUpdateRequest(
contractAddress: AztecAddress,
): PublicDataUpdateRequest {
return new PublicDataUpdateRequest(
computePublicDataTreeIndex(wasm, contractAddress.toField(), update.storageSlot),
computePublicDataTreeIndex(wasm, contractAddress, update.storageSlot),
computePublicDataTreeValue(wasm, update.oldValue),
computePublicDataTreeValue(wasm, update.newValue),
update.sideEffectCounter!,
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
L1_TO_L2_MSG_TREE_HEIGHT,
PRIVATE_DATA_TREE_HEIGHT,
} from '@aztec/circuits.js';
import { computePublicDataTreeIndex } from '@aztec/circuits.js/abis';
import { L1ContractAddresses } from '@aztec/ethereum';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { createDebugLogger } from '@aztec/foundation/log';
Expand Down Expand Up @@ -41,7 +42,6 @@ import {
ServerWorldStateSynchronizer,
WorldStateConfig,
WorldStateSynchronizer,
computePublicDataTreeLeafIndex,
getConfigEnvVars as getWorldStateConfig,
} from '@aztec/world-state';

Expand Down Expand Up @@ -321,8 +321,8 @@ export class AztecNodeService implements AztecNode {
*/
public async getPublicStorageAt(contract: AztecAddress, slot: bigint): Promise<Buffer | undefined> {
const committedDb = await this.#getWorldState();
const leafIndex = computePublicDataTreeLeafIndex(contract, new Fr(slot), await CircuitsWasm.get());
return committedDb.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex);
const leafIndex = computePublicDataTreeIndex(await CircuitsWasm.get(), contract, new Fr(slot));
return committedDb.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex.value);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export function computePublicDataTreeValue(wasm: IWasmModule, value: Fr): Fr {
* @returns Public data tree index computed from contract address and storage slot.
*/
export function computePublicDataTreeIndex(wasm: IWasmModule, contractAddress: Fr, storageSlot: Fr): Fr {
export function computePublicDataTreeIndex(wasm: IWasmModule, contractAddress: AztecAddress, storageSlot: Fr): Fr {
wasm.call('pedersen__init');
return abisComputePublicDataTreeIndex(wasm, contractAddress, storageSlot);
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/cbind/circuits.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3208,7 +3208,7 @@ export function abisComputeGlobalsHash(wasm: IWasmModule, arg0: GlobalVariables)
export function abisComputePublicDataTreeValue(wasm: IWasmModule, arg0: Fr): Fr {
return Fr.fromBuffer(callCbind(wasm, 'abis__compute_public_data_tree_value', [toBuffer(arg0)]));
}
export function abisComputePublicDataTreeIndex(wasm: IWasmModule, arg0: Fr, arg1: Fr): Fr {
export function abisComputePublicDataTreeIndex(wasm: IWasmModule, arg0: Address, arg1: Fr): Fr {
return Fr.fromBuffer(callCbind(wasm, 'abis__compute_public_data_tree_index', [toBuffer(arg0), toBuffer(arg1)]));
}
export function privateKernelDummyPreviousKernel(wasm: IWasmModule): PreviousKernelData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ import {
VerificationKey,
makeTuple,
} from '@aztec/circuits.js';
import { computeBlockHash, computeBlockHashWithGlobals, computeContractLeaf } from '@aztec/circuits.js/abis';
import {
computeBlockHash,
computeBlockHashWithGlobals,
computeContractLeaf,
computeGlobalsHash,
} from '@aztec/circuits.js/abis';
import { toFriendlyJSON } from '@aztec/circuits.js/utils';
import { toBigIntBE } from '@aztec/foundation/bigint-buffer';
import { padArrayEnd } from '@aztec/foundation/collection';
import { Fr } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { Tuple, assertLength } from '@aztec/foundation/serialize';
import { ContractData, L2Block, L2BlockL2Logs, MerkleTreeId, PublicDataWrite, TxL2Logs } from '@aztec/types';
import { MerkleTreeOperations, computeGlobalVariablesHash } from '@aztec/world-state';
import { MerkleTreeOperations } from '@aztec/world-state';

import chunk from 'lodash.chunk';
import flatMap from 'lodash.flatmap';
Expand Down Expand Up @@ -308,7 +313,7 @@ export class SoloBlockBuilder implements BlockBuilder {
// Update the root trees with the latest data and contract tree roots,
// and validate them against the output of the root circuit simulation
this.debug(`Updating and validating root trees`);
const globalVariablesHash = await computeGlobalVariablesHash(left[0].constants.globalVariables);
const globalVariablesHash = computeGlobalsHash(await CircuitsWasm.get(), left[0].constants.globalVariables);
await this.db.updateLatestGlobalVariablesHash(globalVariablesHash);
await this.db.updateHistoricBlocksTree(globalVariablesHash);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
PublicStateDB,
} from '@aztec/acir-simulator';
import { AztecAddress, CircuitsWasm, EthAddress, Fr, FunctionSelector, HistoricBlockData } from '@aztec/circuits.js';
import { computePublicDataTreeIndex } from '@aztec/circuits.js/abis';
import { ContractDataSource, ExtendedContractData, L1ToL2MessageSource, MerkleTreeId, Tx } from '@aztec/types';
import { MerkleTreeOperations, computePublicDataTreeLeafIndex } from '@aztec/world-state';
import { MerkleTreeOperations } from '@aztec/world-state';

/**
* Returns a new PublicExecutor simulator backed by the supplied merkle tree db and contract data source.
Expand All @@ -31,7 +32,7 @@ export function getPublicExecutor(

/**
* Implements the PublicContractsDB using a ContractDataSource.
* Progresively records contracts in transaction as they are processed in a block.
* Progressively records contracts in transaction as they are processed in a block.
*/
export class ContractsDataSourcePublicDB implements PublicContractsDB {
cache = new Map<string, ExtendedContractData>();
Expand Down Expand Up @@ -106,7 +107,7 @@ class WorldStatePublicDB implements PublicStateDB {
* @returns The current value in the storage slot.
*/
public async storageRead(contract: AztecAddress, slot: Fr): Promise<Fr> {
const index = computePublicDataTreeLeafIndex(contract, slot, await CircuitsWasm.get());
const index = computePublicDataTreeIndex(await CircuitsWasm.get(), contract, slot).value;
const cached = this.writeCache.get(index);
if (cached !== undefined) return cached;
const value = await this.db.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, index);
Expand All @@ -120,7 +121,7 @@ class WorldStatePublicDB implements PublicStateDB {
* @param newValue - The new value to store.
*/
public async storageWrite(contract: AztecAddress, slot: Fr, newValue: Fr): Promise<void> {
const index = computePublicDataTreeLeafIndex(contract, slot, await CircuitsWasm.get());
const index = computePublicDataTreeIndex(await CircuitsWasm.get(), contract, slot).value;
this.writeCache.set(index, newValue);
}
}
Expand Down
1 change: 0 additions & 1 deletion yarn-project/world-state/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './synchronizer/index.js';
export * from './world-state-db/index.js';
export * from './utils.js';
export * from './synchronizer/config.js';
35 changes: 0 additions & 35 deletions yarn-project/world-state/src/utils.ts

This file was deleted.

9 changes: 4 additions & 5 deletions yarn-project/world-state/src/world-state-db/merkle_trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
PRIVATE_DATA_TREE_HEIGHT,
PUBLIC_DATA_TREE_HEIGHT,
} from '@aztec/circuits.js';
import { computeBlockHash } from '@aztec/circuits.js/abis';
import { computeBlockHash, computeGlobalsHash } from '@aztec/circuits.js/abis';
import { Committable } from '@aztec/foundation/committable';
import { SerialQueue } from '@aztec/foundation/fifo';
import { createDebugLogger } from '@aztec/foundation/log';
Expand All @@ -33,7 +33,6 @@ import { L2Block, MerkleTreeId, SiblingPath } from '@aztec/types';
import { default as levelup } from 'levelup';

import { MerkleTreeOperationsFacade } from '../merkle-tree/merkle_tree_operations_facade.js';
import { computeGlobalVariablesHash } from '../utils.js';
import {
CurrentTreeRoots,
HandleL2BlockResult,
Expand Down Expand Up @@ -127,12 +126,12 @@ export class MerkleTrees implements MerkleTreeDb {

// The first leaf in the blocks tree contains the empty roots of the other trees and empty global variables.
if (!fromDb) {
const initialGlobalVariablesHash = await computeGlobalVariablesHash(GlobalVariables.empty());
const initialGlobalVariablesHash = computeGlobalsHash(wasm, GlobalVariables.empty());
await this._updateLatestGlobalVariablesHash(initialGlobalVariablesHash);
await this._updateHistoricBlocksTree(initialGlobalVariablesHash, true);
await this._commit();
} else {
await this._updateLatestGlobalVariablesHash(await computeGlobalVariablesHash(fromDbOptions.globalVariables));
await this._updateLatestGlobalVariablesHash(computeGlobalsHash(wasm, fromDbOptions.globalVariables));
}
}

Expand Down Expand Up @@ -575,7 +574,7 @@ export class MerkleTrees implements MerkleTreeDb {
}

// Sync and add the block to the historic blocks tree
const globalVariablesHash = await computeGlobalVariablesHash(l2Block.globalVariables);
const globalVariablesHash = computeGlobalsHash(await CircuitsWasm.get(), l2Block.globalVariables);
await this._updateLatestGlobalVariablesHash(globalVariablesHash);
this.log(`Synced global variables with hash ${globalVariablesHash}`);

Expand Down

0 comments on commit e8099cc

Please sign in to comment.