@optimystic/db-p2p 0.1.1 → 0.1.3

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.
Files changed (64) hide show
  1. package/{readme.md → README.md} +7 -0
  2. package/dist/index.min.js +31 -30
  3. package/dist/index.min.js.map +4 -4
  4. package/dist/src/cluster/cluster-repo.d.ts +27 -0
  5. package/dist/src/cluster/cluster-repo.d.ts.map +1 -1
  6. package/dist/src/cluster/cluster-repo.js +139 -18
  7. package/dist/src/cluster/cluster-repo.js.map +1 -1
  8. package/dist/src/cluster/service.d.ts +13 -2
  9. package/dist/src/cluster/service.d.ts.map +1 -1
  10. package/dist/src/cluster/service.js +17 -7
  11. package/dist/src/cluster/service.js.map +1 -1
  12. package/dist/src/index.d.ts +1 -1
  13. package/dist/src/index.d.ts.map +1 -1
  14. package/dist/src/index.js +1 -1
  15. package/dist/src/index.js.map +1 -1
  16. package/dist/src/libp2p-node.d.ts +13 -2
  17. package/dist/src/libp2p-node.d.ts.map +1 -1
  18. package/dist/src/libp2p-node.js +35 -16
  19. package/dist/src/libp2p-node.js.map +1 -1
  20. package/dist/src/protocol-client.d.ts.map +1 -1
  21. package/dist/src/protocol-client.js +8 -7
  22. package/dist/src/protocol-client.js.map +1 -1
  23. package/dist/src/repo/cluster-coordinator.d.ts +7 -2
  24. package/dist/src/repo/cluster-coordinator.d.ts.map +1 -1
  25. package/dist/src/repo/cluster-coordinator.js +18 -3
  26. package/dist/src/repo/cluster-coordinator.js.map +1 -1
  27. package/dist/src/repo/coordinator-repo.d.ts +26 -3
  28. package/dist/src/repo/coordinator-repo.d.ts.map +1 -1
  29. package/dist/src/repo/coordinator-repo.js +117 -22
  30. package/dist/src/repo/coordinator-repo.js.map +1 -1
  31. package/dist/src/repo/service.d.ts +13 -2
  32. package/dist/src/repo/service.d.ts.map +1 -1
  33. package/dist/src/repo/service.js +25 -12
  34. package/dist/src/repo/service.js.map +1 -1
  35. package/dist/src/storage/memory-storage.d.ts +15 -0
  36. package/dist/src/storage/memory-storage.d.ts.map +1 -1
  37. package/dist/src/storage/memory-storage.js +23 -4
  38. package/dist/src/storage/memory-storage.js.map +1 -1
  39. package/dist/src/storage/storage-repo.d.ts.map +1 -1
  40. package/dist/src/storage/storage-repo.js.map +1 -1
  41. package/dist/src/sync/service.d.ts.map +1 -1
  42. package/dist/src/sync/service.js +7 -2
  43. package/dist/src/sync/service.js.map +1 -1
  44. package/package.json +27 -21
  45. package/src/cluster/cluster-repo.ts +836 -711
  46. package/src/cluster/service.ts +44 -31
  47. package/src/index.ts +1 -1
  48. package/src/libp2p-key-network.ts +334 -334
  49. package/src/libp2p-node.ts +371 -339
  50. package/src/network/network-manager-service.ts +334 -334
  51. package/src/protocol-client.ts +53 -54
  52. package/src/repo/client.ts +112 -112
  53. package/src/repo/cluster-coordinator.ts +613 -592
  54. package/src/repo/coordinator-repo.ts +269 -137
  55. package/src/repo/service.ts +237 -219
  56. package/src/storage/block-storage.ts +182 -182
  57. package/src/storage/memory-storage.ts +24 -5
  58. package/src/storage/storage-repo.ts +321 -320
  59. package/src/sync/service.ts +7 -6
  60. package/dist/src/storage/file-storage.d.ts +0 -30
  61. package/dist/src/storage/file-storage.d.ts.map +0 -1
  62. package/dist/src/storage/file-storage.js +0 -127
  63. package/dist/src/storage/file-storage.js.map +0 -1
  64. package/src/storage/file-storage.ts +0 -163
@@ -1,17 +1,13 @@
1
1
  import { pipe } from 'it-pipe';
2
2
  import { decode as lpDecode, encode as lpEncode } from 'it-length-prefixed';
3
- import type { Startable, Logger, IncomingStreamData } from '@libp2p/interface';
3
+ import type { Startable, Logger, Stream, Connection, StreamHandler } from '@libp2p/interface';
4
4
  import type { ICluster, ClusterRecord } from '@optimystic/db-core';
5
- import { computeResponsibility } from '../routing/responsibility.js'
6
- import { peersEqual } from '../peer-utils.js'
7
- import { buildKnownPeers } from '../routing/libp2p-known-peers.js'
8
- import { encodePeers } from '../repo/redirect.js'
9
5
  import type { Uint8ArrayList } from 'uint8arraylist';
10
6
 
11
7
  interface BaseComponents {
12
8
  logger: { forComponent: (name: string) => Logger },
13
9
  registrar: {
14
- handle: (protocol: string, handler: (data: IncomingStreamData) => void, options: any) => Promise<void>,
10
+ handle: (protocol: string, handler: StreamHandler, options: any) => Promise<void>,
15
11
  unhandle: (protocol: string) => Promise<void>
16
12
  }
17
13
  }
