@midnight-ntwrk/midnight-js-protocol 5.0.0-beta.7 → 5.0.0-beta.8

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.
@@ -0,0 +1,121 @@
1
+ /**
2
+ * The two ledger runtimes midnight-js can talk to. `v8` backs the node 1.x
3
+ * line; `v9` backs the 2.x line. This is a closed, exhaustive set — see
4
+ * `protocolVersionToLedger` (`../version.ts`) for how a raw `protocolVersion`
5
+ * integer maps onto it.
6
+ *
7
+ * @see {@link SharedTableDiscipline} for why the array is frozen.
8
+ * @see {@link ModuleGraphAndLazyLoading} for why the constant is declared in
9
+ * this leaf module and re-exported by `../version.ts`.
10
+ */
11
+ declare const LEDGER_VERSIONS: readonly ["v8", "v9"];
12
+ type LedgerVersion = (typeof LEDGER_VERSIONS)[number];
13
+ /**
14
+ * The era whose objects this build hands out live, through `./ledger`.
15
+ *
16
+ * A protocol fact rather than a consumer's choice: it is decided by which
17
+ * ledger the package links eagerly, and every other era is reached lazily and
18
+ * crosses package boundaries as bytes. Declared here so that no package
19
+ * downstream restates "which era is now" as a literal of its own.
20
+ *
21
+ * The type is the single literal, not {@link LedgerVersion}, so a value typed
22
+ * by it DISCRIMINATES — the same discipline `CurrentPipelineEra` follows in
23
+ * `midnight-js-contracts`.
24
+ */
25
+ declare const CURRENT_LEDGER_VERSION: "v9";
26
+ type CurrentLedgerVersion = typeof CURRENT_LEDGER_VERSION;
27
+ /**
28
+ * Every era this build still speaks but does not run live — the eras that
29
+ * cross a package boundary as serialized bytes.
30
+ *
31
+ * Defined as the complement of {@link CURRENT_LEDGER_VERSION}, so a further
32
+ * era joins this set by being added to {@link LEDGER_VERSIONS} and nothing
33
+ * else. The value list below is checked against that complement at build time.
34
+ */
35
+ type RetainedLedgerVersion = Exclude<LedgerVersion, CurrentLedgerVersion>;
36
+ declare const RETAINED_LEDGER_VERSIONS: readonly ["v8"];
37
+
38
+ /**
39
+ * Which call path asked for a ledger version:
40
+ * - `'read'` — the version was taken off an existing record.
41
+ * - `'construct'` — the version was chosen to build something new against the
42
+ * network's current head.
43
+ */
44
+ type VersionResolutionPath = 'read' | 'construct';
45
+
46
+ /**
47
+ * Anything that can report the network's current head protocol version —
48
+ * typically an indexer or node client. Consumed by {@link networkHeadVersion}.
49
+ */
50
+ interface ProtocolVersionSource {
51
+ /**
52
+ * @returns The network's current head `protocolVersion` integer.
53
+ */
54
+ queryLatestProtocolVersion(): Promise<number>;
55
+ }
56
+ /**
57
+ * Any record carrying a raw `protocolVersion` integer field, e.g. a
58
+ * transaction or block already read from the indexer. Consumed by
59
+ * {@link versionOfRecord}.
60
+ */
61
+ interface VersionedRecord {
62
+ readonly protocolVersion: number;
63
+ }
64
+ /**
65
+ * Maps a raw `protocolVersion` integer (as returned by the indexer or node)
66
+ * onto the ledger runtime it corresponds to.
67
+ *
68
+ * | protocolVersion range | node version | ledger |
69
+ * | --------------------- | ------------ | ------ |
70
+ * | 1_000_000 – 1_999_999 | 1.x | v8 |
71
+ * | 2_000_000 – 2_999_999 | 2.x | v9 |
72
+ *
73
+ * Most call sites should not call this directly. Prefer
74
+ * {@link versionOfRecord} for a `protocolVersion` read off an existing
75
+ * indexer/node record, or {@link networkHeadVersion} for the network's
76
+ * current head version — both tag the resulting error with the correct
77
+ * `path` automatically. Pass `path` explicitly here only when neither helper
78
+ * fits the call site.
79
+ *
80
+ * @param protocolVersion The raw integer read from an indexer or node record.
81
+ * @param path Which resolution path a failure is attributed to. Defaults to
82
+ * `'construct'`, and decides which of the two error codes a failure carries.
83
+ * @returns The {@link LedgerVersion} that `protocolVersion`'s node major maps
84
+ * onto.
85
+ * @throws {@link UnknownProtocolVersionError} with `reason: 'malformed'` when
86
+ * `protocolVersion` is not a non-negative integer, and with `reason: 'unknown'`
87
+ * when it is a well-formed integer outside every range above.
88
+ * @see {@link SharedTableDiscipline}
89
+ */
90
+ declare const protocolVersionToLedger: (protocolVersion: number, path?: VersionResolutionPath) => LedgerVersion;
91
+ /**
92
+ * Resolves the ledger version for a record's `protocolVersion` field (e.g. a
93
+ * transaction or block already read from the indexer).
94
+ *
95
+ * @param record Any object carrying a raw `protocolVersion` integer field.
96
+ * @returns The {@link LedgerVersion} that record was written under.
97
+ * @throws {@link UnknownProtocolVersionError} tagged with the `read` path, on
98
+ * the same two conditions as {@link protocolVersionToLedger}.
99
+ */
100
+ declare const versionOfRecord: (record: VersionedRecord) => LedgerVersion;
101
+ /**
102
+ * Queries `source` for the network's current head protocol version and
103
+ * resolves it to a {@link LedgerVersion}.
104
+ *
105
+ * The source is expected to read the network on every call. This is the
106
+ * construct path: the era being resolved is the one a transaction built now
107
+ * will land in, and a stale reading is wrong exactly at the fork boundary,
108
+ * where that question matters. `PublicDataProvider.queryLatestProtocolVersion`
109
+ * states the same prohibition as a requirement on its implementations; this
110
+ * parameter is a structural type, so nothing here can enforce it. See ADR 0007.
111
+ *
112
+ * @param source The indexer or node client to ask for the head version.
113
+ * @returns A promise for the {@link LedgerVersion} at the network head.
114
+ * @throws {@link UnknownProtocolVersionError} tagged with the `construct`
115
+ * path, on the same two conditions as {@link protocolVersionToLedger}. A
116
+ * rejection from `source.queryLatestProtocolVersion()` propagates unchanged.
117
+ */
118
+ declare const networkHeadVersion: (source: ProtocolVersionSource) => Promise<LedgerVersion>;
119
+
120
+ export { CURRENT_LEDGER_VERSION, LEDGER_VERSIONS, RETAINED_LEDGER_VERSIONS, networkHeadVersion, protocolVersionToLedger, versionOfRecord };
121
+ export type { CurrentLedgerVersion, LedgerVersion, ProtocolVersionSource, RetainedLedgerVersion, VersionedRecord };
@@ -0,0 +1,132 @@
1
+ import { UnknownProtocolVersionError } from './errors.js';
2
+
3
+ /*
4
+ * This file is part of midnight-js.
5
+ * Copyright (C) Midnight Foundation
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * You may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ /**
18
+ * The two ledger runtimes midnight-js can talk to. `v8` backs the node 1.x
19
+ * line; `v9` backs the 2.x line. This is a closed, exhaustive set — see
20
+ * `protocolVersionToLedger` (`../version.ts`) for how a raw `protocolVersion`
21
+ * integer maps onto it.
22
+ *
23
+ * @see {@link SharedTableDiscipline} for why the array is frozen.
24
+ * @see {@link ModuleGraphAndLazyLoading} for why the constant is declared in
25
+ * this leaf module and re-exported by `../version.ts`.
26
+ */
27
+ const LEDGER_VERSIONS = Object.freeze(['v8', 'v9']);
28
+ /**
29
+ * The era whose objects this build hands out live, through `./ledger`.
30
+ *
31
+ * A protocol fact rather than a consumer's choice: it is decided by which
32
+ * ledger the package links eagerly, and every other era is reached lazily and
33
+ * crosses package boundaries as bytes. Declared here so that no package
34
+ * downstream restates "which era is now" as a literal of its own.
35
+ *
36
+ * The type is the single literal, not {@link LedgerVersion}, so a value typed
37
+ * by it DISCRIMINATES — the same discipline `CurrentPipelineEra` follows in
38
+ * `midnight-js-contracts`.
39
+ */
40
+ const CURRENT_LEDGER_VERSION = 'v9';
41
+ const RETAINED_LEDGER_VERSIONS = Object.freeze(['v8']);
42
+
43
+ /*
44
+ * This file is part of midnight-js.
45
+ * Copyright (C) Midnight Foundation
46
+ * SPDX-License-Identifier: Apache-2.0
47
+ * Licensed under the Apache License, Version 2.0 (the "License");
48
+ * You may not use this file except in compliance with the License.
49
+ * You may obtain a copy of the License at
50
+ * http://www.apache.org/licenses/LICENSE-2.0
51
+ * Unless required by applicable law or agreed to in writing, software
52
+ * distributed under the License is distributed on an "AS IS" BASIS,
53
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
54
+ * See the License for the specific language governing permissions and
55
+ * limitations under the License.
56
+ */
57
+ // Deliberately narrower than the indexer's table: do not "restore" the 0.x
58
+ // major to match it without a caller needing it — see SharedTableDiscipline.
59
+ const NODE_MAJOR_TO_LEDGER = {
60
+ 1: 'v8',
61
+ 2: 'v9'
62
+ };
63
+ // A typed function rather than a direct index, because a `number`-typed key
64
+ // cannot index the `as const` table — see SharedTableDiscipline.
65
+ const lookupLedger = (table, key) => table[key];
66
+ /**
67
+ * Maps a raw `protocolVersion` integer (as returned by the indexer or node)
68
+ * onto the ledger runtime it corresponds to.
69
+ *
70
+ * | protocolVersion range | node version | ledger |
71
+ * | --------------------- | ------------ | ------ |
72
+ * | 1_000_000 – 1_999_999 | 1.x | v8 |
73
+ * | 2_000_000 – 2_999_999 | 2.x | v9 |
74
+ *
75
+ * Most call sites should not call this directly. Prefer
76
+ * {@link versionOfRecord} for a `protocolVersion` read off an existing
77
+ * indexer/node record, or {@link networkHeadVersion} for the network's
78
+ * current head version — both tag the resulting error with the correct
79
+ * `path` automatically. Pass `path` explicitly here only when neither helper
80
+ * fits the call site.
81
+ *
82
+ * @param protocolVersion The raw integer read from an indexer or node record.
83
+ * @param path Which resolution path a failure is attributed to. Defaults to
84
+ * `'construct'`, and decides which of the two error codes a failure carries.
85
+ * @returns The {@link LedgerVersion} that `protocolVersion`'s node major maps
86
+ * onto.
87
+ * @throws {@link UnknownProtocolVersionError} with `reason: 'malformed'` when
88
+ * `protocolVersion` is not a non-negative integer, and with `reason: 'unknown'`
89
+ * when it is a well-formed integer outside every range above.
90
+ * @see {@link SharedTableDiscipline}
91
+ */
92
+ const protocolVersionToLedger = (protocolVersion, path = 'construct') => {
93
+ if (!Number.isInteger(protocolVersion) || protocolVersion < 0) {
94
+ throw new UnknownProtocolVersionError(protocolVersion, path, 'malformed');
95
+ }
96
+ const ledger = lookupLedger(NODE_MAJOR_TO_LEDGER, Math.floor(protocolVersion / 1_000_000));
97
+ if (ledger === undefined) {
98
+ throw new UnknownProtocolVersionError(protocolVersion, path, 'unknown');
99
+ }
100
+ return ledger;
101
+ };
102
+ /**
103
+ * Resolves the ledger version for a record's `protocolVersion` field (e.g. a
104
+ * transaction or block already read from the indexer).
105
+ *
106
+ * @param record Any object carrying a raw `protocolVersion` integer field.
107
+ * @returns The {@link LedgerVersion} that record was written under.
108
+ * @throws {@link UnknownProtocolVersionError} tagged with the `read` path, on
109
+ * the same two conditions as {@link protocolVersionToLedger}.
110
+ */
111
+ const versionOfRecord = (record) => protocolVersionToLedger(record.protocolVersion, 'read');
112
+ /**
113
+ * Queries `source` for the network's current head protocol version and
114
+ * resolves it to a {@link LedgerVersion}.
115
+ *
116
+ * The source is expected to read the network on every call. This is the
117
+ * construct path: the era being resolved is the one a transaction built now
118
+ * will land in, and a stale reading is wrong exactly at the fork boundary,
119
+ * where that question matters. `PublicDataProvider.queryLatestProtocolVersion`
120
+ * states the same prohibition as a requirement on its implementations; this
121
+ * parameter is a structural type, so nothing here can enforce it. See ADR 0007.
122
+ *
123
+ * @param source The indexer or node client to ask for the head version.
124
+ * @returns A promise for the {@link LedgerVersion} at the network head.
125
+ * @throws {@link UnknownProtocolVersionError} tagged with the `construct`
126
+ * path, on the same two conditions as {@link protocolVersionToLedger}. A
127
+ * rejection from `source.queryLatestProtocolVersion()` propagates unchanged.
128
+ */
129
+ const networkHeadVersion = async (source) => protocolVersionToLedger(await source.queryLatestProtocolVersion(), 'construct');
130
+
131
+ export { CURRENT_LEDGER_VERSION, LEDGER_VERSIONS, RETAINED_LEDGER_VERSIONS, networkHeadVersion, protocolVersionToLedger, versionOfRecord };
132
+ //# sourceMappingURL=version.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.js","sources":["../src/lib/shared/ledger-version.ts","../src/version.ts"],"sourcesContent":[null,null],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;AAaG;AAEH;;;;;;;;;AASG;AACI,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,IAAI,CAAU;AAGlE;;;;;;;;;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
  "type": "module",
