Skip to content

Commit

Permalink
finished get_block_data oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Nov 23, 2023
1 parent cf031c5 commit ebe17dc
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 35 deletions.
10 changes: 10 additions & 0 deletions yarn-project/acir-simulator/src/acvm/oracle/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ export class Oracle {
return witness.map(toACVMField);
}

async getBlockData([blockNumber]: ACVMField[]): Promise<ACVMField[]> {
const parsedBlockNumber = frToNumber(fromACVMField(blockNumber));

const blockData = await this.typedOracle.getBlockData(parsedBlockNumber);
if (!blockData) {
throw new Error(`Block data not found for block ${parsedBlockNumber}.`);
}
return blockData.toArray().map(toACVMField);
}

async getAuthWitness([messageHash]: ACVMField[]): Promise<ACVMField[]> {
const messageHashField = fromACVMField(messageHash);
const witness = await this.typedOracle.getAuthWitness(messageHashField);
Expand Down
6 changes: 5 additions & 1 deletion yarn-project/acir-simulator/src/acvm/oracle/typed_oracle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrivateCallStackItem, PublicCallRequest } from '@aztec/circuits.js';
import { HistoricBlockData, PrivateCallStackItem, PublicCallRequest } from '@aztec/circuits.js';
import { FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
Expand Down Expand Up @@ -80,6 +80,10 @@ export abstract class TypedOracle {
throw new Error('Not available.');
}

getBlockData(_blockNumber: number): Promise<HistoricBlockData | undefined> {
throw new Error('Not available.');
}

getCompleteAddress(_address: AztecAddress): Promise<CompleteAddress> {
throw new Error('Not available.');
}
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
Expand Up @@ -3,7 +3,7 @@ import { FunctionArtifact, FunctionDebugMetadata, FunctionSelector } from '@azte
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { MerkleTreeId } from '@aztec/types';
import { L2Block, MerkleTreeId } from '@aztec/types';

import { NoteData } from '../acvm/index.js';
import { CommitmentsDB } from '../public/index.js';
Expand Down Expand Up @@ -130,6 +130,14 @@ export interface DBOracle extends CommitmentsDB {
* @param blockNumber - The block number at which to get the sibling path.
* @param treeId - The id of the tree to search.
* @param leafIndex - The index of the leaf.
* @returns - The sibling path of the leaf. Undefined if it does not exist in the tree.
*/
getSiblingPath(blockNumber: number, treeId: MerkleTreeId, leafIndex: bigint): Promise<Fr[]>;

/**
* Fetch a block corresponding to the given block number.
* @param blockNumber - The block number of a block to fetch.
* @returns - The block corresponding to the given block number. Undefined if it does not exist.
*/
getBlock(blockNumber: number): Promise<L2Block | undefined>;
}
24 changes: 23 additions & 1 deletion yarn-project/acir-simulator/src/client/view_data_oracle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HistoricBlockData, PublicKey } from '@aztec/circuits.js';
import { siloNullifier } from '@aztec/circuits.js/abis';
import { computeGlobalsHash, siloNullifier } from '@aztec/circuits.js/abis';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
Expand Down Expand Up @@ -51,6 +51,28 @@ export class ViewDataOracle extends TypedOracle {
return [new Fr(index), ...siblingPath];
}

/**
* Fetches historic block data for a given block.
* @param blockNumber - The block number at which to get the historic block data.
* @returns Historic block data extracted from a block with block number `blockNumber`.
*/
public async getBlockData(blockNumber: number): Promise<HistoricBlockData | undefined> {
const block = await this.db.getBlock(blockNumber);
if (!block) {
return undefined;
}
return new HistoricBlockData(
block.endNoteHashTreeSnapshot.root,
block.endNullifierTreeSnapshot.root,
block.endContractTreeSnapshot.root,
block.endL1ToL2MessagesTreeSnapshot.root,
block.endHistoricBlocksTreeSnapshot.root,
new Fr(0), // privateKernelVkTreeRoot is not present in L2Block and it's not yet populated in noir
block.endPublicDataTreeRoot,
computeGlobalsHash(block.globalVariables),
);
}

