Compare commits

...

4 Commits

Author SHA1 Message Date
semantic-release-bot e6aa5039e2 chore(release): 0.1.0-develop.65 [skip ci]
# [0.1.0-develop.65](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.64...v0.1.0-develop.65) (2023-11-18)

### Features

* add hashQuery protocol method ([5391666](5391666388))
2023-11-18 09:49:58 +00:00
Derrick Hammer 479e6b8bd5
Merge remote-tracking branch 'origin/develop' into develop 2023-11-18 04:49:15 -05:00
Derrick Hammer 5391666388
feat: add hashQuery protocol method 2023-11-18 04:49:08 -05:00
Derrick Hammer 8091e7ab36
refactor: move hashQueryRoutingTable to S5Node 2023-11-18 04:48:39 -05:00
8 changed files with 70 additions and 12 deletions

View File

@ -1,3 +1,10 @@
# [0.1.0-develop.65](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.64...v0.1.0-develop.65) (2023-11-18)
### Features
* add hashQuery protocol method ([5391666](https://git.lumeweb.com/LumeWeb/libs5/commit/53916663886b54f4adf5192f17f79e72c457d4be))
# [0.1.0-develop.64](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.63...v0.1.0-develop.64) (2023-11-18)

4
npm-shrinkwrap.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@lumeweb/libs5",
"version": "0.1.0-develop.64",
"version": "0.1.0-develop.65",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@lumeweb/libs5",
"version": "0.1.0-develop.64",
"version": "0.1.0-develop.65",
"dependencies": {
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",

View File

@ -1,6 +1,6 @@
{
"name": "@lumeweb/libs5",
"version": "0.1.0-develop.64",
"version": "0.1.0-develop.65",
"type": "module",
"main": "lib/index.js",
"repository": {

48
src/messages/hashQuery.ts Normal file
View File

@ -0,0 +1,48 @@
import { S5Node } from "#node.js";
import { Peer } from "#types.js";
import Unpacker from "#serialization/unpack.js";
import { Multihash } from "#multihash.js";
export default async function (
node: S5Node,
peer: Peer,
data: Unpacker,
rawData: Uint8Array,
) {
const hash = new Multihash(data.unpackBinary());
const types = data.unpackList().map((item) => Number(item));
try {
const map = await node.getCachedStorageLocations(hash, types);
if (Object.keys(map).length > 0) {
const availableNodes = [...map.keys()];
node.services.p2p.sortNodesByScore(availableNodes);
const entry = map.get(availableNodes[0]);
peer.sendMessage(entry?.providerMessage as Uint8Array);
return;
}
} catch (e) {
node.logger.catched(e);
}
const hashCode = hash.hashCode;
if (node.hashQueryRoutingTable.has(hashCode)) {
if (!node.hashQueryRoutingTable[hashCode].includes(peer.id)) {
node.hashQueryRoutingTable[hashCode].push(peer.id);
}
return;
}
node.hashQueryRoutingTable.set(hashCode, new Set([peer.id]));
for (const p of node.services.p2p.peers.values()) {
if (p.id !== peer.id) {
p.sendMessage(rawData);
}
}
return;
}

View File

@ -2,6 +2,7 @@ import { P2PMessageHandler } from "#types.js";
import handshakeOpen from "#messages/handshakeOpen.js";
import {
protocolMethodHandshakeOpen,
protocolMethodHashQuery,
protocolMethodRegistryQuery,
protocolMethodSignedMessage,
recordTypeRegistryEntry,
@ -11,6 +12,7 @@ import registryQuery from "#messages/registryQuery.js";
import registryEntry from "#messages/registryEntry.js";
import storageLocation from "#messages/storageLocation.js";
import signedMessage from "#messages/signedMessage.js";
import hashQuery from "#messages/hashQuery.js";
const messages = new Map<number, P2PMessageHandler>(
Object.entries({
@ -19,6 +21,7 @@ const messages = new Map<number, P2PMessageHandler>(
[recordTypeRegistryEntry]: registryEntry,
[recordTypeStorageLocation]: storageLocation,
[protocolMethodSignedMessage]: signedMessage,
[protocolMethodHashQuery]: hashQuery,
}).map(([key, value]) => [Number(key), value]),
);

View File

@ -57,7 +57,8 @@ export default async function (
config: node.config,
});
const list = p2p.hashQueryRoutingTable.get(hash) || new Set<NodeId>();
const list =
node.hashQueryRoutingTable.get(hash.hashCode) || new Set<NodeId>();
for (const peerId of list) {
if (peerId.equals(nodeId)) {
continue;
@ -74,5 +75,5 @@ export default async function (
}
}
}
p2p.hashQueryRoutingTable.delete(hash);
node.hashQueryRoutingTable.delete(hash.hashCode);
}

View File

@ -50,6 +50,12 @@ export class S5Node {
private _started = false;
private _hashQueryRoutingTable: Map<number, Set<NodeId>> = new Map();
get hashQueryRoutingTable(): Map<number, Set<NodeId>> {
return this._hashQueryRoutingTable;
}
get started(): boolean {
return this._started;
}

View File

@ -48,13 +48,6 @@ export class P2PService extends EventEmitter {
node.services.p2p = this;
}
private _hashQueryRoutingTable: Map<Multihash, Set<NodeId>> = new Map();
get hashQueryRoutingTable(): Map<Multihash, Set<NodeId>> {
return this._hashQueryRoutingTable;
}
private _networkId?: string;
get networkId(): string {