Skip to content

Commit

Permalink
feat: PublicDataTreeRootsTree on TS side
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed May 2, 2023
1 parent 823979a commit f0e2737
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/structs/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const NULLIFIER_TREE_HEIGHT = 8;

export const PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT = 8;
export const CONTRACT_TREE_ROOTS_TREE_HEIGHT = 8;
export const PUBLIC_DATA_TREE_ROOTS_TREE_HEIGHT = 8;
export const ROLLUP_VK_TREE_HEIGHT = 8;

export const FUNCTION_SELECTOR_NUM_BYTES = 4;
16 changes: 15 additions & 1 deletion yarn-project/circuits.js/src/structs/rollup/root_rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { BufferReader, Fr } from '@aztec/foundation';
import { assertLength, FieldsOf } from '../../utils/jsUtils.js';
import { serializeToBuffer } from '../../utils/serialize.js';
import { AppendOnlyTreeSnapshot } from './append_only_tree_snapshot.js';
import { CONTRACT_TREE_ROOTS_TREE_HEIGHT, PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT } from '../constants.js';
import {
CONTRACT_TREE_ROOTS_TREE_HEIGHT,
PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT,
PUBLIC_DATA_TREE_ROOTS_TREE_HEIGHT,
} from '../constants.js';
import { PreviousRollupData } from './previous_rollup_data.js';
import { AggregationObject } from '../aggregation_object.js';

Expand All @@ -12,9 +16,11 @@ export class RootRollupInputs {

public newHistoricPrivateDataTreeRootSiblingPath: Fr[],
public newHistoricContractDataTreeRootSiblingPath: Fr[],
public newHistoricPublicDataTreeRootSiblingPath: Fr[],
) {
assertLength(this, 'newHistoricPrivateDataTreeRootSiblingPath', PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT);
assertLength(this, 'newHistoricContractDataTreeRootSiblingPath', CONTRACT_TREE_ROOTS_TREE_HEIGHT);
assertLength(this, 'newHistoricPublicDataTreeRootSiblingPath', PUBLIC_DATA_TREE_ROOTS_TREE_HEIGHT);
}

toBuffer() {
Expand All @@ -34,6 +40,7 @@ export class RootRollupInputs {
fields.previousRollupData,
fields.newHistoricPrivateDataTreeRootSiblingPath,
fields.newHistoricContractDataTreeRootSiblingPath,
fields.newHistoricPublicDataTreeRootSiblingPath,
] as const;
}
}
Expand Down Expand Up @@ -63,6 +70,9 @@ export class RootRollupPublicInputs {
public startTreeOfHistoricContractTreeRootsSnapshot: AppendOnlyTreeSnapshot,
public endTreeOfHistoricContractTreeRootsSnapshot: AppendOnlyTreeSnapshot,

public startTreeOfHistoricPublicDataTreeRootsSnapshot: AppendOnlyTreeSnapshot,
public endTreeOfHistoricPublicDataTreeRootsSnapshot: AppendOnlyTreeSnapshot,

public calldataHash: [Fr, Fr],
) {}

Expand All @@ -81,6 +91,8 @@ export class RootRollupPublicInputs {
fields.endTreeOfHistoricPrivateDataTreeRootsSnapshot,
fields.startTreeOfHistoricContractTreeRootsSnapshot,
fields.endTreeOfHistoricContractTreeRootsSnapshot,
fields.startTreeOfHistoricPublicDataTreeRootsSnapshot,
fields.endTreeOfHistoricPublicDataTreeRootsSnapshot,
fields.calldataHash,
] as const;
}
Expand Down Expand Up @@ -109,6 +121,8 @@ export class RootRollupPublicInputs {
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
[reader.readFr(), reader.readFr()],
);
}
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/circuits.js/src/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT,
PUBLIC_CALL_STACK_LENGTH,
PUBLIC_DATA_TREE_HEIGHT,
PUBLIC_DATA_TREE_ROOTS_TREE_HEIGHT,
RETURN_VALUES_LENGTH,
ROLLUP_VK_TREE_HEIGHT,
STATE_READS_LENGTH,
Expand Down Expand Up @@ -430,6 +431,7 @@ export function makeRootRollupInputs(seed = 0) {
[makePreviousBaseRollupData(seed), makePreviousBaseRollupData(seed + 0x1000)],
range(PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT, 0x2000).map(fr),
range(CONTRACT_TREE_ROOTS_TREE_HEIGHT, 0x2100).map(fr),
range(PUBLIC_DATA_TREE_ROOTS_TREE_HEIGHT, 0x2200).map(fr),
);
}

