@aztec/prover-client 0.0.0-test.0
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.
- package/README.md +1 -0
- package/dest/bin/get-proof-inputs.d.ts +2 -0
- package/dest/bin/get-proof-inputs.d.ts.map +1 -0
- package/dest/bin/get-proof-inputs.js +51 -0
- package/dest/block_builder/index.d.ts +6 -0
- package/dest/block_builder/index.d.ts.map +1 -0
- package/dest/block_builder/index.js +1 -0
- package/dest/block_builder/light.d.ts +33 -0
- package/dest/block_builder/light.d.ts.map +1 -0
- package/dest/block_builder/light.js +82 -0
- package/dest/config.d.ts +17 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +39 -0
- package/dest/index.d.ts +4 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +2 -0
- package/dest/mocks/fixtures.d.ts +20 -0
- package/dest/mocks/fixtures.d.ts.map +1 -0
- package/dest/mocks/fixtures.js +77 -0
- package/dest/mocks/test_context.d.ts +55 -0
- package/dest/mocks/test_context.d.ts.map +1 -0
- package/dest/mocks/test_context.js +193 -0
- package/dest/orchestrator/block-building-helpers.d.ts +55 -0
- package/dest/orchestrator/block-building-helpers.d.ts.map +1 -0
- package/dest/orchestrator/block-building-helpers.js +285 -0
- package/dest/orchestrator/block-proving-state.d.ts +76 -0
- package/dest/orchestrator/block-proving-state.d.ts.map +1 -0
- package/dest/orchestrator/block-proving-state.js +269 -0
- package/dest/orchestrator/epoch-proving-state.d.ts +60 -0
- package/dest/orchestrator/epoch-proving-state.d.ts.map +1 -0
- package/dest/orchestrator/epoch-proving-state.js +163 -0
- package/dest/orchestrator/index.d.ts +2 -0
- package/dest/orchestrator/index.d.ts.map +1 -0
- package/dest/orchestrator/index.js +1 -0
- package/dest/orchestrator/orchestrator.d.ts +110 -0
- package/dest/orchestrator/orchestrator.d.ts.map +1 -0
- package/dest/orchestrator/orchestrator.js +690 -0
- package/dest/orchestrator/orchestrator_metrics.d.ts +8 -0
- package/dest/orchestrator/orchestrator_metrics.d.ts.map +1 -0
- package/dest/orchestrator/orchestrator_metrics.js +17 -0
- package/dest/orchestrator/tx-proving-state.d.ts +34 -0
- package/dest/orchestrator/tx-proving-state.d.ts.map +1 -0
- package/dest/orchestrator/tx-proving-state.js +94 -0
- package/dest/prover-client/factory.d.ts +6 -0
- package/dest/prover-client/factory.d.ts.map +1 -0
- package/dest/prover-client/factory.js +5 -0
- package/dest/prover-client/index.d.ts +3 -0
- package/dest/prover-client/index.d.ts.map +1 -0
- package/dest/prover-client/index.js +2 -0
- package/dest/prover-client/prover-client.d.ts +42 -0
- package/dest/prover-client/prover-client.d.ts.map +1 -0
- package/dest/prover-client/prover-client.js +110 -0
- package/dest/prover-client/server-epoch-prover.d.ts +28 -0
- package/dest/prover-client/server-epoch-prover.d.ts.map +1 -0
- package/dest/prover-client/server-epoch-prover.js +40 -0
- package/dest/proving_broker/broker_prover_facade.d.ts +46 -0
- package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -0
- package/dest/proving_broker/broker_prover_facade.js +344 -0
- package/dest/proving_broker/config.d.ts +83 -0
- package/dest/proving_broker/config.d.ts.map +1 -0
- package/dest/proving_broker/config.js +104 -0
- package/dest/proving_broker/factory.d.ts +5 -0
- package/dest/proving_broker/factory.d.ts.map +1 -0
- package/dest/proving_broker/factory.js +9 -0
- package/dest/proving_broker/fixtures.d.ts +5 -0
- package/dest/proving_broker/fixtures.d.ts.map +1 -0
- package/dest/proving_broker/fixtures.js +12 -0
- package/dest/proving_broker/index.d.ts +10 -0
- package/dest/proving_broker/index.d.ts.map +1 -0
- package/dest/proving_broker/index.js +9 -0
- package/dest/proving_broker/proof_store/factory.d.ts +6 -0
- package/dest/proving_broker/proof_store/factory.d.ts.map +1 -0
- package/dest/proving_broker/proof_store/factory.js +36 -0
- package/dest/proving_broker/proof_store/gcs_proof_store.d.ts +14 -0
- package/dest/proving_broker/proof_store/gcs_proof_store.d.ts.map +1 -0
- package/dest/proving_broker/proof_store/gcs_proof_store.js +51 -0
- package/dest/proving_broker/proof_store/index.d.ts +4 -0
- package/dest/proving_broker/proof_store/index.d.ts.map +1 -0
- package/dest/proving_broker/proof_store/index.js +3 -0
- package/dest/proving_broker/proof_store/inline_proof_store.d.ts +15 -0
- package/dest/proving_broker/proof_store/inline_proof_store.d.ts.map +1 -0
- package/dest/proving_broker/proof_store/inline_proof_store.js +41 -0
- package/dest/proving_broker/proof_store/proof_store.d.ts +36 -0
- package/dest/proving_broker/proof_store/proof_store.d.ts.map +1 -0
- package/dest/proving_broker/proof_store/proof_store.js +3 -0
- package/dest/proving_broker/proving_agent.d.ts +46 -0
- package/dest/proving_broker/proving_agent.d.ts.map +1 -0
- package/dest/proving_broker/proving_agent.js +134 -0
- package/dest/proving_broker/proving_agent_instrumentation.d.ts +8 -0
- package/dest/proving_broker/proving_agent_instrumentation.d.ts.map +1 -0
- package/dest/proving_broker/proving_agent_instrumentation.js +16 -0
- package/dest/proving_broker/proving_broker.d.ts +64 -0
- package/dest/proving_broker/proving_broker.d.ts.map +1 -0
- package/dest/proving_broker/proving_broker.js +570 -0
- package/dest/proving_broker/proving_broker_database/memory.d.ts +16 -0
- package/dest/proving_broker/proving_broker_database/memory.d.ts.map +1 -0
- package/dest/proving_broker/proving_broker_database/memory.js +54 -0
- package/dest/proving_broker/proving_broker_database/persisted.d.ts +25 -0
- package/dest/proving_broker/proving_broker_database/persisted.d.ts.map +1 -0
- package/dest/proving_broker/proving_broker_database/persisted.js +182 -0
- package/dest/proving_broker/proving_broker_database.d.ts +39 -0
- package/dest/proving_broker/proving_broker_database.d.ts.map +1 -0
- package/dest/proving_broker/proving_broker_database.js +3 -0
- package/dest/proving_broker/proving_broker_instrumentation.d.ts +29 -0
- package/dest/proving_broker/proving_broker_instrumentation.d.ts.map +1 -0
- package/dest/proving_broker/proving_broker_instrumentation.js +110 -0
- package/dest/proving_broker/proving_job_controller.d.ts +33 -0
- package/dest/proving_broker/proving_job_controller.d.ts.map +1 -0
- package/dest/proving_broker/proving_job_controller.js +166 -0
- package/dest/proving_broker/rpc.d.ts +27 -0
- package/dest/proving_broker/rpc.d.ts.map +1 -0
- package/dest/proving_broker/rpc.js +66 -0
- package/dest/test/mock_prover.d.ts +35 -0
- package/dest/test/mock_prover.d.ts.map +1 -0
- package/dest/test/mock_prover.js +82 -0
- package/package.json +112 -0
- package/src/bin/get-proof-inputs.ts +59 -0
- package/src/block_builder/index.ts +6 -0
- package/src/block_builder/light.ts +101 -0
- package/src/config.ts +55 -0
- package/src/index.ts +4 -0
- package/src/mocks/fixtures.ts +117 -0
- package/src/mocks/test_context.ts +257 -0
- package/src/orchestrator/block-building-helpers.ts +553 -0
- package/src/orchestrator/block-proving-state.ts +379 -0
- package/src/orchestrator/epoch-proving-state.ts +252 -0
- package/src/orchestrator/index.ts +1 -0
- package/src/orchestrator/orchestrator.ts +971 -0
- package/src/orchestrator/orchestrator_metrics.ts +22 -0
- package/src/orchestrator/tx-proving-state.ts +139 -0
- package/src/prover-client/factory.ts +14 -0
- package/src/prover-client/index.ts +2 -0
- package/src/prover-client/prover-client.ts +162 -0
- package/src/prover-client/server-epoch-prover.ts +51 -0
- package/src/proving_broker/broker_prover_facade.ts +585 -0
- package/src/proving_broker/config.ts +138 -0
- package/src/proving_broker/factory.ts +18 -0
- package/src/proving_broker/fixtures.ts +15 -0
- package/src/proving_broker/index.ts +9 -0
- package/src/proving_broker/proof_store/factory.ts +42 -0
- package/src/proving_broker/proof_store/gcs_proof_store.ts +72 -0
- package/src/proving_broker/proof_store/index.ts +3 -0
- package/src/proving_broker/proof_store/inline_proof_store.ts +63 -0
- package/src/proving_broker/proof_store/proof_store.ts +54 -0
- package/src/proving_broker/proving_agent.ts +181 -0
- package/src/proving_broker/proving_agent_instrumentation.ts +21 -0
- package/src/proving_broker/proving_broker.ts +687 -0
- package/src/proving_broker/proving_broker_database/memory.ts +63 -0
- package/src/proving_broker/proving_broker_database/persisted.ts +218 -0
- package/src/proving_broker/proving_broker_database.ts +44 -0
- package/src/proving_broker/proving_broker_instrumentation.ts +145 -0
- package/src/proving_broker/proving_job_controller.ts +194 -0
- package/src/proving_broker/rpc.ts +95 -0
- package/src/test/mock_prover.ts +253 -0
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
import { Blob, type SpongeBlob } from '@aztec/blob-lib';
|
|
2
|
+
import {
|
|
3
|
+
ARCHIVE_HEIGHT,
|
|
4
|
+
MAX_CONTRACT_CLASS_LOGS_PER_TX,
|
|
5
|
+
MAX_NOTE_HASHES_PER_TX,
|
|
6
|
+
MAX_NULLIFIERS_PER_TX,
|
|
7
|
+
NOTE_HASH_SUBTREE_HEIGHT,
|
|
8
|
+
NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH,
|
|
9
|
+
NULLIFIER_SUBTREE_HEIGHT,
|
|
10
|
+
NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH,
|
|
11
|
+
NULLIFIER_TREE_HEIGHT,
|
|
12
|
+
NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
|
|
13
|
+
PUBLIC_DATA_TREE_HEIGHT,
|
|
14
|
+
} from '@aztec/constants';
|
|
15
|
+
import { makeTuple } from '@aztec/foundation/array';
|
|
16
|
+
import { padArrayEnd } from '@aztec/foundation/collection';
|
|
17
|
+
import { sha256Trunc } from '@aztec/foundation/crypto';
|
|
18
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
19
|
+
import type { Logger } from '@aztec/foundation/log';
|
|
20
|
+
import { type Tuple, assertLength, serializeToBuffer, toFriendlyJSON } from '@aztec/foundation/serialize';
|
|
21
|
+
import { MembershipWitness, MerkleTreeCalculator, computeUnbalancedMerkleRoot } from '@aztec/foundation/trees';
|
|
22
|
+
import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
|
|
23
|
+
import { protocolContractTreeRoot } from '@aztec/protocol-contracts';
|
|
24
|
+
import { computeFeePayerBalanceLeafSlot } from '@aztec/protocol-contracts/fee-juice';
|
|
25
|
+
import { PublicDataHint } from '@aztec/stdlib/avm';
|
|
26
|
+
import { Body } from '@aztec/stdlib/block';
|
|
27
|
+
import type { MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server';
|
|
28
|
+
import { ContractClassLog } from '@aztec/stdlib/logs';
|
|
29
|
+
import type { ParityPublicInputs } from '@aztec/stdlib/parity';
|
|
30
|
+
import {
|
|
31
|
+
type BaseOrMergeRollupPublicInputs,
|
|
32
|
+
type BlockRootOrBlockMergePublicInputs,
|
|
33
|
+
ConstantRollupData,
|
|
34
|
+
PrivateBaseRollupHints,
|
|
35
|
+
PrivateBaseStateDiffHints,
|
|
36
|
+
PublicBaseRollupHints,
|
|
37
|
+
} from '@aztec/stdlib/rollup';
|
|
38
|
+
import {
|
|
39
|
+
AppendOnlyTreeSnapshot,
|
|
40
|
+
MerkleTreeId,
|
|
41
|
+
NullifierLeafPreimage,
|
|
42
|
+
PublicDataTreeLeaf,
|
|
43
|
+
PublicDataTreeLeafPreimage,
|
|
44
|
+
getTreeHeight,
|
|
45
|
+
} from '@aztec/stdlib/trees';
|
|
46
|
+
import {
|
|
47
|
+
BlockHeader,
|
|
48
|
+
ContentCommitment,
|
|
49
|
+
type GlobalVariables,
|
|
50
|
+
PartialStateReference,
|
|
51
|
+
type ProcessedTx,
|
|
52
|
+
StateReference,
|
|
53
|
+
TxEffect,
|
|
54
|
+
} from '@aztec/stdlib/tx';
|
|
55
|
+
import { Attributes, type Span, runInSpan } from '@aztec/telemetry-client';
|
|
56
|
+
import type { MerkleTreeReadOperations } from '@aztec/world-state';
|
|
57
|
+
|
|
58
|
+
import { inspect } from 'util';
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Type representing the names of the trees for the base rollup.
|
|
62
|
+
*/
|
|
63
|
+
type BaseTreeNames = 'NoteHashTree' | 'ContractTree' | 'NullifierTree' | 'PublicDataTree';
|
|
64
|
+
/**
|
|
65
|
+
* Type representing the names of the trees.
|
|
66
|
+
*/
|
|
67
|
+
export type TreeNames = BaseTreeNames | 'L1ToL2MessageTree' | 'Archive';
|
|
68
|
+
|
|
69
|
+
// Builds the hints for base rollup. Updating the contract, nullifier, and data trees in the process.
|
|
70
|
+
export const buildBaseRollupHints = runInSpan(
|
|
71
|
+
'BlockBuilderHelpers',
|
|
72
|
+
'buildBaseRollupHints',
|
|
73
|
+
async (
|
|
74
|
+
span: Span,
|
|
75
|
+
tx: ProcessedTx,
|
|
76
|
+
globalVariables: GlobalVariables,
|
|
77
|
+
db: MerkleTreeWriteOperations,
|
|
78
|
+
startSpongeBlob: SpongeBlob,
|
|
79
|
+
) => {
|
|
80
|
+
span.setAttribute(Attributes.TX_HASH, tx.hash.toString());
|
|
81
|
+
// Get trees info before any changes hit
|
|
82
|
+
const constants = await getConstantRollupData(globalVariables, db);
|
|
83
|
+
const start = new PartialStateReference(
|
|
84
|
+
await getTreeSnapshot(MerkleTreeId.NOTE_HASH_TREE, db),
|
|
85
|
+
await getTreeSnapshot(MerkleTreeId.NULLIFIER_TREE, db),
|
|
86
|
+
await getTreeSnapshot(MerkleTreeId.PUBLIC_DATA_TREE, db),
|
|
87
|
+
);
|
|
88
|
+
// Get the subtree sibling paths for the circuit
|
|
89
|
+
const noteHashSubtreeSiblingPathArray = await getSubtreeSiblingPath(
|
|
90
|
+
MerkleTreeId.NOTE_HASH_TREE,
|
|
91
|
+
NOTE_HASH_SUBTREE_HEIGHT,
|
|
92
|
+
db,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const noteHashSubtreeSiblingPath = makeTuple(NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH, i =>
|
|
96
|
+
i < noteHashSubtreeSiblingPathArray.length ? noteHashSubtreeSiblingPathArray[i] : Fr.ZERO,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
// Update the note hash trees with the new items being inserted to get the new roots
|
|
100
|
+
// that will be used by the next iteration of the base rollup circuit, skipping the empty ones
|
|
101
|
+
const noteHashes = padArrayEnd(tx.txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX);
|
|
102
|
+
await db.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashes);
|
|
103
|
+
|
|
104
|
+
// Create data hint for reading fee payer initial balance in Fee Juice
|
|
105
|
+
const leafSlot = await computeFeePayerBalanceLeafSlot(tx.data.feePayer);
|
|
106
|
+
const feePayerFeeJuiceBalanceReadHint = await getPublicDataHint(db, leafSlot.toBigInt());
|
|
107
|
+
|
|
108
|
+
// The read witnesses for a given TX should be generated before the writes of the same TX are applied.
|
|
109
|
+
// All reads that refer to writes in the same tx are transient and can be simplified out.
|
|
110
|
+
const txPublicDataUpdateRequestInfo = await processPublicDataUpdateRequests(tx, db);
|
|
111
|
+
|
|
112
|
+
// Update the nullifier tree, capturing the low nullifier info for each individual operation
|
|
113
|
+
const {
|
|
114
|
+
lowLeavesWitnessData: nullifierWitnessLeaves,
|
|
115
|
+
newSubtreeSiblingPath: nullifiersSubtreeSiblingPath,
|
|
116
|
+
sortedNewLeaves: sortednullifiers,
|
|
117
|
+
sortedNewLeavesIndexes,
|
|
118
|
+
} = await db.batchInsert(
|
|
119
|
+
MerkleTreeId.NULLIFIER_TREE,
|
|
120
|
+
padArrayEnd(tx.txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(n => n.toBuffer()),
|
|
121
|
+
NULLIFIER_SUBTREE_HEIGHT,
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
if (nullifierWitnessLeaves === undefined) {
|
|
125
|
+
throw new Error(`Could not craft nullifier batch insertion proofs`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Extract witness objects from returned data
|
|
129
|
+
const nullifierPredecessorMembershipWitnessesWithoutPadding: MembershipWitness<typeof NULLIFIER_TREE_HEIGHT>[] =
|
|
130
|
+
nullifierWitnessLeaves.map(l =>
|
|
131
|
+
MembershipWitness.fromBufferArray(l.index, assertLength(l.siblingPath.toBufferArray(), NULLIFIER_TREE_HEIGHT)),
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const nullifierSubtreeSiblingPathArray = nullifiersSubtreeSiblingPath.toFields();
|
|
135
|
+
|
|
136
|
+
const nullifierSubtreeSiblingPath = makeTuple(NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH, i =>
|
|
137
|
+
i < nullifierSubtreeSiblingPathArray.length ? nullifierSubtreeSiblingPathArray[i] : Fr.ZERO,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
// Append new data to startSpongeBlob
|
|
141
|
+
const inputSpongeBlob = startSpongeBlob.clone();
|
|
142
|
+
await startSpongeBlob.absorb(tx.txEffect.toBlobFields());
|
|
143
|
+
|
|
144
|
+
const contractClassLogsPreimages = makeTuple(
|
|
145
|
+
MAX_CONTRACT_CLASS_LOGS_PER_TX,
|
|
146
|
+
i => tx.txEffect.contractClassLogs[i]?.toUnsiloed() || ContractClassLog.empty(),
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
if (tx.avmProvingRequest) {
|
|
150
|
+
const blockHash = await tx.constants.historicalHeader.hash();
|
|
151
|
+
const archiveRootMembershipWitness = await getMembershipWitnessFor(
|
|
152
|
+
blockHash,
|
|
153
|
+
MerkleTreeId.ARCHIVE,
|
|
154
|
+
ARCHIVE_HEIGHT,
|
|
155
|
+
db,
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
return PublicBaseRollupHints.from({
|
|
159
|
+
startSpongeBlob: inputSpongeBlob,
|
|
160
|
+
archiveRootMembershipWitness,
|
|
161
|
+
contractClassLogsPreimages,
|
|
162
|
+
constants,
|
|
163
|
+
});
|
|
164
|
+
} else {
|
|
165
|
+
if (
|
|
166
|
+
txPublicDataUpdateRequestInfo.lowPublicDataWritesMembershipWitnesses.length > 1 ||
|
|
167
|
+
txPublicDataUpdateRequestInfo.lowPublicDataWritesPreimages.length > 1 ||
|
|
168
|
+
txPublicDataUpdateRequestInfo.publicDataWritesSiblingPaths.length > 1
|
|
169
|
+
) {
|
|
170
|
+
throw new Error(`More than one public data write in a private only tx`);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const feeWriteLowLeafPreimage =
|
|
174
|
+
txPublicDataUpdateRequestInfo.lowPublicDataWritesPreimages[0] || PublicDataTreeLeafPreimage.empty();
|
|
175
|
+
const feeWriteLowLeafMembershipWitness =
|
|
176
|
+
txPublicDataUpdateRequestInfo.lowPublicDataWritesMembershipWitnesses[0] ||
|
|
177
|
+
MembershipWitness.empty<typeof PUBLIC_DATA_TREE_HEIGHT>(PUBLIC_DATA_TREE_HEIGHT);
|
|
178
|
+
const feeWriteSiblingPath =
|
|
179
|
+
txPublicDataUpdateRequestInfo.publicDataWritesSiblingPaths[0] ||
|
|
180
|
+
makeTuple(PUBLIC_DATA_TREE_HEIGHT, () => Fr.ZERO);
|
|
181
|
+
|
|
182
|
+
const stateDiffHints = PrivateBaseStateDiffHints.from({
|
|
183
|
+
nullifierPredecessorPreimages: makeTuple(MAX_NULLIFIERS_PER_TX, i =>
|
|
184
|
+
i < nullifierWitnessLeaves.length
|
|
185
|
+
? (nullifierWitnessLeaves[i].leafPreimage as NullifierLeafPreimage)
|
|
186
|
+
: NullifierLeafPreimage.empty(),
|
|
187
|
+
),
|
|
188
|
+
nullifierPredecessorMembershipWitnesses: makeTuple(MAX_NULLIFIERS_PER_TX, i =>
|
|
189
|
+
i < nullifierPredecessorMembershipWitnessesWithoutPadding.length
|
|
190
|
+
? nullifierPredecessorMembershipWitnessesWithoutPadding[i]
|
|
191
|
+
: makeEmptyMembershipWitness(NULLIFIER_TREE_HEIGHT),
|
|
192
|
+
),
|
|
193
|
+
sortedNullifiers: makeTuple(MAX_NULLIFIERS_PER_TX, i => Fr.fromBuffer(sortednullifiers[i])),
|
|
194
|
+
sortedNullifierIndexes: makeTuple(MAX_NULLIFIERS_PER_TX, i => sortedNewLeavesIndexes[i]),
|
|
195
|
+
noteHashSubtreeSiblingPath,
|
|
196
|
+
nullifierSubtreeSiblingPath,
|
|
197
|
+
feeWriteLowLeafPreimage,
|
|
198
|
+
feeWriteLowLeafMembershipWitness,
|
|
199
|
+
feeWriteSiblingPath,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const blockHash = await tx.constants.historicalHeader.hash();
|
|
203
|
+
const archiveRootMembershipWitness = await getMembershipWitnessFor(
|
|
204
|
+
blockHash,
|
|
205
|
+
MerkleTreeId.ARCHIVE,
|
|
206
|
+
ARCHIVE_HEIGHT,
|
|
207
|
+
db,
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
return PrivateBaseRollupHints.from({
|
|
211
|
+
start,
|
|
212
|
+
startSpongeBlob: inputSpongeBlob,
|
|
213
|
+
stateDiffHints,
|
|
214
|
+
feePayerFeeJuiceBalanceReadHint,
|
|
215
|
+
archiveRootMembershipWitness,
|
|
216
|
+
contractClassLogsPreimages,
|
|
217
|
+
constants,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
export async function getPublicDataHint(db: MerkleTreeWriteOperations, leafSlot: bigint) {
|
|
224
|
+
const { index } = (await db.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot)) ?? {};
|
|
225
|
+
if (index === undefined) {
|
|
226
|
+
throw new Error(`Cannot find the previous value index for public data ${leafSlot}.`);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const siblingPath = await db.getSiblingPath<typeof PUBLIC_DATA_TREE_HEIGHT>(MerkleTreeId.PUBLIC_DATA_TREE, index);
|
|
230
|
+
const membershipWitness = new MembershipWitness(PUBLIC_DATA_TREE_HEIGHT, index, siblingPath.toTuple());
|
|
231
|
+
|
|
232
|
+
const leafPreimage = (await db.getLeafPreimage(MerkleTreeId.PUBLIC_DATA_TREE, index)) as PublicDataTreeLeafPreimage;
|
|
233
|
+
if (!leafPreimage) {
|
|
234
|
+
throw new Error(`Cannot find the leaf preimage for public data tree at index ${index}.`);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const exists = leafPreimage.slot.toBigInt() === leafSlot;
|
|
238
|
+
const value = exists ? leafPreimage.value : Fr.ZERO;
|
|
239
|
+
|
|
240
|
+
return new PublicDataHint(new Fr(leafSlot), value, membershipWitness, leafPreimage);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export const buildBlobHints = runInSpan(
|
|
244
|
+
'BlockBuilderHelpers',
|
|
245
|
+
'buildBlobHints',
|
|
246
|
+
async (_span: Span, txEffects: TxEffect[]) => {
|
|
247
|
+
const blobFields = txEffects.flatMap(tx => tx.toBlobFields());
|
|
248
|
+
const blobs = await Blob.getBlobs(blobFields);
|
|
249
|
+
const blobCommitments = blobs.map(b => b.commitmentToFields());
|
|
250
|
+
const blobsHash = new Fr(getBlobsHashFromBlobs(blobs));
|
|
251
|
+
return { blobFields, blobCommitments, blobs, blobsHash };
|
|
252
|
+
},
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
export const buildHeaderFromCircuitOutputs = runInSpan(
|
|
256
|
+
'BlockBuilderHelpers',
|
|
257
|
+
'buildHeaderFromCircuitOutputs',
|
|
258
|
+
async (
|
|
259
|
+
_span,
|
|
260
|
+
previousRollupData: BaseOrMergeRollupPublicInputs[],
|
|
261
|
+
parityPublicInputs: ParityPublicInputs,
|
|
262
|
+
rootRollupOutputs: BlockRootOrBlockMergePublicInputs,
|
|
263
|
+
endState: StateReference,
|
|
264
|
+
logger?: Logger,
|
|
265
|
+
) => {
|
|
266
|
+
if (previousRollupData.length > 2) {
|
|
267
|
+
throw new Error(`There can't be more than 2 previous rollups. Received ${previousRollupData.length}.`);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const blobsHash = rootRollupOutputs.blobPublicInputs[0].getBlobsHash();
|
|
271
|
+
const numTxs = previousRollupData.reduce((sum, d) => sum + d.numTxs, 0);
|
|
272
|
+
const outHash =
|
|
273
|
+
previousRollupData.length === 0
|
|
274
|
+
? Fr.ZERO.toBuffer()
|
|
275
|
+
: previousRollupData.length === 1
|
|
276
|
+
? previousRollupData[0].outHash.toBuffer()
|
|
277
|
+
: sha256Trunc(
|
|
278
|
+
Buffer.concat([previousRollupData[0].outHash.toBuffer(), previousRollupData[1].outHash.toBuffer()]),
|
|
279
|
+
);
|
|
280
|
+
const contentCommitment = new ContentCommitment(
|
|
281
|
+
new Fr(numTxs),
|
|
282
|
+
blobsHash,
|
|
283
|
+
parityPublicInputs.shaRoot.toBuffer(),
|
|
284
|
+
outHash,
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
const accumulatedFees = previousRollupData.reduce((sum, d) => sum.add(d.accumulatedFees), Fr.ZERO);
|
|
288
|
+
const accumulatedManaUsed = previousRollupData.reduce((sum, d) => sum.add(d.accumulatedManaUsed), Fr.ZERO);
|
|
289
|
+
const header = new BlockHeader(
|
|
290
|
+
rootRollupOutputs.previousArchive,
|
|
291
|
+
contentCommitment,
|
|
292
|
+
endState,
|
|
293
|
+
rootRollupOutputs.endGlobalVariables,
|
|
294
|
+
accumulatedFees,
|
|
295
|
+
accumulatedManaUsed,
|
|
296
|
+
);
|
|
297
|
+
if (!(await header.hash()).equals(rootRollupOutputs.endBlockHash)) {
|
|
298
|
+
logger?.error(
|
|
299
|
+
`Block header mismatch when building header from circuit outputs.` +
|
|
300
|
+
`\n\nHeader: ${inspect(header)}` +
|
|
301
|
+
`\n\nCircuit: ${toFriendlyJSON(rootRollupOutputs)}`,
|
|
302
|
+
);
|
|
303
|
+
throw new Error(`Block header mismatch when building from circuit outputs`);
|
|
304
|
+
}
|
|
305
|
+
return header;
|
|
306
|
+
},
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
export const buildHeaderAndBodyFromTxs = runInSpan(
|
|
310
|
+
'BlockBuilderHelpers',
|
|
311
|
+
'buildHeaderAndBodyFromTxs',
|
|
312
|
+
async (
|
|
313
|
+
span,
|
|
314
|
+
txs: ProcessedTx[],
|
|
315
|
+
globalVariables: GlobalVariables,
|
|
316
|
+
l1ToL2Messages: Fr[],
|
|
317
|
+
db: MerkleTreeReadOperations,
|
|
318
|
+
) => {
|
|
319
|
+
span.setAttribute(Attributes.BLOCK_NUMBER, globalVariables.blockNumber.toNumber());
|
|
320
|
+
const stateReference = new StateReference(
|
|
321
|
+
await getTreeSnapshot(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, db),
|
|
322
|
+
new PartialStateReference(
|
|
323
|
+
await getTreeSnapshot(MerkleTreeId.NOTE_HASH_TREE, db),
|
|
324
|
+
await getTreeSnapshot(MerkleTreeId.NULLIFIER_TREE, db),
|
|
325
|
+
await getTreeSnapshot(MerkleTreeId.PUBLIC_DATA_TREE, db),
|
|
326
|
+
),
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
const previousArchive = await getTreeSnapshot(MerkleTreeId.ARCHIVE, db);
|
|
330
|
+
|
|
331
|
+
const txEffects = txs.map(tx => tx.txEffect);
|
|
332
|
+
const body = new Body(txEffects);
|
|
333
|
+
|
|
334
|
+
const numTxs = body.txEffects.length;
|
|
335
|
+
const outHash =
|
|
336
|
+
numTxs === 0
|
|
337
|
+
? Fr.ZERO.toBuffer()
|
|
338
|
+
: numTxs === 1
|
|
339
|
+
? body.txEffects[0].txOutHash()
|
|
340
|
+
: computeUnbalancedMerkleRoot(
|
|
341
|
+
body.txEffects.map(tx => tx.txOutHash()),
|
|
342
|
+
TxEffect.empty().txOutHash(),
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
l1ToL2Messages = padArrayEnd(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
|
|
346
|
+
const hasher = (left: Buffer, right: Buffer) => Promise.resolve(sha256Trunc(Buffer.concat([left, right])));
|
|
347
|
+
const parityHeight = Math.ceil(Math.log2(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP));
|
|
348
|
+
const parityCalculator = await MerkleTreeCalculator.create(parityHeight, Fr.ZERO.toBuffer(), hasher);
|
|
349
|
+
const parityShaRoot = await parityCalculator.computeTreeRoot(l1ToL2Messages.map(msg => msg.toBuffer()));
|
|
350
|
+
const blobsHash = getBlobsHashFromBlobs(await Blob.getBlobs(body.toBlobFields()));
|
|
351
|
+
|
|
352
|
+
const contentCommitment = new ContentCommitment(new Fr(numTxs), blobsHash, parityShaRoot, outHash);
|
|
353
|
+
|
|
354
|
+
const fees = body.txEffects.reduce((acc, tx) => acc.add(tx.transactionFee), Fr.ZERO);
|
|
355
|
+
const manaUsed = txs.reduce((acc, tx) => acc.add(new Fr(tx.gasUsed.billedGas.l2Gas)), Fr.ZERO);
|
|
356
|
+
|
|
357
|
+
const header = new BlockHeader(previousArchive, contentCommitment, stateReference, globalVariables, fees, manaUsed);
|
|
358
|
+
|
|
359
|
+
return { header, body };
|
|
360
|
+
},
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
export function getBlobsHashFromBlobs(inputs: Blob[]): Buffer {
|
|
364
|
+
const blobHashes = serializeToBuffer(inputs.map(b => b.getEthVersionedBlobHash()));
|
|
365
|
+
return sha256Trunc(serializeToBuffer(blobHashes));
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Validate that the roots of all local trees match the output of the root circuit simulation
|
|
369
|
+
export async function validateBlockRootOutput(
|
|
370
|
+
blockRootOutput: BlockRootOrBlockMergePublicInputs,
|
|
371
|
+
blockHeader: BlockHeader,
|
|
372
|
+
db: MerkleTreeReadOperations,
|
|
373
|
+
) {
|
|
374
|
+
await Promise.all([
|
|
375
|
+
validateState(blockHeader.state, db),
|
|
376
|
+
validateSimulatedTree(await getTreeSnapshot(MerkleTreeId.ARCHIVE, db), blockRootOutput.newArchive, 'Archive'),
|
|
377
|
+
]);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export const validateState = runInSpan(
|
|
381
|
+
'BlockBuilderHelpers',
|
|
382
|
+
'validateState',
|
|
383
|
+
async (_span, state: StateReference, db: MerkleTreeReadOperations) => {
|
|
384
|
+
const promises = [MerkleTreeId.NOTE_HASH_TREE, MerkleTreeId.NULLIFIER_TREE, MerkleTreeId.PUBLIC_DATA_TREE].map(
|
|
385
|
+
async (id: MerkleTreeId) => {
|
|
386
|
+
return { key: id, value: await getTreeSnapshot(id, db) };
|
|
387
|
+
},
|
|
388
|
+
);
|
|
389
|
+
const snapshots: Map<MerkleTreeId, AppendOnlyTreeSnapshot> = new Map(
|
|
390
|
+
(await Promise.all(promises)).map(obj => [obj.key, obj.value]),
|
|
391
|
+
);
|
|
392
|
+
validatePartialState(state.partial, snapshots);
|
|
393
|
+
validateSimulatedTree(
|
|
394
|
+
await getTreeSnapshot(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, db),
|
|
395
|
+
state.l1ToL2MessageTree,
|
|
396
|
+
'L1ToL2MessageTree',
|
|
397
|
+
);
|
|
398
|
+
},
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
export async function getRootTreeSiblingPath<TID extends MerkleTreeId>(treeId: TID, db: MerkleTreeReadOperations) {
|
|
402
|
+
const { size } = await db.getTreeInfo(treeId);
|
|
403
|
+
const path = await db.getSiblingPath(treeId, size);
|
|
404
|
+
return padArrayEnd(path.toFields(), Fr.ZERO, getTreeHeight(treeId));
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export const getConstantRollupData = runInSpan(
|
|
408
|
+
'BlockBuilderHelpers',
|
|
409
|
+
'getConstantRollupData',
|
|
410
|
+
async (_span, globalVariables: GlobalVariables, db: MerkleTreeReadOperations): Promise<ConstantRollupData> => {
|
|
411
|
+
return ConstantRollupData.from({
|
|
412
|
+
vkTreeRoot: getVKTreeRoot(),
|
|
413
|
+
protocolContractTreeRoot,
|
|
414
|
+
lastArchive: await getTreeSnapshot(MerkleTreeId.ARCHIVE, db),
|
|
415
|
+
globalVariables,
|
|
416
|
+
});
|
|
417
|
+
},
|
|
418
|
+
);
|
|
419
|
+
|
|
420
|
+
export async function getTreeSnapshot(id: MerkleTreeId, db: MerkleTreeReadOperations): Promise<AppendOnlyTreeSnapshot> {
|
|
421
|
+
const treeInfo = await db.getTreeInfo(id);
|
|
422
|
+
return new AppendOnlyTreeSnapshot(Fr.fromBuffer(treeInfo.root), Number(treeInfo.size));
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export function makeEmptyMembershipWitness<N extends number>(height: N) {
|
|
426
|
+
return new MembershipWitness(
|
|
427
|
+
height,
|
|
428
|
+
0n,
|
|
429
|
+
makeTuple(height, () => Fr.ZERO),
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const processPublicDataUpdateRequests = runInSpan(
|
|
434
|
+
'BlockBuilderHelpers',
|
|
435
|
+
'processPublicDataUpdateRequests',
|
|
436
|
+
async (span, tx: ProcessedTx, db: MerkleTreeWriteOperations) => {
|
|
437
|
+
span.setAttribute(Attributes.TX_HASH, tx.hash.toString());
|
|
438
|
+
const allPublicDataWrites = tx.txEffect.publicDataWrites.map(
|
|
439
|
+
({ leafSlot, value }) => new PublicDataTreeLeaf(leafSlot, value),
|
|
440
|
+
);
|
|
441
|
+
|
|
442
|
+
const { lowLeavesWitnessData, insertionWitnessData } = await db.sequentialInsert(
|
|
443
|
+
MerkleTreeId.PUBLIC_DATA_TREE,
|
|
444
|
+
allPublicDataWrites.map(write => {
|
|
445
|
+
if (write.isEmpty()) {
|
|
446
|
+
throw new Error(`Empty public data write in tx: ${toFriendlyJSON(tx)}`);
|
|
447
|
+
}
|
|
448
|
+
return write.toBuffer();
|
|
449
|
+
}),
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
const lowPublicDataWritesPreimages = lowLeavesWitnessData.map(
|
|
453
|
+
lowLeafWitness => lowLeafWitness.leafPreimage as PublicDataTreeLeafPreimage,
|
|
454
|
+
);
|
|
455
|
+
const lowPublicDataWritesMembershipWitnesses = lowLeavesWitnessData.map(lowLeafWitness =>
|
|
456
|
+
MembershipWitness.fromBufferArray<typeof PUBLIC_DATA_TREE_HEIGHT>(
|
|
457
|
+
lowLeafWitness.index,
|
|
458
|
+
assertLength(lowLeafWitness.siblingPath.toBufferArray(), PUBLIC_DATA_TREE_HEIGHT),
|
|
459
|
+
),
|
|
460
|
+
);
|
|
461
|
+
const publicDataWritesSiblingPaths = insertionWitnessData.map(w => {
|
|
462
|
+
const insertionSiblingPath = w.siblingPath.toFields();
|
|
463
|
+
assertLength(insertionSiblingPath, PUBLIC_DATA_TREE_HEIGHT);
|
|
464
|
+
return insertionSiblingPath as Tuple<Fr, typeof PUBLIC_DATA_TREE_HEIGHT>;
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
return {
|
|
468
|
+
lowPublicDataWritesPreimages,
|
|
469
|
+
lowPublicDataWritesMembershipWitnesses,
|
|
470
|
+
publicDataWritesSiblingPaths,
|
|
471
|
+
};
|
|
472
|
+
},
|
|
473
|
+
);
|
|
474
|
+
|
|
475
|
+
export async function getSubtreeSiblingPath(
|
|
476
|
+
treeId: MerkleTreeId,
|
|
477
|
+
subtreeHeight: number,
|
|
478
|
+
db: MerkleTreeReadOperations,
|
|
479
|
+
): Promise<Fr[]> {
|
|
480
|
+
const nextAvailableLeafIndex = await db.getTreeInfo(treeId).then(t => t.size);
|
|
481
|
+
const fullSiblingPath = await db.getSiblingPath(treeId, nextAvailableLeafIndex);
|
|
482
|
+
|
|
483
|
+
// Drop the first subtreeHeight items since we only care about the path to the subtree root
|
|
484
|
+
return fullSiblingPath.getSubtreeSiblingPath(subtreeHeight).toFields();
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Scan a tree searching for a specific value and return a membership witness proof for it
|
|
488
|
+
export async function getMembershipWitnessFor<N extends number>(
|
|
489
|
+
value: Fr,
|
|
490
|
+
treeId: MerkleTreeId,
|
|
491
|
+
height: N,
|
|
492
|
+
db: MerkleTreeReadOperations,
|
|
493
|
+
): Promise<MembershipWitness<N>> {
|
|
494
|
+
// If this is an empty tx, then just return zeroes
|
|
495
|
+
if (value.isZero()) {
|
|
496
|
+
return makeEmptyMembershipWitness(height);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const index = (await db.findLeafIndices(treeId, [value.toBuffer()]))[0];
|
|
500
|
+
if (index === undefined) {
|
|
501
|
+
throw new Error(`Leaf with value ${value} not found in tree ${MerkleTreeId[treeId]}`);
|
|
502
|
+
}
|
|
503
|
+
const path = await db.getSiblingPath(treeId, index);
|
|
504
|
+
return new MembershipWitness(height, index, assertLength(path.toFields(), height));
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export function validatePartialState(
|
|
508
|
+
partialState: PartialStateReference,
|
|
509
|
+
treeSnapshots: Map<MerkleTreeId, AppendOnlyTreeSnapshot>,
|
|
510
|
+
) {
|
|
511
|
+
validateSimulatedTree(treeSnapshots.get(MerkleTreeId.NOTE_HASH_TREE)!, partialState.noteHashTree, 'NoteHashTree');
|
|
512
|
+
validateSimulatedTree(treeSnapshots.get(MerkleTreeId.NULLIFIER_TREE)!, partialState.nullifierTree, 'NullifierTree');
|
|
513
|
+
validateSimulatedTree(
|
|
514
|
+
treeSnapshots.get(MerkleTreeId.PUBLIC_DATA_TREE)!,
|
|
515
|
+
partialState.publicDataTree,
|
|
516
|
+
'PublicDataTree',
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// Helper for comparing two trees snapshots
|
|
521
|
+
function validateSimulatedTree(
|
|
522
|
+
localTree: AppendOnlyTreeSnapshot,
|
|
523
|
+
simulatedTree: AppendOnlyTreeSnapshot,
|
|
524
|
+
name: TreeNames,
|
|
525
|
+
label?: string,
|
|
526
|
+
) {
|
|
527
|
+
if (!simulatedTree.root.toBuffer().equals(localTree.root.toBuffer())) {
|
|
528
|
+
throw new Error(`${label ?? name} tree root mismatch (local ${localTree.root}, simulated ${simulatedTree.root})`);
|
|
529
|
+
}
|
|
530
|
+
if (simulatedTree.nextAvailableLeafIndex !== localTree.nextAvailableLeafIndex) {
|
|
531
|
+
throw new Error(
|
|
532
|
+
`${label ?? name} tree next available leaf index mismatch (local ${localTree.nextAvailableLeafIndex}, simulated ${
|
|
533
|
+
simulatedTree.nextAvailableLeafIndex
|
|
534
|
+
})`,
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
export function validateTx(tx: ProcessedTx) {
|
|
540
|
+
const txHeader = tx.constants.historicalHeader;
|
|
541
|
+
if (txHeader.state.l1ToL2MessageTree.isZero()) {
|
|
542
|
+
throw new Error(`Empty L1 to L2 messages tree in tx: ${toFriendlyJSON(tx)}`);
|
|
543
|
+
}
|
|
544
|
+
if (txHeader.state.partial.noteHashTree.isZero()) {
|
|
545
|
+
throw new Error(`Empty note hash tree in tx: ${toFriendlyJSON(tx)}`);
|
|
546
|
+
}
|
|
547
|
+
if (txHeader.state.partial.nullifierTree.isZero()) {
|
|
548
|
+
throw new Error(`Empty nullifier tree in tx: ${toFriendlyJSON(tx)}`);
|
|
549
|
+
}
|
|
550
|
+
if (txHeader.state.partial.publicDataTree.isZero()) {
|
|
551
|
+
throw new Error(`Empty public data tree in tx: ${toFriendlyJSON(tx)}`);
|
|
552
|
+
}
|
|
553
|
+
}
|