Skip to content

Commit

Permalink
feat: l2 -> l1 ts
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed May 4, 2023
1 parent 984b213 commit 32eed8a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/structs/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const KERNEL_NEW_NULLIFIERS_LENGTH = 4;
export const KERNEL_NEW_CONTRACTS_LENGTH = 1;
export const KERNEL_PRIVATE_CALL_STACK_LENGTH = 8;
export const KERNEL_PUBLIC_CALL_STACK_LENGTH = 8;
export const KERNEL_NEW_L2_TO_L1_MSGS_LENGTH = 4;
export const KERNEL_NEW_L2_TO_L1_MSGS_LENGTH = 2;
export const KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH = 4;

export const VK_TREE_HEIGHT = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ describe('sequencer/solo_block_builder', () => {
const newPublicDataWrites = flatMap(txs, tx =>
tx.data.end.stateTransitions.map(t => new PublicDataWrite(t.leafIndex, t.newValue)),
);
const newL2ToL1Msgs = flatMap(txs, tx => tx.data.end.newL2ToL1Msgs);

const l2Block = L2Block.fromFields({
number: blockNumber,
Expand All @@ -200,6 +201,7 @@ describe('sequencer/solo_block_builder', () => {
newContractData,
newPublicDataWrites,
newL1ToL2Messages: mockL1ToL2Messages,
newL2ToL1Msgs,
});

const callDataHash = l2Block.getCalldataHash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export class SoloBlockBuilder implements BlockBuilder {
const newPublicDataWrites = flatMap(txs, tx =>
tx.data.end.stateTransitions.map(t => new PublicDataWrite(t.leafIndex, t.newValue)),
);
const newL2ToL1Msgs = flatMap(txs, tx => tx.data.end.newL2ToL1Msgs);

const l2Block = L2Block.fromFields({
number: blockNumber,
Expand All @@ -174,6 +175,7 @@ export class SoloBlockBuilder implements BlockBuilder {
endTreeOfHistoricL1ToL2MessageTreeRootsSnapshot,
newCommitments,
newNullifiers,
newL2ToL1Msgs,
newContracts,
newContractData,
newPublicDataWrites,
Expand Down
28 changes: 27 additions & 1 deletion yarn-project/types/src/l2_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
KERNEL_NEW_NULLIFIERS_LENGTH,
STATE_TRANSITIONS_LENGTH,
NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
KERNEL_NEW_L2_TO_L1_MSGS_LENGTH,
} from '@aztec/circuits.js';
import { makeAppendOnlyTreeSnapshot } from '@aztec/circuits.js/factories';
import { BufferReader, serializeToBuffer } from '@aztec/circuits.js/utils';
Expand Down Expand Up @@ -101,6 +102,10 @@ export class L2Block {
* The public data writes to be inserted into the public data tree.
*/
public newPublicDataWrites: PublicDataWrite[],
/**
* The L2 to L1 messages to be inserted into the messagebox on L1.
*/
public newL2ToL1Msgs: Fr[],
/**
* The contracts leafs to be inserted into the contract tree.
*/
Expand Down Expand Up @@ -128,6 +133,7 @@ export class L2Block {
const newContractData = times(KERNEL_NEW_CONTRACTS_LENGTH * txsPerBlock, ContractData.random);
const newPublicDataWrites = times(STATE_TRANSITIONS_LENGTH * txsPerBlock, PublicDataWrite.random);
const newL1ToL2Messages = times(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP, Fr.random);
const newL2ToL1Msgs = times(KERNEL_NEW_L2_TO_L1_MSGS_LENGTH, Fr.random);

return L2Block.fromFields({
number: l2BlockNum,
Expand All @@ -153,6 +159,7 @@ export class L2Block {
newContractData,
newPublicDataWrites,
newL1ToL2Messages,
newL2ToL1Msgs,
});
}

Expand Down Expand Up @@ -242,6 +249,10 @@ export class L2Block {
* The public data writes to be inserted into the public data tree.
*/
newPublicDataWrites: PublicDataWrite[];
/**
* The L2 to L1 messages to be inserted into the messagebox on L1.
*/
newL2ToL1Msgs: Fr[];
/**
* The contracts leafs to be inserted into the contract tree.
*/
Expand Down Expand Up @@ -276,6 +287,7 @@ export class L2Block {
fields.newCommitments,
fields.newNullifiers,
fields.newPublicDataWrites,
fields.newL2ToL1Msgs,
fields.newContracts,
fields.newContractData,
fields.newL1ToL2Messages,
Expand Down Expand Up @@ -311,6 +323,8 @@ export class L2Block {
this.newNullifiers,
this.newPublicDataWrites.length,
this.newPublicDataWrites,
this.newL2ToL1Msgs.length,
this.newL2ToL1Msgs,
this.newContracts.length,
this.newContracts,
this.newContractData,
Expand Down Expand Up @@ -354,6 +368,7 @@ export class L2Block {
const newCommitments = reader.readVector(Fr);
const newNullifiers = reader.readVector(Fr);
const newPublicDataWrites = reader.readVector(PublicDataWrite);
const newL2ToL1Msgs = reader.readVector(Fr);
const newContracts = reader.readVector(Fr);
const newContractData = reader.readArray(newContracts.length, ContractData);
// TODO(sean): could an optimisation of this be that it is encoded such that zeros are assumed
Expand All @@ -380,6 +395,7 @@ export class L2Block {
newCommitments,
newNullifiers,
newPublicDataWrites,
newL2ToL1Msgs,
newContracts,
newContractData,
newL1ToL2Messages,
Expand Down Expand Up @@ -493,6 +509,7 @@ export class L2Block {
const commitmentPerBase = KERNEL_NEW_COMMITMENTS_LENGTH * 2;
const nullifierPerBase = KERNEL_NEW_NULLIFIERS_LENGTH * 2;
const publicDataWritesPerBase = STATE_TRANSITIONS_LENGTH * 2; // @note why is this constant named differently?
const l2ToL1MsgsPerBase = KERNEL_NEW_L2_TO_L1_MSGS_LENGTH * 2;
const commitmentBuffer = Buffer.concat(
this.newCommitments.slice(i * commitmentPerBase, (i + 1) * commitmentPerBase).map(x => x.toBuffer()),
);
Expand All @@ -504,11 +521,15 @@ export class L2Block {
.slice(i * publicDataWritesPerBase, (i + 1) * publicDataWritesPerBase)
.map(x => x.toBuffer()),
);
const newL2ToL1MsgsBuffer = Buffer.concat(
this.newL2ToL1Msgs.slice(i * l2ToL1MsgsPerBase, (i + 1) * l2ToL1MsgsPerBase).map(x => x.toBuffer()),
);

const inputValue = Buffer.concat([
commitmentBuffer,
nullifierBuffer,
dataWritesBuffer,
newL2ToL1MsgsBuffer,
this.newContracts[i * 2].toBuffer(),
this.newContracts[i * 2 + 1].toBuffer(),
this.newContractData[i * 2].contractAddress.toBuffer(),
Expand Down Expand Up @@ -555,6 +576,10 @@ export class L2Block {
STATE_TRANSITIONS_LENGTH * txIndex,
STATE_TRANSITIONS_LENGTH * (txIndex + 1),
);
const newL2ToL1Msgs = this.newL2ToL1Msgs.slice(
KERNEL_NEW_L2_TO_L1_MSGS_LENGTH * txIndex,
KERNEL_NEW_L2_TO_L1_MSGS_LENGTH * (txIndex + 1),
);
const newContracts = this.newContracts.slice(
KERNEL_NEW_CONTRACTS_LENGTH * txIndex,
KERNEL_NEW_CONTRACTS_LENGTH * (txIndex + 1),
Expand All @@ -564,7 +589,7 @@ export class L2Block {
KERNEL_NEW_CONTRACTS_LENGTH * (txIndex + 1),
);

return new L2Tx(newCommitments, newNullifiers, newPublicDataWrites, newContracts, newContractData);
return new L2Tx(newCommitments, newNullifiers, newPublicDataWrites, newL2ToL1Msgs, newContracts, newContractData);
}

/**
Expand Down Expand Up @@ -624,6 +649,7 @@ export class L2Block {
`newCommitments: ${inspectFrArray(this.newCommitments)}`,
`newNullifiers: ${inspectFrArray(this.newNullifiers)}`,
`newPublicDataWrite: ${inspectPublicDataWriteArray(this.newPublicDataWrites)}`,
`newL2ToL1Msgs: ${inspectFrArray(this.newL2ToL1Msgs)}`,
`newContracts: ${inspectFrArray(this.newContracts)}`,
`newContractData: ${inspectContractDataArray(this.newContractData)}`,
`newPublicDataWrite: ${inspectPublicDataWriteArray(this.newPublicDataWrites)}`,
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/types/src/l2_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class L2Tx {
* New public data writes created by the transaction.
*/
public newPublicDataWrites: PublicDataWrite[],
/**
* New L2 to L1 messages created by the transaction.
*/
public newL2ToL1Msgs: Fr[],
/**
* New contracts leafs created by the transaction to be inserted into the contract tree.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const getMockBlock = (blockNumber: number, newContractsCommitments?: Buffer[]) =
newContractData: [getMockContractData()],
newPublicDataWrites: [PublicDataWrite.random()],
newL1ToL2Messages: getMockL1ToL2MessagesData(),
newL2ToL1Msgs: [Fr.random()],
});
return block;
};
Expand Down

0 comments on commit 32eed8a

Please sign in to comment.