@aztec/prover-client 0.0.1-commit.b655e406 → 0.0.1-commit.b8a057fa

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