Skip to content

Commit b52ebea

Browse files
committed
Merged new ST Prover with atomic transactions arch
1 parent 1a421e7 commit b52ebea

File tree

11 files changed

+107
-118
lines changed

11 files changed

+107
-118
lines changed

packages/common/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,3 @@ export * from "./compiling/AtomicCompileHelper";
2323
export * from "./compiling/CompileRegistry";
2424
export * from "./compiling/CompilableModule";
2525
export * from "./compiling/services/ChildVerificationKeyService";
26-
export * from "./config/injectAlias";

packages/persistance/src/services/prisma/PrismaBlockStorage.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ export class PrismaBlockStorage
121121
statusMessage: tx.statusMessage,
122122
txHash: tx.txHash,
123123

124-
stateTransitions:
125-
tx.stateTransitions as Prisma.InputJsonArray,
124+
stateTransitions: tx.stateTransitions as Prisma.InputJsonArray,
126125
events: tx.events as Prisma.InputJsonArray,
127126
};
128127
}),

packages/sequencer/src/protocol/production/BatchProducerModule.ts

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ export type StateRecord = Record<string, Field[] | undefined>;
2626

2727
interface BatchMetadata {
2828
batch: SettleableBatch;
29-
changes: {
30-
commit: () => Promise<void>;
31-
};
29+
changes: CachedMerkleTreeStore;
3230
}
3331

