Skip to content

Commit 535b6ad

Browse files
committed
split DatabaseVersion from DatabaseVersionManager to make it buildable for browser targets
1 parent 35e9150 commit 535b6ad

File tree

6 files changed

+93
-85
lines changed

6 files changed

+93
-85
lines changed

yarn-project/kv-store/src/indexeddb/version_management.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EthAddress } from '@aztec/foundation/eth-address';
2-
import { DatabaseVersion } from '@aztec/stdlib/database-version';
2+
import { DatabaseVersion } from '@aztec/stdlib/database-version/version';
33

44
import { expect } from 'chai';
55

yarn-project/kv-store/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EthAddress } from '@aztec/foundation/eth-address';
22
import type { Logger } from '@aztec/foundation/log';
3-
import { DatabaseVersion } from '@aztec/stdlib/database-version';
3+
import { DatabaseVersion } from '@aztec/stdlib/database-version/version';
44

55
import type { AztecAsyncSingleton, AztecSingleton } from './interfaces/singleton.js';
66
import type { AztecAsyncKVStore, AztecKVStore } from './interfaces/store.js';

yarn-project/stdlib/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"./config": "./dest/config/index.js",
5454
"./testing/jest": "./dest/tests/jest.js",
5555
"./database-version": "./dest/database-version/index.js",
56+
"./database-version/version": "./dest/database-version/database_version.js",
5657
"./validators": "./dest/validators/index.js",
5758
"./file-store": "./dest/file-store/index.js",
5859
"./snapshots": "./dest/snapshots/index.js",
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { EthAddress } from '@aztec/foundation/eth-address';
2+
import { jsonParseWithSchema, jsonStringify } from '@aztec/foundation/json-rpc';
3+
4+
import { z } from 'zod';
5+
6+
/**
7+
* Symbol for Node.js custom inspect. Using Symbol.for() is the documented way to
8+
* reference this without importing node:util. In browsers, objects with this symbol
9+
* simply won't have custom inspect behavior (which is fine).
10+
* @see https://nodejs.org/api/util.html#utilinspectcustom
11+
*/
12+
const inspectCustomSymbol = Symbol.for('nodejs.util.inspect.custom');
13+
14+
/**
15+
* Represents a version record for storing in a version file.
16+
*/
17+
export class DatabaseVersion {
18+
constructor(
19+
/** The version of the data on disk. Used to perform upgrades */
20+
public readonly schemaVersion: number,
21+
/** The rollup the data pertains to */
22+
public readonly rollupAddress: EthAddress,
23+
) {}
24+
25+
public toBuffer(): Buffer {
26+
return Buffer.from(jsonStringify(this));
27+
}
28+
29+
public static fromBuffer(buf: Buffer): DatabaseVersion {
30+
try {
31+
return jsonParseWithSchema(buf.toString('utf-8'), DatabaseVersion.schema);
32+
} catch (err) {
33+
throw new Error(`Failed to deserialize version information: ${err}`, { cause: err });
34+
}
35+
}
36+
37+
/**
38+
* Compares two versions. If the rollups addresses are different then it returns undefined
39+
*/
40+
public cmp(other: DatabaseVersion): undefined | -1 | 0 | 1 {
41+
if (this.rollupAddress.equals(other.rollupAddress)) {
42+
if (this.schemaVersion < other.schemaVersion) {
43+
return -1;
44+
} else if (this.schemaVersion > other.schemaVersion) {
45+
return 1;
46+
} else {
47+
return 0;
48+
}
49+
}
50+
return undefined;
51+
}
52+
53+
/**
54+
* Checks if two versions exactly match
55+
*/
56+
public equals(other: DatabaseVersion): boolean {
57+
return this.cmp(other) === 0;
58+
}
59+
60+
/**
61+
* Returns the schema for this class
62+
*/
63+
static get schema() {
64+
return z
65+
.object({
66+
schemaVersion: z.number(),
67+
rollupAddress: EthAddress.schema,
68+
})
69+
.transform(({ schemaVersion, rollupAddress }) => new DatabaseVersion(schemaVersion, rollupAddress));
70+
}
71+
72+
/** Allows for better introspection in Node.js console. Ignored in browser envs. */
73+
public [inspectCustomSymbol](): string {
74+
return this.toString();
75+
}
76+
77+
public toString(): string {
78+
return `DatabaseVersion{schemaVersion=${this.schemaVersion},rollupAddress=${this.rollupAddress}"}`;
79+
}
80+
81+
/**
82+
* Returns an empty instance
83+
*/
84+
static empty() {
85+
return new DatabaseVersion(0, EthAddress.ZERO);
86+
}
87+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './database_version.js';
12
export * from './version_manager.js';

yarn-project/stdlib/src/database-version/version_manager.ts

Lines changed: 2 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,12 @@
11
import { EthAddress } from '@aztec/foundation/eth-address';
2-
import { jsonParseWithSchema, jsonStringify } from '@aztec/foundation/json-rpc';
32
import { type Logger, createLogger } from '@aztec/foundation/log';
43

54
import fs from 'fs/promises';
65
import { join } from 'path';
7-
import { z } from 'zod';
86

9-
/**
10-
* Symbol for Node.js custom inspect. Using Symbol.for() is the documented way to
11-
* reference this without importing node:util. In browsers, objects with this symbol
12-
* simply won't have custom inspect behavior (which is fine).
13-
* @see https://nodejs.org/api/util.html#utilinspectcustom
14-
*/
15-
const inspectCustomSymbol = Symbol.for('nodejs.util.inspect.custom');
16-
17-
/**
18-
* Represents a version record for storing in a version file.
19-
*/
20-
export class DatabaseVersion {
21-
constructor(
22-
/** The version of the data on disk. Used to perform upgrades */
23-
public readonly schemaVersion: number,
24-
/** The rollup the data pertains to */
25-
public readonly rollupAddress: EthAddress,
26-
) {}
27-
28-
public toBuffer(): Buffer {
29-
return Buffer.from(jsonStringify(this));
30-
}
31-
32-
public static fromBuffer(buf: Buffer): DatabaseVersion {
33-
try {
34-
return jsonParseWithSchema(buf.toString('utf-8'), DatabaseVersion.schema);
35-
} catch (err) {
36-
throw new Error(`Failed to deserialize version information: ${err}`, { cause: err });
37-
}
38-
}
7+
import { DatabaseVersion } from './database_version.js';
398

40-
/**
41-
* Compares two versions. If the rollups addresses are different then it returns undefined
42-
*/
43-
public cmp(other: DatabaseVersion): undefined | -1 | 0 | 1 {
44-
if (this.rollupAddress.equals(other.rollupAddress)) {
45-
if (this.schemaVersion < other.schemaVersion) {
46-
return -1;
47-
} else if (this.schemaVersion > other.schemaVersion) {
48-
return 1;
49-
} else {
50-
return 0;
51-
}
52-
}
53-
return undefined;
54-
}
55-
56-
/**
57-
* Checks if two versions exactly match
58-
*/
59-
public equals(other: DatabaseVersion): boolean {
60-
return this.cmp(other) === 0;
61-
}
62-
63-
/**
64-
* Returns the schema for this class
65-
*/
66-
static get schema() {
67-
return z
68-
.object({
69-
schemaVersion: z.number(),
70-
rollupAddress: EthAddress.schema,
71-
})
72-
.transform(({ schemaVersion, rollupAddress }) => new DatabaseVersion(schemaVersion, rollupAddress));
73-
}
74-
75-
/** Allows for better introspection in Node.js console. Ignored in browser envs. */
76-
public [inspectCustomSymbol](): string {
77-
return this.toString();
78-
}
79-
80-
public toString(): string {
81-
return `DatabaseVersion{schemaVersion=${this.schemaVersion},rollupAddress=${this.rollupAddress}"}`;
82-
}
83-
84-
/**
85-
* Returns an empty instance
86-
*/
87-
static empty() {
88-
return new DatabaseVersion(0, EthAddress.ZERO);
89-
}
90-
}
9+
export { DatabaseVersion };
9110

9211
export type DatabaseVersionManagerFs = Pick<typeof fs, 'readFile' | 'writeFile' | 'rm' | 'mkdir'>;
9312

0 commit comments

Comments
 (0)