@@ -26,10 +22,19 @@ export interface ClusterServiceInit {
26
22
  maxInboundStreams?: number,
27
23
  maxOutboundStreams?: number,
28
24
  logPrefix?: string,
29
- kBucketSize?: number,
30
- configuredClusterSize?: number,
31
- allowClusterDownsize?: boolean,
32
- clusterSizeTolerance?: number,
25
+ kBucketSize?: number,
26
+ configuredClusterSize?: number,
27
+ allowClusterDownsize?: boolean,
28
+ clusterSizeTolerance?: number,
29
+ /**
30
+ * Responsibility K - the replica set size for determining cluster membership.
31
+ * This is distinct from kBucketSize (DHT routing) and configuredClusterSize (consensus quorum).
32
+ * When set, this determines how many peers (by XOR distance) are considered
33
+ * responsible for a key. If this node is not in the top responsibilityK peers,
34
+ * it will redirect requests to closer peers.
35
+ * Default: 1 (only the closest peer handles requests)
36
+ */
37
+ responsibilityK?: number,
33
38
  }
34
39
 
35
40
  export function clusterService(init: ClusterServiceInit = {}): (components: ClusterServiceComponents) => ClusterService {
@@ -47,10 +52,12 @@ export class ClusterService implements Startable {
47
52
  private readonly cluster: ICluster;
48
53
  private readonly components: ClusterServiceComponents;
49
54
  private running: boolean;
50
- private readonly k: number;
51
- private readonly configuredClusterSize: number;
52
- private readonly allowDownsize: boolean;
53
- private readonly sizeTolerance: number;
55
+ private readonly k: number;
56
+ private readonly configuredClusterSize: number;
57
+ private readonly allowDownsize: boolean;
58
+ private readonly sizeTolerance: number;
59
+ /** Responsibility K - how many peers are responsible for a key (for redirect decisions) */
60
+ private readonly responsibilityK: number;
54
61
 
55
62
  constructor(components: ClusterServiceComponents, init: ClusterServiceInit = {}) {
56
63
  this.components = components;
@@ -60,10 +67,11 @@ export class ClusterService implements Startable {
60
67
  this.log = components.logger.forComponent(init.logPrefix ?? 'db-p2p:cluster');
61
68
  this.cluster = components.cluster;
62
69
  this.running = false;
63
- this.k = init.kBucketSize ?? 10;
64
- this.configuredClusterSize = init.configuredClusterSize ?? 10;
65
- this.allowDownsize = init.allowClusterDownsize ?? true;
66
- this.sizeTolerance = init.clusterSizeTolerance ?? 0.5;
70
+ this.k = init.kBucketSize ?? 10;
71
+ this.configuredClusterSize = init.configuredClusterSize ?? 10;
72
+ this.allowDownsize = init.allowClusterDownsize ?? true;
73
+ this.sizeTolerance = init.clusterSizeTolerance ?? 0.5;
74
+ this.responsibilityK = init.responsibilityK ?? 1;
67
75
  }
68
76
 
69
77
  readonly [Symbol.toStringTag] = '@libp2p/cluster';
@@ -90,8 +98,7 @@ export class ClusterService implements Startable {
90
98
  this.running = false;
91
99
  }
92
100
 
93
- private handleIncomingStream(data: IncomingStreamData): void {
94
- const { stream, connection } = data;
101
+ private handleIncomingStream(stream: Stream, connection: Connection): void {
95
102
  const peerId = connection.remotePeer;
96
103
 
97
104
  const processStream = async function* (this: ClusterService, source: AsyncIterable<Uint8ArrayList>) {
@@ -141,16 +148,22 @@ export class ClusterService implements Startable {
141
148
  }
142
149
  };
143
150
 
144
- Promise.resolve().then(async () => {
145
- await pipe(
146
- stream,
147
- (source) => lpDecode(source),
148
- processStream.bind(this),
149
- (source) => lpEncode(source),
150
- stream
151
- );
152
- }).catch((err: Error) => {
153
- this.log.error('error handling cluster protocol message from %p - %e', peerId, err);
154
- });
151
+ void (async () => {
152
+ try {
153
+ const responses = pipe(
154
+ stream,
155
+ (source) => lpDecode(source),
156
+ processStream.bind(this),
157
+ (source) => lpEncode(source)
158
+ );
159
+ for await (const chunk of responses) {
160
+ stream.send(chunk);
161
+ }
162
+ await stream.close();
163
+ } catch (err) {
164
+ this.log.error('error handling cluster protocol message from %p - %e', peerId, err);
165
+ stream.abort(err instanceof Error ? err : new Error(String(err)));
166
+ }
167
+ })();
155
168
  }
156
169
  }
package/src/index.ts CHANGED
@@ -7,10 +7,10 @@ export * from "./repo/cluster-coordinator.js";
7
7
  export * from "./repo/coordinator-repo.js";
8
8
  export * from "./repo/service.js";
9
9
  export * from "./storage/block-storage.js";
10
- export * from "./storage/file-storage.js";
11
10
  export * from "./storage/memory-storage.js";
12
11
  export * from "./storage/i-block-storage.js";
13
12
  export * from "./storage/i-raw-storage.js";
13
+ export * from "./storage/struct.js";
14
14
  export * from "./storage/storage-repo.js";
15
15
  export * from "./storage/restoration-coordinator-v2.js";
16
16
  export * from "./storage/ring-selector.js";