@optimystic/db-p2p 0.0.1
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/dist/index.min.js +52 -0
- package/dist/index.min.js.map +7 -0
- package/dist/src/cluster/client.d.ts +12 -0
- package/dist/src/cluster/client.d.ts.map +1 -0
- package/dist/src/cluster/client.js +65 -0
- package/dist/src/cluster/client.js.map +1 -0
- package/dist/src/cluster/cluster-repo.d.ts +79 -0
- package/dist/src/cluster/cluster-repo.d.ts.map +1 -0
- package/dist/src/cluster/cluster-repo.js +613 -0
- package/dist/src/cluster/cluster-repo.js.map +1 -0
- package/dist/src/cluster/partition-detector.d.ts +59 -0
- package/dist/src/cluster/partition-detector.d.ts.map +1 -0
- package/dist/src/cluster/partition-detector.js +129 -0
- package/dist/src/cluster/partition-detector.js.map +1 -0
- package/dist/src/cluster/service.d.ts +49 -0
- package/dist/src/cluster/service.d.ts.map +1 -0
- package/dist/src/cluster/service.js +107 -0
- package/dist/src/cluster/service.js.map +1 -0
- package/dist/src/index.d.ts +29 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +29 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/it-utility.d.ts +4 -0
- package/dist/src/it-utility.d.ts.map +1 -0
- package/dist/src/it-utility.js +32 -0
- package/dist/src/it-utility.js.map +1 -0
- package/dist/src/libp2p-key-network.d.ts +59 -0
- package/dist/src/libp2p-key-network.d.ts.map +1 -0
- package/dist/src/libp2p-key-network.js +278 -0
- package/dist/src/libp2p-key-network.js.map +1 -0
- package/dist/src/libp2p-node.d.ts +28 -0
- package/dist/src/libp2p-node.d.ts.map +1 -0
- package/dist/src/libp2p-node.js +270 -0
- package/dist/src/libp2p-node.js.map +1 -0
- package/dist/src/logger.d.ts +3 -0
- package/dist/src/logger.d.ts.map +1 -0
- package/dist/src/logger.js +6 -0
- package/dist/src/logger.js.map +1 -0
- package/dist/src/network/get-network-manager.d.ts +4 -0
- package/dist/src/network/get-network-manager.d.ts.map +1 -0
- package/dist/src/network/get-network-manager.js +17 -0
- package/dist/src/network/get-network-manager.js.map +1 -0
- package/dist/src/network/network-manager-service.d.ts +82 -0
- package/dist/src/network/network-manager-service.d.ts.map +1 -0
- package/dist/src/network/network-manager-service.js +283 -0
- package/dist/src/network/network-manager-service.js.map +1 -0
- package/dist/src/peer-utils.d.ts +2 -0
- package/dist/src/peer-utils.d.ts.map +1 -0
- package/dist/src/peer-utils.js +28 -0
- package/dist/src/peer-utils.js.map +1 -0
- package/dist/src/protocol-client.d.ts +12 -0
- package/dist/src/protocol-client.d.ts.map +1 -0
- package/dist/src/protocol-client.js +34 -0
- package/dist/src/protocol-client.js.map +1 -0
- package/dist/src/repo/client.d.ts +17 -0
- package/dist/src/repo/client.d.ts.map +1 -0
- package/dist/src/repo/client.js +82 -0
- package/dist/src/repo/client.js.map +1 -0
- package/dist/src/repo/cluster-coordinator.d.ts +59 -0
- package/dist/src/repo/cluster-coordinator.d.ts.map +1 -0
- package/dist/src/repo/cluster-coordinator.js +539 -0
- package/dist/src/repo/cluster-coordinator.js.map +1 -0
- package/dist/src/repo/coordinator-repo.d.ts +29 -0
- package/dist/src/repo/coordinator-repo.d.ts.map +1 -0
- package/dist/src/repo/coordinator-repo.js +102 -0
- package/dist/src/repo/coordinator-repo.js.map +1 -0
- package/dist/src/repo/redirect.d.ts +14 -0
- package/dist/src/repo/redirect.d.ts.map +1 -0
- package/dist/src/repo/redirect.js +9 -0
- package/dist/src/repo/redirect.js.map +1 -0
- package/dist/src/repo/service.d.ts +52 -0
- package/dist/src/repo/service.d.ts.map +1 -0
- package/dist/src/repo/service.js +181 -0
- package/dist/src/repo/service.js.map +1 -0
- package/dist/src/repo/types.d.ts +7 -0
- package/dist/src/repo/types.d.ts.map +1 -0
- package/dist/src/repo/types.js +2 -0
- package/dist/src/repo/types.js.map +1 -0
- package/dist/src/routing/libp2p-known-peers.d.ts +4 -0
- package/dist/src/routing/libp2p-known-peers.d.ts.map +1 -0
- package/dist/src/routing/libp2p-known-peers.js +19 -0
- package/dist/src/routing/libp2p-known-peers.js.map +1 -0
- package/dist/src/routing/responsibility.d.ts +14 -0
- package/dist/src/routing/responsibility.d.ts.map +1 -0
- package/dist/src/routing/responsibility.js +45 -0
- package/dist/src/routing/responsibility.js.map +1 -0
- package/dist/src/routing/simple-cluster-coordinator.d.ts +23 -0
- package/dist/src/routing/simple-cluster-coordinator.d.ts.map +1 -0
- package/dist/src/routing/simple-cluster-coordinator.js +59 -0
- package/dist/src/routing/simple-cluster-coordinator.js.map +1 -0
- package/dist/src/storage/arachnode-fret-adapter.d.ts +65 -0
- package/dist/src/storage/arachnode-fret-adapter.d.ts.map +1 -0
- package/dist/src/storage/arachnode-fret-adapter.js +93 -0
- package/dist/src/storage/arachnode-fret-adapter.js.map +1 -0
- package/dist/src/storage/block-storage.d.ts +31 -0
- package/dist/src/storage/block-storage.d.ts.map +1 -0
- package/dist/src/storage/block-storage.js +154 -0
- package/dist/src/storage/block-storage.js.map +1 -0
- package/dist/src/storage/file-storage.d.ts +30 -0
- package/dist/src/storage/file-storage.d.ts.map +1 -0
- package/dist/src/storage/file-storage.js +127 -0
- package/dist/src/storage/file-storage.js.map +1 -0
- package/dist/src/storage/helpers.d.ts +3 -0
- package/dist/src/storage/helpers.d.ts.map +1 -0
- package/dist/src/storage/helpers.js +28 -0
- package/dist/src/storage/helpers.js.map +1 -0
- package/dist/src/storage/i-block-storage.d.ts +32 -0
- package/dist/src/storage/i-block-storage.d.ts.map +1 -0
- package/dist/src/storage/i-block-storage.js +2 -0
- package/dist/src/storage/i-block-storage.js.map +1 -0
- package/dist/src/storage/i-raw-storage.d.ts +20 -0
- package/dist/src/storage/i-raw-storage.d.ts.map +1 -0
- package/dist/src/storage/i-raw-storage.js +2 -0
- package/dist/src/storage/i-raw-storage.js.map +1 -0
- package/dist/src/storage/memory-storage.d.ts +27 -0
- package/dist/src/storage/memory-storage.d.ts.map +1 -0
- package/dist/src/storage/memory-storage.js +87 -0
- package/dist/src/storage/memory-storage.js.map +1 -0
- package/dist/src/storage/restoration-coordinator-v2.d.ts +63 -0
- package/dist/src/storage/restoration-coordinator-v2.d.ts.map +1 -0
- package/dist/src/storage/restoration-coordinator-v2.js +157 -0
- package/dist/src/storage/restoration-coordinator-v2.js.map +1 -0
- package/dist/src/storage/ring-selector.d.ts +56 -0
- package/dist/src/storage/ring-selector.d.ts.map +1 -0
- package/dist/src/storage/ring-selector.js +118 -0
- package/dist/src/storage/ring-selector.js.map +1 -0
- package/dist/src/storage/storage-monitor.d.ts +23 -0
- package/dist/src/storage/storage-monitor.d.ts.map +1 -0
- package/dist/src/storage/storage-monitor.js +40 -0
- package/dist/src/storage/storage-monitor.js.map +1 -0
- package/dist/src/storage/storage-repo.d.ts +17 -0
- package/dist/src/storage/storage-repo.d.ts.map +1 -0
- package/dist/src/storage/storage-repo.js +267 -0
- package/dist/src/storage/storage-repo.js.map +1 -0
- package/dist/src/storage/struct.d.ts +29 -0
- package/dist/src/storage/struct.d.ts.map +1 -0
- package/dist/src/storage/struct.js +2 -0
- package/dist/src/storage/struct.js.map +1 -0
- package/dist/src/sync/client.d.ts +27 -0
- package/dist/src/sync/client.d.ts.map +1 -0
- package/dist/src/sync/client.js +32 -0
- package/dist/src/sync/client.js.map +1 -0
- package/dist/src/sync/protocol.d.ts +58 -0
- package/dist/src/sync/protocol.d.ts.map +1 -0
- package/dist/src/sync/protocol.js +12 -0
- package/dist/src/sync/protocol.js.map +1 -0
- package/dist/src/sync/service.d.ts +62 -0
- package/dist/src/sync/service.d.ts.map +1 -0
- package/dist/src/sync/service.js +168 -0
- package/dist/src/sync/service.js.map +1 -0
- package/package.json +73 -0
- package/readme.md +497 -0
- package/src/cluster/client.ts +63 -0
- package/src/cluster/cluster-repo.ts +711 -0
- package/src/cluster/partition-detector.ts +158 -0
- package/src/cluster/service.ts +156 -0
- package/src/index.ts +30 -0
- package/src/it-utility.ts +36 -0
- package/src/libp2p-key-network.ts +334 -0
- package/src/libp2p-node.ts +335 -0
- package/src/logger.ts +9 -0
- package/src/network/get-network-manager.ts +17 -0
- package/src/network/network-manager-service.ts +334 -0
- package/src/peer-utils.ts +24 -0
- package/src/protocol-client.ts +54 -0
- package/src/repo/client.ts +112 -0
- package/src/repo/cluster-coordinator.ts +592 -0
- package/src/repo/coordinator-repo.ts +137 -0
- package/src/repo/redirect.ts +17 -0
- package/src/repo/service.ts +219 -0
- package/src/repo/types.ts +7 -0
- package/src/routing/libp2p-known-peers.ts +26 -0
- package/src/routing/responsibility.ts +63 -0
- package/src/routing/simple-cluster-coordinator.ts +70 -0
- package/src/storage/arachnode-fret-adapter.ts +128 -0
- package/src/storage/block-storage.ts +182 -0
- package/src/storage/file-storage.ts +163 -0
- package/src/storage/helpers.ts +29 -0
- package/src/storage/i-block-storage.ts +40 -0
- package/src/storage/i-raw-storage.ts +30 -0
- package/src/storage/memory-storage.ts +108 -0
- package/src/storage/restoration-coordinator-v2.ts +191 -0
- package/src/storage/ring-selector.ts +155 -0
- package/src/storage/storage-monitor.ts +59 -0
- package/src/storage/storage-repo.ts +320 -0
- package/src/storage/struct.ts +34 -0
- package/src/sync/client.ts +42 -0
- package/src/sync/protocol.ts +71 -0
- package/src/sync/service.ts +229 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { PendRequest, ActionBlocks, IRepo, MessageOptions, CommitResult, GetBlockResults, PendResult, BlockGets, CommitRequest, IKeyNetwork, ICluster, ClusterConsensusConfig } from "@optimystic/db-core";
|
|
2
|
+
import type { ClusterClient } from "../cluster/client.js";
|
|
3
|
+
import type { PeerId } from "@libp2p/interface";
|
|
4
|
+
import type { FretService } from "p2p-fret";
|
|
5
|
+
interface CoordinatorRepoComponents {
|
|
6
|
+
storageRepo: IRepo;
|
|
7
|
+
localCluster?: ICluster;
|
|
8
|
+
localPeerId?: PeerId;
|
|
9
|
+
}
|
|
10
|
+
export declare function coordinatorRepo(keyNetwork: IKeyNetwork, createClusterClient: (peerId: PeerId) => ClusterClient, cfg?: Partial<ClusterConsensusConfig> & {
|
|
11
|
+
clusterSize?: number;
|
|
12
|
+
}, fretService?: FretService): (components: CoordinatorRepoComponents) => CoordinatorRepo;
|
|
13
|
+
/** Cluster coordination repo - uses local store, as well as distributes changes to other nodes using cluster consensus. */
|
|
14
|
+
export declare class CoordinatorRepo implements IRepo {
|
|
15
|
+
readonly keyNetwork: IKeyNetwork;
|
|
16
|
+
readonly createClusterClient: (peerId: PeerId) => ClusterClient;
|
|
17
|
+
private readonly storageRepo;
|
|
18
|
+
private coordinator;
|
|
19
|
+
private readonly DEFAULT_TIMEOUT;
|
|
20
|
+
constructor(keyNetwork: IKeyNetwork, createClusterClient: (peerId: PeerId) => ClusterClient, storageRepo: IRepo, cfg?: Partial<ClusterConsensusConfig> & {
|
|
21
|
+
clusterSize?: number;
|
|
22
|
+
}, localCluster?: ICluster, localPeerId?: PeerId, fretService?: FretService);
|
|
23
|
+
get(blockGets: BlockGets, options?: MessageOptions): Promise<GetBlockResults>;
|
|
24
|
+
pend(request: PendRequest, options?: MessageOptions): Promise<PendResult>;
|
|
25
|
+
cancel(actionRef: ActionBlocks, options?: MessageOptions): Promise<void>;
|
|
26
|
+
commit(request: CommitRequest, options?: MessageOptions): Promise<CommitResult>;
|
|
27
|
+
}
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=coordinator-repo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coordinator-repo.d.ts","sourceRoot":"","sources":["../../../src/repo/coordinator-repo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,eAAe,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAe,WAAW,EAAE,QAAQ,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAE7N,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,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CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,UAAU,CAAC;IAwBzE,MAAM,CAAC,SAAS,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IA6BxE,MAAM,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,YAAY,CAAC;CA2BrF"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ClusterCoordinator } from "./cluster-coordinator.js";
|
|
2
|
+
export function coordinatorRepo(keyNetwork, createClusterClient, cfg, fretService) {
|
|
3
|
+
return (components) => new CoordinatorRepo(keyNetwork, createClusterClient, components.storageRepo, cfg, components.localCluster, components.localPeerId, fretService);
|
|
4
|
+
}
|
|
5
|
+
/** Cluster coordination repo - uses local store, as well as distributes changes to other nodes using cluster consensus. */
|
|
6
|
+
export class CoordinatorRepo {
|
|
7
|
+
keyNetwork;
|
|
8
|
+
createClusterClient;
|
|
9
|
+
storageRepo;
|
|
10
|
+
coordinator;
|
|
11
|
+
DEFAULT_TIMEOUT = 30000; // 30 seconds default timeout
|
|
12
|
+
constructor(keyNetwork, createClusterClient, storageRepo, cfg, localCluster, localPeerId, fretService) {
|
|
13
|
+
this.keyNetwork = keyNetwork;
|
|
14
|
+
this.createClusterClient = createClusterClient;
|
|
15
|
+
this.storageRepo = storageRepo;
|
|
16
|
+
const policy = {
|
|
17
|
+
clusterSize: cfg?.clusterSize ?? 10,
|
|
18
|
+
superMajorityThreshold: cfg?.superMajorityThreshold ?? 0.75,
|
|
19
|
+
simpleMajorityThreshold: cfg?.simpleMajorityThreshold ?? 0.51,
|
|
20
|
+
minAbsoluteClusterSize: cfg?.minAbsoluteClusterSize ?? 3,
|
|
21
|
+
allowClusterDownsize: cfg?.allowClusterDownsize ?? true,
|
|
22
|
+
clusterSizeTolerance: cfg?.clusterSizeTolerance ?? 0.5,
|
|
23
|
+
partitionDetectionWindow: cfg?.partitionDetectionWindow ?? 60000
|
|
24
|
+
};
|
|
25
|
+
const localClusterRef = localCluster && localPeerId ? { update: localCluster.update.bind(localCluster), peerId: localPeerId } : undefined;
|
|
26
|
+
this.coordinator = new ClusterCoordinator(keyNetwork, createClusterClient, policy, localClusterRef, fretService);
|
|
27
|
+
}
|
|
28
|
+
async get(blockGets, options) {
|
|
29
|
+
// TODO: Verify that we are a proximate node for all block IDs in the request
|
|
30
|
+
// For read operations, just use the local store
|
|
31
|
+
// TODO: Implement read-path cluster verification without creating full 2PC transactions
|
|
32
|
+
return await this.storageRepo.get(blockGets, options);
|
|
33
|
+
}
|
|
34
|
+
async pend(request, options) {
|
|
35
|
+
const allBlockIds = Object.keys(request.transforms);
|
|
36
|
+
const coordinatingBlockIds = options?.coordinatingBlockIds ?? allBlockIds;
|
|
37
|
+
const peerCount = await this.coordinator.getClusterSize(coordinatingBlockIds[0]);
|
|
38
|
+
if (peerCount <= 1) {
|
|
39
|
+
return await this.storageRepo.pend(request, options);
|
|
40
|
+
}
|
|
41
|
+
const message = {
|
|
42
|
+
operations: [{ pend: request }],
|
|
43
|
+
expiration: options?.expiration ?? Date.now() + this.DEFAULT_TIMEOUT,
|
|
44
|
+
coordinatingBlockIds
|
|
45
|
+
};
|
|
46
|
+
try {
|
|
47
|
+
await this.coordinator.executeClusterTransaction(coordinatingBlockIds[0], message, options);
|
|
48
|
+
return await this.storageRepo.pend(request, options);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error('Failed to complete pend operation:', error);
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async cancel(actionRef, options) {
|
|
56
|
+
// TODO: Verify that we are a proximate node for all block IDs in the request
|
|
57
|
+
// Extract all block IDs affected by this cancel operation
|
|
58
|
+
const blockIds = actionRef.blockIds;
|
|
59
|
+
// Create a message for this cancel operation with timeout
|
|
60
|
+
const message = {
|
|
61
|
+
operations: [{ cancel: { actionRef } }],
|
|
62
|
+
expiration: options?.expiration ?? Date.now() + this.DEFAULT_TIMEOUT
|
|
63
|
+
};
|
|
64
|
+
try {
|
|
65
|
+
// For each block ID, execute a cluster transaction
|
|
66
|
+
const clusterPromises = blockIds.map(blockId => this.coordinator.executeClusterTransaction(blockId, message, options));
|
|
67
|
+
// Wait for all cluster transactions to complete
|
|
68
|
+
await Promise.all(clusterPromises);
|
|
69
|
+
// If all cluster transactions succeeded, apply the cancel to the local store
|
|
70
|
+
await this.storageRepo.cancel(actionRef, options);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.error('Failed to complete cancel operation:', error);
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async commit(request, options) {
|
|
78
|
+
// TODO: Verify that we are a proximate node for all block IDs in the request
|
|
79
|
+
// Extract all block IDs affected by this commit operation
|
|
80
|
+
const blockIds = request.blockIds;
|
|
81
|
+
const peerCount = await this.coordinator.getClusterSize(blockIds[0]);
|
|
82
|
+
if (peerCount <= 1) {
|
|
83
|
+
return await this.storageRepo.commit(request, options);
|
|
84
|
+
}
|
|
85
|
+
// Create a single message for the entire commit operation
|
|
86
|
+
const message = {
|
|
87
|
+
operations: [{ commit: request }],
|
|
88
|
+
expiration: options?.expiration ?? Date.now() + this.DEFAULT_TIMEOUT
|
|
89
|
+
};
|
|
90
|
+
try {
|
|
91
|
+
// Execute cluster transaction using the first block ID
|
|
92
|
+
// All blocks in this operation should map to the same cluster
|
|
93
|
+
await this.coordinator.executeClusterTransaction(blockIds[0], message, options);
|
|
94
|
+
return await this.storageRepo.commit(request, options);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
console.error('Failed to complete commit operation:', error);
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=coordinator-repo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coordinator-repo.js","sourceRoot":"","sources":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CAAE,CAAC,CAAA;QACrE,IAAI,SAAS,IAAI,CAAC,EAAE,CAAC;YACnB,OAAO,MAAM,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACxD,CAAC;QAEH,0DAA0D;QAC1D,MAAM,OAAO,GAAgB;YAC5B,UAAU,EAAE,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;YACjC,UAAU,EAAE,OAAO,EAAE,UAAU,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,eAAe;SACpE,CAAC;QAEA,IAAI,CAAC;YACN,uDAAuD;YACvD,8DAA8D;YAC9D,MAAM,IAAI,CAAC,WAAW,CAAC,yBAAyB,CAAC,QAAQ,CAAC,CAAC,CAAE,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;YACjF,OAAO,MAAM,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACtD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,sCAAsC,EAAE,KAAK,CAAC,CAAA;YAC5D,MAAM,KAAK,CAAA;QACb,CAAC;IACJ,CAAC;CACD"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type RedirectPayload = {
|
|
2
|
+
redirect: {
|
|
3
|
+
peers: Array<{
|
|
4
|
+
id: string;
|
|
5
|
+
addrs: string[];
|
|
6
|
+
}>;
|
|
7
|
+
reason: 'not_in_cluster';
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export declare function encodePeers(peers: Array<{
|
|
11
|
+
id: string;
|
|
12
|
+
addrs: string[];
|
|
13
|
+
}>): RedirectPayload;
|
|
14
|
+
//# sourceMappingURL=redirect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redirect.d.ts","sourceRoot":"","sources":["../../../src/repo/redirect.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,EAAE;QACR,KAAK,EAAE,KAAK,CAAC;YAAE,EAAE,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,EAAE,CAAA;SAAE,CAAC,CAAA;QAC7C,MAAM,EAAE,gBAAgB,CAAA;KACzB,CAAA;CACF,CAAA;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,GAAG,eAAe,CAO1F"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redirect.js","sourceRoot":"","sources":["../../../src/repo/redirect.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,WAAW,CAAC,KAA6C;IACvE,OAAO;QACL,QAAQ,EAAE;YACR,KAAK;YACL,MAAM,EAAE,gBAAgB;SACzB;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Startable, Logger, IncomingStreamData } from '@libp2p/interface';
|
|
2
|
+
import type { IRepo } from '@optimystic/db-core';
|
|
3
|
+
interface BaseComponents {
|
|
4
|
+
logger: {
|
|
5
|
+
forComponent: (name: string) => Logger;
|
|
6
|
+
};
|
|
7
|
+
registrar: {
|
|
8
|
+
handle: (protocol: string, handler: (data: IncomingStreamData) => void, options: any) => Promise<void>;
|
|
9
|
+
unhandle: (protocol: string) => Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export type RepoServiceComponents = BaseComponents & {
|
|
13
|
+
repo: IRepo;
|
|
14
|
+
};
|
|
15
|
+
export type RepoServiceInit = {
|
|
16
|
+
protocol?: string;
|
|
17
|
+
protocolPrefix?: string;
|
|
18
|
+
maxInboundStreams?: number;
|
|
19
|
+
maxOutboundStreams?: number;
|
|
20
|
+
logPrefix?: string;
|
|
21
|
+
kBucketSize?: number;
|
|
22
|
+
};
|
|
23
|
+
export declare function repoService(init?: RepoServiceInit): (components: RepoServiceComponents) => RepoService;
|
|
24
|
+
/**
|
|
25
|
+
* A libp2p service that handles repo protocol messages
|
|
26
|
+
*/
|
|
27
|
+
export declare class RepoService implements Startable {
|
|
28
|
+
private readonly protocol;
|
|
29
|
+
private readonly maxInboundStreams;
|
|
30
|
+
private readonly maxOutboundStreams;
|
|
31
|
+
private readonly log;
|
|
32
|
+
private readonly repo;
|
|
33
|
+
private readonly components;
|
|
34
|
+
private running;
|
|
35
|
+
private readonly k;
|
|
36
|
+
constructor(components: RepoServiceComponents, init?: RepoServiceInit);
|
|
37
|
+
readonly [Symbol.toStringTag] = "@libp2p/repo-service";
|
|
38
|
+
/**
|
|
39
|
+
* Start the service
|
|
40
|
+
*/
|
|
41
|
+
start(): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Stop the service
|
|
44
|
+
*/
|
|
45
|
+
stop(): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Handle incoming streams on the repo protocol
|
|
48
|
+
*/
|
|
49
|
+
private handleIncomingStream;
|
|
50
|
+
}
|
|
51
|
+
export {};
|
|
52
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/repo/service.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC9E,OAAO,KAAK,EAAE,KAAK,EAAe,MAAM,qBAAqB,CAAA;AAS7D,UAAU,cAAc;IACvB,MAAM,EAAE;QAAE,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAA;KAAE,CAAC;IACnD,SAAS,EAAE;QACV,MAAM,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,kBAAkB,KAAK,IAAI,EAAE,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC,I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
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { pipe } from 'it-pipe';
|
|
2
|
+
import { decode as lpDecode, encode as lpEncode } from 'it-length-prefixed';
|
|
3
|
+
import { peersEqual } from '../peer-utils.js';
|
|
4
|
+
import { sha256 } from 'multiformats/hashes/sha2';
|
|
5
|
+
import { encodePeers } from './redirect.js';
|
|
6
|
+
export function repoService(init = {}) {
|
|
7
|
+
return (components) => new RepoService(components, init);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A libp2p service that handles repo protocol messages
|
|
11
|
+
*/
|
|
12
|
+
export class RepoService {
|
|
13
|
+
protocol;
|
|
14
|
+
maxInboundStreams;
|
|
15
|
+
maxOutboundStreams;
|
|
16
|
+
log;
|
|
17
|
+
repo;
|
|
18
|
+
components;
|
|
19
|
+
running;
|
|
20
|
+
k;
|
|
21
|
+
constructor(components, init = {}) {
|
|
22
|
+
this.components = components;
|
|
23
|
+
const computed = init.protocol ?? (init.protocolPrefix ?? '/db-p2p') + '/repo/1.0.0';
|
|
24
|
+
this.protocol = computed;
|
|
25
|
+
this.maxInboundStreams = init.maxInboundStreams ?? 32;
|
|
26
|
+
this.maxOutboundStreams = init.maxOutboundStreams ?? 64;
|
|
27
|
+
this.log = components.logger.forComponent(init.logPrefix ?? 'db-p2p:repo-service');
|
|
28
|
+
this.repo = components.repo;
|
|
29
|
+
this.running = false;
|
|
30
|
+
this.k = init.kBucketSize ?? 10;
|
|
31
|
+
}
|
|
32
|
+
[Symbol.toStringTag] = '@libp2p/repo-service';
|
|
33
|
+
/**
|
|
34
|
+
* Start the service
|
|
35
|
+
*/
|
|
36
|
+
async start() {
|
|
37
|
+
if (this.running) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
await this.components.registrar.handle(this.protocol, this.handleIncomingStream.bind(this), {
|
|
41
|
+
maxInboundStreams: this.maxInboundStreams,
|
|
42
|
+
maxOutboundStreams: this.maxOutboundStreams
|
|
43
|
+
});
|
|
44
|
+
this.running = true;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Stop the service
|
|
48
|
+
*/
|
|
49
|
+
async stop() {
|
|
50
|
+
if (!this.running) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await this.components.registrar.unhandle(this.protocol);
|
|
54
|
+
this.running = false;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Handle incoming streams on the repo protocol
|
|
58
|
+
*/
|
|
59
|
+
handleIncomingStream(data) {
|
|
60
|
+
const { stream, connection } = data;
|
|
61
|
+
const peerId = connection.remotePeer;
|
|
62
|
+
const processStream = async function* (source) {
|
|
63
|
+
for await (const msg of source) {
|
|
64
|
+
// Decode the message
|
|
65
|
+
const decoded = new TextDecoder().decode(msg.subarray());
|
|
66
|
+
const message = JSON.parse(decoded);
|
|
67
|
+
// Process each operation
|
|
68
|
+
const operation = message.operations[0];
|
|
69
|
+
let response;
|
|
70
|
+
if ('get' in operation) {
|
|
71
|
+
{
|
|
72
|
+
// Use sha256 digest of block id string for consistent key space
|
|
73
|
+
const mh = await sha256.digest(new TextEncoder().encode(operation.get.blockIds[0]));
|
|
74
|
+
const key = mh.digest;
|
|
75
|
+
const nm = this.components.libp2p?.services?.networkManager;
|
|
76
|
+
if (nm?.getCluster) {
|
|
77
|
+
const cluster = await nm.getCluster(key);
|
|
78
|
+
message.cluster = cluster.map(p => p.toString?.() ?? String(p));
|
|
79
|
+
const selfId = this.components.libp2p.peerId;
|
|
80
|
+
const isMember = cluster.some((p) => peersEqual(p, selfId));
|
|
81
|
+
const smallMesh = cluster.length < this.k;
|
|
82
|
+
if (!smallMesh && !isMember) {
|
|
83
|
+
const peers = cluster.filter((p) => !peersEqual(p, selfId));
|
|
84
|
+
console.debug('repo-service:redirect', {
|
|
85
|
+
peerId: selfId.toString(),
|
|
86
|
+
reason: 'not-cluster-member',
|
|
87
|
+
operation: 'get',
|
|
88
|
+
blockId: operation.get.blockIds[0],
|
|
89
|
+
cluster: cluster.map((p) => p.toString?.() ?? String(p))
|
|
90
|
+
});
|
|
91
|
+
response = encodePeers(peers.map((pid) => ({ id: pid.toString(), addrs: [] })));
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
response = await this.repo.get(operation.get, { expiration: message.expiration });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
response = await this.repo.get(operation.get, { expiration: message.expiration });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else if ('pend' in operation) {
|
|
103
|
+
{
|
|
104
|
+
const id = Object.keys(operation.pend.transforms)[0];
|
|
105
|
+
const mh = await sha256.digest(new TextEncoder().encode(id));
|
|
106
|
+
const key = mh.digest;
|
|
107
|
+
const nm = this.components.libp2p?.services?.networkManager;
|
|
108
|
+
if (nm?.getCluster) {
|
|
109
|
+
const cluster = await nm.getCluster(key);
|
|
110
|
+
message.cluster = cluster.map(p => p.toString?.() ?? String(p));
|
|
111
|
+
const selfId = this.components.libp2p.peerId;
|
|
112
|
+
const isMember = cluster.some((p) => peersEqual(p, selfId));
|
|
113
|
+
const smallMesh = cluster.length < this.k;
|
|
114
|
+
if (!smallMesh && !isMember) {
|
|
115
|
+
const peers = cluster.filter((p) => !peersEqual(p, selfId));
|
|
116
|
+
console.debug('repo-service:redirect', {
|
|
117
|
+
peerId: selfId.toString(),
|
|
118
|
+
reason: 'not-cluster-member',
|
|
119
|
+
operation: 'pend',
|
|
120
|
+
blockId: id,
|
|
121
|
+
cluster: cluster.map((p) => p.toString?.() ?? String(p))
|
|
122
|
+
});
|
|
123
|
+
response = encodePeers(peers.map((pid) => ({ id: pid.toString(), addrs: [] })));
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
response = await this.repo.pend(operation.pend, { expiration: message.expiration });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
response = await this.repo.pend(operation.pend, { expiration: message.expiration });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else if ('cancel' in operation) {
|
|
135
|
+
response = await this.repo.cancel(operation.cancel.actionRef, {
|
|
136
|
+
expiration: message.expiration
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
else if ('commit' in operation) {
|
|
140
|
+
{
|
|
141
|
+
const mh = await sha256.digest(new TextEncoder().encode(operation.commit.tailId));
|
|
142
|
+
const key = mh.digest;
|
|
143
|
+
const nm = this.components.libp2p?.services?.networkManager;
|
|
144
|
+
if (nm?.getCluster) {
|
|
145
|
+
const cluster = await nm.getCluster(key);
|
|
146
|
+
message.cluster = cluster.map(p => p.toString?.() ?? String(p));
|
|
147
|
+
const selfId = this.components.libp2p.peerId;
|
|
148
|
+
const isMember = cluster.some((p) => peersEqual(p, selfId));
|
|
149
|
+
const smallMesh = cluster.length < this.k;
|
|
150
|
+
if (!smallMesh && !isMember) {
|
|
151
|
+
const peers = cluster.filter((p) => !peersEqual(p, selfId));
|
|
152
|
+
console.debug('repo-service:redirect', {
|
|
153
|
+
peerId: selfId.toString(),
|
|
154
|
+
reason: 'not-cluster-member',
|
|
155
|
+
operation: 'commit',
|
|
156
|
+
tailId: operation.commit.tailId,
|
|
157
|
+
cluster: cluster.map((p) => p.toString?.() ?? String(p))
|
|
158
|
+
});
|
|
159
|
+
response = encodePeers(peers.map((pid) => ({ id: pid.toString(), addrs: [] })));
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
response = await this.repo.commit(operation.commit, { expiration: message.expiration });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
response = await this.repo.commit(operation.commit, { expiration: message.expiration });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// Encode and yield the response
|
|
171
|
+
yield new TextEncoder().encode(JSON.stringify(response));
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
Promise.resolve().then(async () => {
|
|
175
|
+
await pipe(stream, (source) => lpDecode(source), processStream.bind(this), (source) => lpEncode(source), stream);
|
|
176
|
+
}).catch(err => {
|
|
177
|
+
this.log.error('error handling repo protocol message from %p - %e', peerId, err);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/repo/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAI3E,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAEjD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAyB3C,MAAM,UAAU,WAAW,CAAC,OAAwB,EAAE;IACrD,OAAO,CAAC,UAAiC,EAAE,EAAE,CAAC,IAAI,WAAW,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;AACjF,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,WAAW;IACN,QAAQ,CAAQ;IAChB,iBAAiB,CAAQ;IACzB,kBAAkB,CAAQ;IAC1B,GAAG,CAAQ;IACX,IAAI,CAAO;IACX,UAAU,CAAuB;IAC1C,OAAO,CAAS;IACP,CAAC,CAAQ;IAE1B,YAAY,UAAiC,EAAE,OAAwB,EAAE;QACxE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,IAAI,CAAC,IAAI,CAAC,cAAc,IAAI,SAAS,CAAC,GAAG,aAAa,CAAA;QACpF,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,iBAAiB,GAAG,IAAI,CAAC,iBAAiB,IAAI,EAAE,CAAA;QACrD,IAAI,CAAC,kB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"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACrC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,gEAAgE;IAChE,KAAK,CAAC,EAAE,MAAM,CAAC;CACf"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/repo/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"libp2p-known-peers.d.ts","sourceRoot":"","sources":["../../../src/routing/libp2p-known-peers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAEpC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAEpD,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,EAAE,CAmB3D"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function buildKnownPeers(libp2p) {
|
|
2
|
+
const self = {
|
|
3
|
+
id: libp2p.peerId,
|
|
4
|
+
addrs: libp2p.getMultiaddrs().map(ma => ma.toString())
|
|
5
|
+
};
|
|
6
|
+
const connections = libp2p.getConnections();
|
|
7
|
+
const byPeer = {};
|
|
8
|
+
for (const c of connections) {
|
|
9
|
+
const pid = c.remotePeer;
|
|
10
|
+
const key = pid.toString();
|
|
11
|
+
const entry = byPeer[key] ?? (byPeer[key] = { id: pid, addrs: new Set() });
|
|
12
|
+
const addrStr = c.remoteAddr?.toString?.();
|
|
13
|
+
if (addrStr)
|
|
14
|
+
entry.addrs.add(addrStr);
|
|
15
|
+
}
|
|
16
|
+
const others = Object.values(byPeer).map(e => ({ id: e.id, addrs: Array.from(e.addrs) }));
|
|
17
|
+
return [self, ...others];
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=libp2p-known-peers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"libp2p-known-peers.js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
|
2
|
+
export type KnownPeer = {
|
|
3
|
+
id: PeerId;
|
|
4
|
+
addrs: string[];
|
|
5
|
+
};
|
|
6
|
+
export type ResponsibilityResult = {
|
|
7
|
+
inCluster: boolean;
|
|
8
|
+
nearest: KnownPeer[];
|
|
9
|
+
};
|
|
10
|
+
export declare function xorDistanceBytes(a: Uint8Array, b: Uint8Array): Uint8Array;
|
|
11
|
+
export declare function lessThanLex(a: Uint8Array, b: Uint8Array): boolean;
|
|
12
|
+
export declare function sortPeersByDistance(peers: KnownPeer[], key: Uint8Array): KnownPeer[];
|
|
13
|
+
export declare function computeResponsibility(key: Uint8Array, self: KnownPeer, others: KnownPeer[], k: number): ResponsibilityResult;
|
|
14
|
+
//# sourceMappingURL=responsibility.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responsibility.d.ts","sourceRoot":"","sources":["../../../src/routing/responsibility.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE/C,MAAM,MAAM,SAAS,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAAA;AAEvD,MAAM,MAAM,oBAAoB,GAAG;IACjC,SAAS,EAAE,OAAO,CAAA;IAClB,OAAO,EAAE,SAAS,EAAE,CAAA;CACrB,CAAA;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,UAAU,CASzE;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CASjE;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,UAAU,GAAG,SAAS,EAAE,CAKpF;AAED,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,SAAS,EACf,MAAM,EAAE,SAAS,EAAE,EACnB,CAAC,EAAE,MAAM,GACR,oBAAoB,CAiBtB"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export function xorDistanceBytes(a, b) {
|
|
2
|
+
const len = Math.max(a.length, b.length);
|
|
3
|
+
const out = new Uint8Array(len);
|
|
4
|
+
for (let i = 0; i < len; i++) {
|
|
5
|
+
const ai = a[a.length - 1 - i] ?? 0;
|
|
6
|
+
const bi = b[b.length - 1 - i] ?? 0;
|
|
7
|
+
out[len - 1 - i] = ai ^ bi;
|
|
8
|
+
}
|
|
9
|
+
return out;
|
|
10
|
+
}
|
|
11
|
+
export function lessThanLex(a, b) {
|
|
12
|
+
const len = Math.max(a.length, b.length);
|
|
13
|
+
for (let i = 0; i < len; i++) {
|
|
14
|
+
const av = a[i] ?? 0;
|
|
15
|
+
const bv = b[i] ?? 0;
|
|
16
|
+
if (av < bv)
|
|
17
|
+
return true;
|
|
18
|
+
if (av > bv)
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
export function sortPeersByDistance(peers, key) {
|
|
24
|
+
return peers
|
|
25
|
+
.map(p => ({ p, d: xorDistanceBytes(p.id.toMultihash().bytes, key) }))
|
|
26
|
+
.sort((a, b) => (lessThanLex(a.d, b.d) ? -1 : 1))
|
|
27
|
+
.map(x => x.p);
|
|
28
|
+
}
|
|
29
|
+
export function computeResponsibility(key, self, others, k) {
|
|
30
|
+
const all = [self, ...others];
|
|
31
|
+
const sorted = sortPeersByDistance(all, key);
|
|
32
|
+
// For small meshes, use a different strategy
|
|
33
|
+
if (all.length <= 3) {
|
|
34
|
+
// With 3 or fewer nodes, the first node in XOR order handles it
|
|
35
|
+
// This ensures only ONE node considers itself responsible
|
|
36
|
+
const inCluster = sorted[0].id.equals(self.id);
|
|
37
|
+
return { inCluster, nearest: sorted };
|
|
38
|
+
}
|
|
39
|
+
// For larger meshes, use traditional k-nearest
|
|
40
|
+
const effectiveK = Math.min(k, Math.max(1, Math.floor(all.length / 2)));
|
|
41
|
+
const topK = sorted.slice(0, effectiveK);
|
|
42
|
+
const inCluster = topK.some(p => p.id.equals(self.id));
|
|
43
|
+
return { inCluster, nearest: topK };
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=responsibility.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responsibility.js","sourceRoot":"","sources":["../../../src/routing/responsibility.ts"],"names":[],"mappings":"AASA,MAAM,UAAU,gBAAgB,CAAC,CAAa,EAAE,CAAa;IAC3D,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAA;IACxC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAA;IAC/B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7B,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAA;QACnC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAA;QACnC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAA;IAC5B,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,CAAa,EAAE,CAAa;IACtD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAA;IACxC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7B,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;QACp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,+CAA+C;IAC/C,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IACvE,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,UAAU,CAAC,CAAA;IACxC,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAA;IACtD,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAA;AACrC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interface';
|
|
2
|
+
export interface SimpleClusterCoordinator {
|
|
3
|
+
selectCoordinator(key: Uint8Array, peers: PeerId[]): Promise<PeerId>;
|
|
4
|
+
selectReplicas(key: Uint8Array, peers: PeerId[], replicationFactor: number): Promise<PeerId[]>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Simple consistent hashing for small clusters
|
|
8
|
+
* Uses modulo arithmetic instead of XOR distance
|
|
9
|
+
*/
|
|
10
|
+
export declare class ModuloCoordinator implements SimpleClusterCoordinator {
|
|
11
|
+
hashPeer(peerId: PeerId): Promise<bigint>;
|
|
12
|
+
hashKey(key: Uint8Array): Promise<bigint>;
|
|
13
|
+
selectCoordinator(key: Uint8Array, peers: PeerId[]): Promise<PeerId>;
|
|
14
|
+
selectReplicas(key: Uint8Array, peers: PeerId[], replicationFactor: number): Promise<PeerId[]>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* For very small clusters, just replicate everywhere
|
|
18
|
+
*/
|
|
19
|
+
export declare class FullReplicationCoordinator implements SimpleClusterCoordinator {
|
|
20
|
+
selectCoordinator(key: Uint8Array, peers: PeerId[]): Promise<PeerId>;
|
|
21
|
+
selectReplicas(key: Uint8Array, peers: PeerId[], replicationFactor: number): Promise<PeerId[]>;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=simple-cluster-coordinator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simple-cluster-coordinator.d.ts","sourceRoot":"","sources":["../../../src/routing/simple-cluster-coordinator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAG/C,MAAM,WAAW,wBAAwB;IACvC,iBAAiB,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IACpE,cAAc,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,iBAAiB,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;CAC/F;AAED;;;GAGG;AACH,qBAAa,iBAAkB,YAAW,wBAAwB;IAC1D,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAOzC,OAAO,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAMzC,iBAAiB,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAUpE,cAAc,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,iBAAiB,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAiBrG;AAED;;GAEG;AACH,qBAAa,0BAA2B,YAAW,wBAAwB;IACnE,iBAAiB,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAMpE,cAAc,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,iBAAiB,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAIrG"}
|