@aztec/prover-client 0.0.1-fake-ceab37513c → 0.0.2-commit.217f559981

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,140 +1,160 @@
1
- import { BatchedBlobAccumulator, BlobAccumulatorPublicInputs, SpongeBlob } from '@aztec/blob-lib';
1
+ import { type BlockBlobData, type BlockEndBlobData, type SpongeBlob, encodeBlockEndBlobData } from '@aztec/blob-lib';
2
2
  import {
3
3
  type ARCHIVE_HEIGHT,
4
- BLOBS_PER_BLOCK,
5
- FIELDS_PER_BLOB,
6
- type L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH,
4
+ type L1_TO_L2_MSG_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
5
+ NESTED_RECURSIVE_PROOF_LENGTH,
7
6
  type NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
8
7
  NUM_BASE_PARITY_PER_ROOT_PARITY,
9
- type RECURSIVE_PROOF_LENGTH,
10
8
  } from '@aztec/constants';
11
- import { padArrayEnd } from '@aztec/foundation/collection';
12
- import { BLS12Point, Fr } from '@aztec/foundation/fields';
13
- import type { Tuple } from '@aztec/foundation/serialize';
9
+ import { BlockNumber } from '@aztec/foundation/branded-types';
10
+ import { Fr } from '@aztec/foundation/curves/bn254';
11
+ import { type Tuple, assertLength } from '@aztec/foundation/serialize';
14
12
  import { type TreeNodeLocation, UnbalancedTreeStore } from '@aztec/foundation/trees';
