@aztec/stdlib 0.84.0-nightly.20250409 → 0.84.0-nightly.20250412
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/dest/avm/avm.d.ts +627 -5
- package/dest/avm/avm.d.ts.map +1 -1
- package/dest/avm/avm.js +13 -7
- package/dest/avm/avm_proving_request.d.ts +331 -0
- package/dest/avm/avm_proving_request.d.ts.map +1 -1
- package/dest/block/index.d.ts +1 -1
- package/dest/block/index.d.ts.map +1 -1
- package/dest/block/index.js +1 -1
- package/dest/block/l2_block_source.d.ts +2 -0
- package/dest/block/l2_block_source.d.ts.map +1 -1
- package/dest/block/l2_block_source.js +9 -0
- package/dest/block/l2_block_stream/index.d.ts +4 -0
- package/dest/block/l2_block_stream/index.d.ts.map +1 -0
- package/dest/block/l2_block_stream/index.js +3 -0
- package/dest/block/l2_block_stream/interfaces.d.ts +26 -0
- package/dest/block/l2_block_stream/interfaces.d.ts.map +1 -0
- package/dest/block/l2_block_stream/interfaces.js +1 -0
- package/dest/block/{l2_block_downloader → l2_block_stream}/l2_block_stream.d.ts +4 -24
- package/dest/block/l2_block_stream/l2_block_stream.d.ts.map +1 -0
- package/dest/block/{l2_block_downloader → l2_block_stream}/l2_block_stream.js +29 -10
- package/dest/block/l2_block_stream/l2_tips_memory_store.d.ts +18 -0
- package/dest/block/l2_block_stream/l2_tips_memory_store.d.ts.map +1 -0
- package/dest/block/l2_block_stream/l2_tips_memory_store.js +70 -0
- package/dest/block/test/index.d.ts +2 -0
- package/dest/block/test/index.d.ts.map +1 -0
- package/dest/block/test/index.js +1 -0
- package/dest/block/test/l2_tips_store_test_suite.d.ts +3 -0
- package/dest/block/test/l2_tips_store_test_suite.d.ts.map +1 -0
- package/dest/block/test/l2_tips_store_test_suite.js +107 -0
- package/dest/database-version/version_manager.d.ts +21 -5
- package/dest/database-version/version_manager.d.ts.map +1 -1
- package/dest/database-version/version_manager.js +25 -15
- package/dest/interfaces/proving-job.d.ts +331 -0
- package/dest/interfaces/proving-job.d.ts.map +1 -1
- package/dest/proofs/proof.d.ts +4 -1
- package/dest/proofs/proof.d.ts.map +1 -1
- package/dest/proofs/proof.js +9 -17
- package/dest/tests/factories.d.ts.map +1 -1
- package/dest/tests/factories.js +3 -2
- package/package.json +8 -6
- package/src/avm/avm.ts +19 -2
- package/src/block/index.ts +1 -1
- package/src/block/l2_block_source.ts +8 -0
- package/src/block/l2_block_stream/index.ts +3 -0
- package/src/block/l2_block_stream/interfaces.ts +33 -0
- package/src/block/{l2_block_downloader → l2_block_stream}/l2_block_stream.ts +34 -44
- package/src/block/l2_block_stream/l2_tips_memory_store.ts +75 -0
- package/src/block/test/index.ts +1 -0
- package/src/block/test/l2_tips_store_test_suite.ts +87 -0
- package/src/database-version/version_manager.ts +56 -17
- package/src/proofs/proof.ts +9 -19
- package/src/tests/factories.ts +3 -0
- package/dest/block/l2_block_downloader/index.d.ts +0 -3
- package/dest/block/l2_block_downloader/index.d.ts.map +0 -1
- package/dest/block/l2_block_downloader/index.js +0 -2
- package/dest/block/l2_block_downloader/l2_block_downloader.d.ts +0 -58
- package/dest/block/l2_block_downloader/l2_block_downloader.d.ts.map +0 -1
- package/dest/block/l2_block_downloader/l2_block_downloader.js +0 -124
- package/dest/block/l2_block_downloader/l2_block_stream.d.ts.map +0 -1
- package/src/block/l2_block_downloader/index.ts +0 -2
- package/src/block/l2_block_downloader/l2_block_downloader.ts +0 -149
|
@@ -118,6 +118,8 @@ export type L2BlockTag = 'latest' | 'proven' | 'finalized';
|
|
|
118
118
|
export type L2Tips = Record<L2BlockTag, L2BlockId>;
|
|
119
119
|
/** Identifies a block by number and hash. */
|
|
120
120
|
export type L2BlockId = z.infer<typeof L2BlockIdSchema>;
|
|
121
|
+
/** Creates an L2 block id */
|
|
122
|
+
export declare function makeL2BlockId(number: number, hash?: string): L2BlockId;
|
|
121
123
|
declare const L2BlockIdSchema: z.ZodUnion<[z.ZodObject<{
|
|
122
124
|
number: z.Z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Q,GAAG,WAAW,CAAC;AAE3D,4BAA4B;AAC5B,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;AAEnD,6CAA6C;AAC7C,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAExD,6BAA6B;AAC7B,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAKtE;AAGD,QAAA,MAAM,eAAe;;;;;;;;;;;;;;;;;;IASnB,CAAC;AAEH,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAIK,CAAC;AAE/B,oBAAY,mBAAmB;IAC7B,eAAe,oBAAoB;CACpC;AAED,MAAM,MAAM,kBAAkB,GAAG;IAC/B,IAAI,EAAE,iBAAiB,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC"}
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
/** Creates an L2 block id */ export function makeL2BlockId(number, hash) {
|
|
3
|
+
if (number !== 0 && !hash) {
|
|
4
|
+
throw new Error(`Hash is required for non-genesis blocks (got block number ${number})`);
|
|
5
|
+
}
|
|
6
|
+
return {
|
|
7
|
+
number,
|
|
8
|
+
hash: hash
|
|
9
|
+
};
|
|
10
|
+
}
|
|
2
11
|
// TODO(palla/schemas): This package should know what is the block hash of the genesis block 0.
|
|
3
12
|
const L2BlockIdSchema = z.union([
|
|
4
13
|
z.object({
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/block/l2_block_stream/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC;AACrC,cAAc,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { L2BlockId, L2Tips } from '../l2_block_source.js';
|
|
2
|
+
import type { PublishedL2Block } from '../published_l2_block.js';
|
|
3
|
+
/** Interface to the local view of the chain. Implemented by world-state and l2-tips-store. */
|
|
4
|
+
export interface L2BlockStreamLocalDataProvider {
|
|
5
|
+
getL2BlockHash(number: number): Promise<string | undefined>;
|
|
6
|
+
getL2Tips(): Promise<L2Tips>;
|
|
7
|
+
}
|
|
8
|
+
/** Interface to a handler of events emitted. */
|
|
9
|
+
export interface L2BlockStreamEventHandler {
|
|
10
|
+
handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export type L2BlockStreamEvent = /** Emits blocks added to the chain. */ {
|
|
13
|
+
type: 'blocks-added';
|
|
14
|
+
blocks: PublishedL2Block[];
|
|
15
|
+
} | /** Reports last correct block (new tip of the unproven chain). */ {
|
|
16
|
+
type: 'chain-pruned';
|
|
17
|
+
block: L2BlockId;
|
|
18
|
+
} | /** Reports new proven block. */ {
|
|
19
|
+
type: 'chain-proven';
|
|
20
|
+
block: L2BlockId;
|
|
21
|
+
} | /** Reports new finalized block (proven and finalized on L1). */ {
|
|
22
|
+
type: 'chain-finalized';
|
|
23
|
+
block: L2BlockId;
|
|
24
|
+
};
|
|
25
|
+
export type L2TipsStore = L2BlockStreamEventHandler & L2BlockStreamLocalDataProvider;
|
|
26
|
+
//# sourceMappingURL=interfaces.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../../../src/block/l2_block_stream/interfaces.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAEjE,8FAA8F;AAC9F,MAAM,WAAW,8BAA8B;IAC7C,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAC5D,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CAC9B;AAED,gDAAgD;AAChD,MAAM,WAAW,yBAAyB;IACxC,sBAAsB,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAClE;AAED,MAAM,MAAM,kBAAkB,GAC1B,uCAAuC,CAAC;IACtC,IAAI,EAAE,cAAc,CAAC;IACrB,MAAM,EAAE,gBAAgB,EAAE,CAAC;CAC5B,GACD,kEAAkE,CAAC;IACjE,IAAI,EAAE,cAAc,CAAC;IACrB,KAAK,EAAE,SAAS,CAAC;CAClB,GACD,gCAAgC,CAAC;IAC/B,IAAI,EAAE,cAAc,CAAC;IACrB,KAAK,EAAE,SAAS,CAAC;CAClB,GACD,gEAAgE,CAAC;IAC/D,IAAI,EAAE,iBAAiB,CAAC;IACxB,KAAK,EAAE,SAAS,CAAC;CAClB,CAAC;AAEN,MAAM,MAAM,WAAW,GAAG,yBAAyB,GAAG,8BAA8B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
1
|
+
import { type L2BlockSource } from '../l2_block_source.js';
|
|
2
|
+
import type { L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider } from './interfaces.js';
|
|
3
3
|
/** Creates a stream of events for new blocks, chain tips updates, and reorgs, out of polling an archiver or a node. */
|
|
4
4
|
export declare class L2BlockStream {
|
|
5
5
|
private l2BlockSource;
|
|
@@ -15,6 +15,8 @@ export declare class L2BlockStream {
|
|
|
15
15
|
pollIntervalMS?: number;
|
|
16
16
|
batchSize?: number;
|
|
17
17
|
startingBlock?: number;
|
|
18
|
+
/** Instead of downloading all blocks, only fetch the smallest subset that results in reliable reorg detection. */
|
|
19
|
+
skipFinalized?: boolean;
|
|
18
20
|
});
|
|
19
21
|
start(): void;
|
|
20
22
|
stop(): Promise<void>;
|
|
@@ -30,26 +32,4 @@ export declare class L2BlockStream {
|
|
|
30
32
|
private getBlockHashFromSource;
|
|
31
33
|
private emitEvent;
|
|
32
34
|
}
|
|
33
|
-
/** Interface to the local view of the chain. Implemented by world-state and l2-tips-store. */
|
|
34
|
-
export interface L2BlockStreamLocalDataProvider {
|
|
35
|
-
getL2BlockHash(number: number): Promise<string | undefined>;
|
|
36
|
-
getL2Tips(): Promise<L2Tips>;
|
|
37
|
-
}
|
|
38
|
-
/** Interface to a handler of events emitted. */
|
|
39
|
-
export interface L2BlockStreamEventHandler {
|
|
40
|
-
handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void>;
|
|
41
|
-
}
|
|
42
|
-
export type L2BlockStreamEvent = /** Emits blocks added to the chain. */ {
|
|
43
|
-
type: 'blocks-added';
|
|
44
|
-
blocks: PublishedL2Block[];
|
|
45
|
-
} | /** Reports last correct block (new tip of the unproven chain). */ {
|
|
46
|
-
type: 'chain-pruned';
|
|
47
|
-
block: L2BlockId;
|
|
48
|
-
} | /** Reports new proven block. */ {
|
|
49
|
-
type: 'chain-proven';
|
|
50
|
-
block: L2BlockId;
|
|
51
|
-
} | /** Reports new finalized block (proven and finalized on L1). */ {
|
|
52
|
-
type: 'chain-finalized';
|
|
53
|
-
block: L2BlockId;
|
|
54
|
-
};
|
|
55
35
|
//# sourceMappingURL=l2_block_stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"l2_block_stream.d.ts","sourceRoot":"","sources":["../../../src/block/l2_block_stream/l2_block_stream.ts"],"names":[],"mappings":"AAIA,OAAO,EAAkB,KAAK,aAAa,EAAiB,MAAM,uBAAuB,CAAC;AAC1F,OAAO,KAAK,EAAsB,yBAAyB,EAAE,8BAA8B,EAAE,MAAM,iBAAiB,CAAC;AAErH,uHAAuH;AACvH,qBAAa,aAAa;IAMtB,OAAO,CAAC,aAAa;IACrB,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,IAAI;IATd,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAiB;IAChD,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,UAAU,CAAS;gBAGjB,aAAa,EAAE,IAAI,CAAC,aAAa,EAAE,oBAAoB,GAAG,gBAAgB,GAAG,WAAW,CAAC,EACzF,SAAS,EAAE,8BAA8B,EACzC,OAAO,EAAE,yBAAyB,EACzB,GAAG,yCAAqC,EACjD,IAAI,GAAE;QACZ,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB,cAAc,CAAC,EAAE,MAAM,CAAC;QACxB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,kHAAkH;QAClH,aAAa,CAAC,EAAE,OAAO,CAAC;KACpB;IAKD,KAAK;IAKC,IAAI;IAIV,SAAS;IAIH,IAAI;cAMD,IAAI;IAuFpB;;;;OAIG;YACW,qBAAqB;IAwBnC,OAAO,CAAC,sBAAsB;YAOhB,SAAS;CASxB"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AbortError } from '@aztec/foundation/error';
|
|
2
2
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
3
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
4
|
+
import { makeL2BlockId } from '../l2_block_source.js';
|
|
4
5
|
/** Creates a stream of events for new blocks, chain tips updates, and reorgs, out of polling an archiver or a node. */ export class L2BlockStream {
|
|
5
6
|
l2BlockSource;
|
|
6
7
|
localData;
|
|
@@ -64,13 +65,15 @@ import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
|
64
65
|
latestBlockNumber--;
|
|
65
66
|
}
|
|
66
67
|
if (latestBlockNumber < localTips.latest.number) {
|
|
68
|
+
latestBlockNumber = Math.min(latestBlockNumber, sourceTips.latest.number); // see #13471
|
|
69
|
+
const hash = sourceCache.get(latestBlockNumber) ?? await this.getBlockHashFromSource(latestBlockNumber);
|
|
70
|
+
if (latestBlockNumber !== 0 && !hash) {
|
|
71
|
+
throw new Error(`Block hash not found in block source for block number ${latestBlockNumber}`);
|
|
72
|
+
}
|
|
67
73
|
this.log.verbose(`Reorg detected. Pruning blocks from ${latestBlockNumber + 1} to ${localTips.latest.number}.`);
|
|
68
74
|
await this.emitEvent({
|
|
69
75
|
type: 'chain-pruned',
|
|
70
|
-
block:
|
|
71
|
-
number: latestBlockNumber,
|
|
72
|
-
hash: sourceCache.get(latestBlockNumber) ?? await this.getBlockHashFromSource(latestBlockNumber)
|
|
73
|
-
}
|
|
76
|
+
block: makeL2BlockId(latestBlockNumber, hash)
|
|
74
77
|
});
|
|
75
78
|
}
|
|
76
79
|
// If we are just starting, use the starting block number from the options.
|
|
@@ -82,12 +85,20 @@ import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
|
82
85
|
this.log.verbose(`Starting sync from block number ${latestBlockNumber}`);
|
|
83
86
|
this.hasStarted = true;
|
|
84
87
|
}
|
|
88
|
+
let nextBlockNumber = latestBlockNumber + 1;
|
|
89
|
+
if (this.opts.skipFinalized) {
|
|
90
|
+
// When skipping finalized blocks we need to provide reliable reorg detection while fetching as few blocks as
|
|
91
|
+
// possible. Finalized blocks cannot be reorged by definition, so we can skip most of them. We do need the very
|
|
92
|
+
// last finalized block however in order to guarantee that we will eventually find a block in which our local
|
|
93
|
+
// store matches the source.
|
|
94
|
+
// If the last finalized block is behind our local tip, there is nothing to skip.
|
|
95
|
+
nextBlockNumber = Math.max(sourceTips.finalized.number, nextBlockNumber);
|
|
96
|
+
}
|
|
85
97
|
// Request new blocks from the source.
|
|
86
|
-
while(
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const blocks = await this.l2BlockSource.getPublishedBlocks(from, limit, this.opts.proven);
|
|
98
|
+
while(nextBlockNumber <= sourceTips.latest.number){
|
|
99
|
+
const limit = Math.min(this.opts.batchSize ?? 20, sourceTips.latest.number - nextBlockNumber + 1);
|
|
100
|
+
this.log.trace(`Requesting blocks from ${nextBlockNumber} limit ${limit} proven=${this.opts.proven}`);
|
|
101
|
+
const blocks = await this.l2BlockSource.getPublishedBlocks(nextBlockNumber, limit, this.opts.proven);
|
|
91
102
|
if (blocks.length === 0) {
|
|
92
103
|
break;
|
|
93
104
|
}
|
|
@@ -95,7 +106,7 @@ import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
|
95
106
|
type: 'blocks-added',
|
|
96
107
|
blocks
|
|
97
108
|
});
|
|
98
|
-
|
|
109
|
+
nextBlockNumber = blocks.at(-1).block.number + 1;
|
|
99
110
|
}
|
|
100
111
|
// Update the proven and finalized tips.
|
|
101
112
|
if (localTips.proven !== undefined && sourceTips.proven.number !== localTips.proven.number) {
|
|
@@ -126,6 +137,14 @@ import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
|
126
137
|
return true;
|
|
127
138
|
}
|
|
128
139
|
const localBlockHash = await this.localData.getL2BlockHash(blockNumber);
|
|
140
|
+
if (!localBlockHash && this.opts.skipFinalized) {
|
|
141
|
+
// Failing to find a block hash when skipping finalized blocks can be highly problematic as we'd potentially need
|
|
142
|
+
// to go all the way back to the genesis block to find a block in which we agree with the source (since we've
|
|
143
|
+
// potentially skipped all history). This means that stores that prune old blocks must be careful to leave no gaps
|
|
144
|
+
// when going back from latest block to the last finalized one.
|
|
145
|
+
this.log.error(`No local block hash for block number ${blockNumber}`);
|
|
146
|
+
throw new AbortError();
|
|
147
|
+
}
|
|
129
148
|
const sourceBlockHashFromCache = args.sourceCache.get(blockNumber);
|
|
130
149
|
const sourceBlockHash = args.sourceCache.get(blockNumber) ?? await this.getBlockHashFromSource(blockNumber);
|
|
131
150
|
if (!sourceBlockHashFromCache && sourceBlockHash) {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { L2Block } from '../l2_block.js';
|
|
2
|
+
import type { L2BlockId, L2BlockTag, L2Tips } from '../l2_block_source.js';
|
|
3
|
+
import type { L2BlockStreamEvent, L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider } from './interfaces.js';
|
|
4
|
+
/**
|
|
5
|
+
* Stores currently synced L2 tips and unfinalized block hashes.
|
|
6
|
+
* @dev tests in kv-store/src/stores/l2_tips_memory_store.test.ts
|
|
7
|
+
*/
|
|
8
|
+
export declare class L2TipsMemoryStore implements L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider {
|
|
9
|
+
protected readonly l2TipsStore: Map<L2BlockTag, number>;
|
|
10
|
+
protected readonly l2BlockHashesStore: Map<number, string>;
|
|
11
|
+
getL2BlockHash(number: number): Promise<string | undefined>;
|
|
12
|
+
getL2Tips(): Promise<L2Tips>;
|
|
13
|
+
private getL2Tip;
|
|
14
|
+
handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void>;
|
|
15
|
+
protected saveTag(name: L2BlockTag, block: L2BlockId): void;
|
|
16
|
+
protected computeBlockHash(block: L2Block): Promise<`0x${string}`>;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=l2_tips_memory_store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"l2_tips_memory_store.d.ts","sourceRoot":"","sources":["../../../src/block/l2_block_stream/l2_tips_memory_store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC3E,OAAO,KAAK,EAAE,kBAAkB,EAAE,yBAAyB,EAAE,8BAA8B,EAAE,MAAM,iBAAiB,CAAC;AAErH;;;GAGG;AACH,qBAAa,iBAAkB,YAAW,yBAAyB,EAAE,8BAA8B;IACjG,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,CAAa;IACpE,SAAS,CAAC,QAAQ,CAAC,kBAAkB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAa;IAEhE,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAI3D,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC;IAQnC,OAAO,CAAC,QAAQ;IAaH,sBAAsB,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;IA2B7E,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS;IAOpD,SAAS,CAAC,gBAAgB,CAAC,KAAK,EAAE,OAAO;CAG1C"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stores currently synced L2 tips and unfinalized block hashes.
|
|
3
|
+
* @dev tests in kv-store/src/stores/l2_tips_memory_store.test.ts
|
|
4
|
+
*/ export class L2TipsMemoryStore {
|
|
5
|
+
l2TipsStore = new Map();
|
|
6
|
+
l2BlockHashesStore = new Map();
|
|
7
|
+
getL2BlockHash(number) {
|
|
8
|
+
return Promise.resolve(this.l2BlockHashesStore.get(number));
|
|
9
|
+
}
|
|
10
|
+
getL2Tips() {
|
|
11
|
+
return Promise.resolve({
|
|
12
|
+
latest: this.getL2Tip('latest'),
|
|
13
|
+
finalized: this.getL2Tip('finalized'),
|
|
14
|
+
proven: this.getL2Tip('proven')
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
getL2Tip(tag) {
|
|
18
|
+
const blockNumber = this.l2TipsStore.get(tag);
|
|
19
|
+
if (blockNumber === undefined || blockNumber === 0) {
|
|
20
|
+
return {
|
|
21
|
+
number: 0,
|
|
22
|
+
hash: undefined
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const blockHash = this.l2BlockHashesStore.get(blockNumber);
|
|
26
|
+
if (!blockHash) {
|
|
27
|
+
throw new Error(`Block hash not found for block number ${blockNumber}`);
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
number: blockNumber,
|
|
31
|
+
hash: blockHash
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async handleBlockStreamEvent(event) {
|
|
35
|
+
switch(event.type){
|
|
36
|
+
case 'blocks-added':
|
|
37
|
+
{
|
|
38
|
+
const blocks = event.blocks.map((b)=>b.block);
|
|
39
|
+
for (const block of blocks){
|
|
40
|
+
this.l2BlockHashesStore.set(block.number, await this.computeBlockHash(block));
|
|
41
|
+
}
|
|
42
|
+
this.l2TipsStore.set('latest', blocks.at(-1).number);
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case 'chain-pruned':
|
|
46
|
+
this.saveTag('latest', event.block);
|
|
47
|
+
break;
|
|
48
|
+
case 'chain-proven':
|
|
49
|
+
this.saveTag('proven', event.block);
|
|
50
|
+
break;
|
|
51
|
+
case 'chain-finalized':
|
|
52
|
+
this.saveTag('finalized', event.block);
|
|
53
|
+
for (const key of this.l2BlockHashesStore.keys()){
|
|
54
|
+
if (key < event.block.number) {
|
|
55
|
+
this.l2BlockHashesStore.delete(key);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
saveTag(name, block) {
|
|
62
|
+
this.l2TipsStore.set(name, block.number);
|
|
63
|
+
if (block.hash) {
|
|
64
|
+
this.l2BlockHashesStore.set(block.number, block.hash);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
computeBlockHash(block) {
|
|
68
|
+
return block.header.hash().then((hash)=>hash.toString());
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/block/test/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './l2_tips_store_test_suite.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"l2_tips_store_test_suite.d.ts","sourceRoot":"","sources":["../../../src/block/test/l2_tips_store_test_suite.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAE/D,wBAAgB,eAAe,CAAC,aAAa,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,QA6ExE"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { times } from '@aztec/foundation/collection';
|
|
2
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
+
import { jestExpect as expect } from '@jest/expect';
|
|
4
|
+
export function testL2TipsStore(makeTipsStore) {
|
|
5
|
+
let tipsStore;
|
|
6
|
+
beforeEach(async ()=>{
|
|
7
|
+
tipsStore = await makeTipsStore();
|
|
8
|
+
});
|
|
9
|
+
const makeBlock = (number)=>({
|
|
10
|
+
block: {
|
|
11
|
+
number,
|
|
12
|
+
header: {
|
|
13
|
+
hash: ()=>Promise.resolve(new Fr(number))
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
l1: {
|
|
17
|
+
blockNumber: BigInt(number),
|
|
18
|
+
blockHash: `0x${number}`,
|
|
19
|
+
timestamp: BigInt(number)
|
|
20
|
+
},
|
|
21
|
+
signatures: []
|
|
22
|
+
});
|
|
23
|
+
const makeBlockId = (number)=>({
|
|
24
|
+
number,
|
|
25
|
+
hash: new Fr(number).toString()
|
|
26
|
+
});
|
|
27
|
+
const makeTip = (number)=>({
|
|
28
|
+
number,
|
|
29
|
+
hash: number === 0 ? undefined : new Fr(number).toString()
|
|
30
|
+
});
|
|
31
|
+
const makeTips = (latest, proven, finalized)=>({
|
|
32
|
+
latest: makeTip(latest),
|
|
33
|
+
proven: makeTip(proven),
|
|
34
|
+
finalized: makeTip(finalized)
|
|
35
|
+
});
|
|
36
|
+
it('returns zero if no tips are stored', async ()=>{
|
|
37
|
+
const tips = await tipsStore.getL2Tips();
|
|
38
|
+
expect(tips).toEqual(makeTips(0, 0, 0));
|
|
39
|
+
});
|
|
40
|
+
it('stores chain tips', async ()=>{
|
|
41
|
+
await tipsStore.handleBlockStreamEvent({
|
|
42
|
+
type: 'blocks-added',
|
|
43
|
+
blocks: times(20, (i)=>makeBlock(i + 1))
|
|
44
|
+
});
|
|
45
|
+
await tipsStore.handleBlockStreamEvent({
|
|
46
|
+
type: 'chain-finalized',
|
|
47
|
+
block: makeBlockId(5)
|
|
48
|
+
});
|
|
49
|
+
await tipsStore.handleBlockStreamEvent({
|
|
50
|
+
type: 'chain-proven',
|
|
51
|
+
block: makeBlockId(8)
|
|
52
|
+
});
|
|
53
|
+
await tipsStore.handleBlockStreamEvent({
|
|
54
|
+
type: 'chain-pruned',
|
|
55
|
+
block: makeBlockId(10)
|
|
56
|
+
});
|
|
57
|
+
const tips = await tipsStore.getL2Tips();
|
|
58
|
+
expect(tips).toEqual(makeTips(10, 8, 5));
|
|
59
|
+
});
|
|
60
|
+
it('sets latest tip from blocks added', async ()=>{
|
|
61
|
+
await tipsStore.handleBlockStreamEvent({
|
|
62
|
+
type: 'blocks-added',
|
|
63
|
+
blocks: times(3, (i)=>makeBlock(i + 1))
|
|
64
|
+
});
|
|
65
|
+
const tips = await tipsStore.getL2Tips();
|
|
66
|
+
expect(tips).toEqual(makeTips(3, 0, 0));
|
|
67
|
+
expect(await tipsStore.getL2BlockHash(1)).toEqual(new Fr(1).toString());
|
|
68
|
+
expect(await tipsStore.getL2BlockHash(2)).toEqual(new Fr(2).toString());
|
|
69
|
+
expect(await tipsStore.getL2BlockHash(3)).toEqual(new Fr(3).toString());
|
|
70
|
+
});
|
|
71
|
+
it('clears block hashes when setting finalized chain', async ()=>{
|
|
72
|
+
await tipsStore.handleBlockStreamEvent({
|
|
73
|
+
type: 'blocks-added',
|
|
74
|
+
blocks: times(5, (i)=>makeBlock(i + 1))
|
|
75
|
+
});
|
|
76
|
+
await tipsStore.handleBlockStreamEvent({
|
|
77
|
+
type: 'chain-proven',
|
|
78
|
+
block: makeBlockId(3)
|
|
79
|
+
});
|
|
80
|
+
await tipsStore.handleBlockStreamEvent({
|
|
81
|
+
type: 'chain-finalized',
|
|
82
|
+
block: makeBlockId(3)
|
|
83
|
+
});
|
|
84
|
+
const tips = await tipsStore.getL2Tips();
|
|
85
|
+
expect(tips).toEqual(makeTips(5, 3, 3));
|
|
86
|
+
expect(await tipsStore.getL2BlockHash(1)).toBeUndefined();
|
|
87
|
+
expect(await tipsStore.getL2BlockHash(2)).toBeUndefined();
|
|
88
|
+
expect(await tipsStore.getL2BlockHash(3)).toEqual(new Fr(3).toString());
|
|
89
|
+
expect(await tipsStore.getL2BlockHash(4)).toEqual(new Fr(4).toString());
|
|
90
|
+
expect(await tipsStore.getL2BlockHash(5)).toEqual(new Fr(5).toString());
|
|
91
|
+
});
|
|
92
|
+
// Regression test for #13142
|
|
93
|
+
it('does not blow up when setting proven chain on an unseen block number', async ()=>{
|
|
94
|
+
await tipsStore.handleBlockStreamEvent({
|
|
95
|
+
type: 'blocks-added',
|
|
96
|
+
blocks: [
|
|
97
|
+
makeBlock(5)
|
|
98
|
+
]
|
|
99
|
+
});
|
|
100
|
+
await tipsStore.handleBlockStreamEvent({
|
|
101
|
+
type: 'chain-proven',
|
|
102
|
+
block: makeBlockId(3)
|
|
103
|
+
});
|
|
104
|
+
const tips = await tipsStore.getL2Tips();
|
|
105
|
+
expect(tips).toEqual(makeTips(5, 3, 0));
|
|
106
|
+
});
|
|
107
|
+
}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
/// <reference types="node" resolution-mode="require"/>
|
|
4
4
|
/// <reference types="node" resolution-mode="require"/>
|
|
5
5
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
6
|
+
import { type Logger } from '@aztec/foundation/log';
|
|
6
7
|
import fs from 'fs/promises';
|
|
7
8
|
import { inspect } from 'node:util';
|
|
8
9
|
import { z } from 'zod';
|
|
@@ -10,9 +11,15 @@ import { z } from 'zod';
|
|
|
10
11
|
* Represents a version record for storing in a version file.
|
|
11
12
|
*/
|
|
12
13
|
export declare class DatabaseVersion {
|
|
14
|
+
/** The version of the data on disk. Used to perform upgrades */
|
|
13
15
|
readonly schemaVersion: number;
|
|
16
|
+
/** The rollup the data pertains to */
|
|
14
17
|
readonly rollupAddress: EthAddress;
|
|
15
|
-
constructor(
|
|
18
|
+
constructor(
|
|
19
|
+
/** The version of the data on disk. Used to perform upgrades */
|
|
20
|
+
schemaVersion: number,
|
|
21
|
+
/** The rollup the data pertains to */
|
|
22
|
+
rollupAddress: EthAddress);
|
|
16
23
|
toBuffer(): Buffer;
|
|
17
24
|
static fromBuffer(buf: Buffer): DatabaseVersion;
|
|
18
25
|
/**
|
|
@@ -49,20 +56,29 @@ export declare class DatabaseVersion {
|
|
|
49
56
|
}
|
|
50
57
|
export type DatabaseVersionManagerFs = Pick<typeof fs, 'readFile' | 'writeFile' | 'rm' | 'mkdir'>;
|
|
51
58
|
export declare const DATABASE_VERSION_FILE_NAME = "db_version";
|
|
59
|
+
export type DatabaseVersionManagerOptions<T> = {
|
|
60
|
+
schemaVersion: number;
|
|
61
|
+
rollupAddress: EthAddress;
|
|
62
|
+
dataDirectory: string;
|
|
63
|
+
onOpen: (dataDir: string) => Promise<T>;
|
|
64
|
+
onUpgrade?: (dataDir: string, currentVersion: number, latestVersion: number) => Promise<void>;
|
|
65
|
+
fileSystem?: DatabaseVersionManagerFs;
|
|
66
|
+
log?: Logger;
|
|
67
|
+
};
|
|
52
68
|
/**
|
|
53
69
|
* A manager for handling database versioning and migrations.
|
|
54
70
|
* This class will check the version of data in a directory and either
|
|
55
71
|
* reset or upgrade based on version compatibility.
|
|
56
72
|
*/
|
|
57
73
|
export declare class DatabaseVersionManager<T> {
|
|
74
|
+
static readonly VERSION_FILE = "db_version";
|
|
75
|
+
private readonly versionFile;
|
|
76
|
+
private readonly currentVersion;
|
|
58
77
|
private dataDirectory;
|
|
59
78
|
private onOpen;
|
|
60
79
|
private onUpgrade?;
|
|
61
80
|
private fileSystem;
|
|
62
81
|
private log;
|
|
63
|
-
static readonly VERSION_FILE = "db_version";
|
|
64
|
-
private readonly versionFile;
|
|
65
|
-
private readonly currentVersion;
|
|
66
82
|
/**
|
|
67
83
|
* Create a new version manager
|
|
68
84
|
*
|
|
@@ -75,7 +91,7 @@ export declare class DatabaseVersionManager<T> {
|
|
|
75
91
|
* @param log - Optional custom logger
|
|
76
92
|
* @param options - Configuration options
|
|
77
93
|
*/
|
|
78
|
-
constructor(schemaVersion
|
|
94
|
+
constructor({ schemaVersion, rollupAddress, dataDirectory, onOpen, onUpgrade, fileSystem, log, }: DatabaseVersionManagerOptions<T>);
|
|
79
95
|
static writeVersion(version: DatabaseVersion, dataDir: string, fileSystem?: DatabaseVersionManagerFs): Promise<void>;
|
|
80
96
|
/**
|
|
81
97
|
* Checks the stored version against the current version and handles the outcome
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version_manager.d.ts","sourceRoot":"","sources":["../../src/database-version/version_manager.ts"],"names":[],"mappings":";;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"version_manager.d.ts","sourceRoot":"","sources":["../../src/database-version/version_manager.ts"],"names":[],"mappings":";;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAE3D,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,uBAAuB,CAAC;AAElE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,qBAAa,eAAe;IAExB,gEAAgE;aAChD,aAAa,EAAE,MAAM;IACrC,sCAAsC;aACtB,aAAa,EAAE,UAAU;;IAHzC,gEAAgE;IAChD,aAAa,EAAE,MAAM;IACr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CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,MAAM,CAAkC;IAChD,OAAO,CAAC,SAAS,CAAC,CAAoF;IACtG,OAAO,CAAC,UAAU,CAA2B;IAC7C,OAAO,CAAC,GAAG,CAAS;IAEpB;;;;;;;;;;;OAWG;gBACS,EACV,aAAa,EACb,aAAa,EACb,aAAa,EACb,MAAM,EACN,SAAS,EACT,UAAe,EACf,GAAgD,GACjD,EAAE,6BAA6B,CAAC,CAAC,CAAC;WAetB,YAAY,CAAC,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,GAAE,wBAA6B;IAK9G;;;;;;;OAOG;IACU,IAAI,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IA+D1C;;OAEG;IACI,YAAY,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIhD;;OAEG;IACU,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;IAUhD;;OAEG;IACI,gBAAgB,IAAI,MAAM;IAIjC;;OAEG;IACI,gBAAgB,IAAI,MAAM;CAGlC"}
|
|
@@ -10,7 +10,7 @@ import { z } from 'zod';
|
|
|
10
10
|
*/ export class DatabaseVersion {
|
|
11
11
|
schemaVersion;
|
|
12
12
|
rollupAddress;
|
|
13
|
-
constructor(schemaVersion, rollupAddress){
|
|
13
|
+
constructor(/** The version of the data on disk. Used to perform upgrades */ schemaVersion, /** The rollup the data pertains to */ rollupAddress){
|
|
14
14
|
this.schemaVersion = schemaVersion;
|
|
15
15
|
this.rollupAddress = rollupAddress;
|
|
16
16
|
}
|
|
@@ -57,7 +57,7 @@ import { z } from 'zod';
|
|
|
57
57
|
return this.toString();
|
|
58
58
|
}
|
|
59
59
|
toString() {
|
|
60
|
-
return this.schemaVersion.
|
|
60
|
+
return `DatabaseVersion{schemaVersion=${this.schemaVersion},rollupAddress=${this.rollupAddress}"}`;
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
63
|
* Returns an empty instance
|
|
@@ -71,14 +71,14 @@ export const DATABASE_VERSION_FILE_NAME = 'db_version';
|
|
|
71
71
|
* This class will check the version of data in a directory and either
|
|
72
72
|
* reset or upgrade based on version compatibility.
|
|
73
73
|
*/ export class DatabaseVersionManager {
|
|
74
|
+
static VERSION_FILE = DATABASE_VERSION_FILE_NAME;
|
|
75
|
+
versionFile;
|
|
76
|
+
currentVersion;
|
|
74
77
|
dataDirectory;
|
|
75
78
|
onOpen;
|
|
76
79
|
onUpgrade;
|
|
77
80
|
fileSystem;
|
|
78
81
|
log;
|
|
79
|
-
static VERSION_FILE = DATABASE_VERSION_FILE_NAME;
|
|
80
|
-
versionFile;
|
|
81
|
-
currentVersion;
|
|
82
82
|
/**
|
|
83
83
|
* Create a new version manager
|
|
84
84
|
*
|
|
@@ -90,17 +90,17 @@ export const DATABASE_VERSION_FILE_NAME = 'db_version';
|
|
|
90
90
|
* @param fileSystem - An interface to access the filesystem
|
|
91
91
|
* @param log - Optional custom logger
|
|
92
92
|
* @param options - Configuration options
|
|
93
|
-
*/ constructor(schemaVersion, rollupAddress, dataDirectory, onOpen, onUpgrade, fileSystem = fs, log = createLogger(`foundation:version-manager`)){
|
|
93
|
+
*/ constructor({ schemaVersion, rollupAddress, dataDirectory, onOpen, onUpgrade, fileSystem = fs, log = createLogger(`foundation:version-manager`) }){
|
|
94
|
+
if (schemaVersion < 1) {
|
|
95
|
+
throw new TypeError(`Invalid schema version received: ${schemaVersion}`);
|
|
96
|
+
}
|
|
97
|
+
this.versionFile = join(dataDirectory, DatabaseVersionManager.VERSION_FILE);
|
|
98
|
+
this.currentVersion = new DatabaseVersion(schemaVersion, rollupAddress);
|
|
94
99
|
this.dataDirectory = dataDirectory;
|
|
95
100
|
this.onOpen = onOpen;
|
|
96
101
|
this.onUpgrade = onUpgrade;
|
|
97
102
|
this.fileSystem = fileSystem;
|
|
98
103
|
this.log = log;
|
|
99
|
-
if (schemaVersion < 1) {
|
|
100
|
-
throw new TypeError(`Invalid schema version received: ${schemaVersion}`);
|
|
101
|
-
}
|
|
102
|
-
this.versionFile = join(this.dataDirectory, DatabaseVersionManager.VERSION_FILE);
|
|
103
|
-
this.currentVersion = new DatabaseVersion(schemaVersion, rollupAddress);
|
|
104
104
|
}
|
|
105
105
|
static async writeVersion(version, dataDir, fileSystem = fs) {
|
|
106
106
|
await fileSystem.mkdir(dataDir, {
|
|
@@ -118,12 +118,16 @@ export const DATABASE_VERSION_FILE_NAME = 'db_version';
|
|
|
118
118
|
*/ async open() {
|
|
119
119
|
// const storedVersion = await DatabaseVersion.readVersion(this.versionFile);
|
|
120
120
|
let storedVersion;
|
|
121
|
+
// a flag to suppress logs about 'resetting the data dir' when starting from an empty state
|
|
122
|
+
let shouldLogDataReset = true;
|
|
121
123
|
try {
|
|
122
124
|
const versionBuf = await this.fileSystem.readFile(this.versionFile);
|
|
123
125
|
storedVersion = DatabaseVersion.fromBuffer(versionBuf);
|
|
124
126
|
} catch (err) {
|
|
125
127
|
if (err && err.code === 'ENOENT') {
|
|
126
128
|
storedVersion = DatabaseVersion.empty();
|
|
129
|
+
// only turn off these logs if the data dir didn't exist before
|
|
130
|
+
shouldLogDataReset = false;
|
|
127
131
|
} else {
|
|
128
132
|
this.log.warn(`Failed to read stored version information: ${err}. Defaulting to empty version`);
|
|
129
133
|
storedVersion = DatabaseVersion.empty();
|
|
@@ -142,13 +146,19 @@ export const DATABASE_VERSION_FILE_NAME = 'db_version';
|
|
|
142
146
|
needsReset = true;
|
|
143
147
|
}
|
|
144
148
|
} else if (cmp !== 0) {
|
|
145
|
-
|
|
149
|
+
if (shouldLogDataReset) {
|
|
150
|
+
this.log.info(`Can't upgrade from version ${storedVersion} to ${this.currentVersion}. Resetting database at ${this.dataDirectory}`);
|
|
151
|
+
}
|
|
146
152
|
needsReset = true;
|
|
147
153
|
}
|
|
148
154
|
} else {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
155
|
+
if (shouldLogDataReset) {
|
|
156
|
+
this.log.warn('Rollup address has changed, resetting data directory', {
|
|
157
|
+
versionFile: this.versionFile,
|
|
158
|
+
storedVersion,
|
|
159
|
+
currentVersion: this.currentVersion
|
|
160
|
+
});
|
|
161
|
+
}
|
|
152
162
|
needsReset = true;
|
|
153
163
|
}
|
|
154
164
|
// Handle reset if needed
|