Skip to content

Commit 46c50de

Browse files
committed
Removed previous results from new blocks query
1 parent 7d6145b commit 46c50de

File tree

5 files changed

+27
-64
lines changed

5 files changed

+27
-64
lines changed

packages/library/src/hooks/TransactionFeeHook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class TransactionFeeHook extends ProvableTransactionHook<TransactionFeeHo
126126
*
127127
* @param executionData
128128
*/
129-
public async onTransaction(
129+
public async beforeTransaction(
130130
executionData: BeforeTransactionHookArguments
131131
): Promise<void> {
132132
const feeConfig = Provable.witness(MethodFeeConfigData, () =>
@@ -159,7 +159,7 @@ export class TransactionFeeHook extends ProvableTransactionHook<TransactionFeeHo
159159
);
160160
}
161161

162-
public async onAfterTransaction(): Promise<void> {
162+
public async afterTransaction(): Promise<void> {
163163
noop();
164164
}
165165
}

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

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import {
2-
distinctByString,
32
HistoricalBlockStorage,
43
TransactionExecutionResult,
54
Block,
65
BlockResult,
76
BlockQueue,
87
BlockStorage,
98
BlockWithResult,
10-
BlockWithPreviousResult,
119
} from "@proto-kit/sequencer";
12-
import { filterNonNull, log } from "@proto-kit/common";
10+
import { log } from "@proto-kit/common";
1311
import {
1412
Prisma,
1513
TransactionExecutionResult as DBTransactionExecutionResult,
@@ -111,6 +109,8 @@ export class PrismaBlockStorage
111109
prismaClient.block.create({
112110
data: {
113111
...encodedBlock,
112+
beforeBlockStateTransitions:
113+
encodedBlock.beforeBlockStateTransitions as Prisma.InputJsonArray,
114114
beforeNetworkState:
115115
encodedBlock.beforeNetworkState as Prisma.InputJsonObject,
116116
duringNetworkState:
@@ -126,8 +126,6 @@ export class PrismaBlockStorage
126126

127127
stateTransitions:
128128
tx.stateTransitions as Prisma.InputJsonArray,
129-
protocolTransitions:
130-
tx.protocolTransitions as Prisma.InputJsonArray,
131129
events: tx.events as Prisma.InputJsonArray,
132130
};
133131
}),
@@ -148,8 +146,8 @@ export class PrismaBlockStorage
148146
data: {
149147
afterNetworkState: encoded.afterNetworkState as Prisma.InputJsonValue,
150148
blockHashWitness: encoded.blockHashWitness as Prisma.InputJsonValue,
151-
blockStateTransitions:
152-
encoded.blockStateTransitions as Prisma.InputJsonValue,
149+
afterBlockStateTransitions:
150+
encoded.afterBlockStateTransitions as Prisma.InputJsonValue,
153151

154152
stateRoot: encoded.stateRoot,
155153
blockHash: encoded.blockHash,
@@ -185,7 +183,7 @@ export class PrismaBlockStorage
185183
});
186184
}
187185

188-
public async getNewBlocks(): Promise<BlockWithPreviousResult[]> {
186+
public async getNewBlocks(): Promise<BlockWithResult[]> {
189187
const blocks = await this.connection.prismaClient.block.findMany({
190188
where: {
191189
batch: null,
@@ -196,24 +194,13 @@ export class PrismaBlockStorage
196194
tx: true,
197195
},
198196
},
197+
result: true,
199198
},
200199
orderBy: {
201200
height: Prisma.SortOrder.asc,
202201
},
203202
});
204203

