@typeberry/lib 0.10.0-9e3af22 → 0.11.0-411c828
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/package.json +1 -3
- package/packages/jam/config-node/node-config.d.ts +1 -11
- package/packages/jam/config-node/node-config.d.ts.map +1 -1
- package/packages/jam/config-node/node-config.js +3 -25
- package/packages/jam/config-node/node-config.test.js +3 -7
- package/packages/jam/database/states.d.ts +1 -8
- package/packages/jam/database/states.d.ts.map +1 -1
- package/packages/jam/database/value-refs.d.ts +4 -5
- package/packages/jam/database/value-refs.d.ts.map +1 -1
- package/packages/jam/database-fjall/blocks.d.ts +1 -0
- package/packages/jam/database-fjall/blocks.d.ts.map +1 -1
- package/packages/jam/database-fjall/blocks.js +8 -3
- package/packages/jam/database-fjall/hybrid-states.d.ts +10 -4
- package/packages/jam/database-fjall/hybrid-states.d.ts.map +1 -1
- package/packages/jam/database-fjall/hybrid-states.js +22 -6
- package/packages/jam/database-fjall/root.d.ts +2 -0
- package/packages/jam/database-fjall/root.d.ts.map +1 -1
- package/packages/jam/database-fjall/root.js +8 -1
- package/packages/jam/database-fjall/root.test.d.ts +2 -0
- package/packages/jam/database-fjall/root.test.d.ts.map +1 -0
- package/packages/jam/database-fjall/root.test.js +23 -0
- package/packages/jam/database-fjall/states.d.ts +1 -0
- package/packages/jam/database-fjall/states.d.ts.map +1 -1
- package/packages/jam/database-fjall/states.js +6 -3
- package/packages/jam/node/export.d.ts.map +1 -1
- package/packages/jam/node/export.js +8 -17
- package/packages/jam/node/main-fuzz.d.ts.map +1 -1
- package/packages/jam/node/main-fuzz.js +51 -46
- package/packages/jam/node/main-importer.d.ts +6 -11
- package/packages/jam/node/main-importer.d.ts.map +1 -1
- package/packages/jam/node/main-importer.js +14 -28
- package/packages/jam/node/main.d.ts.map +1 -1
- package/packages/jam/node/main.js +6 -16
- package/packages/jam/rpc/test/e2e-server.d.ts.map +1 -1
- package/packages/jam/rpc/test/e2e-server.js +3 -5
- package/packages/jam/rpc/test/e2e.js +1 -1
- package/packages/jam/state-merkleization/index.d.ts +1 -1
- package/packages/jam/state-merkleization/index.js +1 -1
- package/packages/workers/api-node/config.d.ts +14 -46
- package/packages/workers/api-node/config.d.ts.map +1 -1
- package/packages/workers/api-node/config.js +31 -96
- package/packages/workers/api-node/config.test.js +82 -63
- package/packages/jam/database-lmdb/blocks.d.ts +0 -24
- package/packages/jam/database-lmdb/blocks.d.ts.map +0 -1
- package/packages/jam/database-lmdb/blocks.js +0 -82
- package/packages/jam/database-lmdb/hybrid-states.d.ts +0 -56
- package/packages/jam/database-lmdb/hybrid-states.d.ts.map +0 -1
- package/packages/jam/database-lmdb/hybrid-states.js +0 -149
- package/packages/jam/database-lmdb/hybrid-states.test.d.ts +0 -2
- package/packages/jam/database-lmdb/hybrid-states.test.d.ts.map +0 -1
- package/packages/jam/database-lmdb/hybrid-states.test.js +0 -180
- package/packages/jam/database-lmdb/index.d.ts +0 -5
- package/packages/jam/database-lmdb/index.d.ts.map +0 -1
- package/packages/jam/database-lmdb/index.js +0 -4
- package/packages/jam/database-lmdb/root.d.ts +0 -26
- package/packages/jam/database-lmdb/root.d.ts.map +0 -1
- package/packages/jam/database-lmdb/root.js +0 -53
- package/packages/jam/database-lmdb/states.d.ts +0 -76
- package/packages/jam/database-lmdb/states.d.ts.map +0 -1
- package/packages/jam/database-lmdb/states.js +0 -145
- package/packages/jam/database-lmdb/states.test.d.ts +0 -2
- package/packages/jam/database-lmdb/states.test.d.ts.map +0 -1
- package/packages/jam/database-lmdb/states.test.js +0 -202
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert";
|
|
2
|
-
import * as fs from "node:fs";
|
|
3
|
-
import { afterEach, before, beforeEach, describe, it } from "node:test";
|
|
4
|
-
import { tryAsServiceGas, tryAsServiceId, tryAsTimeSlot } from "#@typeberry/block";
|
|
5
|
-
import { Bytes, BytesBlob } from "#@typeberry/bytes";
|
|
6
|
-
import { SortedSet } from "#@typeberry/collections";
|
|
7
|
-
import { tinyChainSpec } from "#@typeberry/config";
|
|
8
|
-
import { Blake2b, HASH_SIZE } from "#@typeberry/hash";
|
|
9
|
-
import { tryAsU32, tryAsU64 } from "#@typeberry/numbers";
|
|
10
|
-
import { InMemoryState, LookupHistoryItem, PrivilegedServices, ServiceAccountInfo, tryAsLookupHistorySlots, tryAsPerCore, UpdateService, } from "#@typeberry/state";
|
|
11
|
-
import { testState } from "#@typeberry/state/test.utils.js";
|
|
12
|
-
import { StateEntries } from "#@typeberry/state-merkleization";
|
|
13
|
-
import { InMemoryTrie, leafComparator } from "#@typeberry/trie";
|
|
14
|
-
import { getBlake2bTrieHasher } from "#@typeberry/trie/hasher.js";
|
|
15
|
-
import { deepEqual, OK, Result } from "#@typeberry/utils";
|
|
16
|
-
import { LmdbRoot } from "./root.js";
|
|
17
|
-
import { LmdbStates } from "./states.js";
|
|
18
|
-
let blake2bTrieHasher;
|
|
19
|
-
let blake2b;
|
|
20
|
-
before(async () => {
|
|
21
|
-
blake2b = await Blake2b.createHasher();
|
|
22
|
-
blake2bTrieHasher = getBlake2bTrieHasher(blake2b);
|
|
23
|
-
});
|
|
24
|
-
function createTempDir(suffix = "lmdb") {
|
|
25
|
-
return fs.mkdtempSync(`typeberry-${suffix}`);
|
|
26
|
-
}
|
|
27
|
-
describe("LMDB States database", () => {
|
|
28
|
-
let tmpDir = "";
|
|
29
|
-
beforeEach(() => {
|
|
30
|
-
tmpDir = createTempDir();
|
|
31
|
-
});
|
|
32
|
-
afterEach(() => {
|
|
33
|
-
fs.rmSync(tmpDir, {
|
|
34
|
-
recursive: true,
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
const headerHash = Bytes.zero(HASH_SIZE).asOpaque();
|
|
38
|
-
const spec = tinyChainSpec;
|
|
39
|
-
it("should import state and read state", async () => {
|
|
40
|
-
const root = LmdbRoot.new(tmpDir, {});
|
|
41
|
-
const states = LmdbStates.new(spec, blake2b, root);
|
|
42
|
-
try {
|
|
43
|
-
const emptyState = InMemoryState.empty(spec);
|
|
44
|
-
const serialized = StateEntries.serializeInMemory(spec, blake2b, emptyState);
|
|
45
|
-
const emptyRoot = serialized.getRootHash(blake2b);
|
|
46
|
-
// when
|
|
47
|
-
const res = await states.insertInitialState(headerHash, serialized);
|
|
48
|
-
deepEqual(res, Result.ok(OK));
|
|
49
|
-
const newState = states.getState(headerHash);
|
|
50
|
-
assert.ok(newState !== null);
|
|
51
|
-
const newRoot = await states.getStateRoot(newState);
|
|
52
|
-
assert.deepStrictEqual(`${newRoot}`, `${emptyRoot}`);
|
|
53
|
-
deepEqual(InMemoryState.copyFrom(spec, newState, new Map()), emptyState);
|
|
54
|
-
}
|
|
55
|
-
finally {
|
|
56
|
-
await states.close();
|
|
57
|
-
await root.close();
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
it("should update the state", async () => {
|
|
61
|
-
const root = LmdbRoot.new(tmpDir, {});
|
|
62
|
-
const states = LmdbStates.new(spec, blake2b, root);
|
|
63
|
-
try {
|
|
64
|
-
const state = InMemoryState.empty(spec);
|
|
65
|
-
await states.insertInitialState(headerHash, StateEntries.serializeInMemory(spec, blake2b, state));
|
|
66
|
-
const newState = states.getState(headerHash);
|
|
67
|
-
assert.ok(newState !== null);
|
|
68
|
-
const headerHash2 = Bytes.fill(HASH_SIZE, 2).asOpaque();
|
|
69
|
-
const lookupHistory = LookupHistoryItem.new(Bytes.fill(HASH_SIZE, 0xff).asOpaque(), tryAsU32(5), tryAsLookupHistorySlots([]));
|
|
70
|
-
const stateUpdate = {
|
|
71
|
-
timeslot: tryAsTimeSlot(15),
|
|
72
|
-
privilegedServices: PrivilegedServices.create({
|
|
73
|
-
manager: tryAsServiceId(1),
|
|
74
|
-
assigners: tryAsPerCore(new Array(spec.coresCount).fill(tryAsServiceId(2)), spec),
|
|
75
|
-
delegator: tryAsServiceId(3),
|
|
76
|
-
registrar: tryAsServiceId(4),
|
|
77
|
-
autoAccumulateServices: new Map(),
|
|
78
|
-
}),
|
|
79
|
-
updated: new Map([
|
|
80
|
-
[
|
|
81
|
-
tryAsServiceId(1),
|
|
82
|
-
UpdateService.create({
|
|
83
|
-
serviceInfo: ServiceAccountInfo.create({
|
|
84
|
-
codeHash: Bytes.zero(HASH_SIZE).asOpaque(),
|
|
85
|
-
balance: tryAsU64(1_000_000),
|
|
86
|
-
accumulateMinGas: tryAsServiceGas(10_000),
|
|
87
|
-
onTransferMinGas: tryAsServiceGas(5_000),
|
|
88
|
-
storageUtilisationBytes: tryAsU64(1_000),
|
|
89
|
-
gratisStorage: tryAsU64(0),
|
|
90
|
-
storageUtilisationCount: tryAsU32(1),
|
|
91
|
-
created: tryAsTimeSlot(0),
|
|
92
|
-
lastAccumulation: tryAsTimeSlot(0),
|
|
93
|
-
parentService: tryAsServiceId(0),
|
|
94
|
-
}),
|
|
95
|
-
lookupHistory,
|
|
96
|
-
}),
|
|
97
|
-
],
|
|
98
|
-
]),
|
|
99
|
-
};
|
|
100
|
-
// when
|
|
101
|
-
// in-memory state update
|
|
102
|
-
const res1 = state.applyUpdate(stateUpdate);
|
|
103
|
-
deepEqual(res1, Result.ok(OK));
|
|
104
|
-
// on-disk state update
|
|
105
|
-
const res2 = await states.updateAndSetState(headerHash2, newState, stateUpdate);
|
|
106
|
-
deepEqual(res2, Result.ok(OK));
|
|
107
|
-
const updatedState = states.getState(headerHash2);
|
|
108
|
-
assert.ok(updatedState !== null);
|
|
109
|
-
const updatedStateRoot = await states.getStateRoot(updatedState);
|
|
110
|
-
deepEqual(InMemoryState.copyFrom(spec, updatedState, new Map([
|
|
111
|
-
[
|
|
112
|
-
tryAsServiceId(1),
|
|
113
|
-
{
|
|
114
|
-
storageKeys: [],
|
|
115
|
-
preimages: [],
|
|
116
|
-
lookupHistory: [{ hash: lookupHistory.hash, length: lookupHistory.length }],
|
|
117
|
-
},
|
|
118
|
-
],
|
|
119
|
-
])), state);
|
|
120
|
-
assert.strictEqual(`${updatedStateRoot}`, `${StateEntries.serializeInMemory(spec, blake2b, state).getRootHash(blake2b)}`);
|
|
121
|
-
}
|
|
122
|
-
finally {
|
|
123
|
-
await states.close();
|
|
124
|
-
await root.close();
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
it("sorted set should be actual to trie", () => {
|
|
128
|
-
const data = [
|
|
129
|
-
[Bytes.fill(HASH_SIZE, 5), BytesBlob.blobFromString("five")],
|
|
130
|
-
[Bytes.fill(HASH_SIZE, 1), BytesBlob.blobFromString("one")],
|
|
131
|
-
[Bytes.fill(HASH_SIZE, 3), BytesBlob.blobFromString("three")],
|
|
132
|
-
[Bytes.fill(HASH_SIZE, 4), BytesBlob.blobFromString("four")],
|
|
133
|
-
[Bytes.fill(HASH_SIZE, 2), BytesBlob.blobFromString("two")],
|
|
134
|
-
];
|
|
135
|
-
const trie = InMemoryTrie.empty(blake2bTrieHasher);
|
|
136
|
-
for (const [key, val] of data) {
|
|
137
|
-
trie.set(key.asOpaque(), val);
|
|
138
|
-
}
|
|
139
|
-
const set = SortedSet.fromArray(leafComparator, data.map(([key, value]) => {
|
|
140
|
-
return InMemoryTrie.constructLeaf(blake2bTrieHasher, key.asOpaque(), value);
|
|
141
|
-
}));
|
|
142
|
-
deepEqual(Array.from(set), Array.from(SortedSet.fromArray(leafComparator, Array.from(trie.nodes.leaves()))));
|
|
143
|
-
});
|
|
144
|
-
it("should import more complex state", async () => {
|
|
145
|
-
const root = LmdbRoot.new(tmpDir, {});
|
|
146
|
-
const states = LmdbStates.new(spec, blake2b, root);
|
|
147
|
-
try {
|
|
148
|
-
const initialState = testState();
|
|
149
|
-
const initialService = initialState.services.get(tryAsServiceId(0));
|
|
150
|
-
if (initialService === undefined) {
|
|
151
|
-
throw new Error("Expected service in test state!");
|
|
152
|
-
}
|
|
153
|
-
const serialized = StateEntries.serializeInMemory(spec, blake2b, initialState);
|
|
154
|
-
const initialRoot = serialized.getRootHash(blake2b);
|
|
155
|
-
// when
|
|
156
|
-
const res = await states.insertInitialState(headerHash, serialized);
|
|
157
|
-
deepEqual(res, Result.ok(OK));
|
|
158
|
-
const newState = states.getState(headerHash);
|
|
159
|
-
assert.ok(newState !== null);
|
|
160
|
-
const newRoot = await states.getStateRoot(newState);
|
|
161
|
-
assert.deepStrictEqual(`${newRoot}`, `${initialRoot}`);
|
|
162
|
-
deepEqual(InMemoryState.copyFrom(spec, newState, new Map([[initialService.serviceId, initialService.getEntries()]])), initialState);
|
|
163
|
-
}
|
|
164
|
-
finally {
|
|
165
|
-
await states.close();
|
|
166
|
-
await root.close();
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
it("should update more complex entries", async () => {
|
|
170
|
-
const root = LmdbRoot.new(tmpDir, {});
|
|
171
|
-
const states = LmdbStates.new(spec, blake2b, root);
|
|
172
|
-
try {
|
|
173
|
-
const state = testState();
|
|
174
|
-
const initialService = state.services.get(tryAsServiceId(0));
|
|
175
|
-
if (initialService === undefined) {
|
|
176
|
-
throw new Error("Expected service in test state!");
|
|
177
|
-
}
|
|
178
|
-
await states.insertInitialState(headerHash, StateEntries.serializeInMemory(spec, blake2b, state));
|
|
179
|
-
const newState = states.getState(headerHash);
|
|
180
|
-
assert.ok(newState !== null);
|
|
181
|
-
const headerHash2 = Bytes.fill(HASH_SIZE, 2).asOpaque();
|
|
182
|
-
// attempt to update all entries
|
|
183
|
-
const stateUpdate = Object.assign({}, state);
|
|
184
|
-
// when
|
|
185
|
-
// in-memory state update
|
|
186
|
-
const res1 = state.applyUpdate(stateUpdate);
|
|
187
|
-
deepEqual(res1, Result.ok(OK));
|
|
188
|
-
// on-disk state update
|
|
189
|
-
const res2 = await states.updateAndSetState(headerHash2, newState, stateUpdate);
|
|
190
|
-
deepEqual(res2, Result.ok(OK));
|
|
191
|
-
const updatedState = states.getState(headerHash2);
|
|
192
|
-
assert.ok(updatedState !== null);
|
|
193
|
-
const updatedStateRoot = await states.getStateRoot(updatedState);
|
|
194
|
-
deepEqual(InMemoryState.copyFrom(spec, updatedState, new Map([[initialService.serviceId, initialService.getEntries()]])), state);
|
|
195
|
-
assert.strictEqual(`${updatedStateRoot}`, `${StateEntries.serializeInMemory(spec, blake2b, state).getRootHash(blake2b)}`);
|
|
196
|
-
}
|
|
197
|
-
finally {
|
|
198
|
-
await states.close();
|
|
199
|
-
await root.close();
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
});
|