5
5
  "description": "Protocol type re-exports for midnight-js framework",
6
6
  "types": "dist/index.d.ts",
@@ -9,10 +9,30 @@
9
9
  "types": "./dist/index.d.ts",
10
10
  "default": "./dist/index.js"
11
11
  },
12
+ "./errors": {
13
+ "types": "./dist/errors.d.ts",
14
+ "default": "./dist/errors.js"
15
+ },
16
+ "./version": {
17
+ "types": "./dist/version.d.ts",
18
+ "default": "./dist/version.js"
19
+ },
20
+ "./prove": {
21
+ "types": "./dist/prove.d.ts",
22
+ "default": "./dist/prove.js"
23
+ },
12
24
  "./ledger": {
13
25
  "types": "./dist/ledger.d.ts",
14
26
  "default": "./dist/ledger.js"
15
27
  },
28
+ "./v8": {
29
+ "types": "./dist/v8.d.ts",
30
+ "default": "./dist/v8.js"
31
+ },
32
+ "./engine": {
33
+ "types": "./dist/engine.d.ts",
34
+ "default": "./dist/engine.js"
35
+ },
16
36
  "./compact-runtime": {
17
37
  "types": "./dist/compact-runtime.d.ts",
18
38
  "default": "./dist/compact-runtime.js"
@@ -64,15 +84,19 @@
64
84
  ],
