Skip to content

Commit

Permalink
syntax sugar + fix
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jan 31, 2024
1 parent f80741e commit f2b292b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('L1Publisher integration', () => {
l1BlockPublishRetryIntervalMS: 100,
});

prevHeader = await buildInitialHeader(builderDb);
prevHeader = await builderDb.buildInitialHeader();
}, 100_000);

const makeEmptyProcessedTx = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ export class SoloBlockBuilder implements BlockBuilder {

const rootProof = await this.prover.getRootRollupProof(rootInput, rootOutput);

// Update the root trees with the latest data and contract tree roots,
// and validate them against the output of the root circuit simulation
// Update the archive with the latest block header
this.debug(`Updating and validating root trees`);
await this.db.updateArchive(rootOutput.header);

Expand Down Expand Up @@ -702,7 +701,7 @@ export class SoloBlockBuilder implements BlockBuilder {
publicDataReadsPreimages: txPublicDataReadsInfo.newPublicDataReadsPreimages,
publicDataReadsMembershipWitnesses: txPublicDataReadsInfo.newPublicDataReadsWitnesses,

archiveRootMembershipWitness, // TODO(benesjan): This is a bad name. Rename it to kernelHeaderMembershipWitness
archiveRootMembershipWitness,

constants,
});
Expand Down
65 changes: 25 additions & 40 deletions yarn-project/world-state/src/world-state-db/merkle_trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class MerkleTrees implements MerkleTreeDb {
await this.#updateArchive(initialHeder, true);
}

await this._commit();
await this.#commit();
}

