@chaincraft/core 1.4.0 → 1.5.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/README.md CHANGED
@@ -3,9 +3,9 @@
3
3
  TypeScript implementation of the Chaincraft wire protocol, object semantics, and
4
4
  Level 2 stack (consensus, ledgers, BitTorrent, channels). Same observable
5
5
  behaviour as the Python `chaincraft` package and the Rust `chaincraft` crate.
6
- Release **[1.4.0](https://www.npmjs.com/package/@chaincraft/core)** on npm.
7
- 1.4.0 adds pedagogical Avalanche (Slush, Snowflake, Snowball) and a
8
- last-write-wins CRDT.
6
+ Release **[1.5.0](https://www.npmjs.com/package/@chaincraft/core)** on npm.
7
+ 1.5.0 adds `storage.index` on `MessageStore` (`register`, `query`). 45 of 45
8
+ registry features (44 implemented plus the UDP framing exemption).
9
9
 
10
10
  Open-source toolkit for peer-to-peer protocols like blockchains, chat rooms,
11
11
  BitTorrent swarms, and content-addressed stores.
@@ -7,9 +7,9 @@
7
7
  *
8
8
  * Add an ID here only when the feature is implemented and its vectors pass.
9
9
  */
10
- export declare const VERSION = "1.4.0";
10
+ export declare const VERSION = "1.5.0";
11
11
  export declare const WIRE_VERSION = 1;
12
- export declare const IMPL = "ts-chaincraft/1.4.0";
12
+ export declare const IMPL = "ts-chaincraft/1.5.0";
13
13
  export declare const LANGUAGE = "typescript";
14
14
  export declare const FEATURES: readonly string[];
15
15
  export interface Manifest {
package/dist/features.js CHANGED
@@ -7,7 +7,7 @@
7
7
  *
8
8
  * Add an ID here only when the feature is implemented and its vectors pass.
9
9
  */
10
- export const VERSION = '1.4.0';
10
+ export const VERSION = '1.5.0';
11
11
  export const WIRE_VERSION = 1;
12
12
  export const IMPL = `ts-chaincraft/${VERSION}`;
13
13
  export const LANGUAGE = 'typescript';
@@ -47,6 +47,7 @@ export const FEATURES = [
47
47
  'mempool.basic',
48
48
  'fees.policy',
49
49
  'storage.persistent',
50
+ 'storage.index',
50
51
  'cli.node',
51
52
  'bittorrent.swarm',
52
53
  'wire.channel',
package/dist/storage.d.ts CHANGED
@@ -1,12 +1,18 @@
1
- /** Persistent message store — SPEC/features/storage-persistent.md. */
1
+ /** Persistent message store — SPEC/features/storage-persistent.md, storage-index.md. */
2
+ import { type JsonValue } from './canonical.ts';
2
3
  export declare const DEFAULT_CAPACITY = 8;
3
4
  export declare function isValidId(value: string): boolean;
4
5
  export declare class MessageStore {
5
6
  readonly capacity: number;
6
7
  private readonly entries;
8
+ private readonly registrations;
9
+ private readonly postings;
10
+ private readonly idKeys;
7
11
  constructor(capacity?: number);
8
12
  get size(): number;
9
13
  ids(): string[];
14
+ register(type: string, field: string): boolean;
15
+ query(type: string, field: string, value: JsonValue): string[];
10
16
  put(id: string, body: string): boolean;
11
17
  get(id: string): {
12
18
  body: string;
@@ -21,4 +27,7 @@ export declare class MessageStore {
21
27
  v: number;
22
28
  };
23
29
  load(snapshot: unknown): boolean;
30
+ private rebuildIndex;
31
+ private unindexId;
32
+ private indexBody;
24
33
  }
package/dist/storage.js CHANGED
@@ -1,18 +1,69 @@
1
- /** Persistent message store — SPEC/features/storage-persistent.md. */
1
+ /** Persistent message store — SPEC/features/storage-persistent.md, storage-index.md. */
2
+ import { MAX_SAFE_INT, MIN_SAFE_INT } from "./canonical.js";
3
+ import { Envelope } from "./envelope.js";
4
+ import { ProtocolError } from "./errors.js";
2
5
  export const DEFAULT_CAPACITY = 8;
6
+ const MAX_REGISTRATIONS = 8;
3
7
  const HEX64 = /^[0-9a-f]{64}$/;
8
+ const TYPE_RE = /^[a-z0-9._-]{1,64}$/;
9
+ const FIELD_RE = /^[A-Za-z0-9._-]{1,64}$/;
4
10
  export function isValidId(value) {
5
11
  return HEX64.test(value);
6
12
  }
13
+ function regKey(type, field) {
14
+ return `${type}\0${field}`;
15
+ }
16
+ function indexKey(type, field, encoded) {
17
+ return `${type}\0${field}\0${encoded}`;
18
+ }
19
+ function isWireInt(value) {
20
+ return typeof value === 'number' && Number.isSafeInteger(value) && value >= MIN_SAFE_INT && value <= MAX_SAFE_INT;
21
+ }
22
+ function encodeValue(value) {
23
+ if (typeof value === 'string')
24
+ return `s:${value}`;
25
+ if (typeof value === 'boolean')
26
+ return `b:${value ? 1 : 0}`;
27
+ if (isWireInt(value))
28
+ return `i:${value}`;
29
+ return undefined;
30
+ }
31
+ function indexValues(raw) {
32
+ if (typeof raw === 'string' || typeof raw === 'boolean')
33
+ return [raw];
34
+ if (isWireInt(raw))
35
+ return [raw];
36
+ if (Array.isArray(raw)) {
37
+ const out = [];
38
+ const seen = new Set();
39
+ for (const item of raw) {
40
+ const enc = encodeValue(item);
41
+ if (enc === undefined)
42
+ continue;
43
+ if (seen.has(enc))
44
+ continue;
45
+ seen.add(enc);
46
+ out.push(item);
47
+ }
48
+ return out;
49
+ }
50
+ return [];
51
+ }
7
52
  export class MessageStore {
8
53
  capacity;
9
54
  entries;
55
+ registrations;
56
+ postings;
57
+ idKeys;
10
58
  constructor(capacity = DEFAULT_CAPACITY) {
11
59
  if (!Number.isSafeInteger(capacity) || capacity < 1) {
12
60
  throw new RangeError('capacity must be >= 1');
13
61
  }
14
62
  this.capacity = capacity;
15
63
  this.entries = new Map();
64
+ this.registrations = new Set();
65
+ this.postings = new Map();
66
+ this.idKeys = new Map();
16
67
  }
17
68
  get size() {
18
69
  return this.entries.size;
@@ -20,19 +71,43 @@ export class MessageStore {
20
71
  ids() {
21
72
  return [...this.entries.keys()];
22
73
  }
74
+ register(type, field) {
75
+ if (!TYPE_RE.test(type) || !FIELD_RE.test(field))
76
+ return false;
77
+ const key = regKey(type, field);
78
+ if (this.registrations.has(key))
79
+ return true;
80
+ if (this.registrations.size >= MAX_REGISTRATIONS)
81
+ return false;
82
+ this.registrations.add(key);
83
+ return true;
84
+ }
85
+ query(type, field, value) {
86
+ const enc = encodeValue(value);
87
+ if (enc === undefined)
88
+ return [];
89
+ if (!this.registrations.has(regKey(type, field)))
90
+ return [];
91
+ return [...(this.postings.get(indexKey(type, field, enc)) ?? [])];
92
+ }
23
93
  put(id, body) {
24
94
  if (!isValidId(id) || body.length === 0)
25
95
  return false;
26
96
  if (this.entries.has(id)) {
97
+ this.unindexId(id);
27
98
  this.entries.set(id, body);
99
+ this.indexBody(id, body);
28
100
  return false;
29
101
  }
30
102
  if (this.entries.size >= this.capacity) {
31
103
  const oldest = this.entries.keys().next().value;
32
- if (oldest !== undefined)
104
+ if (oldest !== undefined) {
105
+ this.unindexId(oldest);
33
106
  this.entries.delete(oldest);
107
+ }
34
108
  }
35
109
  this.entries.set(id, body);
110
+ this.indexBody(id, body);
36
111
  return true;
37
112
  }
38
113
  get(id) {
@@ -42,7 +117,10 @@ export class MessageStore {
42
117
  return { body, found: true };
43
118
  }
44
119
  remove(id) {
45
- return this.entries.delete(id);
120
+ if (!this.entries.delete(id))
121
+ return false;
122
+ this.unindexId(id);
123
+ return true;
46
124
  }
47
125
  dump() {
48
126
  const entries = [];
@@ -57,8 +135,69 @@ export class MessageStore {
57
135
  this.entries.clear();
58
136
  for (const [id, body] of parsed)
59
137
  this.entries.set(id, body);
138
+ this.rebuildIndex();
60
139
  return true;
61
140
  }
141
+ rebuildIndex() {
142
+ this.postings.clear();
143
+ this.idKeys.clear();
144
+ for (const [id, body] of this.entries)
145
+ this.indexBody(id, body);
146
+ }
147
+ unindexId(id) {
148
+ const keys = this.idKeys.get(id);
149
+ if (keys === undefined)
150
+ return;
151
+ this.idKeys.delete(id);
152
+ for (const key of keys) {
153
+ const bucket = this.postings.get(key);
154
+ if (bucket === undefined)
155
+ continue;
156
+ const next = bucket.filter((entry) => entry !== id);
157
+ if (next.length === 0)
158
+ this.postings.delete(key);
159
+ else
160
+ this.postings.set(key, next);
161
+ }
162
+ }
163
+ indexBody(id, body) {
164
+ let env;
165
+ try {
166
+ env = Envelope.parse(body);
167
+ }
168
+ catch (e) {
169
+ if (e instanceof ProtocolError)
170
+ return;
171
+ throw e;
172
+ }
173
+ if (env.data === null || typeof env.data !== 'object' || Array.isArray(env.data))
174
+ return;
175
+ const data = env.data;
176
+ const keysForId = new Set();
177
+ for (const reg of this.registrations) {
178
+ const [type, field] = reg.split('\0');
179
+ if (type === undefined || field === undefined)
180
+ continue;
181
+ if (env.type !== type)
182
+ continue;
183
+ const raw = data[field];
184
+ if (raw === undefined)
185
+ continue;
186
+ for (const value of indexValues(raw)) {
187
+ const enc = encodeValue(value);
188
+ if (enc === undefined)
189
+ continue;
190
+ const idx = indexKey(type, field, enc);
191
+ keysForId.add(idx);
192
+ const bucket = this.postings.get(idx) ?? [];
193
+ if (!bucket.includes(id))
194
+ bucket.push(id);
195
+ this.postings.set(idx, bucket);
196
+ }
197
+ }
198
+ if (keysForId.size > 0)
199
+ this.idKeys.set(id, keysForId);
200
+ }
62
201
  }
63
202
  function parseSnapshot(snapshot, capacity) {
64
203
  if (snapshot === null || typeof snapshot !== 'object' || Array.isArray(snapshot)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chaincraft/core",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "A platform for decentralized protocols — wire, objects, and pedagogical consensus",
5
5
  "license": "MIT",
6
6
  "type": "module",