205-
const blockHashes = blocks
206-
.flatMap((block) => [block.parentHash, block.hash])
207-
.filter(filterNonNull)
208-
.filter(distinctByString);
209-
const result = await this.connection.prismaClient.blockResult.findMany({
210-
where: {
211-
blockHash: {
212-
in: blockHashes,
213-
},
214-
},
215-
});
216-
217204
return blocks.map((block, index) => {
218205
const transactions = block.transactions.map<TransactionExecutionResult>(
219206
(txresult) => {
@@ -223,28 +210,17 @@ export class PrismaBlockStorage
223210
const decodedBlock = this.blockMapper.mapIn(block);
224211
decodedBlock.transactions = transactions;
225212

226-
const correspondingResult = result.find(
227-
(candidate) => candidate.blockHash === block.hash
228-
);
213+
const { result } = block;
229214

230-
if (correspondingResult === undefined) {
215+
if (result === null) {
231216
throw new Error(
232217
`No BlockResult has been set for block ${block.hash} yet`
233218
);
234219
}
235220

236-
const parentResult = result.find(
237-
(candidate) => candidate.blockHash === block.parentHash
238-
);
239221
return {
240-
block: {
241-
block: decodedBlock,
242-
result: this.blockResultMapper.mapIn(correspondingResult),
243-
},
244-
lastBlockResult:
245-
parentResult !== undefined
246-
? this.blockResultMapper.mapIn(parentResult)
247-
: undefined,
222+
block: decodedBlock,
223+
result: this.blockResultMapper.mapIn(result),
248224
};
249225
});
250226
}

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@ import { CachedStateService } from "../../state/state/CachedStateService";
1717
import { CachedMerkleTreeStore } from "../../state/merkle/CachedMerkleTreeStore";
1818
import { AsyncStateService } from "../../state/async/AsyncStateService";
1919
import { AsyncMerkleTreeStore } from "../../state/async/AsyncMerkleTreeStore";
20-
import { BlockResult, BlockWithResult } from "../../storage/model/Block";
20+
import { BlockWithResult } from "../../storage/model/Block";
2121

2222
import { BlockProofSerializer } from "./tasks/serializers/BlockProofSerializer";
2323
import { BatchTracingService } from "./tracing/BatchTracingService";
2424
import { BatchFlow } from "./flow/BatchFlow";
2525

2626
export type StateRecord = Record<string, Field[] | undefined>;
2727

28-
export interface BlockWithPreviousResult {
29-
block: BlockWithResult;
30-
lastBlockResult?: BlockResult;
31-
}
32-
3328
interface BatchMetadata {
3429
batch: SettleableBatch;
3530
stateService: CachedStateService;
@@ -77,7 +72,7 @@ export class BatchProducerModule extends SequencerModule {
7772
* be the one called by BlockTriggers
7873
*/
7974
public async createBatch(
80-
blocks: BlockWithPreviousResult[]
75+
blocks: BlockWithResult[]
8176
): Promise<SettleableBatch | undefined> {
8277
log.info("Producing batch...");
8378

@@ -87,7 +82,7 @@ export class BatchProducerModule extends SequencerModule {
8782

8883
if (batchWithStateDiff !== undefined) {
8984
const numTxs = blocks.reduce(
90-
(sum, block) => sum + block.block.block.transactions.length,
85+
(sum, block) => sum + block.block.transactions.length,
9186
0
9287
);
9388
log.info(
@@ -105,7 +100,7 @@ export class BatchProducerModule extends SequencerModule {
105100
}
106101

107102
private async tryProduceBatch(
108-
blocks: BlockWithPreviousResult[],
103+
blocks: BlockWithResult[],
109104
height: number
110105
): Promise<BatchMetadata | undefined> {
111106
if (!this.productionInProgress) {
@@ -143,14 +138,12 @@ export class BatchProducerModule extends SequencerModule {
143138
}
144139

145140
private async produceBatch(
146-
blocks: BlockWithPreviousResult[],
141+
blocks: BlockWithResult[],
147142
height: number
148143
): Promise<BatchMetadata | undefined> {
149144
const batch = await this.computeBatch(blocks, height);
150145

151-
const blockHashes = blocks.map((bundle) =>
152-
bundle.block.block.hash.toString()
153-
);
146+
const blockHashes = blocks.map((bundle) => bundle.block.hash.toString());
154147

155148
const jsonProof = this.blockProofSerializer
156149
.getBlockProofSerializer()
@@ -184,8 +177,7 @@ export class BatchProducerModule extends SequencerModule {
184177
* @private
185178
*/
186179
private async computeBatch(
187-
// TODO Remove previous results
188-
blocks: BlockWithPreviousResult[],
180+
blocks: BlockWithResult[],
189181
blockId: number
190182
): Promise<{
191183
proof: Proof<BlockProverPublicInput, BlockProverPublicOutput>;
@@ -206,14 +198,14 @@ export class BatchProducerModule extends SequencerModule {
206198
};
207199

208200
const trace = await this.batchTraceService.traceBatch(
209-
blocks.map((block) => block.block),
201+
blocks.map((block) => block),
210202
stateServices
211203
);
212204

213205
const proof = await this.batchFlow.executeBatch(trace, blockId);
214206

215-
const fromNetworkState = blocks[0].block.block.networkState.before;
216-
const toNetworkState = blocks.at(-1)!.block.result.afterNetworkState;
207+
const fromNetworkState = blocks[0].block.networkState.before;
208+
const toNetworkState = blocks.at(-1)!.result.afterNetworkState;
217209

218210
return {
219211
proof,

packages/sequencer/src/storage/inmemory/InMemoryBlockStorage.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
BlockStorage,
77
} from "../repositories/BlockStorage";
88
import type { Block, BlockResult, BlockWithResult } from "../model/Block";
9-
import { BlockWithPreviousResult } from "../../protocol/production/BatchProducerModule";
109
import { BatchStorage } from "../repositories/BatchStorage";
1110

1211
@injectable()
@@ -42,7 +41,7 @@ export class InMemoryBlockStorage
4241
};
4342
}
4443

45-
public async getNewBlocks(): Promise<BlockWithPreviousResult[]> {
44+
public async getNewBlocks(): Promise<BlockWithResult[]> {
4645
const latestBatch = await this.batchStorage.getLatestBatch();
4746

4847
let cursor = 0;
@@ -66,11 +65,8 @@ export class InMemoryBlockStorage
6665
}
6766

6867
return slice.map((block, index) => ({
69-
block: {
70-
block,
71-
result: results[index + 1]!,
72-
},
73-
lastBlockResult: results[index],
68+
block,
69+
result: results[index + 1]!,
7470
}));
7571
}
7672

packages/sequencer/src/storage/repositories/BlockStorage.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { BlockWithPreviousResult } from "../../protocol/production/BatchProducerModule";
21
import type { Block, BlockResult, BlockWithResult } from "../model/Block";
32

43
export interface BlockQueue {
54
pushBlock: (block: Block) => Promise<void>;
65
pushResult: (result: BlockResult) => Promise<void>;
7-
getNewBlocks: () => Promise<BlockWithPreviousResult[]>;
6+
getNewBlocks: () => Promise<BlockWithResult[]>;
87
getLatestBlock: () => Promise<BlockWithResult | undefined>;
98
}
109

0 commit comments

Comments
 (0)