@based/db 0.0.66 → 0.0.67
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/README.md +2 -2
- package/dist/lib/darwin_aarch64/include/selva/colvec.h +71 -0
- package/dist/lib/darwin_aarch64/include/selva/db.h +3 -0
- package/dist/lib/darwin_aarch64/include/selva/fields.h +1 -1
- package/dist/lib/darwin_aarch64/include/selva/types.h +7 -0
- package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
- package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
- package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
- package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
- package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
- package/dist/lib/linux_aarch64/include/selva/colvec.h +71 -0
- package/dist/lib/linux_aarch64/include/selva/db.h +3 -0
- package/dist/lib/linux_aarch64/include/selva/fields.h +1 -1
- package/dist/lib/linux_aarch64/include/selva/types.h +7 -0
- package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
- package/dist/lib/linux_aarch64/libselva.so +0 -0
- package/dist/lib/linux_x86_64/include/selva/colvec.h +71 -0
- package/dist/lib/linux_x86_64/include/selva/db.h +3 -0
- package/dist/lib/linux_x86_64/include/selva/fields.h +1 -1
- package/dist/lib/linux_x86_64/include/selva/types.h +7 -0
- package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
- package/dist/lib/linux_x86_64/libselva.so +0 -0
- package/dist/src/client/flushModify.d.ts +2 -1
- package/dist/src/client/flushModify.js +12 -4
- package/dist/src/client/modify/create.js +11 -0
- package/dist/src/client/modify/delete.js +3 -0
- package/dist/src/client/modify/modify.js +2 -2
- package/dist/src/client/modify/setCursor.d.ts +2 -1
- package/dist/src/client/query/BasedDbQuery.d.ts +7 -2
- package/dist/src/client/query/BasedDbQuery.js +111 -14
- package/dist/src/client/query/aggregates/aggregation.js +24 -11
- package/dist/src/client/query/aggregates/types.d.ts +21 -2
- package/dist/src/client/query/aggregates/types.js +34 -1
- package/dist/src/client/query/display.js +8 -2
- package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +2 -3
- package/dist/src/client/query/filter/createVariableFilterBuffer.js +20 -7
- package/dist/src/client/query/filter/filter.js +13 -3
- package/dist/src/client/query/filter/primitiveFilter.d.ts +1 -2
- package/dist/src/client/query/include/props.js +18 -2
- package/dist/src/client/query/include/toBuffer.js +11 -3
- package/dist/src/client/query/include/walk.js +5 -1
- package/dist/src/client/query/queryDef.js +4 -1
- package/dist/src/client/query/read/read.js +50 -23
- package/dist/src/client/query/registerQuery.js +1 -0
- package/dist/src/client/query/search/index.d.ts +1 -1
- package/dist/src/client/query/search/index.js +21 -7
- package/dist/src/client/query/sort.d.ts +1 -1
- package/dist/src/client/query/toByteCode/default.d.ts +1 -1
- package/dist/src/client/query/toByteCode/default.js +0 -2
- package/dist/src/client/query/toByteCode/toBuffer.js +0 -7
- package/dist/src/client/query/types.d.ts +16 -5
- package/dist/src/client/query/validation.js +25 -2
- package/dist/src/client/xxHash64.d.ts +1 -1
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.js +0 -1
- package/dist/src/native.d.ts +2 -2
- package/dist/src/native.js +7 -8
- package/dist/src/server/IoWorker.d.ts +8 -0
- package/dist/src/server/IoWorker.js +39 -0
- package/dist/src/server/QueryWorker.d.ts +8 -0
- package/dist/src/server/QueryWorker.js +26 -0
- package/dist/src/server/blocks.d.ts +24 -0
- package/dist/src/server/blocks.js +112 -0
- package/dist/src/server/dbHash.d.ts +1 -1
- package/dist/src/server/index.d.ts +9 -16
- package/dist/src/server/index.js +37 -15
- package/dist/src/server/migrate/index.d.ts +5 -0
- package/dist/src/server/migrate/index.js +10 -7
- package/dist/src/server/save.d.ts +8 -6
- package/dist/src/server/save.js +34 -78
- package/dist/src/server/schema.js +6 -5
- package/dist/src/server/start.js +57 -60
- package/dist/src/server/tree.d.ts +24 -13
- package/dist/src/server/tree.js +95 -66
- package/dist/src/server/workers/DbWorker.d.ts +17 -0
- package/dist/src/server/{DbWorker.js → workers/DbWorker.js} +15 -17
- package/dist/src/server/workers/io_worker.js +39 -0
- package/dist/src/server/workers/io_worker_types.d.ts +12 -0
- package/dist/src/server/workers/io_worker_types.js +2 -0
- package/dist/src/server/workers/query_worker.d.ts +1 -0
- package/dist/src/server/workers/query_worker.js +4 -0
- package/dist/src/server/workers/worker.d.ts +1 -0
- package/dist/src/server/workers/worker.js +41 -0
- package/dist/src/shared/Emitter.d.ts +1 -0
- package/dist/src/types.d.ts +1 -1
- package/dist/src/types.js +1 -1
- package/package.json +3 -3
- package/dist/lib/darwin_aarch64/include/selva/history.h +0 -64
- package/dist/lib/linux_aarch64/include/selva/history.h +0 -64
- package/dist/lib/linux_x86_64/include/selva/history.h +0 -64
- package/dist/src/client/query/serialize.d.ts +0 -4
- package/dist/src/client/query/serialize.js +0 -26
- package/dist/src/server/DbWorker.d.ts +0 -13
- package/dist/src/server/csmt/draw-dot.d.ts +0 -4
- package/dist/src/server/csmt/draw-dot.js +0 -38
- package/dist/src/server/csmt/index.d.ts +0 -4
- package/dist/src/server/csmt/index.js +0 -5
- package/dist/src/server/csmt/match.d.ts +0 -7
- package/dist/src/server/csmt/match.js +0 -10
- package/dist/src/server/csmt/memebership-proof.d.ts +0 -7
- package/dist/src/server/csmt/memebership-proof.js +0 -122
- package/dist/src/server/csmt/tree-utils.d.ts +0 -6
- package/dist/src/server/csmt/tree-utils.js +0 -33
- package/dist/src/server/csmt/tree.d.ts +0 -3
- package/dist/src/server/csmt/tree.js +0 -270
- package/dist/src/server/csmt/types.d.ts +0 -46
- package/dist/src/server/csmt/types.js +0 -2
- package/dist/src/server/worker.js +0 -33
- /package/dist/src/server/{worker.d.ts → workers/io_worker.d.ts} +0 -0
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2025 SAULX
|
|
3
|
-
* SPDX-License-Identifier: MIT
|
|
4
|
-
*/
|
|
5
|
-
#pragma once
|
|
6
|
-
|
|
7
|
-
#include <stddef.h>
|
|
8
|
-
#include <stdint.h>
|
|
9
|
-
#include "selva/types.h"
|
|
10
|
-
#include "selva/_export.h"
|
|
11
|
-
|
|
12
|
-
struct selva_history;
|
|
13
|
-
|
|
14
|
-
struct selva_history_event {
|
|
15
|
-
int64_t ts;
|
|
16
|
-
node_id_t node_id;
|
|
17
|
-
uint32_t crc;
|
|
18
|
-
} __packed __attribute__((aligned(4)));
|
|
19
|
-
|
|
20
|
-
static_assert(alignof(struct selva_history_event) == alignof(uint32_t));
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Create a new history object.
|
|
24
|
-
*/
|
|
25
|
-
SELVA_EXPORT
|
|
26
|
-
int selva_history_create(const char *pathname, size_t bsize, struct selva_history **hist_out);
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Destroy a history object.
|
|
30
|
-
*/
|
|
31
|
-
SELVA_EXPORT
|
|
32
|
-
void selva_history_destroy(struct selva_history *hist);
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Append a block of size `bsize` to a history object.
|
|
36
|
-
*/
|
|
37
|
-
SELVA_EXPORT
|
|
38
|
-
void selva_history_append(struct selva_history *hist, int64_t ts, node_id_t node_id, void *buf);
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Ensure that the history file is fully written to the disk.
|
|
42
|
-
*/
|
|
43
|
-
SELVA_EXPORT
|
|
44
|
-
void selva_history_fsync(struct selva_history *hist);
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Find a range.
|
|
48
|
-
* The returned buffer must be freed with selva_history_free_range().
|
|
49
|
-
*/
|
|
50
|
-
SELVA_EXPORT
|
|
51
|
-
uint32_t *selva_history_find_range(struct selva_history *hist, int64_t from, int64_t to, size_t *size_out);
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Find a range.
|
|
55
|
-
* The returned buffer must be freed with selva_history_free_range().
|
|
56
|
-
*/
|
|
57
|
-
SELVA_EXPORT
|
|
58
|
-
uint32_t *selva_history_find_range_node(struct selva_history *hist, int64_t from, int64_t to, node_id_t node_id, size_t *size_out);
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Free a range returned by selva_history_find_range() or selva_history_find_range_node().
|
|
62
|
-
*/
|
|
63
|
-
SELVA_EXPORT
|
|
64
|
-
void selva_history_free_range(uint32_t *range);
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2025 SAULX
|
|
3
|
-
* SPDX-License-Identifier: MIT
|
|
4
|
-
*/
|
|
5
|
-
#pragma once
|
|
6
|
-
|
|
7
|
-
#include <stddef.h>
|
|
8
|
-
#include <stdint.h>
|
|
9
|
-
#include "selva/types.h"
|
|
10
|
-
#include "selva/_export.h"
|
|
11
|
-
|
|
12
|
-
struct selva_history;
|
|
13
|
-
|
|
14
|
-
struct selva_history_event {
|
|
15
|
-
int64_t ts;
|
|
16
|
-
node_id_t node_id;
|
|
17
|
-
uint32_t crc;
|
|
18
|
-
} __packed __attribute__((aligned(4)));
|
|
19
|
-
|
|
20
|
-
static_assert(alignof(struct selva_history_event) == alignof(uint32_t));
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Create a new history object.
|
|
24
|
-
*/
|
|
25
|
-
SELVA_EXPORT
|
|
26
|
-
int selva_history_create(const char *pathname, size_t bsize, struct selva_history **hist_out);
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Destroy a history object.
|
|
30
|
-
*/
|
|
31
|
-
SELVA_EXPORT
|
|
32
|
-
void selva_history_destroy(struct selva_history *hist);
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Append a block of size `bsize` to a history object.
|
|
36
|
-
*/
|
|
37
|
-
SELVA_EXPORT
|
|
38
|
-
void selva_history_append(struct selva_history *hist, int64_t ts, node_id_t node_id, void *buf);
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Ensure that the history file is fully written to the disk.
|
|
42
|
-
*/
|
|
43
|
-
SELVA_EXPORT
|
|
44
|
-
void selva_history_fsync(struct selva_history *hist);
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Find a range.
|
|
48
|
-
* The returned buffer must be freed with selva_history_free_range().
|
|
49
|
-
*/
|
|
50
|
-
SELVA_EXPORT
|
|
51
|
-
uint32_t *selva_history_find_range(struct selva_history *hist, int64_t from, int64_t to, size_t *size_out);
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Find a range.
|
|
55
|
-
* The returned buffer must be freed with selva_history_free_range().
|
|
56
|
-
*/
|
|
57
|
-
SELVA_EXPORT
|
|
58
|
-
uint32_t *selva_history_find_range_node(struct selva_history *hist, int64_t from, int64_t to, node_id_t node_id, size_t *size_out);
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Free a range returned by selva_history_find_range() or selva_history_find_range_node().
|
|
62
|
-
*/
|
|
63
|
-
SELVA_EXPORT
|
|
64
|
-
void selva_history_free_range(uint32_t *range);
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2025 SAULX
|
|
3
|
-
* SPDX-License-Identifier: MIT
|
|
4
|
-
*/
|
|
5
|
-
#pragma once
|
|
6
|
-
|
|
7
|
-
#include <stddef.h>
|
|
8
|
-
#include <stdint.h>
|
|
9
|
-
#include "selva/types.h"
|
|
10
|
-
#include "selva/_export.h"
|
|
11
|
-
|
|
12
|
-
struct selva_history;
|
|
13
|
-
|
|
14
|
-
struct selva_history_event {
|
|
15
|
-
int64_t ts;
|
|
16
|
-
node_id_t node_id;
|
|
17
|
-
uint32_t crc;
|
|
18
|
-
} __packed __attribute__((aligned(4)));
|
|
19
|
-
|
|
20
|
-
static_assert(alignof(struct selva_history_event) == alignof(uint32_t));
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Create a new history object.
|
|
24
|
-
*/
|
|
25
|
-
SELVA_EXPORT
|
|
26
|
-
int selva_history_create(const char *pathname, size_t bsize, struct selva_history **hist_out);
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Destroy a history object.
|
|
30
|
-
*/
|
|
31
|
-
SELVA_EXPORT
|
|
32
|
-
void selva_history_destroy(struct selva_history *hist);
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Append a block of size `bsize` to a history object.
|
|
36
|
-
*/
|
|
37
|
-
SELVA_EXPORT
|
|
38
|
-
void selva_history_append(struct selva_history *hist, int64_t ts, node_id_t node_id, void *buf);
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Ensure that the history file is fully written to the disk.
|
|
42
|
-
*/
|
|
43
|
-
SELVA_EXPORT
|
|
44
|
-
void selva_history_fsync(struct selva_history *hist);
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Find a range.
|
|
48
|
-
* The returned buffer must be freed with selva_history_free_range().
|
|
49
|
-
*/
|
|
50
|
-
SELVA_EXPORT
|
|
51
|
-
uint32_t *selva_history_find_range(struct selva_history *hist, int64_t from, int64_t to, size_t *size_out);
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Find a range.
|
|
55
|
-
* The returned buffer must be freed with selva_history_free_range().
|
|
56
|
-
*/
|
|
57
|
-
SELVA_EXPORT
|
|
58
|
-
uint32_t *selva_history_find_range_node(struct selva_history *hist, int64_t from, int64_t to, node_id_t node_id, size_t *size_out);
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Free a range returned by selva_history_find_range() or selva_history_find_range_node().
|
|
62
|
-
*/
|
|
63
|
-
SELVA_EXPORT
|
|
64
|
-
void selva_history_free_range(uint32_t *range);
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const walk = (q) => {
|
|
2
|
-
const obj = {};
|
|
3
|
-
for (const key in q) {
|
|
4
|
-
if (key === 'schema') {
|
|
5
|
-
obj[key] = q[key].type;
|
|
6
|
-
}
|
|
7
|
-
else {
|
|
8
|
-
// if PROPDEF
|
|
9
|
-
if (typeof q[key] === 'object') {
|
|
10
|
-
obj[key] = walk(q[key]);
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
obj[key] = q[key];
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return obj;
|
|
18
|
-
};
|
|
19
|
-
export const serialize = (q) => {
|
|
20
|
-
const obj = walk(q);
|
|
21
|
-
return JSON.stringify(obj);
|
|
22
|
-
};
|
|
23
|
-
export const parse = (str, parsedSchema) => {
|
|
24
|
-
//
|
|
25
|
-
};
|
|
26
|
-
//# sourceMappingURL=serialize.js.map
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Worker, MessagePort } from 'node:worker_threads';
|
|
2
|
-
import { DbServer } from './index.js';
|
|
3
|
-
export declare class DbWorker {
|
|
4
|
-
constructor(address: BigInt, db: DbServer, workerIndex: number);
|
|
5
|
-
db: DbServer;
|
|
6
|
-
channel: MessagePort;
|
|
7
|
-
worker: Worker;
|
|
8
|
-
resolvers: ((x: any) => any)[];
|
|
9
|
-
readyPromise: Promise<true>;
|
|
10
|
-
callback: (resolve: (x: any) => any) => void;
|
|
11
|
-
updateCtx(address: BigInt): Promise<void>;
|
|
12
|
-
getQueryBuf(buf: Uint8Array): Promise<Uint8Array>;
|
|
13
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { encodeBase64 } from '@saulx/utils';
|
|
2
|
-
function makeLabel(node) {
|
|
3
|
-
return `${node.key}\n${encodeBase64(node.hash).substring(0, 5)}`;
|
|
4
|
-
}
|
|
5
|
-
function wrapFormatter(dataFormatter, node) {
|
|
6
|
-
return `\n${dataFormatter(node.data).replace('"', '\\"')}`;
|
|
7
|
-
}
|
|
8
|
-
// This can be visualized with Graphviz dot.
|
|
9
|
-
// Online: https://dreampuf.github.io/GraphvizOnline/?engine=dot
|
|
10
|
-
export default function draw(csmt, dataFormatter) {
|
|
11
|
-
const root = csmt.getRoot();
|
|
12
|
-
const lines = [];
|
|
13
|
-
const nodes = [];
|
|
14
|
-
let i = 0;
|
|
15
|
-
const walk = (node, prev) => {
|
|
16
|
-
const cur = i;
|
|
17
|
-
const left = node.left;
|
|
18
|
-
const right = node.right;
|
|
19
|
-
const isLeaf = !left && !right;
|
|
20
|
-
nodes.push(`n${cur} [label="${makeLabel(node) + ((dataFormatter && node.data) ? wrapFormatter(dataFormatter, node) : '')}"${isLeaf ? ' shape=box' : ''}];`);
|
|
21
|
-
if (cur > 0) {
|
|
22
|
-
lines.push(`n${prev} -- n${cur}`);
|
|
23
|
-
}
|
|
24
|
-
if (left) {
|
|
25
|
-
i++;
|
|
26
|
-
walk(left, cur);
|
|
27
|
-
}
|
|
28
|
-
if (right) {
|
|
29
|
-
i++;
|
|
30
|
-
walk(right, cur);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
if (root) {
|
|
34
|
-
walk(root, i);
|
|
35
|
-
}
|
|
36
|
-
return `graph ethane {\n${nodes.join('\n')}\n${lines.join('\n')}\n}`;
|
|
37
|
-
}
|
|
38
|
-
//# sourceMappingURL=draw-dot.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { TreeKey, TreeNode } from './types.js';
|
|
2
|
-
export declare enum Direction {
|
|
3
|
-
Left = "L",
|
|
4
|
-
Right = "R"
|
|
5
|
-
}
|
|
6
|
-
export type Proof = [TreeKey | null, TreeKey | null] | [TreeKey | Uint8Array, TreeKey | Direction][];
|
|
7
|
-
export default function membershipProof<T = any>(root: TreeNode<T> | null, k: TreeKey): Proof;
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { minInSubtree, maxInSubtree } from './tree-utils.js';
|
|
2
|
-
import match from './match.js';
|
|
3
|
-
export var Direction;
|
|
4
|
-
(function (Direction) {
|
|
5
|
-
Direction["Left"] = "L";
|
|
6
|
-
Direction["Right"] = "R";
|
|
7
|
-
})(Direction || (Direction = {}));
|
|
8
|
-
function reverse(direction) {
|
|
9
|
-
switch (direction) {
|
|
10
|
-
case Direction.Left:
|
|
11
|
-
return Direction.Right;
|
|
12
|
-
case Direction.Right:
|
|
13
|
-
return Direction.Left;
|
|
14
|
-
default:
|
|
15
|
-
throw new TypeError('"direction" is not type of Direction');
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
function aeq(arr) {
|
|
19
|
-
return (x) => x.every((y, i) => y === arr[i]);
|
|
20
|
-
}
|
|
21
|
-
function isList(x) {
|
|
22
|
-
return Array.isArray(x) && x.length > 0 && Array.isArray(x[0]);
|
|
23
|
-
}
|
|
24
|
-
function nonMembershipProof(k, key, direction, sibling) {
|
|
25
|
-
return match([k > key, direction])
|
|
26
|
-
.on(aeq([true, Direction.Left]), () => [key, minInSubtree(sibling)])
|
|
27
|
-
.on(aeq([true, Direction.Right]), () => [key, null])
|
|
28
|
-
.on(aeq([false, Direction.Left]), () => [null, key])
|
|
29
|
-
.on(aeq([false, Direction.Right]), () => [maxInSubtree(sibling), key])
|
|
30
|
-
.otherwise(() => {
|
|
31
|
-
throw new TypeError('"direction" is not type of Direction');
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
function membershipProofR(sibling, direction, node, k) {
|
|
35
|
-
const left = node.left;
|
|
36
|
-
const right = node.right;
|
|
37
|
-
if (k === undefined || k === null) {
|
|
38
|
-
throw new TypeError('k is not a TreeKey');
|
|
39
|
-
}
|
|
40
|
-
// && would be more accurate here but there is never a case where only one
|
|
41
|
-
// would be set and thus this way we save on error handling later on.
|
|
42
|
-
if (!left || !right) {
|
|
43
|
-
if (!direction) {
|
|
44
|
-
throw new Error('"direction" must be set');
|
|
45
|
-
}
|
|
46
|
-
if (!sibling) {
|
|
47
|
-
throw new Error('"sibling" must be set');
|
|
48
|
-
}
|
|
49
|
-
// This is a leaf node
|
|
50
|
-
if (node.key === k) {
|
|
51
|
-
return [
|
|
52
|
-
[sibling.hash, reverse(direction)],
|
|
53
|
-
[node.hash, node.key],
|
|
54
|
-
];
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
// Find the non-membership proof otherwise
|
|
58
|
-
return nonMembershipProof(k, node.key, direction, sibling);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
let result;
|
|
62
|
-
if (k <= left.key) {
|
|
63
|
-
// Going towards left child
|
|
64
|
-
result = membershipProofR(right, Direction.Left, left, k);
|
|
65
|
-
}
|
|
66
|
-
else if (k <= right.key) {
|
|
67
|
-
// Going towards right child
|
|
68
|
-
result = membershipProofR(left, Direction.Right, right, k);
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
if (k > right.key) {
|
|
72
|
-
// The given key `k` is greater than any key in this tree.
|
|
73
|
-
// Trigger a proof for the largest key
|
|
74
|
-
return [right.key, null];
|
|
75
|
-
}
|
|
76
|
-
if (!direction) {
|
|
77
|
-
// TODO How should we set direction?
|
|
78
|
-
throw new TypeError('"Direction" must be set');
|
|
79
|
-
}
|
|
80
|
-
if (!sibling) {
|
|
81
|
-
throw new Error('"sibling" must be set');
|
|
82
|
-
}
|
|
83
|
-
// Find the non-membership proof otherwise
|
|
84
|
-
return nonMembershipProof(k, node.key, direction, sibling);
|
|
85
|
-
}
|
|
86
|
-
if (sibling) {
|
|
87
|
-
if (isList(result) && direction) {
|
|
88
|
-
// @ts-ignore the array thing is confusing for TS
|
|
89
|
-
return [[sibling.hash, reverse(direction)], ...result];
|
|
90
|
-
}
|
|
91
|
-
else if (result[1] === null && direction === Direction.Left) {
|
|
92
|
-
return [node.key, minInSubtree(sibling)];
|
|
93
|
-
}
|
|
94
|
-
else if (result[0] === null && direction === Direction.Right) {
|
|
95
|
-
return [maxInSubtree(sibling), node.key];
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return result;
|
|
99
|
-
}
|
|
100
|
-
export default function membershipProof(root, k) {
|
|
101
|
-
if (!root) {
|
|
102
|
-
return [];
|
|
103
|
-
}
|
|
104
|
-
// Root has no sibling or direction so null is used
|
|
105
|
-
return (match(membershipProofR(null, null, root, k))
|
|
106
|
-
// The key is present in the tree
|
|
107
|
-
// Provide the proof in reverse order
|
|
108
|
-
.on((x) => isList(x), (r) => r.reverse())
|
|
109
|
-
// The key is greater than the largest element in the tree
|
|
110
|
-
// Provide a proof for the largest key
|
|
111
|
-
.on(([_, y]) => y === null, ([x, _]) => [...membershipProof(root, x), null])
|
|
112
|
-
// The key is smaller than the smallest element in the tree
|
|
113
|
-
// Provide a proof for the smallest key
|
|
114
|
-
.on(([x, _]) => x === null, ([_, y]) => [null, ...membershipProof(root, y)])
|
|
115
|
-
// The key is bounded by by two keys in the case of non-membership proff
|
|
116
|
-
// provide a proof for the bounding keys to exist
|
|
117
|
-
.otherwise(([x, y]) => [
|
|
118
|
-
membershipProof(root, x),
|
|
119
|
-
membershipProof(root, y),
|
|
120
|
-
]));
|
|
121
|
-
}
|
|
122
|
-
//# sourceMappingURL=memebership-proof.js.map
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { TreeKey, TreeNode } from './types.js';
|
|
2
|
-
export declare function distance(x: TreeKey, y: TreeKey): TreeKey;
|
|
3
|
-
export declare function min(x: TreeNode<any> | null, y: TreeNode<any> | null): TreeKey;
|
|
4
|
-
export declare function max(x: TreeNode<any> | null, y: TreeNode<any> | null): TreeKey;
|
|
5
|
-
export declare function minInSubtree(node: TreeNode<any>): TreeKey;
|
|
6
|
-
export declare function maxInSubtree(node: TreeNode<any>): TreeKey;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { TreeKeyNil } from './types.js';
|
|
2
|
-
export function distance(x, y) {
|
|
3
|
-
let v = x ^ y;
|
|
4
|
-
let r = TreeKeyNil;
|
|
5
|
-
while ((v >>= 1)) {
|
|
6
|
-
r++;
|
|
7
|
-
}
|
|
8
|
-
return r;
|
|
9
|
-
}
|
|
10
|
-
export function min(x, y) {
|
|
11
|
-
const a = (x && x.key) || TreeKeyNil;
|
|
12
|
-
const b = (y && y.key) || TreeKeyNil;
|
|
13
|
-
return a < b ? a : b;
|
|
14
|
-
}
|
|
15
|
-
export function max(x, y) {
|
|
16
|
-
const a = (x && x.key) || TreeKeyNil;
|
|
17
|
-
const b = (y && y.key) || TreeKeyNil;
|
|
18
|
-
return a > b ? a : b;
|
|
19
|
-
}
|
|
20
|
-
// Find min key in a subtree.
|
|
21
|
-
export function minInSubtree(node) {
|
|
22
|
-
if (!node.left) {
|
|
23
|
-
// We assume that the tree is always full and the last left node we can
|
|
24
|
-
// find is the min.
|
|
25
|
-
return node.key;
|
|
26
|
-
}
|
|
27
|
-
return minInSubtree(node.left);
|
|
28
|
-
}
|
|
29
|
-
// Find max key in a subtree.
|
|
30
|
-
export function maxInSubtree(node) {
|
|
31
|
-
return node.key;
|
|
32
|
-
}
|
|
33
|
-
//# sourceMappingURL=tree-utils.js.map
|