@aztec/prover-client 0.0.0-test.1 → 0.0.1-commit.21caa21

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