Skip to content

Commit

Permalink
Assemble public state date in circuit block builder
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino authored and benesjan committed Apr 28, 2023
1 parent 1a3ba7e commit 916fa8f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
CircuitsWasm,
Fr,
PublicDataRead,
PublicDataWrite,
PublicDataTransition,
RootRollupPublicInputs,
UInt8Vector,
} from '@aztec/circuits.js';
Expand All @@ -19,7 +19,7 @@ import {
makeRootRollupPublicInputs,
} from '@aztec/circuits.js/factories';
import { toBufferBE } from '@aztec/foundation';
import { Tx } from '@aztec/types';
import { PublicDataWrite, Tx } from '@aztec/types';
import { MerkleTreeId, MerkleTreeOperations, MerkleTrees } from '@aztec/world-state';
import { MockProxy, mock } from 'jest-mock-extended';
import { default as levelup } from 'levelup';
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('sequencer/circuit_block_builder', () => {
const tx = await makeProcessedTx(publicTx, makeKernelPublicInputs(seed), makeProof());
await setTxHistoricTreeRoots(tx);
tx.data.end.stateReads[0] = new PublicDataRead(fr(1), fr(0));
tx.data.end.stateTransitions[0] = new PublicDataWrite(fr(2), fr(0), fr(12));
tx.data.end.stateTransitions[0] = new PublicDataTransition(fr(2), fr(0), fr(12));
return tx;
};

Expand Down Expand Up @@ -258,18 +258,15 @@ describe('sequencer/circuit_block_builder', () => {

it('builds an L2 block with private and public txs', async () => {
const txs = await Promise.all([
makeContractDeployProcessedTx(),
makePublicCallProcessedTx(),
makeContractDeployProcessedTx(),
makeEmptyProcessedTx(),
makeEmptyProcessedTx(),
]);

const [l2Block] = await builder.buildL2Block(blockNumber, txs);
expect(l2Block.number).toEqual(blockNumber);

// TODO: Check that l2 block got the new state transitions once we merge
// https://github.com/AztecProtocol/aztec3-packages/pull/360

expect(l2Block.newPublicDataWrites[0]).toEqual(new PublicDataWrite(fr(2), fr(12)));
await updateExpectedTreesFromTxs(txs);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { computeContractLeaf } from '@aztec/circuits.js/abis';
import { Fr, createDebugLogger, toBigIntBE, toBufferBE } from '@aztec/foundation';
import { LeafData, SiblingPath } from '@aztec/merkle-tree';
import { ContractData, L2Block } from '@aztec/types';
import { ContractData, L2Block, PublicDataWrite } from '@aztec/types';
import { MerkleTreeId, MerkleTreeOperations } from '@aztec/world-state';
import chunk from 'lodash.chunk';
import flatMap from 'lodash.flatmap';
Expand Down Expand Up @@ -87,13 +87,15 @@ export class CircuitBlockBuilder implements BlockBuilder {
startPrivateDataTreeSnapshot,
startNullifierTreeSnapshot,
startContractTreeSnapshot,
startPublicDataTreeSnapshot,
startTreeOfHistoricPrivateDataTreeRootsSnapshot,
startTreeOfHistoricContractTreeRootsSnapshot,
] = await Promise.all(
[
MerkleTreeId.PRIVATE_DATA_TREE,
MerkleTreeId.NULLIFIER_TREE,
MerkleTreeId.CONTRACT_TREE,
MerkleTreeId.PUBLIC_DATA_TREE,
MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE,
MerkleTreeId.CONTRACT_TREE_ROOTS_TREE,
].map(tree => this.getTreeSnapshot(tree)),
Expand All @@ -106,6 +108,7 @@ export class CircuitBlockBuilder implements BlockBuilder {
endPrivateDataTreeSnapshot,
endNullifierTreeSnapshot,
endContractTreeSnapshot,
endPublicDataTreeSnapshot,
endTreeOfHistoricPrivateDataTreeRootsSnapshot,
endTreeOfHistoricContractTreeRootsSnapshot,
} = circuitsOutput;
Expand All @@ -118,6 +121,9 @@ export class CircuitBlockBuilder implements BlockBuilder {
const newContractData = flatMap(txs, tx => tx.data.end.newContracts).map(
n => new ContractData(n.contractAddress, n.portalContractAddress),
);
const newPublicDataWrites = flatMap(txs, tx =>
tx.data.end.stateTransitions.map(t => new PublicDataWrite(t.leafIndex, t.newValue)),
);

const l2Block = L2Block.fromFields({
number: blockNumber,
Expand All @@ -127,6 +133,8 @@ export class CircuitBlockBuilder implements BlockBuilder {
endNullifierTreeSnapshot,
startContractTreeSnapshot,
endContractTreeSnapshot,
startPublicDataTreeRoot: startPublicDataTreeSnapshot.root,
endPublicDataTreeRoot: endPublicDataTreeSnapshot.root,
startTreeOfHistoricPrivateDataTreeRootsSnapshot,
endTreeOfHistoricPrivateDataTreeRootsSnapshot,
startTreeOfHistoricContractTreeRootsSnapshot,
Expand All @@ -135,6 +143,7 @@ export class CircuitBlockBuilder implements BlockBuilder {
newNullifiers,
newContracts,
newContractData,
newPublicDataWrites,
});

return [l2Block, proof];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@aztec/circuits.js';
import { computeContractLeaf } from '@aztec/circuits.js/abis';
import { AztecAddress, Fr, createDebugLogger } from '@aztec/foundation';
import { ContractData, L2Block } from '@aztec/types';
import { ContractData, L2Block, PublicDataWrite } from '@aztec/types';
import { MerkleTreeId, MerkleTreeOperations } from '@aztec/world-state';
import { Proof } from '../prover/index.js';
import { ProcessedTx } from '../sequencer/processed_tx.js';
Expand All @@ -35,6 +35,7 @@ export class StandaloneBlockBuilder implements BlockBuilder {
const startPrivateDataTreeSnapshot = await this.getTreeSnapshot(MerkleTreeId.PRIVATE_DATA_TREE);
const startNullifierTreeSnapshot = await this.getTreeSnapshot(MerkleTreeId.NULLIFIER_TREE);
const startContractTreeSnapshot = await this.getTreeSnapshot(MerkleTreeId.CONTRACT_TREE);
const startPublicDataTreeRoot = Fr.fromBuffer((await this.db.getTreeInfo(MerkleTreeId.PUBLIC_DATA_TREE)).root);
const startTreeOfHistoricPrivateDataTreeRootsSnapshot = await this.getTreeSnapshot(
MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE,
);
Expand All @@ -51,6 +52,7 @@ export class StandaloneBlockBuilder implements BlockBuilder {
const endPrivateDataTreeSnapshot = await this.getTreeSnapshot(MerkleTreeId.PRIVATE_DATA_TREE);
const endNullifierTreeSnapshot = await this.getTreeSnapshot(MerkleTreeId.NULLIFIER_TREE);
const endContractTreeSnapshot = await this.getTreeSnapshot(MerkleTreeId.CONTRACT_TREE);
const endPublicDataTreeRoot = Fr.fromBuffer((await this.db.getTreeInfo(MerkleTreeId.PUBLIC_DATA_TREE)).root);
const endTreeOfHistoricPrivateDataTreeRootsSnapshot = await this.getTreeSnapshot(
MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE,
);
Expand All @@ -66,6 +68,8 @@ export class StandaloneBlockBuilder implements BlockBuilder {
endNullifierTreeSnapshot,
startContractTreeSnapshot,
endContractTreeSnapshot,
startPublicDataTreeRoot,
endPublicDataTreeRoot,
startTreeOfHistoricPrivateDataTreeRootsSnapshot,
endTreeOfHistoricPrivateDataTreeRootsSnapshot,
startTreeOfHistoricContractTreeRootsSnapshot,
Expand All @@ -74,6 +78,9 @@ export class StandaloneBlockBuilder implements BlockBuilder {
newNullifiers: this.nullifierTreeLeaves.map(b => Fr.fromBuffer(b)),
newContracts: this.contractTreeLeaves.map(b => Fr.fromBuffer(b)),
newContractData: txs.flatMap(tx => tx.data.end.newContracts.map(mapContractData)),
newPublicDataWrites: txs.flatMap(tx =>
tx.data.end.stateTransitions.map(t => new PublicDataWrite(t.leafIndex, t.newValue)),
),
});
return [l2Block, makeEmptyProof()];
}
Expand Down

0 comments on commit 916fa8f

Please sign in to comment.