Expand All @@ -448,6 +450,8 @@ export function makeRootRollupPublicInputs(seed = 0) {
endTreeOfHistoricPrivateDataTreeRootsSnapshot: makeAppendOnlyTreeSnapshot((seed += 0x100)),
startTreeOfHistoricContractTreeRootsSnapshot: makeAppendOnlyTreeSnapshot((seed += 0x100)),
endTreeOfHistoricContractTreeRootsSnapshot: makeAppendOnlyTreeSnapshot((seed += 0x100)),
startTreeOfHistoricPublicDataTreeRootsSnapshot: makeAppendOnlyTreeSnapshot((seed += 0x100)),
endTreeOfHistoricPublicDataTreeRootsSnapshot: makeAppendOnlyTreeSnapshot((seed += 0x100)),
calldataHash: [new Fr(1n), new Fr(2n)],
});
}
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/l1-contracts/artifacts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './RollupAbi.js';
export * from './RollupBytecode.js';
export * from './UnverifiedDataEmitterAbi.js';
export * from './UnverifiedDataEmitterBytecode.js';
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class CircuitBlockBuilder implements BlockBuilder {
startPublicDataTreeSnapshot,
startTreeOfHistoricPrivateDataTreeRootsSnapshot,
startTreeOfHistoricContractTreeRootsSnapshot,
startTreeOfHistoricPublicDataTreeRootsSnapshot,
] = await Promise.all(
[
MerkleTreeId.PRIVATE_DATA_TREE,
Expand All @@ -98,6 +99,7 @@ export class CircuitBlockBuilder implements BlockBuilder {
MerkleTreeId.PUBLIC_DATA_TREE,
MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE,
MerkleTreeId.CONTRACT_TREE_ROOTS_TREE,
MerkleTreeId.PUBLIC_DATA_TREE_ROOTS_TREE,
].map(tree => this.getTreeSnapshot(tree)),
);

Expand All @@ -111,6 +113,7 @@ export class CircuitBlockBuilder implements BlockBuilder {
endPublicDataTreeRoot,
endTreeOfHistoricPrivateDataTreeRootsSnapshot,
endTreeOfHistoricContractTreeRootsSnapshot,
endTreeOfHistoricPublicDataTreeRootsSnapshot,
} = circuitsOutput;

// Collect all new nullifiers, commitments, and contracts from all txs in this block
Expand Down Expand Up @@ -139,6 +142,8 @@ export class CircuitBlockBuilder implements BlockBuilder {
endTreeOfHistoricPrivateDataTreeRootsSnapshot,
startTreeOfHistoricContractTreeRootsSnapshot,
endTreeOfHistoricContractTreeRootsSnapshot,
startTreeOfHistoricPublicDataTreeRootsSnapshot,
endTreeOfHistoricPublicDataTreeRootsSnapshot,
newCommitments,
newNullifiers,
newContracts,
Expand Down Expand Up @@ -357,11 +362,15 @@ export class CircuitBlockBuilder implements BlockBuilder {
const newHistoricPrivateDataTreeRootSiblingPath = await getRootTreeSiblingPath(
MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE,
);
const newHistoricPublicDataTreeRootSiblingPath = await getRootTreeSiblingPath(
MerkleTreeId.PUBLIC_DATA_TREE_ROOTS_TREE,
);

return RootRollupInputs.from({
previousRollupData,
newHistoricContractDataTreeRootSiblingPath,
newHistoricPrivateDataTreeRootSiblingPath,
newHistoricPublicDataTreeRootSiblingPath,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class StandaloneBlockBuilder implements BlockBuilder {
const startTreeOfHistoricContractTreeRootsSnapshot = await this.getTreeSnapshot(
MerkleTreeId.CONTRACT_TREE_ROOTS_TREE,
);
const startTreeOfHistoricPublicDataTreeRootsSnapshot = await this.getTreeSnapshot(
MerkleTreeId.PUBLIC_DATA_TREE_ROOTS_TREE,
);

for (const tx of txs) {
await this.updateTrees(tx);
Expand All @@ -59,6 +62,9 @@ export class StandaloneBlockBuilder implements BlockBuilder {
const endTreeOfHistoricContractTreeRootsSnapshot = await this.getTreeSnapshot(
MerkleTreeId.CONTRACT_TREE_ROOTS_TREE,
);
const endTreeOfHistoricPublicDataTreeRootsSnapshot = await this.getTreeSnapshot(
MerkleTreeId.PUBLIC_DATA_TREE_ROOTS_TREE,
);

const l2Block = L2Block.fromFields({
number: blockNumber,
Expand All @@ -74,6 +80,8 @@ export class StandaloneBlockBuilder implements BlockBuilder {
endTreeOfHistoricPrivateDataTreeRootsSnapshot,
startTreeOfHistoricContractTreeRootsSnapshot,
endTreeOfHistoricContractTreeRootsSnapshot,
startTreeOfHistoricPublicDataTreeRootsSnapshot,
endTreeOfHistoricPublicDataTreeRootsSnapshot,
newCommitments: this.dataTreeLeaves.map(b => Fr.fromBuffer(b)),
newNullifiers: this.nullifierTreeLeaves.map(b => Fr.fromBuffer(b)),
newContracts: this.contractTreeLeaves.map(b => Fr.fromBuffer(b)),
Expand Down
14 changes: 14 additions & 0 deletions yarn-project/types/src/l2_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ export class L2Block {
* @param startTreeOfHistoricPrivateDataTreeRootsSnapshot - The tree snapshot of the historic private data tree roots at the start of the rollup.
* @param startTreeOfHistoricContractTreeRootsSnapshot - The tree snapshot of the historic contract tree roots at the start of the rollup.
* @param startPublicDataTreeRoot - The tree root of the public data tree at the start of the rollup.
* @param startTreeOfHistoricPublicDataTreeRootsSnapshot - The tree snapshot of the historic public data tree roots at the start of the rollup.
* @param endPrivateDataTreeSnapshot - The tree snapshot of the private data tree at the end of the rollup.
* @param endNullifierTreeSnapshot - The tree snapshot of the nullifier tree at the end of the rollup.
* @param endContractTreeSnapshot - The tree snapshot of the contract tree at the end of the rollup.
* @param endTreeOfHistoricPrivateDataTreeRootsSnapshot - The tree snapshot of the historic private data tree roots at the end of the rollup.
* @param endTreeOfHistoricContractTreeRootsSnapshot - The tree snapshot of the historic contract tree roots at the end of the rollup.
* @param endPublicDataTreeRoot - The tree root of the public data tree at the end of the rollup.
* @param endTreeOfHistoricPublicDataTreeRootsSnapshot - The tree snapshot of the historic public data tree roots at the end of the rollup.
* @param newCommitments - The commitments to be inserted into the private data tree.
* @param newNullifiers - The nullifiers to be inserted into the nullifier tree.
* @param newPublicDataWrites - The public data writes to be inserted into the public data tree.
Expand All @@ -48,12 +50,14 @@ export class L2Block {
public startTreeOfHistoricPrivateDataTreeRootsSnapshot: AppendOnlyTreeSnapshot,
public startTreeOfHistoricContractTreeRootsSnapshot: AppendOnlyTreeSnapshot,
public startPublicDataTreeRoot: Fr,
public startTreeOfHistoricPublicDataTreeRootsSnapshot: AppendOnlyTreeSnapshot,
public endPrivateDataTreeSnapshot: AppendOnlyTreeSnapshot,
public endNullifierTreeSnapshot: AppendOnlyTreeSnapshot,
public endContractTreeSnapshot: AppendOnlyTreeSnapshot,
public endTreeOfHistoricPrivateDataTreeRootsSnapshot: AppendOnlyTreeSnapshot,
public endTreeOfHistoricContractTreeRootsSnapshot: AppendOnlyTreeSnapshot,
public endPublicDataTreeRoot: Fr,
public endTreeOfHistoricPublicDataTreeRootsSnapshot: AppendOnlyTreeSnapshot,
public newCommitments: Fr[],
public newNullifiers: Fr[],
public newPublicDataWrites: PublicDataWrite[],
Expand All @@ -76,12 +80,14 @@ export class L2Block {
startPublicDataTreeRoot: Fr.random(),
startTreeOfHistoricPrivateDataTreeRootsSnapshot: makeAppendOnlyTreeSnapshot(0),
startTreeOfHistoricContractTreeRootsSnapshot: makeAppendOnlyTreeSnapshot(0),
startTreeOfHistoricPublicDataTreeRootsSnapshot: makeAppendOnlyTreeSnapshot(0),
endPrivateDataTreeSnapshot: makeAppendOnlyTreeSnapshot(newCommitments.length),
endNullifierTreeSnapshot: makeAppendOnlyTreeSnapshot(newNullifiers.length),
endContractTreeSnapshot: makeAppendOnlyTreeSnapshot(newContracts.length),
endPublicDataTreeRoot: Fr.random(),
endTreeOfHistoricPrivateDataTreeRootsSnapshot: makeAppendOnlyTreeSnapshot(1),
endTreeOfHistoricContractTreeRootsSnapshot: makeAppendOnlyTreeSnapshot(1),
endTreeOfHistoricPublicDataTreeRootsSnapshot: makeAppendOnlyTreeSnapshot(1),
newCommitments,
newNullifiers,
newContracts,
Expand All @@ -103,12 +109,14 @@ export class L2Block {
startTreeOfHistoricPrivateDataTreeRootsSnapshot: AppendOnlyTreeSnapshot;
startTreeOfHistoricContractTreeRootsSnapshot: AppendOnlyTreeSnapshot;
startPublicDataTreeRoot: Fr;
startTreeOfHistoricPublicDataTreeRootsSnapshot: AppendOnlyTreeSnapshot;
endPrivateDataTreeSnapshot: AppendOnlyTreeSnapshot;
endNullifierTreeSnapshot: AppendOnlyTreeSnapshot;
endContractTreeSnapshot: AppendOnlyTreeSnapshot;
endTreeOfHistoricPrivateDataTreeRootsSnapshot: AppendOnlyTreeSnapshot;
endTreeOfHistoricContractTreeRootsSnapshot: AppendOnlyTreeSnapshot;
endPublicDataTreeRoot: Fr;
endTreeOfHistoricPublicDataTreeRootsSnapshot: AppendOnlyTreeSnapshot;
newCommitments: Fr[];
newNullifiers: Fr[];
newPublicDataWrites: PublicDataWrite[];
Expand All @@ -123,12 +131,14 @@ export class L2Block {
fields.startTreeOfHistoricPrivateDataTreeRootsSnapshot,
fields.startTreeOfHistoricContractTreeRootsSnapshot,
fields.startPublicDataTreeRoot,
fields.startTreeOfHistoricPublicDataTreeRootsSnapshot,
fields.endPrivateDataTreeSnapshot,
fields.endNullifierTreeSnapshot,
fields.endContractTreeSnapshot,
fields.endTreeOfHistoricPrivateDataTreeRootsSnapshot,
fields.endTreeOfHistoricContractTreeRootsSnapshot,
fields.endPublicDataTreeRoot,
fields.endTreeOfHistoricPublicDataTreeRootsSnapshot,
fields.newCommitments,
fields.newNullifiers,
fields.newPublicDataWrites,
Expand Down Expand Up @@ -190,12 +200,14 @@ export class L2Block {
const startTreeOfHistoricPrivateDataTreeRootsSnapshot = reader.readObject(AppendOnlyTreeSnapshot);
const startTreeOfHistoricContractTreeRootsSnapshot = reader.readObject(AppendOnlyTreeSnapshot);
const startPublicDataTreeRoot = reader.readObject(Fr);
const startTreeOfHistoricPublicDataTreeRootsSnapshot = reader.readObject(AppendOnlyTreeSnapshot);
const endPrivateDataTreeSnapshot = reader.readObject(AppendOnlyTreeSnapshot);
const endNullifierTreeSnapshot = reader.readObject(AppendOnlyTreeSnapshot);
const endContractTreeSnapshot = reader.readObject(AppendOnlyTreeSnapshot);
const endTreeOfHistoricPrivateDataTreeRootsSnapshot = reader.readObject(AppendOnlyTreeSnapshot);
const endTreeOfHistoricContractTreeRootsSnapshot = reader.readObject(AppendOnlyTreeSnapshot);
const endPublicDataTreeRoot = reader.readObject(Fr);
const endTreeOfHistoricPublicDataTreeRootsSnapshot = reader.readObject(AppendOnlyTreeSnapshot);
const newCommitments = reader.readVector(Fr);
const newNullifiers = reader.readVector(Fr);
const newPublicDataWrites = reader.readVector(PublicDataWrite);
Expand All @@ -210,12 +222,14 @@ export class L2Block {
startTreeOfHistoricPrivateDataTreeRootsSnapshot,
startTreeOfHistoricContractTreeRootsSnapshot,
startPublicDataTreeRoot,
startTreeOfHistoricPublicDataTreeRootsSnapshot,
endPrivateDataTreeSnapshot,
endNullifierTreeSnapshot,
endContractTreeSnapshot,
endTreeOfHistoricPrivateDataTreeRootsSnapshot,
endTreeOfHistoricContractTreeRootsSnapshot,
endPublicDataTreeRoot,
endTreeOfHistoricPublicDataTreeRootsSnapshot,
newCommitments,
newNullifiers,
newPublicDataWrites,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ const getMockBlock = (blockNumber: number, newContractsCommitments?: Buffer[]) =
startTreeOfHistoricPrivateDataTreeRootsSnapshot: getMockTreeSnapshot(),
startTreeOfHistoricContractTreeRootsSnapshot: getMockTreeSnapshot(),
startPublicDataTreeRoot: Fr.random(),
startTreeOfHistoricPublicDataTreeRootsSnapshot: getMockTreeSnapshot(),
endPrivateDataTreeSnapshot: getMockTreeSnapshot(),
endNullifierTreeSnapshot: getMockTreeSnapshot(),
endContractTreeSnapshot: getMockTreeSnapshot(),
endTreeOfHistoricPrivateDataTreeRootsSnapshot: getMockTreeSnapshot(),
endTreeOfHistoricContractTreeRootsSnapshot: getMockTreeSnapshot(),
endPublicDataTreeRoot: Fr.random(),
endTreeOfHistoricPublicDataTreeRootsSnapshot: getMockTreeSnapshot(),
newCommitments: [Fr.random()],
newNullifiers: [Fr.random()],
newContracts: newContractsCommitments?.map(x => Fr.fromBuffer(x)) ?? [Fr.random()],
Expand Down
1 change: 1 addition & 0 deletions yarn-project/world-state/src/world-state-db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum MerkleTreeId {
PRIVATE_DATA_TREE = 3,
PRIVATE_DATA_TREE_ROOTS_TREE = 4,
PUBLIC_DATA_TREE = 5,
PUBLIC_DATA_TREE_ROOTS_TREE = 6,
}

/**
Expand Down
9 changes: 9 additions & 0 deletions yarn-project/world-state/src/world-state-db/merkle_trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PRIVATE_DATA_TREE_HEIGHT,
PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT,
PUBLIC_DATA_TREE_HEIGHT,
PUBLIC_DATA_TREE_ROOTS_TREE_HEIGHT,
} from '@aztec/circuits.js';
import { SerialQueue } from '@aztec/foundation';
import { WasmWrapper } from '@aztec/foundation/wasm';
Expand Down Expand Up @@ -92,13 +93,21 @@ export class MerkleTrees implements MerkleTreeDb {
`${MerkleTreeId[MerkleTreeId.PUBLIC_DATA_TREE]}`,
PUBLIC_DATA_TREE_HEIGHT,
);
const publicDataTreeRootsTree: AppendOnlyTree = await newTree(
StandardTree,
this.db,
hasher,
`${MerkleTreeId[MerkleTreeId.PUBLIC_DATA_TREE_ROOTS_TREE]}`,
PUBLIC_DATA_TREE_ROOTS_TREE_HEIGHT,
);
this.trees = [
contractTree,
contractTreeRootsTree,
nullifierTree,
privateDataTree,
privateDataTreeRootsTree,
publicDataTree,
publicDataTreeRootsTree,
];
this.jobQueue.start();
}
Expand Down

0 comments on commit f0e2737

Please sign in to comment.