Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Shard DHT #3104

Open
wants to merge 10 commits into
base: v6/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 90 additions & 74 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@
"jsonwebtoken": "^9.0.0",
"libp2p": "^0.32.4",
"libp2p-bootstrap": "^0.13.0",
"libp2p-kad-dht": "^0.24.2",
"shard-dht": "./src/modules/network/implementation/shard-dht",
"libp2p-mplex": "^0.10.7",
"libp2p-noise": "^4.0.0",
"libp2p-tcp": "^0.17.2",
"minimist": "^1.2.7",
"ms": "^2.1.3",
"multiaddr": "^10.0.0",
"mysql2": "^3.3.0",
"peer-id": "^0.15.3",
"pino": "^8.4.2",
Expand Down
23 changes: 18 additions & 5 deletions src/modules/network/implementation/libp2p-service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import appRootPath from 'app-root-path';
import libp2p from 'libp2p';
import KadDHT from 'libp2p-kad-dht';
import ShardDHT from 'shard-dht';
import { join } from 'path';
import Bootstrap, { tag } from 'libp2p-bootstrap';
import { NOISE } from 'libp2p-noise';
import MPLEX from 'libp2p-mplex';
import { Multiaddr } from 'multiaddr';
import TCP from 'libp2p-tcp';
import pipe from 'it-pipe';
import map from 'it-map';
Expand All @@ -31,14 +32,11 @@ const devEnvironment =
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST;

const initializationObject = {
addresses: {
listen: ['/ip4/0.0.0.0/tcp/9000'],
},
modules: {
transport: [TCP],
streamMuxer: [MPLEX],
connEncryption: [NOISE],
dht: KadDHT,
dht: ShardDHT,
},
};

Expand All @@ -65,6 +63,9 @@ class Libp2pService {
list: this.config.bootstrap,
},
};
initializationObject.config.dht.bootstrap = this.config.bootstrap.map((b) =>
new Multiaddr(b).getPeerId(),
);
}
initializationObject.addresses = {
listen: [`/ip4/0.0.0.0/tcp/${this.config.port}`], // for production
Expand Down Expand Up @@ -194,6 +195,18 @@ class Libp2pService {
return this.node.peerId;
}

async addRoutingTablePeer(peerIdString, blockchainId) {
return this.node._dht.add(createFromB58String(peerIdString), blockchainId);
}

removeRoutingTablePeer(peerIdString, blockchainId) {
return this.node._dht.remove(createFromB58String(peerIdString), blockchainId);
}

hasRoutingTablePeer(peerIdString) {
return this.node._dht.has(createFromB58String(peerIdString));
}

handleMessage(protocol, handler) {
this.logger.info(`Enabling network protocol: ${protocol}`);

Expand Down
21 changes: 21 additions & 0 deletions src/modules/network/implementation/shard-dht/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Protocol Labs Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions src/modules/network/implementation/shard-dht/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "shard-dht",
"version": "0.1.0",
"description": "Origintrail shard DHT",
"main": "src/index.js",
"files": [
"src"
],
"license": "MIT",
"engines": {
"node": ">=14.0.0"
},
"dependencies": {
"debug": "^4.3.1",
"err-code": "^3.0.0",
"it-first": "^1.0.4",
"it-length": "^1.0.3",
"heap": "~0.2.6",
"it-length-prefixed": "^5.0.3",
"it-pipe": "^1.1.0",
"libp2p-crypto": "^0.19.5",
"libp2p-interfaces": "^1.0.0",
"libp2p-record": "^0.10.4",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.5",
"p-map": "^4.0.0",
"p-queue": "^6.6.2",
"p-timeout": "^4.1.0",
"peer-id": "^0.15.3",
"uint8arrays": "^3.1.0",
"protobufjs": "^6.10.2",
"streaming-iterables": "^6.0.0"
}
}
34 changes: 34 additions & 0 deletions src/modules/network/implementation/shard-dht/src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable no-multi-assign */
// MaxRecordAge specifies the maximum time that any node will hold onto a record
// from the time its received. This does not apply to any other forms of validity that
// the record may contain.
// For example, a record may contain an ipns entry with an EOL saying its valid
// until the year 2020 (a great time in the future). For that record to stick around
// it must be rebroadcasted more frequently than once every 'MaxRecordAge'

const second = (exports.second = 1000);
const minute = (exports.minute = 60 * second);
const hour = (exports.hour = 60 * minute);

exports.MAX_RECORD_AGE = 36 * hour;

exports.PROTOCOL_DHT = '/kad/1.0.0';

exports.PROVIDERS_KEY_PREFIX = '/providers/';

exports.PROVIDERS_LRU_CACHE_SIZE = 256;

exports.PROVIDERS_VALIDITY = 24 * hour;

exports.PROVIDERS_CLEANUP_INTERVAL = hour;

exports.READ_MESSAGE_TIMEOUT = 10 * second;

// The number of records that will be retrieved on a call to getMany()
exports.GET_MANY_RECORD_COUNT = 16;

// K is the maximum number of requests to perform before returning failure
exports.K = 20;

// Alpha is the concurrency for asynchronous requests
exports.ALPHA = 3;
Loading
Loading