3432
const errors = {
@@ -61,60 +59,19 @@ export class BatchProducerModule extends SequencerModule {
6159
super();
6260
}
6361

64-
// TODO
65-
private async applyStateChanges(batch: BatchMetadata) {
66-
// TODO Introduce Proven and Unproven BlockHashTree stores - for rollbacks
67-
await this.database.executeInTransaction(async () => {
68-
await batch.stateService.mergeIntoParent();
69-
await batch.merkleStore.mergeIntoParent();
70-
});
71-
}
72-
7362
/**
7463
* Main function to call when wanting to create a new block based on the
7564
* transactions that are present in the mempool. This function should also
76-
* be the one called by BlockTriggers
65+
* be the one called by BlockTriggerss
7766
*/
7867
public async createBatch(
7968
blocks: BlockWithResult[]
8069
): Promise<SettleableBatch | undefined> {
81-
log.info("Producing batch...");
82-
83-
const height = await this.batchStorage.getCurrentBatchHeight();
84-
85-
const batchWithStateDiff = await this.tryProduceBatch(blocks, height);
86-
87-
if (batchWithStateDiff !== undefined) {
88-
const numTxs = blocks.reduce(
89-
(sum, block) => sum + block.block.transactions.length,
90-
0
91-
);
92-
log.info(
93-
`Batch produced (${batchWithStateDiff.batch.blockHashes.length} blocks, ${numTxs} txs)`
94-
);
95-
96-
// Apply state changes to current MerkleTreeStore
97-
await batchWithStateDiff.changes.commit();
98-
99-
// TODO Add transition from unproven to proven state for stateservice
100-
// This needs proper DB-level masking
101-
}
102-
return batchWithStateDiff?.batch;
103-
}
104-
105-
public async start(): Promise<void> {
106-
noop();
107-
}
108-
109-
private async tryProduceBatch(
110-
blocks: BlockWithResult[],
111-
height: number
112-
): Promise<BatchMetadata | undefined> {
11370
if (!this.productionInProgress) {
11471
try {
11572
this.productionInProgress = true;
11673

117-
const batch = await this.produceBatch(blocks, height);
74+
const batch = await this.tryProduceBatch(blocks);
11875

11976
this.productionInProgress = false;
12077

@@ -144,6 +101,40 @@ export class BatchProducerModule extends SequencerModule {
144101
return undefined;
145102
}
146103

104+
private async tryProduceBatch(
105+
blocks: BlockWithResult[]
106+
): Promise<SettleableBatch | undefined> {
107+
log.info("Producing batch...");
108+
109+
const height = await this.batchStorage.getCurrentBatchHeight();
110+
111+
const batchWithStateDiff = await this.produceBatch(blocks, height);
112+
113+
if (batchWithStateDiff !== undefined) {
114+
const numTxs = blocks.reduce(
115+
(sum, block) => sum + block.block.transactions.length,
116+
0
117+
);
118+
log.info(
119+
`Batch produced (${batchWithStateDiff.batch.blockHashes.length} blocks, ${numTxs} txs)`
120+
);
121+
122+
// Apply state changes to current MerkleTreeStore
123+
await this.database.executeInTransaction(async () => {
124+
await this.batchStorage.pushBatch(batchWithStateDiff.batch);
125+
await batchWithStateDiff.changes.mergeIntoParent();
126+
});
127+
128+
// TODO Add transition from unproven to proven state for stateservice
129+
// This needs proper DB-level masking
130+
}
131+
return batchWithStateDiff?.batch;
132+
}
133+
134+
public async start(): Promise<void> {
135+
noop();
136+
}
137+
147138
private async produceBatch(
148139
blocks: BlockWithResult[],
149140
height: number
@@ -187,9 +178,7 @@ export class BatchProducerModule extends SequencerModule {
187178
blockId: number
188179
): Promise<{
189180
proof: Proof<BlockProverPublicInput, BlockProverPublicOutput>;
190-
changes: {
191-
commit: () => Promise<void>;
192-
};
181+
changes: CachedMerkleTreeStore;
193182
fromNetworkState: NetworkState;
194183
toNetworkState: NetworkState;
195184
}> {
@@ -209,13 +198,9 @@ export class BatchProducerModule extends SequencerModule {
209198
const fromNetworkState = blocks[0].block.networkState.before;
210199
const toNetworkState = blocks.at(-1)!.result.afterNetworkState;
211200

212-
const changes = {
213-
commit: async () => await merkleTreeStore.mergeIntoParent(),
214-
};
215-
216201
return {
217202
proof,
218-
changes,
203+
changes: merkleTreeStore,
219204
fromNetworkState,
220205
toNetworkState,
221206
};

packages/sequencer/src/protocol/production/sequencing/BlockProducerModule.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
BlockResult,
2424
BlockWithResult,
2525
} from "../../../storage/model/Block";
26-
import { CachedStateService } from "../../../state/state/CachedStateService";
2726
import { MessageStorage } from "../../../storage/repositories/MessageStorage";
2827
import { Database } from "../../../storage/Database";
2928

@@ -108,16 +107,18 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
108107
}
109108

110109
public async generateMetadata(block: Block): Promise<BlockResult> {
111-
const { result, blockHashTreeStore, treeStore } =
110+
const { result, blockHashTreeStore, treeStore, stateService } =
112111
await this.resultService.generateMetadataForNextBlock(
113112
block,
114113
this.unprovenMerkleStore,
115-
this.blockTreeStore
114+
this.blockTreeStore,
115+
this.unprovenStateService
116116
);
117117

118118
await this.database.executeInTransaction(async () => {
119119
await blockHashTreeStore.mergeIntoParent();
120120
await treeStore.mergeIntoParent();
121+
await stateService.mergeIntoParent();
121122

122123
await this.blockQueue.pushResult(result);
123124
});
@@ -210,27 +211,25 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
210211
return undefined;
211212
}
212213

213-
const cachedStateService = new CachedStateService(
214-
this.unprovenStateService
215-
);
216-
217-
const block = await this.productionService.createBlock(
218-
cachedStateService,
214+
const blockResult = await this.productionService.createBlock(
215+
this.unprovenStateService,
219216
txs,
220217
metadata,
221218
this.allowEmptyBlock()
222219
);
223220

224-
if (block !== undefined) {
221+
if (blockResult !== undefined) {
222+
const { block, stateChanges } = blockResult;
223+
225224
await this.database.executeInTransaction(async () => {
226-
await cachedStateService.mergeIntoParent();
225+
await stateChanges.mergeIntoParent();
227226
await this.blockQueue.pushBlock(block);
228227
});
229228
}
230229

231230
this.productionInProgress = false;
232231

233-
return block;
232+
return blockResult?.block;
234233
}
235234

236235
public async blockResultCompleteCheck() {

packages/sequencer/src/protocol/production/sequencing/BlockProductionService.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ export class BlockProductionService {
5151
public async executeBeforeBlockHook(
5252
args: BeforeBlockHookArguments,
5353
inputNetworkState: NetworkState,
54-
asyncStateService: AsyncStateService
54+
cachedStateService: CachedStateService
5555
) {
56-
const cachedStateService = new CachedStateService(asyncStateService);
5756
this.stateServiceProvider.setCurrentStateService(cachedStateService);
5857

5958
// Execute afterBlock hooks
@@ -73,7 +72,9 @@ export class BlockProductionService {
7372
);
7473

7574
this.stateServiceProvider.popCurrentStateService();
76-
await cachedStateService.mergeIntoParent();
75+
await cachedStateService.applyStateTransitions(
76+
executionResult.stateTransitions
77+
);
7778

7879
return executionResult;
7980
}
@@ -83,11 +84,19 @@ export class BlockProductionService {
8384
* attached that is needed for tracing
8485
*/
8586
public async createBlock(
86-
stateService: CachedStateService,
87+
asyncStateService: AsyncStateService,
8788
transactions: PendingTransaction[],
8889
lastBlockWithResult: BlockWithResult,
8990
allowEmptyBlocks: boolean
90-
): Promise<Block | undefined> {
91+
): Promise<
92+
| {
93+
block: Block;
94+
stateChanges: CachedStateService;
95+
}
96+
| undefined
97+
> {
98+
const stateService = new CachedStateService(asyncStateService);
99+
91100
const lastResult = lastBlockWithResult.result;
92101
const lastBlock = lastBlockWithResult.block;
93102
const executionResults: TransactionExecutionResult[] = [];
@@ -147,7 +156,7 @@ export class BlockProductionService {
147156

148157
if (executionResults.length === 0 && !allowEmptyBlocks) {
149158
log.info(
150-
"After sequencing, block has no sequencable transactions left, skipping block"
159+
"After sequencing, block has no sequenceable transactions left, skipping block"
151160
);
152161
return undefined;
153162
}
@@ -175,8 +184,11 @@ export class BlockProductionService {
175184
const hash = Block.hash(block);
176185

177186
return {
178-
...block,
179-
hash,
187+
block: {
188+
...block,
189+
hash,
190+
},
191+
stateChanges: stateService,
180192
};
181193
}
182194
}

packages/sequencer/src/protocol/production/sequencing/BlockResultService.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,14 @@ export class BlockResultService {
103103
);
104104

105105
this.stateServiceProvider.popCurrentStateService();
106-
await cachedStateService.mergeIntoParent();
106+
await cachedStateService.applyStateTransitions(
107+
executionResult.stateTransitions
108+
);
107109

108-
return executionResult;
110+
return {
111+
executionResult,
112+
cachedStateService,
113+
};
109114
}
110115

111116
/** Update the block hash tree with this block */
@@ -134,11 +139,11 @@ export class BlockResultService {
134139
);
135140
const blockHashWitness = blockHashTree.getWitness(block.height.toBigInt());
136141
const newBlockHashRoot = blockHashTree.getRoot();
137-
await blockHashInMemoryStore.mergeIntoParent();
138142

139143
return {
140144
blockHashWitness,
141145
blockHashRoot: newBlockHashRoot,
146+
cachedBlockHashTreeStore: blockHashInMemoryStore,
142147
};
143148
}
144149

@@ -168,9 +173,13 @@ export class BlockResultService {
168173
block: Block,
169174
merkleTreeStore: AsyncMerkleTreeStore,
170175
blockHashTreeStore: AsyncMerkleTreeStore,
171-
stateService: AsyncStateService,
172-
modifyTreeStore = true
173-
): Promise<BlockResult> {
176+
stateService: AsyncStateService
177+
): Promise<{
178+
result: BlockResult;
179+
treeStore: CachedMerkleTreeStore;
180+
blockHashTreeStore: CachedMerkleTreeStore;
181+
stateService: CachedStateService;
182+
}> {
174183
const combinedDiff = createCombinedStateDiff(
175184
block.transactions,
176185
block.beforeBlockStateTransitions
@@ -182,10 +191,13 @@ export class BlockResultService {
182191

183192
const witnessedStateRoot = tree.getRoot();
184193

185-
const { blockHashWitness, blockHashRoot } =
194+
const { blockHashWitness, blockHashRoot, cachedBlockHashTreeStore } =
186195
await this.insertIntoBlockHashTree(block, blockHashTreeStore);
187196

188-
const { stateTransitions, methodResult } = await this.executeAfterBlockHook(
197+
const {
198+
executionResult: { stateTransitions, methodResult },
199+
cachedStateService,
200+
} = await this.executeAfterBlockHook(
189201
{
190202
blockHashRoot,
191203
stateRoot: witnessedStateRoot,
@@ -208,22 +220,24 @@ export class BlockResultService {
208220
);
209221

210222
const stateRoot = tree2.getRoot();
211-
if (modifyTreeStore) {
212-
await inMemoryStore.mergeIntoParent();
213-
}
214223

215224
return {
216-
afterNetworkState: methodResult,
217-
// This is the state root after the last tx and before the afterBlock hook
218-
stateRoot: stateRoot.toBigInt(),
219-
witnessedRoots: [witnessedStateRoot.toBigInt()],
220-
blockHashRoot: blockHashRoot.toBigInt(),
221-
blockHashWitness,
222-
223-
afterBlockStateTransitions: stateTransitions.map((st) =>
224-
UntypedStateTransition.fromStateTransition(st)
225-
),
226-
blockHash: block.hash.toBigInt(),
225+
result: {
226+
afterNetworkState: methodResult,
227+
// This is the state root after the last tx and the afterBlock hook
228+
stateRoot: stateRoot.toBigInt(),
229+
witnessedRoots: [witnessedStateRoot.toBigInt()],
230+
blockHashRoot: blockHashRoot.toBigInt(),
231+
blockHashWitness,
232+
233+
afterBlockStateTransitions: stateTransitions.map((st) =>
234+
UntypedStateTransition.fromStateTransition(st)
235+
),
236+
blockHash: block.hash.toBigInt(),
237+
},
238+
treeStore: inMemoryStore,
239+
blockHashTreeStore: cachedBlockHashTreeStore,
240+
stateService: cachedStateService,
227241
};
228242
}
229243
}

0 commit comments

Comments
 (0)