65
85
  "dependencies": {
66
86
  "@midnight-ntwrk/compact-js": "2.5.5-rc.8",
67
- "@midnight-ntwrk/compact-runtime": "0.19.0-rc.0",
87
+ "@midnight-ntwrk/compact-runtime": "0.19.0",
88
+ "@midnight-ntwrk/onchain-runtime-v3": "3.1.1",
68
89
  "@midnight-ntwrk/platform-js": "3.0.0",
69
- "@midnightntwrk/ledger-v9": "1.0.0-rc.3",
70
- "@midnightntwrk/onchain-runtime-v4": "4.0.0-rc.3"
90
+ "@midnightntwrk/ledger-v8": "8.1.2",
91
+ "@midnightntwrk/ledger-v9": "1.0.0-rc.4",
92
+ "@midnightntwrk/onchain-runtime-v4": "4.0.0-rc.3",
93
+ "compact-runtime-ledger8": "npm:@midnight-ntwrk/compact-runtime@0.16.0"
71
94
  },
72
95
  "devDependencies": {
73
96
  "@rollup/plugin-typescript": "^12.3.0",
74
97
  "@vitest/coverage-v8": "^4.1.10",
75
98
  "eslint": "^10.8.1",
99
+ "onchain-runtime-v3-test-only": "npm:@midnight-ntwrk/onchain-runtime-v3@3.1.0",
76
100
  "rollup": "^4.60.4",
77
101
  "rollup-plugin-dts": "^6.4.1",
78
102
  "typescript": "^6.0.3",