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: added missing oracles, fix block production #7768

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 34 additions & 27 deletions yarn-project/txe/src/oracle/txe_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ export class TXE implements TypedOracle {

// Utils

async #getTreesAt(blockNumber: number) {
const db =
blockNumber === (await this.getBlockNumber())
? this.trees.asLatest()
: new MerkleTreeSnapshotOperationsFacade(this.trees, blockNumber);
return db;
}

getChainId() {
return Promise.resolve(this.chainId);
}
Expand Down Expand Up @@ -174,11 +182,16 @@ export class TXE implements TypedOracle {
isStaticCall = false,
isDelegateCall = false,
) {
const trees = this.getTrees();
const stateReference = await trees.getStateReference(false);
const db = await this.#getTreesAt(blockNumber);
const previousBlockState = await this.#getTreesAt(blockNumber - 1);

const stateReference = await db.getStateReference();
const inputs = PrivateContextInputs.empty();
inputs.historicalHeader.globalVariables.blockNumber = new Fr(blockNumber);
inputs.historicalHeader.state = stateReference;
inputs.historicalHeader.lastArchive.root = Fr.fromBuffer(
(await previousBlockState.getTreeInfo(MerkleTreeId.ARCHIVE)).root,
);
inputs.callContext.msgSender = this.msgSender;
inputs.callContext.storageContractAddress = this.contractAddress;
inputs.callContext.isStaticCall = isStaticCall;
Expand Down Expand Up @@ -284,8 +297,14 @@ export class TXE implements TypedOracle {
return contractInstance;
}

getMembershipWitness(_blockNumber: number, _treeId: MerkleTreeId, _leafValue: Fr): Promise<Fr[] | undefined> {
throw new Error('Method not implemented.');
async getMembershipWitness(blockNumber: number, treeId: MerkleTreeId, leafValue: Fr): Promise<Fr[] | undefined> {
const db = await this.#getTreesAt(blockNumber);
const index = await db.findLeafIndex(treeId, leafValue.toBuffer());
if (!index) {
throw new Error(`Leaf value: ${leafValue} not found in ${MerkleTreeId[treeId]} at block ${blockNumber}`);
}
const siblingPath = await db.getSiblingPath(treeId, index);
return [new Fr(index), ...siblingPath.toFields()];
}

async getSiblingPath(blockNumber: number, treeId: MerkleTreeId, leafIndex: Fr) {
Expand All @@ -298,14 +317,14 @@ export class TXE implements TypedOracle {
blockNumber: number,
nullifier: Fr,
): Promise<NullifierMembershipWitness | undefined> {
const committedDb = new MerkleTreeSnapshotOperationsFacade(this.trees, blockNumber);
const index = await committedDb.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBuffer());
const db = await this.#getTreesAt(blockNumber);
const index = await db.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBuffer());
if (!index) {
return undefined;
}

const leafPreimagePromise = committedDb.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index);
const siblingPathPromise = committedDb.getSiblingPath<typeof NULLIFIER_TREE_HEIGHT>(
const leafPreimagePromise = db.getLeafPreimage(MerkleTreeId.NULLIFIER_TREE, index);
const siblingPathPromise = db.getSiblingPath<typeof NULLIFIER_TREE_HEIGHT>(
MerkleTreeId.NULLIFIER_TREE,
BigInt(index),
);
Expand Down Expand Up @@ -344,8 +363,12 @@ export class TXE implements TypedOracle {
throw new Error('Method not implemented.');
}

getHeader(_blockNumber: number): Promise<Header | undefined> {
throw new Error('Method not implemented.');
async getHeader(blockNumber: number): Promise<Header | undefined> {
const header = Header.empty();
const db = await this.#getTreesAt(blockNumber);
header.state = await db.getStateReference();
header.globalVariables.blockNumber = new Fr(blockNumber);
return header;
}

getCompleteAddress(account: AztecAddress) {
Expand Down Expand Up @@ -470,11 +493,7 @@ export class TXE implements TypedOracle {
blockNumber: number,
numberOfElements: number,
): Promise<Fr[]> {
const db =
blockNumber === (await this.getBlockNumber())
? this.trees.asLatest()
: new MerkleTreeSnapshotOperationsFacade(this.trees, blockNumber);

const db = await this.#getTreesAt(blockNumber);
const values = [];
for (let i = 0n; i < numberOfElements; i++) {
const storageSlot = startStorageSlot.add(new Fr(i));
Expand Down Expand Up @@ -690,18 +709,6 @@ export class TXE implements TypedOracle {
const header = Header.empty();
header.state = await this.trees.getStateReference(true);
header.globalVariables.blockNumber = new Fr(await this.getBlockNumber());
header.state.partial.nullifierTree.root = Fr.fromBuffer(
(await this.trees.getTreeInfo(MerkleTreeId.NULLIFIER_TREE, true)).root,
);
header.state.partial.noteHashTree.root = Fr.fromBuffer(
(await this.trees.getTreeInfo(MerkleTreeId.NOTE_HASH_TREE, true)).root,
);
header.state.partial.publicDataTree.root = Fr.fromBuffer(
(await this.trees.getTreeInfo(MerkleTreeId.PUBLIC_DATA_TREE, true)).root,
);
header.state.l1ToL2MessageTree.root = Fr.fromBuffer(
(await this.trees.getTreeInfo(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, true)).root,
);
const executor = new PublicExecutor(
new TXEPublicStateDB(this),
new ContractsDataSourcePublicDB(new TXEPublicContractDataSource(this)),
Expand Down
35 changes: 28 additions & 7 deletions yarn-project/txe/src/txe_service/txe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ export class TXEService {
const nBlocks = fromSingle(blocks).toNumber();
this.logger.debug(`time traveling ${nBlocks} blocks`);
const trees = (this.typedOracle as TXE).getTrees();
const header = Header.empty();
const l2Block = L2Block.empty();
header.state = await trees.getStateReference(true);
const blockNumber = await this.typedOracle.getBlockNumber();
header.globalVariables.blockNumber = new Fr(blockNumber);
l2Block.archive.root = Fr.fromBuffer((await trees.getTreeInfo(MerkleTreeId.ARCHIVE, true)).root);
l2Block.header = header;
for (let i = 0; i < nBlocks; i++) {
const blockNumber = await this.typedOracle.getBlockNumber();
const header = Header.empty();
const l2Block = L2Block.empty();
header.state = await trees.getStateReference(true);
header.globalVariables.blockNumber = new Fr(blockNumber);
await trees.appendLeaves(MerkleTreeId.ARCHIVE, [header.hash()]);
l2Block.archive.root = Fr.fromBuffer((await trees.getTreeInfo(MerkleTreeId.ARCHIVE, true)).root);
l2Block.header = header;
this.logger.debug(`Block ${blockNumber} created, header hash ${header.hash().toString()}`);
await trees.handleL2BlockAndMessages(l2Block, []);
(this.typedOracle as TXE).setBlockNumber(blockNumber + 1);
}
Expand Down Expand Up @@ -733,4 +733,25 @@ export class TXEService {
await (this.typedOracle as TXE).addNoteHashes(fromSingle(contractAddress), fromArray(noteHashes));
return toForeignCallResult([]);
}

async getHeader(blockNumber: ForeignCallSingle) {
const header = await this.typedOracle.getHeader(fromSingle(blockNumber).toNumber());
if (!header) {
throw new Error(`Block header not found for block ${blockNumber}.`);
}
return toForeignCallResult([toArray(header.toFields())]);
}

async getMembershipWitness(blockNumber: ForeignCallSingle, treeId: ForeignCallSingle, leafValue: ForeignCallSingle) {
const parsedBlockNumber = fromSingle(blockNumber).toNumber();
const parsedTreeId = fromSingle(treeId).toNumber();
const parsedLeafValue = fromSingle(leafValue);
const witness = await this.typedOracle.getMembershipWitness(parsedBlockNumber, parsedTreeId, parsedLeafValue);
if (!witness) {
throw new Error(
`Membership witness in tree ${MerkleTreeId[parsedTreeId]} not found for value ${parsedLeafValue} at block ${parsedBlockNumber}.`,
);
}
return toForeignCallResult([toArray(witness)]);
}
}
Loading