@aztec/prover-client 0.0.1-fake-ceab37513c → 0.0.6-commit.a2d1860fe9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/dest/config.d.ts +2 -2
  2. package/dest/config.d.ts.map +1 -1
  3. package/dest/config.js +2 -2
  4. package/dest/index.d.ts +1 -1
  5. package/dest/light/index.d.ts +2 -0
  6. package/dest/light/index.d.ts.map +1 -0
  7. package/dest/light/index.js +1 -0
  8. package/dest/light/lightweight_checkpoint_builder.d.ts +48 -0
  9. package/dest/light/lightweight_checkpoint_builder.d.ts.map +1 -0
  10. package/dest/light/lightweight_checkpoint_builder.js +202 -0
  11. package/dest/mocks/fixtures.d.ts +5 -5
  12. package/dest/mocks/fixtures.d.ts.map +1 -1
  13. package/dest/mocks/fixtures.js +35 -16
  14. package/dest/mocks/test_context.d.ts +39 -33
  15. package/dest/mocks/test_context.d.ts.map +1 -1
  16. package/dest/mocks/test_context.js +141 -82
  17. package/dest/orchestrator/block-building-helpers.d.ts +34 -34
  18. package/dest/orchestrator/block-building-helpers.d.ts.map +1 -1
  19. package/dest/orchestrator/block-building-helpers.js +151 -187
  20. package/dest/orchestrator/block-proving-state.d.ts +71 -55
  21. package/dest/orchestrator/block-proving-state.d.ts.map +1 -1
  22. package/dest/orchestrator/block-proving-state.js +280 -185
  23. package/dest/orchestrator/checkpoint-proving-state.d.ts +76 -0
  24. package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -0
  25. package/dest/orchestrator/checkpoint-proving-state.js +243 -0
  26. package/dest/orchestrator/epoch-proving-state.d.ts +40 -32
  27. package/dest/orchestrator/epoch-proving-state.d.ts.map +1 -1
  28. package/dest/orchestrator/epoch-proving-state.js +165 -85
  29. package/dest/orchestrator/index.d.ts +1 -1
  30. package/dest/orchestrator/orchestrator.d.ts +51 -35
  31. package/dest/orchestrator/orchestrator.d.ts.map +1 -1
  32. package/dest/orchestrator/orchestrator.js +847 -329
  33. package/dest/orchestrator/orchestrator_metrics.d.ts +1 -3
  34. package/dest/orchestrator/orchestrator_metrics.d.ts.map +1 -1
  35. package/dest/orchestrator/orchestrator_metrics.js +2 -15
  36. package/dest/orchestrator/tx-proving-state.d.ts +15 -12
  37. package/dest/orchestrator/tx-proving-state.d.ts.map +1 -1
  38. package/dest/orchestrator/tx-proving-state.js +27 -33
  39. package/dest/prover-client/factory.d.ts +3 -3
  40. package/dest/prover-client/factory.d.ts.map +1 -1
  41. package/dest/prover-client/index.d.ts +1 -1
  42. package/dest/prover-client/prover-client.d.ts +5 -5
  43. package/dest/prover-client/prover-client.d.ts.map +1 -1
  44. package/dest/prover-client/prover-client.js +15 -10
  45. package/dest/prover-client/server-epoch-prover.d.ts +13 -11
  46. package/dest/prover-client/server-epoch-prover.d.ts.map +1 -1
  47. package/dest/prover-client/server-epoch-prover.js +9 -9
  48. package/dest/proving_broker/broker_prover_facade.d.ts +28 -21
  49. package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -1
  50. package/dest/proving_broker/broker_prover_facade.js +45 -36
  51. package/dest/proving_broker/config.d.ts +24 -8
  52. package/dest/proving_broker/config.d.ts.map +1 -1
  53. package/dest/proving_broker/config.js +26 -3
  54. package/dest/proving_broker/factory.d.ts +1 -1
  55. package/dest/proving_broker/fixtures.d.ts +3 -2
  56. package/dest/proving_broker/fixtures.d.ts.map +1 -1
  57. package/dest/proving_broker/fixtures.js +3 -2
  58. package/dest/proving_broker/index.d.ts +1 -1
  59. package/dest/proving_broker/proof_store/factory.d.ts +2 -5
  60. package/dest/proving_broker/proof_store/factory.d.ts.map +1 -1
  61. package/dest/proving_broker/proof_store/factory.js +7 -30
  62. package/dest/proving_broker/proof_store/file_store_proof_store.d.ts +18 -0
  63. package/dest/proving_broker/proof_store/file_store_proof_store.d.ts.map +1 -0
  64. package/dest/proving_broker/proof_store/file_store_proof_store.js +60 -0
  65. package/dest/proving_broker/proof_store/index.d.ts +2 -1
  66. package/dest/proving_broker/proof_store/index.d.ts.map +1 -1
  67. package/dest/proving_broker/proof_store/index.js +1 -0
  68. package/dest/proving_broker/proof_store/inline_proof_store.d.ts +1 -1
  69. package/dest/proving_broker/proof_store/inline_proof_store.d.ts.map +1 -1
  70. package/dest/proving_broker/proof_store/proof_store.d.ts +1 -1
  71. package/dest/proving_broker/proving_agent.d.ts +5 -9
  72. package/dest/proving_broker/proving_agent.d.ts.map +1 -1
  73. package/dest/proving_broker/proving_agent.js +4 -19
  74. package/dest/proving_broker/proving_broker.d.ts +7 -4
  75. package/dest/proving_broker/proving_broker.d.ts.map +1 -1
  76. package/dest/proving_broker/proving_broker.js +64 -30
  77. package/dest/proving_broker/proving_broker_database/memory.d.ts +3 -2
  78. package/dest/proving_broker/proving_broker_database/memory.d.ts.map +1 -1
  79. package/dest/proving_broker/proving_broker_database/persisted.d.ts +5 -3
  80. package/dest/proving_broker/proving_broker_database/persisted.d.ts.map +1 -1
  81. package/dest/proving_broker/proving_broker_database/persisted.js +392 -3
  82. package/dest/proving_broker/proving_broker_database.d.ts +3 -2
  83. package/dest/proving_broker/proving_broker_database.d.ts.map +1 -1
  84. package/dest/proving_broker/proving_broker_instrumentation.d.ts +1 -1
  85. package/dest/proving_broker/proving_broker_instrumentation.d.ts.map +1 -1
  86. package/dest/proving_broker/proving_broker_instrumentation.js +15 -35
  87. package/dest/proving_broker/proving_job_controller.d.ts +5 -3
  88. package/dest/proving_broker/proving_job_controller.d.ts.map +1 -1
  89. package/dest/proving_broker/proving_job_controller.js +46 -24
  90. package/dest/proving_broker/rpc.d.ts +7 -5
  91. package/dest/proving_broker/rpc.d.ts.map +1 -1
  92. package/dest/proving_broker/rpc.js +8 -0
  93. package/dest/test/mock_proof_store.d.ts +1 -1
  94. package/dest/test/mock_proof_store.d.ts.map +1 -1
  95. package/dest/test/mock_prover.d.ts +23 -19
  96. package/dest/test/mock_prover.d.ts.map +1 -1
  97. package/dest/test/mock_prover.js +38 -23
  98. package/package.json +21 -20
  99. package/src/config.ts +2 -2
  100. package/src/light/index.ts +1 -0
  101. package/src/light/lightweight_checkpoint_builder.ts +294 -0
  102. package/src/mocks/fixtures.ts +43 -37
  103. package/src/mocks/test_context.ts +201 -114
  104. package/src/orchestrator/block-building-helpers.ts +233 -313
  105. package/src/orchestrator/block-proving-state.ts +324 -247
  106. package/src/orchestrator/checkpoint-proving-state.ts +349 -0
  107. package/src/orchestrator/epoch-proving-state.ts +229 -132
  108. package/src/orchestrator/orchestrator.ts +630 -385
  109. package/src/orchestrator/orchestrator_metrics.ts +2 -25
  110. package/src/orchestrator/tx-proving-state.ts +49 -60
  111. package/src/prover-client/factory.ts +6 -2
  112. package/src/prover-client/prover-client.ts +31 -23
  113. package/src/prover-client/server-epoch-prover.ts +30 -21
  114. package/src/proving_broker/broker_prover_facade.ts +183 -118
  115. package/src/proving_broker/config.ts +30 -1
  116. package/src/proving_broker/fixtures.ts +8 -3
  117. package/src/proving_broker/proof_store/factory.ts +10 -32
  118. package/src/proving_broker/proof_store/file_store_proof_store.ts +78 -0
  119. package/src/proving_broker/proof_store/index.ts +1 -0
  120. package/src/proving_broker/proving_agent.ts +6 -19
  121. package/src/proving_broker/proving_broker.ts +72 -28
  122. package/src/proving_broker/proving_broker_database/memory.ts +2 -1
  123. package/src/proving_broker/proving_broker_database/persisted.ts +20 -5
  124. package/src/proving_broker/proving_broker_database.ts +2 -1
  125. package/src/proving_broker/proving_broker_instrumentation.ts +14 -35
  126. package/src/proving_broker/proving_job_controller.ts +51 -25
  127. package/src/proving_broker/rpc.ts +14 -0
  128. package/src/test/mock_prover.ts +144 -74
  129. package/dest/bin/get-proof-inputs.d.ts +0 -2
  130. package/dest/bin/get-proof-inputs.d.ts.map +0 -1
  131. package/dest/bin/get-proof-inputs.js +0 -51
  132. package/dest/block-factory/index.d.ts +0 -2
  133. package/dest/block-factory/index.d.ts.map +0 -1
  134. package/dest/block-factory/index.js +0 -1
  135. package/dest/block-factory/light.d.ts +0 -36
  136. package/dest/block-factory/light.d.ts.map +0 -1
  137. package/dest/block-factory/light.js +0 -87
  138. package/dest/proving_broker/proof_store/gcs_proof_store.d.ts +0 -14
  139. package/dest/proving_broker/proof_store/gcs_proof_store.d.ts.map +0 -1
  140. package/dest/proving_broker/proof_store/gcs_proof_store.js +0 -52
  141. package/dest/proving_broker/proving_agent_instrumentation.d.ts +0 -8
  142. package/dest/proving_broker/proving_agent_instrumentation.d.ts.map +0 -1
  143. package/dest/proving_broker/proving_agent_instrumentation.js +0 -16
  144. package/src/bin/get-proof-inputs.ts +0 -59
  145. package/src/block-factory/index.ts +0 -1
  146. package/src/block-factory/light.ts +0 -114
  147. package/src/proving_broker/proof_store/gcs_proof_store.ts +0 -76
  148. package/src/proving_broker/proving_agent_instrumentation.ts +0 -21
