@feelyourprotocol/binarytree 8141.0.0
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/LICENSE +22 -0
- package/README.md +46 -0
- package/dist/cjs/binaryTree.d.ts +167 -0
- package/dist/cjs/binaryTree.d.ts.map +1 -0
- package/dist/cjs/binaryTree.js +606 -0
- package/dist/cjs/binaryTree.js.map +1 -0
- package/dist/cjs/constructors.d.ts +4 -0
- package/dist/cjs/constructors.d.ts.map +1 -0
- package/dist/cjs/constructors.js +44 -0
- package/dist/cjs/constructors.js.map +1 -0
- package/dist/cjs/db/checkpoint.d.ts +87 -0
- package/dist/cjs/db/checkpoint.d.ts.map +1 -0
- package/dist/cjs/db/checkpoint.js +257 -0
- package/dist/cjs/db/checkpoint.js.map +1 -0
- package/dist/cjs/db/index.d.ts +2 -0
- package/dist/cjs/db/index.d.ts.map +1 -0
- package/dist/cjs/db/index.js +18 -0
- package/dist/cjs/db/index.js.map +1 -0
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/node/index.d.ts +4 -0
- package/dist/cjs/node/index.d.ts.map +1 -0
- package/dist/cjs/node/index.js +20 -0
- package/dist/cjs/node/index.js.map +1 -0
- package/dist/cjs/node/internalNode.d.ts +37 -0
- package/dist/cjs/node/internalNode.d.ts.map +1 -0
- package/dist/cjs/node/internalNode.js +92 -0
- package/dist/cjs/node/internalNode.js.map +1 -0
- package/dist/cjs/node/stemNode.d.ts +34 -0
- package/dist/cjs/node/stemNode.d.ts.map +1 -0
- package/dist/cjs/node/stemNode.js +75 -0
- package/dist/cjs/node/stemNode.js.map +1 -0
- package/dist/cjs/node/types.d.ts +35 -0
- package/dist/cjs/node/types.d.ts.map +1 -0
- package/dist/cjs/node/types.js +9 -0
- package/dist/cjs/node/types.js.map +1 -0
- package/dist/cjs/node/util.d.ts +9 -0
- package/dist/cjs/node/util.d.ts.map +1 -0
- package/dist/cjs/node/util.js +40 -0
- package/dist/cjs/node/util.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/proof.d.ts +16 -0
- package/dist/cjs/proof.d.ts.map +1 -0
- package/dist/cjs/proof.js +49 -0
- package/dist/cjs/proof.js.map +1 -0
- package/dist/cjs/types.d.ts +45 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +6 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/util.d.ts +17 -0
- package/dist/cjs/util.d.ts.map +1 -0
- package/dist/cjs/util.js +71 -0
- package/dist/cjs/util.js.map +1 -0
- package/dist/esm/binaryTree.d.ts +167 -0
- package/dist/esm/binaryTree.d.ts.map +1 -0
- package/dist/esm/binaryTree.js +602 -0
- package/dist/esm/binaryTree.js.map +1 -0
- package/dist/esm/constructors.d.ts +4 -0
- package/dist/esm/constructors.d.ts.map +1 -0
- package/dist/esm/constructors.js +41 -0
- package/dist/esm/constructors.js.map +1 -0
- package/dist/esm/db/checkpoint.d.ts +87 -0
- package/dist/esm/db/checkpoint.d.ts.map +1 -0
- package/dist/esm/db/checkpoint.js +253 -0
- package/dist/esm/db/checkpoint.js.map +1 -0
- package/dist/esm/db/index.d.ts +2 -0
- package/dist/esm/db/index.d.ts.map +1 -0
- package/dist/esm/db/index.js +2 -0
- package/dist/esm/db/index.js.map +1 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/node/index.d.ts +4 -0
- package/dist/esm/node/index.d.ts.map +1 -0
- package/dist/esm/node/index.js +4 -0
- package/dist/esm/node/index.js.map +1 -0
- package/dist/esm/node/internalNode.d.ts +37 -0
- package/dist/esm/node/internalNode.d.ts.map +1 -0
- package/dist/esm/node/internalNode.js +88 -0
- package/dist/esm/node/internalNode.js.map +1 -0
- package/dist/esm/node/stemNode.d.ts +34 -0
- package/dist/esm/node/stemNode.d.ts.map +1 -0
- package/dist/esm/node/stemNode.js +71 -0
- package/dist/esm/node/stemNode.js.map +1 -0
- package/dist/esm/node/types.d.ts +35 -0
- package/dist/esm/node/types.d.ts.map +1 -0
- package/dist/esm/node/types.js +6 -0
- package/dist/esm/node/types.js.map +1 -0
- package/dist/esm/node/util.d.ts +9 -0
- package/dist/esm/node/util.d.ts.map +1 -0
- package/dist/esm/node/util.js +33 -0
- package/dist/esm/node/util.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/proof.d.ts +16 -0
- package/dist/esm/proof.d.ts.map +1 -0
- package/dist/esm/proof.js +45 -0
- package/dist/esm/proof.js.map +1 -0
- package/dist/esm/types.d.ts +45 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +3 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/util.d.ts +17 -0
- package/dist/esm/util.d.ts.map +1 -0
- package/dist/esm/util.js +66 -0
- package/dist/esm/util.js.map +1 -0
- package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.prod.esm.tsbuildinfo +1 -0
- package/package.json +77 -0
- package/src/binaryTree.ts +742 -0
- package/src/constructors.ts +50 -0
- package/src/db/checkpoint.ts +297 -0
- package/src/db/index.ts +1 -0
- package/src/index.ts +6 -0
- package/src/node/index.ts +3 -0
- package/src/node/internalNode.ts +112 -0
- package/src/node/stemNode.ts +87 -0
- package/src/node/types.ts +41 -0
- package/src/node/util.ts +38 -0
- package/src/proof.ts +54 -0
- package/src/types.ts +58 -0
- package/src/util.ts +80 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { KeyEncoding, MapDB, ValueEncoding, bytesToHex, unprefixedHexToBytes, } from '@feelyourprotocol/util';
|
|
2
|
+
import { blake3 } from '@noble/hashes/blake3.js';
|
|
3
|
+
import { BinaryTree } from "./binaryTree.js";
|
|
4
|
+
import { ROOT_DB_KEY } from "./types.js";
|
|
5
|
+
export async function createBinaryTree(opts) {
|
|
6
|
+
const key = bytesToHex(ROOT_DB_KEY);
|
|
7
|
+
// Provide sensible default options
|
|
8
|
+
const parsedOptions = {
|
|
9
|
+
...opts,
|
|
10
|
+
db: opts?.db ?? new MapDB(),
|
|
11
|
+
useRootPersistence: opts?.useRootPersistence ?? false,
|
|
12
|
+
cacheSize: opts?.cacheSize ?? 0,
|
|
13
|
+
hashFunction: opts?.hashFunction ?? blake3,
|
|
14
|
+
};
|
|
15
|
+
if (parsedOptions.useRootPersistence === true) {
|
|
16
|
+
if (parsedOptions.root === undefined) {
|
|
17
|
+
const root = await parsedOptions.db.get(key, {
|
|
18
|
+
keyEncoding: KeyEncoding.Bytes,
|
|
19
|
+
valueEncoding: ValueEncoding.Bytes,
|
|
20
|
+
});
|
|
21
|
+
if (typeof root === 'string') {
|
|
22
|
+
parsedOptions.root = unprefixedHexToBytes(root);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
parsedOptions.root = root;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
await parsedOptions.db.put(key, parsedOptions.root, {
|
|
30
|
+
keyEncoding: KeyEncoding.Bytes,
|
|
31
|
+
valueEncoding: ValueEncoding.Bytes,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const tree = new BinaryTree(parsedOptions);
|
|
36
|
+
// If the root node does not exist, initialize the empty root node
|
|
37
|
+
if (parsedOptions.root === undefined)
|
|
38
|
+
await tree.createRootNode();
|
|
39
|
+
return tree;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=constructors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constructors.js","sourceRoot":"","sources":["../../src/constructors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,KAAK,EACL,aAAa,EACb,UAAU,EACV,oBAAoB,GACrB,MAAM,kBAAkB,CAAA;AACzB,OAAO,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ED,MAAM,IAAI,GAAG,IAAI,UAAU,CAAC,aAAa,CAAC,CAAA;IAC1C,kEAAkE;IAClE,IAAI,aAAa,CAAC,IAAI,KAAK,SAAS;QAAE,MAAM,IAAI,CAAC,cAAc,EAAE,CAAA;IACjE,OAAO,IAAI,CAAA;AACb,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
import type { BatchDBOp, DB } from '@feelyourprotocol/util';
|
|
3
|
+
import type { Checkpoint, CheckpointDBOpts } from '../types.ts';
|
|
4
|
+
/**
|
|
5
|
+
* DB is a thin wrapper around the underlying levelup db,
|
|
6
|
+
* which validates inputs and sets encoding type.
|
|
7
|
+
*/
|
|
8
|
+
export declare class CheckpointDB implements DB {
|
|
9
|
+
checkpoints: Checkpoint[];
|
|
10
|
+
db: DB<string, string | Uint8Array>;
|
|
11
|
+
readonly cacheSize: number;
|
|
12
|
+
private readonly valueEncoding;
|
|
13
|
+
protected _cache?: LRUCache<string, Uint8Array>;
|
|
14
|
+
_stats: {
|
|
15
|
+
cache: {
|
|
16
|
+
reads: number;
|
|
17
|
+
hits: number;
|
|
18
|
+
writes: number;
|
|
19
|
+
};
|
|
20
|
+
db: {
|
|
21
|
+
reads: number;
|
|
22
|
+
hits: number;
|
|
23
|
+
writes: number;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Initialize a DB instance.
|
|
28
|
+
*/
|
|
29
|
+
constructor(opts: CheckpointDBOpts);
|
|
30
|
+
/**
|
|
31
|
+
* Flush the checkpoints and use the given checkpoints instead.
|
|
32
|
+
* @param {Checkpoint[]} checkpoints
|
|
33
|
+
*/
|
|
34
|
+
setCheckpoints(checkpoints: Checkpoint[]): void;
|
|
35
|
+
/**
|
|
36
|
+
* Is the DB during a checkpoint phase?
|
|
37
|
+
*/
|
|
38
|
+
hasCheckpoints(): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Adds a new checkpoint to the stack
|
|
41
|
+
* @param root
|
|
42
|
+
*/
|
|
43
|
+
checkpoint(root: Uint8Array): void;
|
|
44
|
+
/**
|
|
45
|
+
* Commits the latest checkpoint
|
|
46
|
+
*/
|
|
47
|
+
commit(): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Reverts the latest checkpoint
|
|
50
|
+
*/
|
|
51
|
+
revert(): Promise<Uint8Array<ArrayBufferLike>>;
|
|
52
|
+
/**
|
|
53
|
+
* @inheritDoc
|
|
54
|
+
*/
|
|
55
|
+
get(key: Uint8Array): Promise<Uint8Array | undefined>;
|
|
56
|
+
/**
|
|
57
|
+
* @inheritDoc
|
|
58
|
+
*/
|
|
59
|
+
put(key: Uint8Array, value: Uint8Array): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* @inheritDoc
|
|
62
|
+
*/
|
|
63
|
+
del(key: Uint8Array): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* @inheritDoc
|
|
66
|
+
*/
|
|
67
|
+
batch(opStack: BatchDBOp[]): Promise<void>;
|
|
68
|
+
stats(reset?: boolean): {
|
|
69
|
+
size: number;
|
|
70
|
+
cache: {
|
|
71
|
+
reads: number;
|
|
72
|
+
hits: number;
|
|
73
|
+
writes: number;
|
|
74
|
+
};
|
|
75
|
+
db: {
|
|
76
|
+
reads: number;
|
|
77
|
+
hits: number;
|
|
78
|
+
writes: number;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* @inheritDoc
|
|
83
|
+
*/
|
|
84
|
+
shallowCopy(): CheckpointDB;
|
|
85
|
+
open(): Promise<void>;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=checkpoint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkpoint.d.ts","sourceRoot":"","sources":["../../../src/db/checkpoint.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAEpC,OAAO,KAAK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Hex, unprefixedHexToBytes, } from '@feelyourprotocol/util';
|
|
2
|
+
import { LRUCache } from 'lru-cache';
|
|
3
|
+
/**
|
|
4
|
+
* DB is a thin wrapper around the underlying levelup db,
|
|
5
|
+
* which validates inputs and sets encoding type.
|
|
6
|
+
*/
|
|
7
|
+
export class CheckpointDB {
|
|
8
|
+
/**
|
|
9
|
+
* Initialize a DB instance.
|
|
10
|
+
*/
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
// protected _cache?: LRUCache<string, Uint8Array | undefined>
|
|
13
|
+
this._stats = {
|
|
14
|
+
cache: {
|
|
15
|
+
reads: 0,
|
|
16
|
+
hits: 0,
|
|
17
|
+
writes: 0,
|
|
18
|
+
},
|
|
19
|
+
db: {
|
|
20
|
+
reads: 0,
|
|
21
|
+
hits: 0,
|
|
22
|
+
writes: 0,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
this.db = opts.db;
|
|
26
|
+
this.cacheSize = opts.cacheSize ?? 0;
|
|
27
|
+
this.valueEncoding = opts.valueEncoding ?? ValueEncoding.String;
|
|
28
|
+
// Roots of trie at the moment of checkpoint
|
|
29
|
+
this.checkpoints = [];
|
|
30
|
+
if (this.cacheSize > 0) {
|
|
31
|
+
this._cache = new LRUCache({
|
|
32
|
+
max: this.cacheSize,
|
|
33
|
+
updateAgeOnGet: true,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Flush the checkpoints and use the given checkpoints instead.
|
|
39
|
+
* @param {Checkpoint[]} checkpoints
|
|
40
|
+
*/
|
|
41
|
+
setCheckpoints(checkpoints) {
|
|
42
|
+
this.checkpoints = [];
|
|
43
|
+
for (let i = 0; i < checkpoints.length; i++) {
|
|
44
|
+
this.checkpoints.push({
|
|
45
|
+
root: checkpoints[i].root,
|
|
46
|
+
keyValueMap: new Map(checkpoints[i].keyValueMap),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Is the DB during a checkpoint phase?
|
|
52
|
+
*/
|
|
53
|
+
hasCheckpoints() {
|
|
54
|
+
return this.checkpoints.length > 0;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Adds a new checkpoint to the stack
|
|
58
|
+
* @param root
|
|
59
|
+
*/
|
|
60
|
+
checkpoint(root) {
|
|
61
|
+
this.checkpoints.push({ keyValueMap: new Map(), root });
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Commits the latest checkpoint
|
|
65
|
+
*/
|
|
66
|
+
async commit() {
|
|
67
|
+
const { keyValueMap } = this.checkpoints.pop();
|
|
68
|
+
if (!this.hasCheckpoints()) {
|
|
69
|
+
// This was the final checkpoint, we should now commit and flush everything to disk
|
|
70
|
+
const batchOp = [];
|
|
71
|
+
for (const [key, value] of keyValueMap.entries()) {
|
|
72
|
+
if (value === undefined) {
|
|
73
|
+
batchOp.push({
|
|
74
|
+
type: 'del',
|
|
75
|
+
key: unprefixedHexToBytes(key),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
batchOp.push({
|
|
80
|
+
type: 'put',
|
|
81
|
+
key: unprefixedHexToBytes(key),
|
|
82
|
+
value,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
await this.batch(batchOp);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
// dump everything into the current (higher level) diff cache
|
|
90
|
+
const currentKeyValueMap = this.checkpoints[this.checkpoints.length - 1].keyValueMap;
|
|
91
|
+
for (const [key, value] of keyValueMap.entries()) {
|
|
92
|
+
currentKeyValueMap.set(key, value);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Reverts the latest checkpoint
|
|
98
|
+
*/
|
|
99
|
+
async revert() {
|
|
100
|
+
const { root } = this.checkpoints.pop();
|
|
101
|
+
return root;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @inheritDoc
|
|
105
|
+
*/
|
|
106
|
+
async get(key) {
|
|
107
|
+
// Using deprecated bytesToUnprefixedHex for performance: used as cache/database keys (string encoding).
|
|
108
|
+
const keyHex = bytesToUnprefixedHex(key);
|
|
109
|
+
if (this._cache !== undefined) {
|
|
110
|
+
const value = this._cache.get(keyHex);
|
|
111
|
+
this._stats.cache.reads += 1;
|
|
112
|
+
if (value !== undefined) {
|
|
113
|
+
this._stats.cache.hits += 1;
|
|
114
|
+
return value;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// Lookup the value in our diff cache. We return the latest checkpointed value (which should be the value on disk)
|
|
118
|
+
for (let index = this.checkpoints.length - 1; index >= 0; index--) {
|
|
119
|
+
if (this.checkpoints[index].keyValueMap.has(keyHex)) {
|
|
120
|
+
return this.checkpoints[index].keyValueMap.get(keyHex);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Nothing has been found in diff cache, look up from disk
|
|
124
|
+
const value = await this.db.get(keyHex, {
|
|
125
|
+
keyEncoding: KeyEncoding.String,
|
|
126
|
+
valueEncoding: this.valueEncoding,
|
|
127
|
+
});
|
|
128
|
+
this._stats.db.reads += 1;
|
|
129
|
+
if (value !== undefined) {
|
|
130
|
+
this._stats.db.hits += 1;
|
|
131
|
+
}
|
|
132
|
+
const returnValue = value !== undefined
|
|
133
|
+
? value instanceof Uint8Array
|
|
134
|
+
? value
|
|
135
|
+
: unprefixedHexToBytes(value)
|
|
136
|
+
: undefined;
|
|
137
|
+
this._cache?.set(keyHex, returnValue);
|
|
138
|
+
if (this.hasCheckpoints()) {
|
|
139
|
+
// Since we are a checkpoint, put this value in diff cache,
|
|
140
|
+
// so future `get` calls will not look the key up again from disk.
|
|
141
|
+
this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(keyHex, returnValue);
|
|
142
|
+
}
|
|
143
|
+
return returnValue;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* @inheritDoc
|
|
147
|
+
*/
|
|
148
|
+
async put(key, value) {
|
|
149
|
+
// Using deprecated bytesToUnprefixedHex for performance: used as cache/database keys and values (string encoding).
|
|
150
|
+
const keyHex = bytesToUnprefixedHex(key);
|
|
151
|
+
if (this.hasCheckpoints()) {
|
|
152
|
+
// put value in diff cache
|
|
153
|
+
this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(keyHex, value);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
const valuePut = this.valueEncoding === ValueEncoding.Bytes ? value : bytesToUnprefixedHex(value);
|
|
157
|
+
await this.db.put(keyHex, valuePut, {
|
|
158
|
+
keyEncoding: KeyEncoding.String,
|
|
159
|
+
valueEncoding: this.valueEncoding,
|
|
160
|
+
});
|
|
161
|
+
this._stats.db.writes += 1;
|
|
162
|
+
if (this._cache !== undefined) {
|
|
163
|
+
this._cache.set(keyHex, value);
|
|
164
|
+
this._stats.cache.writes += 1;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* @inheritDoc
|
|
170
|
+
*/
|
|
171
|
+
async del(key) {
|
|
172
|
+
// Using deprecated bytesToUnprefixedHex for performance: used as cache/database keys (string encoding).
|
|
173
|
+
const keyHex = bytesToUnprefixedHex(key);
|
|
174
|
+
if (this.hasCheckpoints()) {
|
|
175
|
+
// delete the value in the current diff cache
|
|
176
|
+
this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(keyHex, undefined);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
// delete the value on disk
|
|
180
|
+
await this.db.del(keyHex, {
|
|
181
|
+
keyEncoding: KeyEncoding.String,
|
|
182
|
+
});
|
|
183
|
+
this._stats.db.writes += 1;
|
|
184
|
+
if (this._cache !== undefined) {
|
|
185
|
+
this._cache.set(keyHex, undefined);
|
|
186
|
+
this._stats.cache.writes += 1;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* @inheritDoc
|
|
192
|
+
*/
|
|
193
|
+
async batch(opStack) {
|
|
194
|
+
if (this.hasCheckpoints()) {
|
|
195
|
+
for (const op of opStack) {
|
|
196
|
+
if (op.type === 'put') {
|
|
197
|
+
await this.put(op.key, op.value);
|
|
198
|
+
}
|
|
199
|
+
else if (op.type === 'del') {
|
|
200
|
+
await this.del(op.key);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
const convertedOps = opStack.map((op) => {
|
|
206
|
+
const convertedOp = {
|
|
207
|
+
key: bytesToUnprefixedHex(op.key),
|
|
208
|
+
value: op.type === 'put' ? op.value : undefined,
|
|
209
|
+
type: op.type,
|
|
210
|
+
opts: { ...op.opts, ...{ valueEncoding: this.valueEncoding } },
|
|
211
|
+
};
|
|
212
|
+
this._stats.db.writes += 1;
|
|
213
|
+
if (op.type === 'put' && this.valueEncoding === ValueEncoding.String) {
|
|
214
|
+
convertedOp.value = bytesToUnprefixedHex(convertedOp.value);
|
|
215
|
+
}
|
|
216
|
+
return convertedOp;
|
|
217
|
+
});
|
|
218
|
+
await this.db.batch(convertedOps);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
stats(reset = true) {
|
|
222
|
+
const stats = { ...this._stats, size: this._cache?.size ?? 0 };
|
|
223
|
+
if (reset) {
|
|
224
|
+
this._stats = {
|
|
225
|
+
cache: {
|
|
226
|
+
reads: 0,
|
|
227
|
+
hits: 0,
|
|
228
|
+
writes: 0,
|
|
229
|
+
},
|
|
230
|
+
db: {
|
|
231
|
+
reads: 0,
|
|
232
|
+
hits: 0,
|
|
233
|
+
writes: 0,
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
return stats;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* @inheritDoc
|
|
241
|
+
*/
|
|
242
|
+
shallowCopy() {
|
|
243
|
+
return new CheckpointDB({
|
|
244
|
+
db: this.db,
|
|
245
|
+
cacheSize: this.cacheSize,
|
|
246
|
+
valueEncoding: this.valueEncoding,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
open() {
|
|
250
|
+
return Promise.resolve();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
//# sourceMappingURL=checkpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkpoint.js","sourceRoot":"","sources":["../../../src/db/checkpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,aAAa,EACb,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAKpC;;;GAGG;AACH,MAAM,OAAO,YAAY;IA+BvB;;OAEG;IACH,YAAY,IAAsB;QAlBlC,8DAA8D;QAE9D,WAAM,GAAG;YACP,KAAK,EAAE;gBACL,KAAK,EAAE,CAAC;gBACR,IAAI,EAAE,CAAC;gBACP,MAAM,EAAE,CAAC;aACV;YACD,EAAE,EAAE;gBACF,KAAK,EAAE,CAAC;gBACR,IAAI,EAAE,CAAC;gBACP,MAAM,EAAE,CAAC;aACV;SACF,CAAA;QAMC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,EAAE,CAAA;QACjB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,IAAI,CAAC,CAAA;QACpC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,IAAI,aAAa,CAAC,MAAM,CAAA;QAC/D,4CAA4C;QAC5C,IAAI,CAAC,WAAW,GAAG,EAAE,CAAA;QAErB,IAAI,IAAI,CAAC,SAAS,GAAG,CAAC,EAAE,CAAC;YACvB,IAAI,CAAC,MAAM,GAAG,IAAI,QAAQ,CAAC;gBACzB,GAAG,EAAE,IAAI,CAAC,SAAS;gBACnB,cAAc,EAAE,IAAI;aACrB,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;IAED;;;OAGG;IACH,cAAc,CAAC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":["../../../src/db/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/db/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,eAAe,CAAA;AAC7B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,eAAe,CAAA;AAC7B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/node/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BinaryNodeType } from './types.ts';
|
|
2
|
+
import type { BinaryNodeOptions, ChildBinaryNode } from './types.ts';
|
|
3
|
+
export declare class InternalBinaryNode {
|
|
4
|
+
children: Array<ChildBinaryNode | null>;
|
|
5
|
+
type: 0;
|
|
6
|
+
constructor(options: BinaryNodeOptions[typeof BinaryNodeType.Internal]);
|
|
7
|
+
static fromRawNode(rawNode: Uint8Array[]): InternalBinaryNode;
|
|
8
|
+
/**
|
|
9
|
+
* Generates a new Internal node
|
|
10
|
+
* @param children the children nodes
|
|
11
|
+
* @returns a new Internal node
|
|
12
|
+
*/
|
|
13
|
+
static create(children?: (ChildBinaryNode | null)[]): InternalBinaryNode;
|
|
14
|
+
getChild(index: number): ChildBinaryNode | null;
|
|
15
|
+
setChild(index: number, child: ChildBinaryNode | null): void;
|
|
16
|
+
/**
|
|
17
|
+
* @returns the RLP serialized node
|
|
18
|
+
*/
|
|
19
|
+
serialize(): Uint8Array;
|
|
20
|
+
/**
|
|
21
|
+
* Returns the raw serialized representation of this internal node as an array of Uint8Arrays.
|
|
22
|
+
*
|
|
23
|
+
* The returned array contains:
|
|
24
|
+
* 1. A single-byte Uint8Array indicating the node type (BinaryNodeType.Internal).
|
|
25
|
+
* 2. For each child (left then right):
|
|
26
|
+
* - The child’s hash, or an empty Uint8Array if the child is null.
|
|
27
|
+
* 3. For each child (left then right):
|
|
28
|
+
* - An RLP-encoded tuple [pathLength, packedPathBytes] where:
|
|
29
|
+
* - `pathLength` is a one-byte Uint8Array representing the number of meaningful bits in the child’s path.
|
|
30
|
+
* - `packedPathBytes` is the packed byte representation of the child's bit path (as produced by `bitsToBytes`).
|
|
31
|
+
*
|
|
32
|
+
* @returns {Uint8Array[]} An array of Uint8Arrays representing the node's serialized internal data.
|
|
33
|
+
* @dev When decoding, the stored child path (an RLP-encoded tuple) must be converted back into the original bit array.
|
|
34
|
+
*/
|
|
35
|
+
raw(): Uint8Array[];
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=internalNode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internalNode.d.ts","sourceRoot":"","sources":["../../../src/node/internalNode.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAE3C,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEpE,qBAAa,kBAAkB;IACtB,QAAQ,EAAE,KAAK,CAAC,eAAe,GAAG,IAAI,CAAC,CAAA;IAEvC,IAAI,IAA0B;gBAEzB,OAAO,EAAE,iBAAiB,CAAC,OAAO,cAAc,CAAC,QAAQ,CAAC;IAItE,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,kBAAkB;IAyC7D;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,eAAe,GAAG,IAAI,CAAC,EAAE,GAAG,kBAAkB;IAOxE,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI;IAI/C,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,eAAe,GAAG,IAAI,GAAG,IAAI;IAI5D;;OAEG;IACH,SAAS,IAAI,UAAU;IAIvB;;;;;;;;;;;;;;OAcG;IAEH,GAAG,IAAI,UAAU,EAAE;CAWpB"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { RLP } from '@feelyourprotocol/rlp';
|
|
2
|
+
import { EthereumJSErrorWithoutCode, bitsToBytes, bytesToBits } from '@feelyourprotocol/util';
|
|
3
|
+
import { BinaryNodeType } from "./types.js";
|
|
4
|
+
export class InternalBinaryNode {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.type = BinaryNodeType.Internal;
|
|
7
|
+
this.children = options.children ?? Array(2).fill(null);
|
|
8
|
+
}
|
|
9
|
+
static fromRawNode(rawNode) {
|
|
10
|
+
const nodeType = rawNode[0][0];
|
|
11
|
+
if (nodeType !== BinaryNodeType.Internal) {
|
|
12
|
+
throw EthereumJSErrorWithoutCode('Invalid node type');
|
|
13
|
+
}
|
|
14
|
+
// The length of the rawNode should be the # of children * 2 (for hash and path) + 1 for the node type
|
|
15
|
+
if (rawNode.length !== 2 * 2 + 1) {
|
|
16
|
+
throw EthereumJSErrorWithoutCode('Invalid node length');
|
|
17
|
+
}
|
|
18
|
+
const [, leftChildHash, rightChildHash, leftChildRawPath, rightChildRawPath] = rawNode;
|
|
19
|
+
const decodeChild = (hash, rawPath) => {
|
|
20
|
+
if (hash.length === 0)
|
|
21
|
+
return null;
|
|
22
|
+
const decoded = RLP.decode(rawPath);
|
|
23
|
+
if (!Array.isArray(decoded) || decoded.length !== 2) {
|
|
24
|
+
throw EthereumJSErrorWithoutCode('Invalid RLP encoding for child path');
|
|
25
|
+
}
|
|
26
|
+
const [encodedLength, encodedPath] = decoded;
|
|
27
|
+
if (encodedLength.length !== 1) {
|
|
28
|
+
throw EthereumJSErrorWithoutCode('Invalid path length encoding');
|
|
29
|
+
}
|
|
30
|
+
const pathLength = encodedLength[0];
|
|
31
|
+
const path = bytesToBits(encodedPath, pathLength);
|
|
32
|
+
return { hash, path };
|
|
33
|
+
};
|
|
34
|
+
const children = [
|
|
35
|
+
decodeChild(leftChildHash, leftChildRawPath),
|
|
36
|
+
decodeChild(rightChildHash, rightChildRawPath),
|
|
37
|
+
];
|
|
38
|
+
return new InternalBinaryNode({ children });
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Generates a new Internal node
|
|
42
|
+
* @param children the children nodes
|
|
43
|
+
* @returns a new Internal node
|
|
44
|
+
*/
|
|
45
|
+
static create(children) {
|
|
46
|
+
if (children !== undefined && children.length !== 2) {
|
|
47
|
+
throw EthereumJSErrorWithoutCode('Internal node must have 2 children');
|
|
48
|
+
}
|
|
49
|
+
return new InternalBinaryNode({ children });
|
|
50
|
+
}
|
|
51
|
+
getChild(index) {
|
|
52
|
+
return this.children[index];
|
|
53
|
+
}
|
|
54
|
+
setChild(index, child) {
|
|
55
|
+
this.children[index] = child;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @returns the RLP serialized node
|
|
59
|
+
*/
|
|
60
|
+
serialize() {
|
|
61
|
+
return RLP.encode(this.raw());
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns the raw serialized representation of this internal node as an array of Uint8Arrays.
|
|
65
|
+
*
|
|
66
|
+
* The returned array contains:
|
|
67
|
+
* 1. A single-byte Uint8Array indicating the node type (BinaryNodeType.Internal).
|
|
68
|
+
* 2. For each child (left then right):
|
|
69
|
+
* - The child’s hash, or an empty Uint8Array if the child is null.
|
|
70
|
+
* 3. For each child (left then right):
|
|
71
|
+
* - An RLP-encoded tuple [pathLength, packedPathBytes] where:
|
|
72
|
+
* - `pathLength` is a one-byte Uint8Array representing the number of meaningful bits in the child’s path.
|
|
73
|
+
* - `packedPathBytes` is the packed byte representation of the child's bit path (as produced by `bitsToBytes`).
|
|
74
|
+
*
|
|
75
|
+
* @returns {Uint8Array[]} An array of Uint8Arrays representing the node's serialized internal data.
|
|
76
|
+
* @dev When decoding, the stored child path (an RLP-encoded tuple) must be converted back into the original bit array.
|
|
77
|
+
*/
|
|
78
|
+
raw() {
|
|
79
|
+
return [
|
|
80
|
+
new Uint8Array([BinaryNodeType.Internal]),
|
|
81
|
+
...this.children.map((child) => (child !== null ? child.hash : new Uint8Array())),
|
|
82
|
+
...this.children.map((child) => child !== null
|
|
83
|
+
? RLP.encode([new Uint8Array([child.path.length]), bitsToBytes(child.path)])
|
|
84
|
+
: new Uint8Array()),
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=internalNode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internalNode.js","sourceRoot":"","sources":["../../../src/node/internalNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACrC,OAAO,EAAE,0BAA0B,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEvF,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAI3C,MAAM,OAAO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E,CAAC,CAAC;YACjF,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAC7B,KAAK,KAAK,IAAI;gBACZ,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;gBAC5E,CAAC,CAAC,IAAI,UAAU,EAAE,CACrB;SACF,CAAA;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { BinaryNodeType } from './types.ts';
|
|
2
|
+
import type { BinaryNodeOptions } from './types.ts';
|
|
3
|
+
export declare class StemBinaryNode {
|
|
4
|
+
stem: Uint8Array;
|
|
5
|
+
values: (Uint8Array | null)[];
|
|
6
|
+
type: 1;
|
|
7
|
+
constructor(options: BinaryNodeOptions[typeof BinaryNodeType.Stem]);
|
|
8
|
+
static fromRawNode(rawNode: Uint8Array[]): StemBinaryNode;
|
|
9
|
+
/**
|
|
10
|
+
* Generates a new Stem node
|
|
11
|
+
* @param stem the 31 byte stem corresponding to the where the stem node is located in the tree
|
|
12
|
+
* @returns a new Stem node
|
|
13
|
+
*/
|
|
14
|
+
static create(stem: Uint8Array): StemBinaryNode;
|
|
15
|
+
getValue(index: number): Uint8Array | null;
|
|
16
|
+
setValue(index: number, value: Uint8Array | null): void;
|
|
17
|
+
/**
|
|
18
|
+
* @returns the RLP serialized node
|
|
19
|
+
*/
|
|
20
|
+
serialize(): Uint8Array;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the raw serialized representation of the node as an array of Uint8Arrays.
|
|
23
|
+
* The returned array is constructed as follows:
|
|
24
|
+
* - The first element is a Uint8Array containing a single byte that represents the node type,
|
|
25
|
+
* - The second element is the node's `stem` property.
|
|
26
|
+
* - The remaining elements are derived from the node's `values` array:
|
|
27
|
+
* - For each value, if it is `null`, it is converted to an empty Uint8Array.
|
|
28
|
+
* - Otherwise, the value is included as-is.
|
|
29
|
+
*
|
|
30
|
+
* @returns {Uint8Array[]} An array of Uint8Arrays representing the node's raw data.
|
|
31
|
+
*/
|
|
32
|
+
raw(): Uint8Array[];
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=stemNode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stemNode.d.ts","sourceRoot":"","sources":["../../../src/node/stemNode.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAc,MAAM,YAAY,CAAA;AAEvD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEnD,qBAAa,cAAc;IAClB,IAAI,EAAE,UAAU,CAAA;IAChB,MAAM,EAAE,CAAC,UAAU,GAAG,IAAI,CAAC,EAAE,CAAA;IAE7B,IAAI,IAAsB;gBAErB,OAAO,EAAE,iBAAiB,CAAC,OAAO,cAAc,CAAC,IAAI,CAAC;IAKlE,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,cAAc;IAkBzD;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,cAAc;IAK/C,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI;IAI1C,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI,GAAG,IAAI;IAIvD;;OAEG;IACH,SAAS,IAAI,UAAU;IAIvB;;;;;;;;;;OAUG;IACH,GAAG,IAAI,UAAU,EAAE;CAcpB"}
|