15
- import { getVKIndex, getVKSiblingPath, getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
16
- import { protocolContractTreeRoot } from '@aztec/protocol-contracts';
17
- import type { EthAddress, L2Block } from '@aztec/stdlib/block';
18
13
  import type { PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
19
- import { type ParityPublicInputs, RootParityInput, RootParityInputs } from '@aztec/stdlib/parity';
14
+ import { type ParityPublicInputs, ParityRootPrivateInputs } from '@aztec/stdlib/parity';
15
+ import type { RollupHonkProofData } from '@aztec/stdlib/proofs';
20
16
  import {
21
- type BaseOrMergeRollupPublicInputs,
22
- BlockConstantData,
23
- type BlockRootOrBlockMergePublicInputs,
24
- BlockRootRollupBlobData,
25
- BlockRootRollupData,
26
- BlockRootRollupInputs,
27
- EmptyBlockRootRollupInputs,
28
- EpochConstantData,
29
- MergeRollupInputs,
30
- PaddingBlockRootRollupInputs,
31
- PreviousRollupData,
32
- SingleTxBlockRootRollupInputs,
17
+ BlockRollupPublicInputs,
18
+ BlockRootEmptyTxFirstRollupPrivateInputs,
19
+ BlockRootFirstRollupPrivateInputs,
20
+ BlockRootRollupPrivateInputs,
21
+ BlockRootSingleTxFirstRollupPrivateInputs,
22
+ BlockRootSingleTxRollupPrivateInputs,
23
+ CheckpointConstantData,
24
+ TxMergeRollupPrivateInputs,
25
+ type TxRollupPublicInputs,
33
26
  } from '@aztec/stdlib/rollup';
34
27
  import type { CircuitName } from '@aztec/stdlib/stats';
35
- import { AppendOnlyTreeSnapshot, type MerkleTreeId } from '@aztec/stdlib/trees';
36
- import { type BlockHeader, type GlobalVariables, StateReference } from '@aztec/stdlib/tx';
37
- import { VkData } from '@aztec/stdlib/vks';
28
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
29
+ import { BlockHeader, GlobalVariables, StateReference } from '@aztec/stdlib/tx';
30
+ import type { UInt64 } from '@aztec/stdlib/types';
38
31
 
39
- import {
40
- accumulateBlobs,
41
- buildBlobHints,
42
- buildHeaderFromCircuitOutputs,
43
- getEmptyBlockBlobsHash,
44
- } from './block-building-helpers.js';
45
- import type { EpochProvingState } from './epoch-proving-state.js';
32
+ import { buildHeaderFromCircuitOutputs, toProofData } from './block-building-helpers.js';
33
+ import type { CheckpointProvingState } from './checkpoint-proving-state.js';
46
34
  import type { TxProvingState } from './tx-proving-state.js';
47
35
 
48
- export type TreeSnapshots = Map<MerkleTreeId, AppendOnlyTreeSnapshot>;
36
+ export type ProofState<T, PROOF_LENGTH extends number> = {
37
+ provingOutput?: PublicInputsAndRecursiveProof<T, PROOF_LENGTH>;
38
+ isProving?: boolean;
39
+ };
49
40
 
50
41
  /**
51
42
  * The current state of the proving schedule for a given block. Managed by ProvingState.
52
43
  * Contains the raw inputs and intermediate state to generate every constituent proof in the tree.
53
44
  */
54
45
  export class BlockProvingState {
55
- private baseOrMergeProvingOutputs: UnbalancedTreeStore<
56
- PublicInputsAndRecursiveProof<BaseOrMergeRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
46
+ private baseOrMergeProofs: UnbalancedTreeStore<
47
+ ProofState<TxRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
57
48
  > = new UnbalancedTreeStore(0);
58
- private baseParityProvingOutputs: (PublicInputsAndRecursiveProof<ParityPublicInputs> | undefined)[];
59
- private rootParityProvingOutput: PublicInputsAndRecursiveProof<ParityPublicInputs> | undefined;
60
- private blockRootProvingOutput:
61
- | PublicInputsAndRecursiveProof<BlockRootOrBlockMergePublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
49
+ private baseParityProofs: (ProofState<ParityPublicInputs, typeof NESTED_RECURSIVE_PROOF_LENGTH> | undefined)[] =
50
+ Array.from({
51
+ length: NUM_BASE_PARITY_PER_ROOT_PARITY,
52
+ }).map(_ => undefined);
53
+ private rootParityProof: ProofState<ParityPublicInputs, typeof NESTED_RECURSIVE_PROOF_LENGTH> | undefined;
54
+ private blockRootProof:
55
+ | ProofState<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
62
56
  | undefined;
63
- public blockRootRollupStarted: boolean = false;
64
- public block: L2Block | undefined;
65
- public spongeBlobState: SpongeBlob | undefined;
66
- public startBlobAccumulator: BatchedBlobAccumulator | undefined;
67
- public endBlobAccumulator: BatchedBlobAccumulator | undefined;
68
- public blobsHash: Fr | undefined;
69
- public totalNumTxs: number;
57
+ private builtBlockHeader: BlockHeader | undefined;
58
+ private builtArchive: AppendOnlyTreeSnapshot | undefined;
59
+ private endState: StateReference | undefined;
60
+ private endSpongeBlob: SpongeBlob | undefined;
70
61
  private txs: TxProvingState[] = [];
71
- public error: string | undefined;
62
+ private isFirstBlock: boolean;
63
+ private error: string | undefined;
72
64
 
73
65
  constructor(
74
66
  public readonly index: number,
75
- public readonly globalVariables: GlobalVariables,
76
- public readonly newL1ToL2Messages: Fr[],
77
- public readonly l1ToL2MessageTreeSnapshot: AppendOnlyTreeSnapshot,
78
- private readonly l1ToL2MessageSubtreeSiblingPath: Tuple<Fr, typeof L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH>,
79
- public readonly l1ToL2MessageTreeSnapshotAfterInsertion: AppendOnlyTreeSnapshot,
80
- private readonly lastArchiveSnapshot: AppendOnlyTreeSnapshot,
67
+ public readonly blockNumber: BlockNumber,
68
+ public readonly totalNumTxs: number,
69
+ private readonly constants: CheckpointConstantData,
70
+ private readonly timestamp: UInt64,
71
+ public readonly lastArchiveTreeSnapshot: AppendOnlyTreeSnapshot,
81
72
  private readonly lastArchiveSiblingPath: Tuple<Fr, typeof ARCHIVE_HEIGHT>,
82
- private readonly newArchiveSiblingPath: Tuple<Fr, typeof ARCHIVE_HEIGHT>,
83
- private readonly previousBlockHeader: BlockHeader,
84
- private readonly proverId: EthAddress,
85
- private readonly parentEpoch: EpochProvingState,
73
+ private readonly lastL1ToL2MessageTreeSnapshot: AppendOnlyTreeSnapshot,
74
+ private readonly lastL1ToL2MessageSubtreeRootSiblingPath: Tuple<
75
+ Fr,
76
+ typeof L1_TO_L2_MSG_SUBTREE_ROOT_SIBLING_PATH_LENGTH
77
+ >,
78
+ public readonly newL1ToL2MessageTreeSnapshot: AppendOnlyTreeSnapshot,
79
+ private readonly headerOfLastBlockInPreviousCheckpoint: BlockHeader,
80
+ private readonly startSpongeBlob: SpongeBlob,
81
+ public parentCheckpoint: CheckpointProvingState,
86
82
  ) {
87
- this.baseParityProvingOutputs = Array.from({ length: NUM_BASE_PARITY_PER_ROOT_PARITY }).map(_ => undefined);
88
- this.totalNumTxs = 0;
89
- if (this.blockNumber == parentEpoch.firstBlockNumber) {
90
- this.startBlobAccumulator = BatchedBlobAccumulator.newWithChallenges(parentEpoch.finalBlobBatchingChallenges);
83
+ this.isFirstBlock = index === 0;
84
+ if (!totalNumTxs && !this.isFirstBlock) {
85
+ throw new Error(`Cannot create a block with 0 txs, unless it's the first block.`);
91
86
  }
92
- }
93
87
 
94
- public get blockNumber() {
95
- return this.globalVariables.blockNumber;
88
+ this.baseOrMergeProofs = new UnbalancedTreeStore(totalNumTxs);
96
89
  }
97
90
 
98
- public startNewBlock(numTxs: number, numBlobFields: number) {
99
- if (this.spongeBlobState) {
100
- throw new Error(`Block ${this.blockNumber} already initalised.`);
101
- }
102
-
103
- this.baseOrMergeProvingOutputs = new UnbalancedTreeStore(numTxs);
104
- // Initialize the sponge which will eventually absorb all tx effects to be added to the blob.
105
- // Like l1 to l2 messages, we need to know beforehand how many effects will be absorbed.
106
- this.spongeBlobState = SpongeBlob.init(numBlobFields);
107
- this.totalNumTxs = numTxs;
91
+ public get epochNumber(): number {
92
+ return this.parentCheckpoint.epochNumber;
108
93
  }
109
94
 
110
95
  // Adds a transaction to the proving state, returns it's index
111
96
  public addNewTx(tx: TxProvingState) {
112
- if (!this.spongeBlobState) {
113
- throw new Error(`Invalid block proving state, call startNewBlock before adding transactions.`);
97
+ if (!this.isAcceptingTxs()) {
98
+ throw new Error(`Cannot add more txs to block ${this.blockNumber}.`);
114
99
  }
115
100
  const txIndex = this.txs.length;
116
101
  this.txs[txIndex] = tx;
117
102
  return txIndex;
118
103
  }
119
104
 
105
+ public isAcceptingTxs() {
106
+ return this.txs.length < this.totalNumTxs;
107
+ }
108
+
109
+ public getProcessedTxs() {
110
+ return this.txs.map(t => t.processedTx);
111
+ }
112
+
113
+ public tryStartProvingBase(txIndex: number) {
114
+ if (this.baseOrMergeProofs.getLeaf(txIndex)?.isProving) {
115
+ return false;
116
+ } else {
117
+ this.baseOrMergeProofs.setLeaf(txIndex, { isProving: true });
118
+ return true;
119
+ }
120
+ }
121
+
120
122
  public setBaseRollupProof(
121
123
  txIndex: number,
122
124
  provingOutput: PublicInputsAndRecursiveProof<
123
- BaseOrMergeRollupPublicInputs,
125
+ TxRollupPublicInputs,
124
126
  typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
125
127
  >,
126
128
  ): TreeNodeLocation {
127
- return this.baseOrMergeProvingOutputs.setLeaf(txIndex, provingOutput);
129
+ return this.baseOrMergeProofs.setLeaf(txIndex, { provingOutput });
130
+ }
131
+
132
+ public tryStartProvingMerge(location: TreeNodeLocation) {
133
+ if (this.baseOrMergeProofs.getNode(location)?.isProving) {
134
+ return false;
135
+ } else {
136
+ this.baseOrMergeProofs.setNode(location, { isProving: true });
137
+ return true;
138
+ }
128
139
  }
129
140
 
130
141
  public setMergeRollupProof(
131
142
  location: TreeNodeLocation,
132
143
  provingOutput: PublicInputsAndRecursiveProof<
133
- BaseOrMergeRollupPublicInputs,
144
+ TxRollupPublicInputs,
134
145
  typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
135
146
  >,
136
147
  ) {
137
- this.baseOrMergeProvingOutputs.setNode(location, provingOutput);
148
+ this.baseOrMergeProofs.setNode(location, { provingOutput });
149
+ }
150
+
151
+ public tryStartProvingBaseParity(index: number) {
152
+ if (this.baseParityProofs[index]?.isProving) {
153
+ return false;
154
+ } else {
155
+ this.baseParityProofs[index] = { isProving: true };
156
+ return true;
157
+ }
138
158
  }
139
159
 
140
160
  // Stores a set of root parity inputs at the given index
@@ -144,144 +164,236 @@ export class BlockProvingState {
144
164
  `Unable to set a base parity proofs at index ${index}. Expected at most ${NUM_BASE_PARITY_PER_ROOT_PARITY} proofs.`,
145
165
  );
146
166
  }
147
- this.baseParityProvingOutputs[index] = provingOutput;
167
+ this.baseParityProofs[index] = { provingOutput };
168
+ }
169
+
170
+ public tryStartProvingRootParity() {
171
+ if (this.rootParityProof?.isProving) {
172
+ return false;
173
+ } else {
174
+ this.rootParityProof = { isProving: true };
175
+ return true;
176
+ }
148
177
  }
149
178
 
150
179
  public setRootParityProof(provingOutput: PublicInputsAndRecursiveProof<ParityPublicInputs>) {
151
- this.rootParityProvingOutput = provingOutput;
180
+ this.rootParityProof = { provingOutput };
181
+ }
182
+
183
+ public tryStartProvingBlockRoot() {
184
+ if (this.blockRootProof?.isProving) {
185
+ return false;
186
+ } else {
187
+ this.blockRootProof = { isProving: true };
188
+ return true;
189
+ }
152
190
  }
153
191
 
154
192
  public setBlockRootRollupProof(
155
193
  provingOutput: PublicInputsAndRecursiveProof<
156
- BlockRootOrBlockMergePublicInputs,
194
+ BlockRollupPublicInputs,
157
195
  typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH
158
196
  >,
159
- ) {
160
- this.blockRootProvingOutput = provingOutput;
197
+ ): TreeNodeLocation {
198
+ this.blockRootProof = { provingOutput };
199
+ return this.parentCheckpoint.setBlockRootRollupProof(this.index, provingOutput);
200
+ }
201
+
202
+ public getBlockRootRollupOutput() {
203
+ return this.blockRootProof?.provingOutput?.inputs;
204
+ }
205
+
206
+ public async buildBlockHeader() {
207
+ if (this.isAcceptingTxs()) {
208
+ throw new Error('All txs must be added to the block before building the header.');
209
+ }
210
+ if (!this.endState) {
211
+ throw new Error('Call `setEndState` first.');
212
+ }
213
+ if (!this.endSpongeBlob) {
214
+ throw new Error('Call `setEndSpongeBlob` first.');
215
+ }
216
+
217
+ const endSpongeBlob = this.endSpongeBlob.clone();
218
+ const endSpongeBlobHash = await endSpongeBlob.squeeze();
219
+
220
+ this.builtBlockHeader = new BlockHeader(
221
+ this.lastArchiveTreeSnapshot,
222
+ this.endState,
223
+ endSpongeBlobHash,
224
+ this.#getGlobalVariables(),
225
+ this.#getTotalFees(),
226
+ new Fr(this.#getTotalManaUsed()),
227
+ );
228
+
229
+ return this.builtBlockHeader;
161
230
  }
162
231
 
163
- public setBlock(block: L2Block) {
164
- this.block = block;
232
+ public getBuiltBlockHeader() {
233
+ return this.builtBlockHeader;
165
234
  }
166
235
 
167
- public setStartBlobAccumulator(accumulator: BatchedBlobAccumulator) {
168
- this.startBlobAccumulator = accumulator;
236
+ public setBuiltArchive(archive: AppendOnlyTreeSnapshot) {
237
+ this.builtArchive = archive;
169
238
  }
170
239
 
171
- public setEndBlobAccumulator(accumulator: BatchedBlobAccumulator) {
172
- this.endBlobAccumulator = accumulator;
240
+ public getBuiltArchive() {
241
+ return this.builtArchive;
173
242
  }
174
243
 
175
- public async accumulateBlobs() {
176
- if (!this.block || !this.startBlobAccumulator) {
177
- // We only want to accumulate once we have all txs, so we wait until the block is set.
178
- return;
179
- }
180
- const endBlobAccumulator = await accumulateBlobs(
181
- this.allTxs.map(t => t.processedTx),
182
- this.startBlobAccumulator,
183
- );
184
- this.setEndBlobAccumulator(endBlobAccumulator);
244
+ public getStartSpongeBlob() {
245
+ return this.startSpongeBlob;
185
246
  }
186
247
 
187
- // Returns the complete set of transaction proving state objects
188
- public get allTxs() {
189
- return this.txs;
248
+ public setEndSpongeBlob(endSpongeBlob: SpongeBlob) {
249
+ this.endSpongeBlob = endSpongeBlob;
190
250
  }
191
251
 
192
- /** Returns the block number as an epoch number. Used for prioritizing proof requests. */
193
- public get epochNumber(): number {
194
- return this.parentEpoch.epochNumber;
252
+ public getEndSpongeBlob() {
253
+ return this.endSpongeBlob;
254
+ }
255
+
256
+ public setEndState(endState: StateReference) {
257
+ this.endState = endState;
258
+ }
259
+
260
+ public hasEndState() {
261
+ return !!this.endState;
262
+ }
263
+
264
+ public getBlockEndBlobFields(): Fr[] {
265
+ return encodeBlockEndBlobData(this.getBlockEndBlobData());
266
+ }
267
+
268
+ getBlockEndBlobData(): BlockEndBlobData {
269
+ if (!this.endState) {
270
+ throw new Error('Call `setEndState` first.');
271
+ }
272
+
273
+ const partial = this.endState.partial;
274
+ return {
275
+ blockEndMarker: {
276
+ numTxs: this.totalNumTxs,
277
+ timestamp: this.timestamp,
278
+ blockNumber: this.blockNumber,
279
+ },
280
+ blockEndStateField: {
281
+ l1ToL2MessageNextAvailableLeafIndex: this.newL1ToL2MessageTreeSnapshot.nextAvailableLeafIndex,
282
+ noteHashNextAvailableLeafIndex: partial.noteHashTree.nextAvailableLeafIndex,
283
+ nullifierNextAvailableLeafIndex: partial.nullifierTree.nextAvailableLeafIndex,
284
+ publicDataNextAvailableLeafIndex: partial.publicDataTree.nextAvailableLeafIndex,
285
+ totalManaUsed: this.#getTotalManaUsed(),
286
+ },
287
+ lastArchiveRoot: this.lastArchiveTreeSnapshot.root,
288
+ noteHashRoot: partial.noteHashTree.root,
289
+ nullifierRoot: partial.nullifierTree.root,
290
+ publicDataRoot: partial.publicDataTree.root,
291
+ l1ToL2MessageRoot: this.isFirstBlock ? this.newL1ToL2MessageTreeSnapshot.root : undefined,
292
+ };
293
+ }
294
+
295
+ public getBlockBlobData(): BlockBlobData {
296
+ return {
297
+ ...this.getBlockEndBlobData(),
298
+ txs: this.getTxEffects().map(t => t.toTxBlobData()),
299
+ };
300
+ }
301
+
302
+ public getTxEffects() {
303
+ return this.txs.map(t => t.processedTx.txEffect);
195
304
  }
196
305
 
197
306
  public getParentLocation(location: TreeNodeLocation) {
198
- return this.baseOrMergeProvingOutputs.getParentLocation(location);
307
+ return this.baseOrMergeProofs.getParentLocation(location);
199
308
  }
200
309
 
201
310
  public getMergeRollupInputs(mergeLocation: TreeNodeLocation) {
202
- const [left, right] = this.baseOrMergeProvingOutputs.getChildren(mergeLocation);
311
+ const [left, right] = this.baseOrMergeProofs.getChildren(mergeLocation).map(c => c?.provingOutput);
203
312
  if (!left || !right) {
204
- throw new Error('At lease one child is not ready.');
313
+ throw new Error('At least one child is not ready for the merge rollup.');
205
314
  }
206
315
 
207
- return new MergeRollupInputs([this.#getPreviousRollupData(left), this.#getPreviousRollupData(right)]);
316
+ return new TxMergeRollupPrivateInputs([toProofData(left), toProofData(right)]);
208
317
  }
209
318
 
210
- public async getBlockRootRollupTypeAndInputs() {
211
- if (!this.rootParityProvingOutput) {
212
- throw new Error('Root parity is not ready.');
213
- }
214
-
215
- const proofs = this.#getChildProofsForBlockRoot();
216
- const nonEmptyProofs = proofs.filter(p => !!p);
217
- if (proofs.length !== nonEmptyProofs.length) {
218
- throw new Error('At lease one child is not ready for the block root.');
319
+ public getBlockRootRollupTypeAndInputs() {
320
+ const provingOutputs = this.#getChildProvingOutputsForBlockRoot();
321
+ if (!provingOutputs.every(p => !!p)) {
322
+ throw new Error('At least one child is not ready for the block root rollup.');
219
323
  }
220
324
 
221
- const data = this.#getBlockRootRollupData();
222
-
223
- if (this.totalNumTxs === 0) {
224
- const constants = BlockConstantData.from({
225
- lastArchive: this.lastArchiveSnapshot,
226
- newL1ToL2: this.l1ToL2MessageTreeSnapshotAfterInsertion,
227
- globalVariables: this.globalVariables,
228
- vkTreeRoot: getVKTreeRoot(),
229
- protocolContractTreeRoot,
230
- });
325
+ const previousRollups = provingOutputs.map(p => toProofData(p));
231
326
 
232
- this.blobsHash = await getEmptyBlockBlobsHash();
327
+ if (this.isFirstBlock) {
328
+ return this.#getFirstBlockRootRollupTypeAndInputs(previousRollups);
329
+ }
233
330
 
331
+ const [leftRollup, rightRollup] = previousRollups;
332
+ if (!rightRollup) {
333
+ return {
334
+ rollupType: 'rollup-block-root-single-tx' satisfies CircuitName,
335
+ inputs: new BlockRootSingleTxRollupPrivateInputs(leftRollup, this.lastArchiveSiblingPath),
336
+ };
337
+ } else {
234
338
  return {
235
- rollupType: 'empty-block-root-rollup' satisfies CircuitName,
236
- inputs: EmptyBlockRootRollupInputs.from({
237
- data,
238
- constants,
239
- }),
339
+ rollupType: 'rollup-block-root' satisfies CircuitName,
340
+ inputs: new BlockRootRollupPrivateInputs([leftRollup, rightRollup], this.lastArchiveSiblingPath),
240
341
  };
241
342
  }
343
+ }
242
344
 
243
- const previousRollupData = await Promise.all(nonEmptyProofs.map(p => this.#getPreviousRollupData(p!)));
244
- const blobData = await this.#getBlockRootRollupBlobData();
245
- this.blobsHash = blobData.blobsHash;
345
+ #getFirstBlockRootRollupTypeAndInputs([leftRollup, rightRollup]: RollupHonkProofData<TxRollupPublicInputs>[]) {
346
+ if (!this.rootParityProof?.provingOutput) {
347
+ throw new Error('Root parity is not ready.');
348
+ }
349
+ const l1ToL2Roots = toProofData(this.rootParityProof.provingOutput);
246
350
 
247
- if (previousRollupData.length === 1) {
351
+ if (!leftRollup) {
352
+ return {
353
+ rollupType: 'rollup-block-root-first-empty-tx' satisfies CircuitName,
354
+ inputs: new BlockRootEmptyTxFirstRollupPrivateInputs(
355
+ l1ToL2Roots,
356
+ this.lastArchiveTreeSnapshot,
357
+ this.headerOfLastBlockInPreviousCheckpoint.state,
358
+ this.constants,
359
+ this.timestamp,
360
+ this.lastL1ToL2MessageSubtreeRootSiblingPath,
361
+ this.lastArchiveSiblingPath,
362
+ ),
363
+ };
364
+ } else if (!rightRollup) {
248
365
  return {
249
- rollupType: 'single-tx-block-root-rollup' satisfies CircuitName,
250
- inputs: new SingleTxBlockRootRollupInputs(previousRollupData as [PreviousRollupData], data, blobData),
366
+ rollupType: 'rollup-block-root-first-single-tx' satisfies CircuitName,
367
+ inputs: new BlockRootSingleTxFirstRollupPrivateInputs(
368
+ l1ToL2Roots,
369
+ leftRollup,
370
+ this.lastL1ToL2MessageTreeSnapshot,
371
+ this.lastL1ToL2MessageSubtreeRootSiblingPath,
372
+ this.lastArchiveSiblingPath,
373
+ ),
251
374
  };
252
375
  } else {
253
376
  return {
254
- rollupType: 'block-root-rollup' satisfies CircuitName,
255
- inputs: new BlockRootRollupInputs(
256
- previousRollupData as [PreviousRollupData, PreviousRollupData],
257
- data,
258
- blobData,
377
+ rollupType: 'rollup-block-root-first' satisfies CircuitName,
378
+ inputs: new BlockRootFirstRollupPrivateInputs(
379
+ l1ToL2Roots,
380
+ [leftRollup, rightRollup],
381
+ this.lastL1ToL2MessageTreeSnapshot,
382
+ this.lastL1ToL2MessageSubtreeRootSiblingPath,
383
+ this.lastArchiveSiblingPath,
259
384
  ),
260
385
  };
261
386
  }
262
387
  }
263
388
 
264
- public getPaddingBlockRootInputs() {
265
- const constants = EpochConstantData.from({
266
- vkTreeRoot: getVKTreeRoot(),
267
- protocolContractTreeRoot,
268
- proverId: this.proverId.toField(),
269
- });
270
-
271
- return PaddingBlockRootRollupInputs.from({
272
- constants,
273
- });
274
- }
275
-
276
- public getRootParityInputs() {
277
- if (!this.baseParityProvingOutputs.every(p => !!p)) {
389
+ public getParityRootInputs() {
390
+ const baseParityProvingOutputs = this.baseParityProofs.filter(p => !!p?.provingOutput).map(p => p!.provingOutput!);
391
+ if (baseParityProvingOutputs.length !== this.baseParityProofs.length) {
278
392
  throw new Error('At lease one base parity is not ready.');
279
393
  }
280
394
 
281
- const children = this.baseParityProvingOutputs.map(p => this.#getRootParityData(p!));
282
- return new RootParityInputs(
283
- children as Tuple<RootParityInput<typeof RECURSIVE_PROOF_LENGTH>, typeof NUM_BASE_PARITY_PER_ROOT_PARITY>,
284
- );
395
+ const children = baseParityProvingOutputs.map(p => toProofData(p));
396
+ return new ParityRootPrivateInputs(assertLength(children, NUM_BASE_PARITY_PER_ROOT_PARITY));
285
397
  }
286
398
 
287
399
  // Returns a specific transaction proving state
@@ -290,89 +402,46 @@ export class BlockProvingState {
290
402
  }
291
403
 
292
404
  public async buildHeaderFromProvingOutputs() {
293
- const previousRollupData =
294
- this.totalNumTxs === 0
295
- ? []
296
- : await Promise.all(this.#getChildProofsForBlockRoot().map(p => this.#getPreviousRollupData(p!)));
297
-
298
- let endPartialState = this.previousBlockHeader.state.partial;
299
- if (this.totalNumTxs !== 0) {
300
- const previousRollupData = this.#getChildProofsForBlockRoot();
301
- const lastRollup = previousRollupData[previousRollupData.length - 1];
302
- if (!lastRollup) {
303
- throw new Error('End state of the block is not available. Last rollup is not ready yet.');
304
- }
305
- endPartialState = lastRollup.inputs.end;
405
+ if (!this.blockRootProof?.provingOutput) {
406
+ throw new Error('Block root rollup is not ready.');
306
407
  }
307
- const endState = new StateReference(this.l1ToL2MessageTreeSnapshotAfterInsertion, endPartialState);
308
-
309
- return buildHeaderFromCircuitOutputs(
310
- previousRollupData.map(d => d.baseOrMergeRollupPublicInputs),
311
- this.rootParityProvingOutput!.inputs,
312
- this.blockRootProvingOutput!.inputs,
313
- this.blobsHash!,
314
- endState,
315
- );
408
+
409
+ return await buildHeaderFromCircuitOutputs(this.blockRootProof.provingOutput.inputs);
316
410
  }
317
411
 
318
412
  public isReadyForMergeRollup(location: TreeNodeLocation) {
319
- return this.baseOrMergeProvingOutputs.getSibling(location) !== undefined;
413
+ return !!this.baseOrMergeProofs.getSibling(location)?.provingOutput;
320
414
  }
321
415
 
322
416
  // Returns true if we have sufficient inputs to execute the block root rollup
323
417
  public isReadyForBlockRootRollup() {
324
- const childProofs = this.#getChildProofsForBlockRoot();
325
- return (
326
- this.block !== undefined &&
327
- this.rootParityProvingOutput !== undefined &&
328
- this.endBlobAccumulator !== undefined &&
329
- childProofs.every(p => !!p)
330
- );
418
+ const childProofs = this.#getChildProvingOutputsForBlockRoot();
419
+ return (!this.isFirstBlock || !!this.rootParityProof?.provingOutput) && childProofs.every(p => !!p);
331
420
  }
332
421
 
333
422
  // Returns true if we have sufficient root parity inputs to execute the root parity circuit
334
423
  public isReadyForRootParity() {
335
- return this.baseParityProvingOutputs.every(p => !!p);
424
+ return this.baseParityProofs.every(p => !!p?.provingOutput);
336
425
  }
337
426
 
338
427
  public isComplete() {
339
- return !!this.blockRootProvingOutput;
428
+ return !!this.blockRootProof;
340
429
  }
341
430
 
342
- // Returns whether the proving state is still valid
343
431
  public verifyState() {
344
- return this.parentEpoch.verifyState();
432
+ return this.parentCheckpoint.verifyState();
345
433
  }
346
434
 
347
- public reject(reason: string) {
348
- this.error = reason;
349
- this.parentEpoch.reject(reason);
350
- }
351
-
352
- #getBlockRootRollupData() {
353
- return BlockRootRollupData.from({
354
- l1ToL2Roots: this.#getRootParityData(this.rootParityProvingOutput!),
355
- l1ToL2MessageSubtreeSiblingPath: this.l1ToL2MessageSubtreeSiblingPath,
356
- previousArchiveSiblingPath: this.lastArchiveSiblingPath,
357
- newArchiveSiblingPath: this.newArchiveSiblingPath,
358
- previousBlockHeader: this.previousBlockHeader,
359
- startBlobAccumulator: BlobAccumulatorPublicInputs.fromBatchedBlobAccumulator(this.startBlobAccumulator!),
360
- finalBlobChallenges: this.startBlobAccumulator!.finalBlobChallenges,
361
- proverId: this.proverId.toField(),
362
- });
435
+ public getError() {
436
+ return this.error;
363
437
  }
364
438
 
365
- async #getBlockRootRollupBlobData() {
366
- const txEffects = this.txs.map(txProvingState => txProvingState.processedTx.txEffect);
367
- const { blobFields, blobCommitments, blobsHash } = await buildBlobHints(txEffects);
368
- return BlockRootRollupBlobData.from({
369
- blobFields: padArrayEnd(blobFields, Fr.ZERO, FIELDS_PER_BLOB * BLOBS_PER_BLOCK),
370
- blobCommitments: padArrayEnd(blobCommitments, BLS12Point.ZERO, BLOBS_PER_BLOCK),
371
- blobsHash,
372
- });
439
+ public reject(reason: string) {
440
+ this.error = reason;
441
+ this.parentCheckpoint.reject(reason);
373
442
  }
374
443
 
375
- #getChildProofsForBlockRoot() {
444
+ #getChildProvingOutputsForBlockRoot() {
376
445
  if (this.totalNumTxs === 0) {
377
446
  return [];
378
447
  }
@@ -380,26 +449,34 @@ export class BlockProvingState {
380
449
  const rootLocation = { level: 0, index: 0 };
381
450
  // If there's only 1 tx, its base rollup proof will be stored at the root.
382
451
  return this.totalNumTxs === 1
383
- ? [this.baseOrMergeProvingOutputs.getNode(rootLocation)]
384
- : this.baseOrMergeProvingOutputs.getChildren(rootLocation);
385
- }
386
-
387
- #getPreviousRollupData({
388
- inputs,
389
- proof,
390
- verificationKey,
391
- }: PublicInputsAndRecursiveProof<BaseOrMergeRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>) {
392
- const leafIndex = getVKIndex(verificationKey.keyAsFields);
393
- const vkData = new VkData(verificationKey, leafIndex, getVKSiblingPath(leafIndex));
394
- return new PreviousRollupData(inputs, proof, vkData);
395
- }
396
-
397
- #getRootParityData({ inputs, proof, verificationKey }: PublicInputsAndRecursiveProof<ParityPublicInputs>) {
398
- return new RootParityInput(
399
- proof,
400
- verificationKey.keyAsFields,
401
- getVKSiblingPath(getVKIndex(verificationKey)),
402
- inputs,
403
- );
452
+ ? [this.baseOrMergeProofs.getNode(rootLocation)?.provingOutput]
453
+ : this.baseOrMergeProofs.getChildren(rootLocation).map(c => c?.provingOutput);
454
+ }
455
+
456
+ #getGlobalVariables() {
457
+ if (this.txs.length) {
458
+ return this.txs[0].processedTx.globalVariables;
459
+ }
460
+
461
+ const constants = this.constants;
462
+
463
+ return GlobalVariables.from({
464
+ chainId: constants.chainId,
465
+ version: constants.version,
466
+ blockNumber: this.blockNumber,
467
+ slotNumber: constants.slotNumber,
468
+ timestamp: this.timestamp,
469
+ coinbase: constants.coinbase,
470
+ feeRecipient: constants.feeRecipient,
471
+ gasFees: constants.gasFees,
472
+ });
473
+ }
474
+
475
+ #getTotalFees() {
476
+ return this.txs.reduce((acc, tx) => acc.add(tx.processedTx.txEffect.transactionFee), Fr.ZERO);
477
+ }
478
+
479
+ #getTotalManaUsed() {
480
+ return this.txs.reduce((acc, tx) => acc + BigInt(tx.processedTx.gasUsed.billedGas.l2Gas), 0n);
404
481
  }
405
482
  }