/**
* Retrieve the complete address associated to a given address.
* @param address - Address to fetch the complete address for.
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ export function computeBlockHash(
* Computes the globals hash given the globals.
* @param globals - The global variables to put into the block hash.
* @returns The globals hash.
* TODO: move this to GlobalVariables?
*/
export function computeGlobalsHash(globals: GlobalVariables): Fr {
return Fr.fromBuffer(
Expand Down
19 changes: 6 additions & 13 deletions yarn-project/end-to-end/src/e2e_liquidity_mining.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { AccountWallet, CheatCodes, CompleteAddress, DebugLogger } from '@aztec/aztec.js';
import { Pedersen, SparseTree, newTree } from '@aztec/merkle-tree';
import { AccountWallet, CompleteAddress, DebugLogger, PXE } from '@aztec/aztec.js';
import { LiquidityMiningContract } from '@aztec/noir-contracts/types';

import { jest } from '@jest/globals';
import levelup from 'levelup';
import { type MemDown, default as memdown } from 'memdown';

import { setup } from './fixtures/utils.js';
Expand All @@ -15,25 +13,18 @@ const TIMEOUT = 90_000;
describe('e2e_liquidity_mining', () => {
jest.setTimeout(TIMEOUT);

let pxe: PXE;
let teardown: () => Promise<void>;
let logger: DebugLogger;
let wallets: AccountWallet[];
let accounts: CompleteAddress[];
let cheatCodes: CheatCodes;

let contract: LiquidityMiningContract;

let simulatorTree: SparseTree;

beforeAll(async () => {
({ teardown, logger, wallets, accounts, cheatCodes } = await setup(1));
({ pxe, teardown, logger, wallets, accounts } = await setup(1));

contract = await LiquidityMiningContract.deploy(wallets[0]).send().deployed();

const db = levelup(createMemDown());
const hasher = new Pedersen();
const depth = 254;
simulatorTree = await newTree(SparseTree, db, hasher, 'test', depth);
}, 100_000);

afterAll(() => teardown());
Expand All @@ -55,7 +46,9 @@ describe('e2e_liquidity_mining', () => {

{
// Claim
const receipt = await contract.methods.claim(accounts[0].address).send().wait({ debug: true });
// We prove the note existence at current block number because we don't currently have historical data
const blockNumber = await pxe.getBlockNumber();
const receipt = await contract.methods.claim(accounts[0].address, blockNumber).send().wait({ debug: true });
const { newNullifiers } = receipt.debugInfo!;
expect(newNullifiers.length).toBe(2); // tx hash and note nullifier
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,8 @@ contract LiquidityMining {
) {
let balances = storage.balances.at(owner.address);

let not_ignored_block_number = block_number;
let ignored_block_number = 0; // Sibling path oracle ignores this now and only gets the path at the latest block

// 1.c)
let block_data = get_block_data(not_ignored_block_number);
// let block_data = context.block_data;
let block_data = get_block_data(block_number);

// TODO: Seems to make sense to move the following to `HistoricBlockData` struct.
// TODO: Would make sense to unify the ordering in `HistoricBlockData::serialize` function and the ordering
Expand All @@ -114,16 +110,15 @@ contract LiquidityMining {
let block_hash = pedersen_hash_with_separator(inputs, GENERATOR_INDEX__BLOCK_HASH);

// 1.d)
let blocks_tree_root = block_data.blocks_tree_root;
let blocks_tree_id = 5;
let witness: MembershipWitness<HISTORIC_BLOCKS_TREE_HEIGHT, HISTORIC_BLOCKS_TREE_HEIGHT + 1> =
get_membership_witness(ignored_block_number, blocks_tree_id, block_hash);
get_membership_witness(block_number, blocks_tree_id, block_hash);

// 1.e)
// In our test case this should be tree root at the latest block and should correspond to the membership
// witness we obtain from oracle
assert(
blocks_tree_root == compute_merkle_root(block_hash, witness.index, witness.path),
block_data.blocks_tree_root == compute_merkle_root(block_hash, witness.index, witness.path),
"Proving membership of a block in blocks tree failed"
);

Expand All @@ -138,14 +133,13 @@ contract LiquidityMining {
// 1.h)
let note_hash_tree_id = 2;
let witness: MembershipWitness<NOTE_HASH_TREE_HEIGHT, NOTE_HASH_TREE_HEIGHT + 1> =
get_membership_witness(ignored_block_number, note_hash_tree_id, note_commitment);
get_membership_witness(block_number, note_hash_tree_id, note_commitment);

// 1.i)
// In our test case this should be tree root at the latest block and should correspond to the membership
// witness we obtain from oracle
let note_hash_tree_root = block_data.note_hash_tree_root;
assert(
note_hash_tree_root == compute_merkle_root(note_commitment, witness.index, witness.path),
block_data.note_hash_tree_root == compute_merkle_root(note_commitment, witness.index, witness.path),
"Proving membership of a note in note has tree failed"
);

Expand Down
6 changes: 5 additions & 1 deletion yarn-project/pxe/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
PublicKey,
} from '@aztec/circuits.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { KeyStore, MerkleTreeId, StateInfoProvider } from '@aztec/types';
import { KeyStore, L2Block, MerkleTreeId, StateInfoProvider } from '@aztec/types';

import { ContractDataOracle } from '../contract_data_oracle/index.js';
import { Database } from '../database/index.js';
Expand Down Expand Up @@ -156,6 +156,10 @@ export class SimulatorOracle implements DBOracle {
}
}

public async getBlock(blockNumber: number): Promise<L2Block | undefined> {
return await this.stateInfoProvider.getBlock(blockNumber);
}

/**
* 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.
Expand Down
7 changes: 0 additions & 7 deletions yarn-project/types/src/interfaces/aztec-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ export interface AztecNode extends StateInfoProvider {
*/
isReady(): Promise<boolean>;

/**
* Get the a given block.
* @param number - The block number being requested.
* @returns The blocks requested.
*/
getBlock(number: number): Promise<L2Block | undefined>;

/**
* Method to request blocks. Will attempt to return all requested blocks but will return only those available.
* @param from - The start of the range of blocks to return.
Expand Down
8 changes: 8 additions & 0 deletions yarn-project/types/src/interfaces/state_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@aztec/circuits.js';

import { L1ToL2MessageAndIndex } from '../l1_to_l2_message.js';
import { L2Block } from '../l2_block.js';
import { MerkleTreeId } from '../merkle_tree_id.js';
import { SiblingPath } from '../sibling_path.js';

Expand Down Expand Up @@ -61,4 +62,11 @@ export interface StateInfoProvider {
* TODO: https://github.com/AztecProtocol/aztec-packages/issues/3414
*/
getHistoricBlocksTreeSiblingPath(leafIndex: bigint): Promise<SiblingPath<typeof HISTORIC_BLOCKS_TREE_HEIGHT>>;

/**
* Get the a given block.
* @param number - The block number being requested.
* @returns The blocks requested.
*/
getBlock(number: number): Promise<L2Block | undefined>;
}

0 comments on commit ebe17dc

Please sign in to comment.