@@ -1,52 +1,66 @@
1
1
  import type { BBProverConfig } from '@aztec/bb-prover';
2
+ import { TestCircuitProver } from '@aztec/bb-prover';
2
3
  import { NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
3
- import { padArrayEnd, times, timesParallel } from '@aztec/foundation/collection';
4
- import { Fr } from '@aztec/foundation/fields';
4
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
5
+ import { padArrayEnd, times, timesAsync } from '@aztec/foundation/collection';
6
+ import { Fr } from '@aztec/foundation/curves/bn254';
5
7
  import type { Logger } from '@aztec/foundation/log';
6
- import { TestDateProvider } from '@aztec/foundation/timer';
8
+ import type { FieldsOf } from '@aztec/foundation/types';
7
9
  import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
8
- import { protocolContractTreeRoot } from '@aztec/protocol-contracts';
10
+ import { ProtocolContractsList } from '@aztec/protocol-contracts';
9
11
  import { computeFeePayerBalanceLeafSlot } from '@aztec/protocol-contracts/fee-juice';
10
- import { SimpleContractDataSource } from '@aztec/simulator/public/fixtures';
11
- import { PublicProcessorFactory } from '@aztec/simulator/server';
12
12
  import { PublicDataWrite } from '@aztec/stdlib/avm';
13
13
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
14
- import { EthAddress, type L2Block } from '@aztec/stdlib/block';
15
- import type { ServerCircuitProver } from '@aztec/stdlib/interfaces/server';
16
- import { makeBloatedProcessedTx } from '@aztec/stdlib/testing';
17
- import { type AppendOnlyTreeSnapshot, MerkleTreeId, PublicDataTreeLeaf } from '@aztec/stdlib/trees';
18
- import { type BlockHeader, type GlobalVariables, type ProcessedTx, TreeSnapshots, type Tx } from '@aztec/stdlib/tx';
14
+ import { EthAddress } from '@aztec/stdlib/block';
15
+ import type { Checkpoint } from '@aztec/stdlib/checkpoint';
16
+ import type { MerkleTreeWriteOperations, ServerCircuitProver } from '@aztec/stdlib/interfaces/server';
17
+ import type { CheckpointConstantData } from '@aztec/stdlib/rollup';
18
+ import { mockProcessedTx } from '@aztec/stdlib/testing';
19
+ import { MerkleTreeId, PublicDataTreeLeaf } from '@aztec/stdlib/trees';
20
+ import {
21
+ type BlockHeader,
22
+ type GlobalVariables,
23
+ type ProcessedTx,
24
+ StateReference,
25
+ TreeSnapshots,
26
+ } from '@aztec/stdlib/tx';
19
27
  import type { MerkleTreeAdminDatabase } from '@aztec/world-state';
20
28
  import { NativeWorldStateService } from '@aztec/world-state/native';
21
29
 
22
30
  import { promises as fs } from 'fs';
23
31
 
24
- // TODO(#12613) This means of sharing test code is not ideal.
25
- // eslint-disable-next-line import/no-relative-packages
26
- import { TestCircuitProver } from '../../../bb-prover/src/test/test_circuit_prover.js';
27
- import { buildBlockWithCleanDB } from '../block-factory/light.js';
28
- import { getTreeSnapshot } from '../orchestrator/block-building-helpers.js';
32
+ import { LightweightCheckpointBuilder } from '../light/lightweight_checkpoint_builder.js';
33
+ import {
34
+ buildFinalBlobChallenges,
35
+ getTreeSnapshot,
36
+ insertSideEffects,
37
+ } from '../orchestrator/block-building-helpers.js';
38
+ import type { BlockProvingState } from '../orchestrator/block-proving-state.js';
29
39
  import { ProvingOrchestrator } from '../orchestrator/index.js';
30
40
  import { BrokerCircuitProverFacade } from '../proving_broker/broker_prover_facade.js';
31
41
  import { TestBroker } from '../test/mock_prover.js';
32
- import { getEnvironmentConfig, getSimulator, makeGlobals, updateExpectedTreesFromTxs } from './fixtures.js';
42
+ import { getEnvironmentConfig, getSimulator, makeCheckpointConstants, makeGlobals } from './fixtures.js';
33
43
 
34
44
  export class TestContext {
35
45
  private headers: Map<number, BlockHeader> = new Map();
46
+ private checkpoints: Checkpoint[] = [];
47
+ private checkpointOutHashes: Fr[] = [];
48
+ private nextCheckpointIndex = 0;
49
+ private nextCheckpointNumber = CheckpointNumber(1);
50
+ private nextBlockNumber = 1;
51
+ private epochNumber = 1;
36
52
  private feePayerBalance: Fr;
37
53
 
38
54
  constructor(
39
55
  public worldState: MerkleTreeAdminDatabase,
40
- public globalVariables: GlobalVariables,
41
56
  public prover: ServerCircuitProver,
42
57
  public broker: TestBroker,
43
58
  public brokerProverFacade: BrokerCircuitProverFacade,
44
59
  public orchestrator: TestProvingOrchestrator,
45
- public blockNumber: number,
46
- public feePayer: AztecAddress,
60
+ private feePayer: AztecAddress,
47
61
  initialFeePayerBalance: Fr,
48
- public directoriesToCleanup: string[],
49
- public logger: Logger,
62
+ private directoriesToCleanup: string[],
63
+ private logger: Logger,
50
64
  ) {
51
65
  this.feePayerBalance = initialFeePayerBalance;
52
66
  }
@@ -60,15 +74,12 @@ export class TestContext {
60
74
  {
61
75
  proverCount = 4,
62
76
  createProver = async (bbConfig: BBProverConfig) => new TestCircuitProver(await getSimulator(bbConfig, logger)),
63
- blockNumber = 1,
64
77
  }: {
65
78
  proverCount?: number;
66
79
  createProver?: (bbConfig: BBProverConfig) => Promise<ServerCircuitProver>;
67
- blockNumber?: number;
68
80
  } = {},
69
81
  ) {
70
82
  const directoriesToCleanup: string[] = [];
71
- const globalVariables = makeGlobals(blockNumber);
72
83
 
73
84
  const feePayer = AztecAddress.fromNumber(42222);
74
85
  const initialFeePayerBalance = new Fr(10n ** 20n);
@@ -112,12 +123,10 @@ export class TestContext {
112
123
 
113
124
  return new this(
114
125
  ws,
115
- globalVariables,
116
126
  localProver,
117
127
  broker,
118
128
  facade,
119
129
  orchestrator,
120
- blockNumber,
121
130
  feePayer,
122
131
  initialFeePayerBalance,
123
132
  directoriesToCleanup,
@@ -129,20 +138,6 @@ export class TestContext {
129
138
  return this.worldState.fork();
130
139
  }
131
140
 
132
- public getBlockHeader(blockNumber: 0): BlockHeader;
133
- public getBlockHeader(blockNumber: number): BlockHeader | undefined;
134
- public getBlockHeader(blockNumber = 0) {
135
- return blockNumber === 0 ? this.worldState.getCommitted().getInitialHeader() : this.headers.get(blockNumber);
136
- }
137
-
138
- public setBlockHeader(header: BlockHeader, blockNumber: number) {
139
- this.headers.set(blockNumber, header);
140
- }
141
-
142
- public getPreviousBlockHeader(currentBlockNumber = this.blockNumber): BlockHeader {
143
- return this.getBlockHeader(currentBlockNumber - 1)!;
144
- }
145
-
146
141
  async cleanup() {
147
142
  await this.brokerProverFacade.stop();
148
143
  await this.broker.stop();
@@ -155,93 +150,183 @@ export class TestContext {
155
150
  }
156
151
  }
157
152
 
158
- private async makeProcessedTx(opts?: Parameters<typeof makeBloatedProcessedTx>[0]): Promise<ProcessedTx> {
159
- const blockNum = (opts?.globalVariables ?? this.globalVariables).blockNumber;
160
- const header = this.getBlockHeader(blockNum - 1);
161
- const tx = await makeBloatedProcessedTx({
162
- header,
163
- vkTreeRoot: getVKTreeRoot(),
164
- protocolContractTreeRoot,
165
- globalVariables: this.globalVariables,
166
- feePayer: this.feePayer,
167
- ...opts,
168
- });
169
- this.feePayerBalance = new Fr(this.feePayerBalance.toBigInt() - tx.txEffect.transactionFee.toBigInt());
170
- if (opts?.privateOnly) {
171
- const feePayerSlot = await computeFeePayerBalanceLeafSlot(this.feePayer);
172
- tx.txEffect.publicDataWrites[0] = new PublicDataWrite(feePayerSlot, this.feePayerBalance);
173
- }
174
- return tx;
153
+ public startNewEpoch() {
154
+ this.checkpoints = [];
155
+ this.checkpointOutHashes = [];
156
+ this.nextCheckpointIndex = 0;
157
+ this.epochNumber++;
175
158
  }
176
159
 
177
- /** Creates a block with the given number of txs and adds it to world-state */
178
- public async makePendingBlock(
179
- numTxs: number,
180
- numL1ToL2Messages: number = 0,
181
- blockNumOrGlobals: GlobalVariables | number = this.globalVariables,
182
- makeProcessedTxOpts: (index: number) => Partial<Parameters<typeof makeBloatedProcessedTx>[0]> = () => ({}),
183
- ) {
184
- const globalVariables = typeof blockNumOrGlobals === 'number' ? makeGlobals(blockNumOrGlobals) : blockNumOrGlobals;
185
- const blockNum = globalVariables.blockNumber;
186
- const db = await this.worldState.fork();
187
- const l1ToL2Messages = times(numL1ToL2Messages, i => new Fr(blockNum * 100 + i));
188
- const merkleTrees = await this.worldState.fork();
189
- await merkleTrees.appendLeaves(
190
- MerkleTreeId.L1_TO_L2_MESSAGE_TREE,
191
- padArrayEnd(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP),
192
- );
193
- const newL1ToL2Snapshot = await getTreeSnapshot(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, merkleTrees);
194
- const txs = await timesParallel(numTxs, i =>
195
- this.makeProcessedTx({
196
- seed: i + blockNum * 1000,
197
- globalVariables,
198
- newL1ToL2Snapshot,
199
- ...makeProcessedTxOpts(i),
200
- }),
201
- );
202
- await this.setTreeRoots(txs);
160
+ // Return blob fields of all checkpoints in the epoch.
161
+ public getBlobFields() {
162
+ return this.checkpoints.map(checkpoint => checkpoint.toBlobFields());
163
+ }
203
164
 
204
- const block = await buildBlockWithCleanDB(txs, globalVariables, l1ToL2Messages, db);
205
- this.headers.set(blockNum, block.header);
206
- await this.worldState.handleL2BlockAndMessages(block, l1ToL2Messages);
207
- return { block, txs, l1ToL2Messages };
165
+ public async getFinalBlobChallenges() {
166
+ const blobFields = this.getBlobFields();
167
+ return await buildFinalBlobChallenges(blobFields);
208
168
  }
209
169
 
210
- public async processPublicFunctions(
211
- txs: Tx[],
170
+ public async makeCheckpoint(
171
+ numBlocks: number,
212
172
  {
213
- maxTransactions = txs.length,
173
+ numTxsPerBlock = 0,
214
174
  numL1ToL2Messages = 0,
215
- contractDataSource,
175
+ makeProcessedTxOpts = () => ({}),
176
+ ...constantOpts
216
177
  }: {
217
- maxTransactions?: number;
178
+ numTxsPerBlock?: number | number[];
218
179
  numL1ToL2Messages?: number;
219
- contractDataSource?: SimpleContractDataSource;
220
- } = {},
180
+ makeProcessedTxOpts?: (
181
+ blockGlobalVariables: GlobalVariables,
182
+ txIndex: number,
183
+ ) => Partial<Parameters<typeof mockProcessedTx>[0]>;
184
+ } & Partial<FieldsOf<CheckpointConstantData>> = {},
221
185
  ) {
222
- const l1ToL2Messages = times(numL1ToL2Messages, i => new Fr(this.blockNumber * 100 + i));
223
- const merkleTrees = await this.worldState.fork();
224
- await merkleTrees.appendLeaves(
186
+ if (numBlocks === 0) {
187
+ throw new Error(
188
+ 'Cannot make a checkpoint with 0 blocks. Crate an empty block (numTxsPerBlock = 0) if there are no txs.',
189
+ );
190
+ }
191
+
192
+ const checkpointIndex = this.nextCheckpointIndex++;
193
+ const checkpointNumber = this.nextCheckpointNumber;
194
+ this.nextCheckpointNumber++;
195
+ const slotNumber = checkpointNumber * 15; // times an arbitrary number to make it different to the checkpoint number
196
+
197
+ const constants = makeCheckpointConstants(slotNumber, constantOpts);
198
+
199
+ const fork = await this.worldState.fork();
200
+
201
+ // Build l1 to l2 messages.
202
+ const l1ToL2Messages = times(numL1ToL2Messages, i => new Fr(slotNumber * 100 + i));
203
+ await fork.appendLeaves(
225
204
  MerkleTreeId.L1_TO_L2_MESSAGE_TREE,
226
- padArrayEnd(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP),
205
+ padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP),
227
206
  );
207
+ const newL1ToL2Snapshot = await getTreeSnapshot(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, fork);
228
208
 
229
- const processorFactory = new PublicProcessorFactory(
230
- contractDataSource ?? new SimpleContractDataSource(),
231
- new TestDateProvider(),
209
+ const startBlockNumber = this.nextBlockNumber;
210
+ const previousBlockHeader = this.getBlockHeader(BlockNumber(startBlockNumber - 1));
211
+ // All blocks in the same slot/checkpoint share the same timestamp.
212
+ const timestamp = BigInt(slotNumber * 26);
213
+
214
+ // Build global variables.
215
+ const blockGlobalVariables = times(numBlocks, i =>
216
+ makeGlobals(startBlockNumber + i, slotNumber, {
217
+ coinbase: constants.coinbase,
218
+ feeRecipient: constants.feeRecipient,
219
+ gasFees: constants.gasFees,
220
+ timestamp,
221
+ }),
232
222
  );
233
- const publicProcessor = processorFactory.create(merkleTrees, this.globalVariables, /*skipFeeEnforcement=*/ false);
223
+ this.nextBlockNumber += numBlocks;
234
224
 
235
- return await publicProcessor.process(txs, { maxTransactions });
225
+ // Build txs.
226
+ let totalTxs = 0;
227
+ const blockEndStates: StateReference[] = [];
228
+ const blockTxs = await timesAsync(numBlocks, async blockIndex => {
229
+ const txIndexOffset = totalTxs;
230
+ const numTxs = typeof numTxsPerBlock === 'number' ? numTxsPerBlock : numTxsPerBlock[blockIndex];
231
+ totalTxs += numTxs;
232
+ const txs = await timesAsync(numTxs, txIndex =>
233
+ this.makeProcessedTx({
234
+ seed: (txIndexOffset + txIndex + 1) * 321 + (checkpointIndex + 1) * 123456 + this.epochNumber * 0x99999,
235
+ globalVariables: blockGlobalVariables[blockIndex],
236
+ anchorBlockHeader: previousBlockHeader,
237
+ newL1ToL2Snapshot,
238
+ ...makeProcessedTxOpts(blockGlobalVariables[blockIndex], txIndexOffset + txIndex),
239
+ }),
240
+ );
241
+
242
+ // Insert side effects into the trees.
243
+ const endState = await this.updateTrees(txs, fork);
244
+ blockEndStates.push(endState);
245
+
246
+ return txs;
247
+ });
248
+
249
+ const cleanFork = await this.worldState.fork();
250
+ const previousCheckpointOutHashes = this.checkpointOutHashes;
251
+ const builder = await LightweightCheckpointBuilder.startNewCheckpoint(
252
+ checkpointNumber,
253
+ { ...constants, timestamp },
254
+ l1ToL2Messages,
255
+ previousCheckpointOutHashes,
256
+ cleanFork,
257
+ );
258
+
259
+ // Add tx effects to db and build block headers.
260
+ const blocks = [];
261
+ for (let i = 0; i < numBlocks; i++) {
262
+ const txs = blockTxs[i];
263
+ const state = blockEndStates[i];
264
+
265
+ const block = await builder.addBlock(blockGlobalVariables[i], txs, {
266
+ expectedEndState: state,
267
+ insertTxsEffects: true,
268
+ });
269
+
270
+ const header = block.header;
271
+ this.headers.set(block.number, header);
272
+
273
+ const blockMsgs = block.indexWithinCheckpoint === 0 ? l1ToL2Messages : [];
274
+ await this.worldState.handleL2BlockAndMessages(block, blockMsgs);
275
+
276
+ blocks.push({ header, txs });
277
+ }
278
+
279
+ const checkpoint = await builder.completeCheckpoint();
280
+ this.checkpoints.push(checkpoint);
281
+ this.checkpointOutHashes.push(checkpoint.getCheckpointOutHash());
282
+
283
+ return {
284
+ constants,
285
+ header: checkpoint.header,
286
+ blocks,
287
+ l1ToL2Messages,
288
+ previousBlockHeader,
289
+ };
236
290
  }
237
291
 
238
- private async setTreeRoots(txs: ProcessedTx[]) {
239
- const db = await this.worldState.fork();
292
+ private async makeProcessedTx(opts: Parameters<typeof mockProcessedTx>[0] = {}): Promise<ProcessedTx> {
293
+ const tx = await mockProcessedTx({
294
+ vkTreeRoot: getVKTreeRoot(),
295
+ protocolContracts: ProtocolContractsList,
296
+ feePayer: this.feePayer,
297
+ ...opts,
298
+ });
299
+
300
+ this.feePayerBalance = new Fr(this.feePayerBalance.toBigInt() - tx.txEffect.transactionFee.toBigInt());
301
+
302
+ const feePayerSlot = await computeFeePayerBalanceLeafSlot(this.feePayer);
303
+ const feePaymentPublicDataWrite = new PublicDataWrite(feePayerSlot, this.feePayerBalance);
304
+ tx.txEffect.publicDataWrites[0] = feePaymentPublicDataWrite;
305
+ if (tx.avmProvingRequest) {
306
+ tx.avmProvingRequest.inputs.publicInputs.accumulatedData.publicDataWrites[0] = feePaymentPublicDataWrite;
307
+ }
308
+
309
+ return tx;
310
+ }
311
+
312
+ private getBlockHeader(blockNumber: BlockNumber): BlockHeader {
313
+ if (Number(blockNumber) > 0 && Number(blockNumber) >= this.nextBlockNumber) {
314
+ throw new Error(`Block header not built for block number ${blockNumber}.`);
315
+ }
316
+ return Number(blockNumber) === 0
317
+ ? this.worldState.getCommitted().getInitialHeader()
318
+ : this.headers.get(Number(blockNumber))!;
319
+ }
320
+
321
+ private async updateTrees(txs: ProcessedTx[], fork: MerkleTreeWriteOperations) {
322
+ let startStateReference = await fork.getStateReference();
323
+ let endStateReference = startStateReference;
240
324
  for (const tx of txs) {
241
- const startStateReference = await db.getStateReference();
242
- await updateExpectedTreesFromTxs(db, [tx]);
243
- const endStateReference = await db.getStateReference();
325
+ await insertSideEffects(tx, fork);
326
+ endStateReference = await fork.getStateReference();
327
+
244
328
  if (tx.avmProvingRequest) {
329
+ // Update the trees in the avm public inputs so that the proof won't fail.
245
330
  const l1ToL2MessageTree = tx.avmProvingRequest.inputs.publicInputs.startTreeSnapshots.l1ToL2MessageTree;
246
331
  tx.avmProvingRequest.inputs.publicInputs.startTreeSnapshots = new TreeSnapshots(
247
332
  l1ToL2MessageTree,
@@ -249,6 +334,7 @@ export class TestContext {
249
334
  startStateReference.partial.nullifierTree,
250
335
  startStateReference.partial.publicDataTree,
251
336
  );
337
+
252
338
  tx.avmProvingRequest.inputs.publicInputs.endTreeSnapshots = new TreeSnapshots(
253
339
  l1ToL2MessageTree,
254
340
  endStateReference.partial.noteHashTree,
@@ -256,7 +342,11 @@ export class TestContext {
256
342
  endStateReference.partial.publicDataTree,
257
343
  );
258
344
  }
345
+
346
+ startStateReference = endStateReference;
259
347
  }
348
+
349
+ return endStateReference;
260
350
  }
261
351
  }
262
352
 
@@ -265,12 +355,9 @@ class TestProvingOrchestrator extends ProvingOrchestrator {
265
355
 
266
356
  // Disable this check by default, since it requires seeding world state with the block being built
267
357
  // This is only enabled in some tests with multiple blocks that populate the pending chain via makePendingBlock
268
- protected override verifyBuiltBlockAgainstSyncedState(
269
- l2Block: L2Block,
270
- newArchive: AppendOnlyTreeSnapshot,
271
- ): Promise<void> {
358
+ protected override verifyBuiltBlockAgainstSyncedState(provingState: BlockProvingState): Promise<void> {
272
359
  if (this.isVerifyBuiltBlockAgainstSyncedStateEnabled) {
273
- return super.verifyBuiltBlockAgainstSyncedState(l2Block, newArchive);
360
+ return super.verifyBuiltBlockAgainstSyncedState(provingState);
274
361
  }
275
362
  return Promise.resolve();
276
363
  }