/**
Expand Down Expand Up @@ -192,7 +192,7 @@ export class MerkleTrees implements MerkleTreeDb {
* @returns The tree info for the specified tree.
*/
public async getTreeInfo(treeId: MerkleTreeId, includeUncommitted: boolean): Promise<TreeInfo> {
return await this.synchronize(() => this._getTreeInfo(treeId, includeUncommitted));
return await this.synchronize(() => this.#getTreeInfo(treeId, includeUncommitted));
}

/**
Expand Down Expand Up @@ -248,7 +248,7 @@ export class MerkleTrees implements MerkleTreeDb {
index: bigint,
includeUncommitted: boolean,
): Promise<SiblingPath<N>> {
return await this.synchronize(() => this._getSiblingPath(treeId, index, includeUncommitted));
return await this.synchronize(() => this.trees[treeId].getSiblingPath<N>(index, includeUncommitted));
}

/**
Expand All @@ -258,23 +258,23 @@ export class MerkleTrees implements MerkleTreeDb {
* @returns Empty promise.
*/
public async appendLeaves(treeId: MerkleTreeId, leaves: Buffer[]): Promise<void> {
return await this.synchronize(() => this._appendLeaves(treeId, leaves));
return await this.synchronize(() => this.#appendLeaves(treeId, leaves));
}

/**
* Commits all pending updates.
* @returns Empty promise.
*/
public async commit(): Promise<void> {
return await this.synchronize(() => this._commit());
return await this.synchronize(() => this.#commit());
}

/**
* Rolls back all pending updates.
* @returns Empty promise.
*/
public async rollback(): Promise<void> {
return await this.synchronize(() => this._rollback());
return await this.synchronize(() => this.#rollback());
}

/**
Expand Down Expand Up @@ -302,7 +302,7 @@ export class MerkleTrees implements MerkleTreeDb {
| undefined
> {
return await this.synchronize(() =>
Promise.resolve(this._getIndexedTree(treeId).findIndexOfPreviousKey(value, includeUncommitted)),
Promise.resolve(this.#getIndexedTree(treeId).findIndexOfPreviousKey(value, includeUncommitted)),
);
}

Expand All @@ -319,7 +319,7 @@ export class MerkleTrees implements MerkleTreeDb {
includeUncommitted: boolean,
): Promise<IndexedTreeLeafPreimage | undefined> {
return await this.synchronize(() =>
Promise.resolve(this._getIndexedTree(treeId).getLatestLeafPreimageCopy(index, includeUncommitted)),
Promise.resolve(this.#getIndexedTree(treeId).getLatestLeafPreimageCopy(index, includeUncommitted)),
);
}

Expand Down Expand Up @@ -349,7 +349,7 @@ export class MerkleTrees implements MerkleTreeDb {
* @returns Empty promise.
*/
public async updateLeaf(treeId: IndexedTreeId, leaf: Buffer, index: bigint): Promise<void> {
return await this.synchronize(() => this._updateLeaf(treeId, leaf, index));
return await this.synchronize(() => this.#updateLeaf(treeId, leaf, index));
}

/**
Expand All @@ -358,7 +358,7 @@ export class MerkleTrees implements MerkleTreeDb {
* @returns Whether the block handled was produced by this same node.
*/
public async handleL2Block(block: L2Block): Promise<HandleL2BlockResult> {
return await this.synchronize(() => this._handleL2Block(block));
return await this.synchronize(() => this.#handleL2Block(block));
}

/**
Expand Down Expand Up @@ -401,7 +401,7 @@ export class MerkleTrees implements MerkleTreeDb {
}

const blockHash = header.hash();
await this._appendLeaves(MerkleTreeId.ARCHIVE, [blockHash.toBuffer()]);
await this.#appendLeaves(MerkleTreeId.ARCHIVE, [blockHash.toBuffer()]);
}

/**
Expand All @@ -410,7 +410,7 @@ export class MerkleTrees implements MerkleTreeDb {
* @param includeUncommitted - Indicates whether to include uncommitted data.
* @returns The tree info for the specified tree.
*/
private _getTreeInfo(treeId: MerkleTreeId, includeUncommitted: boolean): Promise<TreeInfo> {
#getTreeInfo(treeId: MerkleTreeId, includeUncommitted: boolean): Promise<TreeInfo> {
const treeInfo = {
treeId,
root: this.trees[treeId].getRoot(includeUncommitted),
Expand All @@ -425,40 +425,25 @@ export class MerkleTrees implements MerkleTreeDb {
* @param treeId - Id of the tree to get an instance of.
* @returns The indexed tree for the specified tree id.
*/
private _getIndexedTree(treeId: IndexedTreeId): IndexedTree {
#getIndexedTree(treeId: IndexedTreeId): IndexedTree {
return this.trees[treeId] as IndexedTree;
}

/**
* Returns the sibling path for a leaf in a tree.
* @param treeId - Id of the tree to get the sibling path from.
* @param index - Index of the leaf to get the sibling path for.
* @param includeUncommitted - Indicates whether to include uncommitted updates in the sibling path.
* @returns Promise containing the sibling path for the leaf.
*/
private _getSiblingPath<N extends number>(
treeId: MerkleTreeId,
index: bigint,
includeUncommitted: boolean,
): Promise<SiblingPath<N>> {
return Promise.resolve(this.trees[treeId].getSiblingPath<N>(index, includeUncommitted));
}

/**
* Appends leaves to a tree.
* @param treeId - Id of the tree to append leaves to.
* @param leaves - Leaves to append.
* @returns Empty promise.
*/
private async _appendLeaves(treeId: MerkleTreeId, leaves: Buffer[]): Promise<void> {
async #appendLeaves(treeId: MerkleTreeId, leaves: Buffer[]): Promise<void> {
const tree = this.trees[treeId];
if (!('appendLeaves' in tree)) {
throw new Error('Tree does not support `appendLeaves` method');
}
return await tree.appendLeaves(leaves);
}

private async _updateLeaf(treeId: IndexedTreeId, leaf: Buffer, index: bigint): Promise<void> {
async #updateLeaf(treeId: IndexedTreeId, leaf: Buffer, index: bigint): Promise<void> {
const tree = this.trees[treeId];
if (!('updateLeaf' in tree)) {
throw new Error('Tree does not support `updateLeaf` method');
Expand All @@ -470,7 +455,7 @@ export class MerkleTrees implements MerkleTreeDb {
* Commits all pending updates.
* @returns Empty promise.
*/
private async _commit(): Promise<void> {
async #commit(): Promise<void> {
for (const tree of this.trees) {
await tree.commit();
}
Expand All @@ -480,7 +465,7 @@ export class MerkleTrees implements MerkleTreeDb {
* Rolls back all pending updates.
* @returns Empty promise.
*/
private async _rollback(): Promise<void> {
async #rollback(): Promise<void> {
for (const tree of this.trees) {
await tree.rollback();
}
Expand All @@ -490,7 +475,7 @@ export class MerkleTrees implements MerkleTreeDb {
return Promise.all(this.trees.map(tree => tree.getSnapshot(blockNumber)));
}

private async _snapshot(blockNumber: number): Promise<void> {
async #snapshot(blockNumber: number): Promise<void> {
for (const tree of this.trees) {
await tree.snapshot(blockNumber);
}
Expand All @@ -500,7 +485,7 @@ export class MerkleTrees implements MerkleTreeDb {
* Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree).
* @param l2Block - The L2 block to handle.
*/
private async _handleL2Block(l2Block: L2Block): Promise<HandleL2BlockResult> {
async #handleL2Block(l2Block: L2Block): Promise<HandleL2BlockResult> {
const treeRootWithIdPairs = [
[l2Block.header.state.partial.contractTree.root, MerkleTreeId.CONTRACT_TREE],
[l2Block.header.state.partial.nullifierTree.root, MerkleTreeId.NULLIFIER_TREE],
Expand All @@ -516,18 +501,18 @@ export class MerkleTrees implements MerkleTreeDb {
const ourBlock = treeRootWithIdPairs.every(([root, id]) => compareRoot(root, id));
if (ourBlock) {
this.log(`Block ${l2Block.number} is ours, committing world state`);
await this._commit();
await this.#commit();
} else {
this.log(`Block ${l2Block.number} is not ours, rolling back world state and committing state from chain`);
await this._rollback();
await this.#rollback();

// Sync the append only trees
for (const [tree, leaves] of [
[MerkleTreeId.CONTRACT_TREE, l2Block.newContracts],
[MerkleTreeId.NOTE_HASH_TREE, l2Block.newCommitments],
[MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l2Block.newL1ToL2Messages],
] as const) {
await this._appendLeaves(
await this.#appendLeaves(
tree,
leaves.map(fr => fr.toBuffer()),
);
Expand All @@ -554,12 +539,12 @@ export class MerkleTrees implements MerkleTreeDb {
// The last thing remaining is to update the archive
await this.#updateArchive(l2Block.header, true);

await this._commit();
await this.#commit();
}

for (const [root, treeId] of treeRootWithIdPairs) {
const treeName = MerkleTreeId[treeId];
const info = await this._getTreeInfo(treeId, false);
const info = await this.#getTreeInfo(treeId, false);
const syncedStr = '0x' + info.root.toString('hex');
const rootStr = root.toString();
// Sanity check that the rebuilt trees match the roots published by the L2 block
Expand All @@ -571,7 +556,7 @@ export class MerkleTrees implements MerkleTreeDb {
this.log(`Tree ${treeName} synched with size ${info.size} root ${rootStr}`);
}
}
await this._snapshot(l2Block.number);
await this.#snapshot(l2Block.number);

return { isBlockOurs: ourBlock };
}
Expand Down

0 comments on commit f2b292b

Please sign in to comment.