Skip to content

Commit

Permalink
Merge pull request #2144 from OriginTrail/v6/prerelease/testnet
Browse files Browse the repository at this point in the history
OriginTrail 6.0.0-beta.2.2.8 Testnet Release
  • Loading branch information
zeroxbt authored Oct 20, 2022
2 parents f5cc98f + 35f0743 commit 2418d05
Show file tree
Hide file tree
Showing 19 changed files with 2,521 additions and 3,743 deletions.
9 changes: 9 additions & 0 deletions ot-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import DependencyInjection from './src/service/dependency-injection.js';
import Logger from './src/logger/logger.js';
import { MIN_NODE_VERSION } from './src/constants/constants.js';
import FileService from './src/service/file-service.js';
import NetworkPrivateKeyMigration from './src/migration/network-private-key-migration.js';

const require = createRequire(import.meta.url);
const pjson = require('./package.json');
Expand All @@ -23,6 +24,14 @@ class OTNode {

async start() {
await this.removeUpdateFile();
const migrationManager = new NetworkPrivateKeyMigration(
'NetworkPrivateKeyMigration',
this.logger,
this.config,
);
if (!(await migrationManager.migrationAlreadyExecuted())) {
await migrationManager.migrate();
}

this.logger.info(' ██████╗ ████████╗███╗ ██╗ ██████╗ ██████╗ ███████╗');
this.logger.info('██╔═══██╗╚══██╔══╝████╗ ██║██╔═══██╗██╔══██╗██╔════╝');
Expand Down
5,591 changes: 2,237 additions & 3,354 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 12 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "origintrail_node",
"version": "6.0.0-beta.2.2.7",
"version": "6.0.0-beta.2.2.8",
"description": "OTNode v6 Beta 2",
"main": "index.js",
"type": "module",
Expand Down Expand Up @@ -61,17 +61,8 @@
"solhint": "^3.3.7"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^8.0.1",
"@comunica/query-sparql": "^2.4.3",
"@libp2p/bootstrap": "^2.0.0",
"@libp2p/crypto": "^1.0.4",
"@libp2p/mplex": "^5.0.0",
"@libp2p/peer-collections": "^2.0.0",
"@libp2p/peer-id-factory": "^1.0.18",
"@libp2p/tcp": "^3.1.2",
"@multiformats/multiaddr": "^11.0.1",
"@polkadot/api": "^9.3.2",
"@tracelabs/kad-dht": "3.0.6",
"app-root-path": "^3.1.0",
"assertion-tools": "^1.0.9",
"async": "^3.2.4",
Expand All @@ -89,24 +80,29 @@
"graphdb": "^2.0.2",
"ip": "^1.1.8",
"it-all": "^1.0.6",
"it-filter": "^1.0.3",
"it-foreach": "^0.1.1",
"it-length-prefixed": "^8.0.2",
"it-concat": "^2.0.0",
"it-length-prefixed": "^5.0.3",
"it-map": "^1.0.6",
"it-merge": "^1.0.4",
"it-pipe": "^2.0.4",
"it-pipe": "^1.1.0",
"it-sort": "^1.0.1",
"it-take": "^1.0.2",
"jsonld": "^8.1.0",
"jsonschema": "^1.4.1",
"jsonwebtoken": "^8.5.1",
"keccak256": "^1.0.6",
"libp2p": "^0.38.0",
"libp2p": "^0.32.4",
"libp2p-bootstrap": "^0.13.0",
"libp2p-kad-dht": "^0.24.2",
"libp2p-mplex": "^0.10.7",
"libp2p-noise": "^4.0.0",
"libp2p-tcp": "^0.17.2",
"merkletreejs": "^0.2.32",
"ms": "^2.1.3",
"multiaddr": "^10.0.0",
"multiformats": "^9.8.1",
"mysql2": "^2.3.3",
"p-iteration": "^1.1.8",
"peer-id": "^0.15.3",
"pino": "^8.4.2",
"pino-pretty": "^9.1.0",
"rc": "^1.2.8",
Expand Down
15 changes: 14 additions & 1 deletion src/commands/command-executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ class CommandExecutor {
}

const handler = this.commandResolver.resolve(command.name);
if (!handler) {
this.logger.warn(`Command '${command.name}' will not be executed.`);
await this._update(command, {
status: COMMAND_STATUS.UNKNOWN,
});
return;
}
if (command.deadline_at && now > command.deadline_at) {
this.logger.warn(`Command ${command.name} and ID ${command.id} is too late...`);
await this._update(command, {
Expand Down Expand Up @@ -231,6 +238,10 @@ class CommandExecutor {
async _startDefaultCommand(name) {
await this._delete(name);
const handler = this.commandResolver.resolve(name);
if (!handler) {
this.logger.warn(`Command '${name}' will not be executed.`);
return;
}
await this.add(handler.default(), DEFAULT_COMMAND_DELAY_IN_MILLS, true);
if (this.verboseLoggingEnabled) {
this.logger.trace(`Permanent command ${name} created.`);
Expand Down Expand Up @@ -349,7 +360,9 @@ class CommandExecutor {
}
if (!command.data) {
const commandInstance = this.commandResolver.resolve(command.name);
command.data = commandInstance.pack(command.data);
if (commandInstance) {
command.data = commandInstance.pack(command.data);
}
}
command.status = COMMAND_STATUS.PENDING;
const opts = {};
Expand Down
3 changes: 2 additions & 1 deletion src/commands/command-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class CommandResolver {
constructor(ctx) {
this.ctx = ctx;
this.logger = ctx.logger;
}

/**
Expand All @@ -15,7 +16,7 @@ class CommandResolver {
try {
return this.ctx[`${name}`];
} catch (e) {
throw new Error(`No handler defined for command '${name}'`);
this.logger.warn(`No handler defined for command '${name}'`);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/common/commands-cleaner-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CommandsCleanerCommand extends Command {
COMMAND_STATUS.COMPLETED,
COMMAND_STATUS.FAILED,
COMMAND_STATUS.EXPIRED,
COMMAND_STATUS.UNKNOWN,
]);
return Command.repeat();
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/common/send-telemetry-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SendTelemetryCommand extends Command {
const signalingMessage = {
nodeData: {
version: pjson.version,
identity: this.networkModuleManager.getPeerId().toString(),
identity: this.networkModuleManager.getPeerId()._idB58String,
hostname: this.config.hostname,
operational_wallet: this.blockchainModuleManager.getPublicKey(),
management_wallet: this.blockchainModuleManager.getManagementKey(),
Expand Down
92 changes: 11 additions & 81 deletions src/commands/protocols/common/find-nodes-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ class FindNodesCommand extends Command {
this.errorType = errorType;
this.logger.debug(`Searching for closest node(s) for keyword ${keyword}`);

// TODO: implement protocol selection
const closestNodes = (await this.findNodes(keyword, operationId)).map((node) => ({
id: node.id,
protocol: networkProtocols[0],
}));
const closestNodes = [];
for (const node of await this.findNodes(keyword, operationId)) {
for (const protocol of networkProtocols) {
if (node.protocols.includes(protocol)) {
closestNodes.push({ id: node.id, protocol });
break;
}
}
}

this.logger.debug(`Found ${closestNodes.length} node(s) for keyword ${keyword}`);

Expand Down Expand Up @@ -53,85 +57,11 @@ class FindNodesCommand extends Command {
OPERATION_ID_STATUS.FIND_NODES_START,
);

const localPeers = (await this.networkModuleManager.findNodesLocal(keyword)).map((peer) =>
peer.toString(),
);

const { nodes: closestNodes, telemetryData } = await this.networkModuleManager.findNodes(
keyword,
);

const promises = [];
for (const telemetry of telemetryData) {
const {
peerId,
openConnectionStart,
createStreamStart,
sendMessageStart,
sendMessageEnd,
} = telemetry;
const stringifiedPeerId = peerId.toString();
const closestNodes = await this.networkModuleManager.findNodes(keyword);

promises.concat([
this.operationIdService.updateOperationIdStatusWithValues(
operationId,
OPERATION_ID_STATUS.FIND_NODES_OPEN_CONNECTION_START,
stringifiedPeerId,
null,
openConnectionStart,
),
this.operationIdService.updateOperationIdStatusWithValues(
operationId,
OPERATION_ID_STATUS.FIND_NODES_OPEN_CONNECTION_END,
stringifiedPeerId,
null,
createStreamStart,
),
this.operationIdService.updateOperationIdStatusWithValues(
operationId,
OPERATION_ID_STATUS.FIND_NODES_CREATE_STREAM_START,
stringifiedPeerId,
null,
createStreamStart,
),
this.operationIdService.updateOperationIdStatusWithValues(
operationId,
OPERATION_ID_STATUS.FIND_NODES_CREATE_STREAM_END,
stringifiedPeerId,
null,
sendMessageStart,
),
this.operationIdService.updateOperationIdStatusWithValues(
operationId,
OPERATION_ID_STATUS.FIND_NODES_SEND_MESSAGE_START,
stringifiedPeerId,
null,
sendMessageStart,
),
this.operationIdService.updateOperationIdStatusWithValues(
operationId,
OPERATION_ID_STATUS.FIND_NODES_SEND_MESSAGE_END,
stringifiedPeerId,
null,
sendMessageEnd,
),
]);
}
await Promise.all(promises);

let differences = 0;
for (const closestNode of closestNodes) {
if (!localPeers.includes(closestNode.id.toString())) {
differences += 1;
}
}
const routingTableSize = this.networkModuleManager.getRoutingTableSize();

await this.operationIdService.updateOperationIdStatusWithValues(
await this.operationIdService.updateOperationIdStatus(
operationId,
OPERATION_ID_STATUS.FIND_NODES_END,
differences,
routingTableSize,
);

return closestNodes;
Expand Down
33 changes: 0 additions & 33 deletions src/commands/protocols/common/protocol-message-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
NETWORK_MESSAGE_TYPES,
OPERATION_REQUEST_STATUS,
OPERATION_STATUS,
OPERATION_ID_STATUS,
ERROR_TYPE,
} from '../../../constants/constants.js';

class ProtocolMessageCommand extends Command {
Expand Down Expand Up @@ -55,37 +53,6 @@ class ProtocolMessageCommand extends Command {
message,
);

if (response?.telemetryData?.start && response?.telemetryData?.end) {
if (response?.telemetryData?.error) {
this.operationIdService.emitChangeEvent(
OPERATION_ID_STATUS.FAILED,
operationId,
response?.telemetryData?.error.message,
ERROR_TYPE.DIAL_PROTOCOL_ERROR,
response.telemetryData.start,
);
} else {
const networkInfo = response.telemetryData.networkInfo
? `${response.telemetryData.networkInfo.publicIp}:${response.telemetryData.networkInfo.port}`
: null;

await this.operationIdService.emitChangeEvent(
OPERATION_ID_STATUS.DIAL_PROTOCOL_START,
operationId,
node.id.toString(),
networkInfo,
response.telemetryData.start,
);
await this.operationIdService.emitChangeEvent(
OPERATION_ID_STATUS.DIAL_PROTOCOL_END,
operationId,
node.id.toString(),
networkInfo,
response.telemetryData.end,
);
}
}

switch (response.header.messageType) {
case NETWORK_MESSAGE_TYPES.RESPONSES.BUSY:
return this.handleBusy(command, response.data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ProtocolScheduleMessagesCommand extends Command {
const currentBatchNodes = leftoverNodes.slice(0, batchSize);
const currentBatchLeftoverNodes =
batchSize < leftoverNodes.length ? leftoverNodes.slice(batchSize) : [];
currentBatchNodes.forEach((node) => nodesSeen.push(node.id.toString()));
currentBatchNodes.forEach((node) => nodesSeen.push(node.id._idB58String));

await this.operationIdService.updateOperationIdStatus(operationId, this.startEvent);

Expand Down
5 changes: 5 additions & 0 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export const SCHEMA_CONTEXT = 'http://schema.org/';

export const LIBP2P_KEY_DIRECTORY = 'libp2p';

export const LIBP2P_KEY_FILENAME = 'privateKey';

export const TRIPLE_STORE_CONNECT_MAX_RETRIES = 10;

export const TRIPLE_STORE_CONNECT_RETRY_FREQUENCY = 10;
Expand Down Expand Up @@ -223,6 +227,7 @@ export const FINALIZED_COMMAND_CLEANUP_TIME_MILLS = 24 * 60 * 60 * 1000;
export const COMMAND_STATUS = {
FAILED: 'FAILED',
EXPIRED: 'EXPIRED',
UNKNOWN: 'UNKNOWN',
STARTED: 'STARTED',
PENDING: 'PENDING',
COMPLETED: 'COMPLETED',
Expand Down
11 changes: 7 additions & 4 deletions src/migration/base-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ class BaseMigration {
this.fileService.getMigrationFolderPath(),
this.migrationName,
);
if (await fs.exists(migrationFilePath)) {
if (await this.fileService.fileExists(migrationFilePath)) {
return true;
}
this.logger.info(`Starting ${this.migrationName} migration.`);
this.startedTimestamp = Date.now();
return false;
}

async finalizeMigration(migrationPath = null) {
async migrate(migrationPath = null) {
this.logger.info(`Starting ${this.migrationName} migration.`);
this.startedTimestamp = Date.now();

await this.executeMigration();

const migrationFolderPath = migrationPath || this.fileService.getMigrationFolderPath();
await fs.ensureDir(migrationFolderPath);
const migrationFilePath = path.join(migrationFolderPath, this.migrationName);
Expand Down
35 changes: 35 additions & 0 deletions src/migration/network-private-key-migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { join } from 'path';
import appRootPath from 'app-root-path';
import { mkdir, writeFile } from 'fs/promises';
import BaseMigration from './base-migration.js';
import { LIBP2P_KEY_DIRECTORY, LIBP2P_KEY_FILENAME } from '../constants/constants.js';

class NetworkPrivateKeyMigration extends BaseMigration {
async executeMigration() {
const networkPrivateKey =
this.config.modules?.network?.implementation?.['libp2p-service']?.config?.privateKey;

if (networkPrivateKey) {
let directoryPath;
if (process.env.NODE_ENV === 'testnet' || process.env.NODE_ENV === 'mainnet') {
directoryPath = join(
appRootPath.path,
'..',
this.config.appDataPath,
LIBP2P_KEY_DIRECTORY,
);
} else {
directoryPath = join(
appRootPath.path,
this.config.appDataPath,
LIBP2P_KEY_DIRECTORY,
);
}
const fullPath = join(directoryPath, LIBP2P_KEY_FILENAME);
await mkdir(directoryPath, { recursive: true });
await writeFile(fullPath, networkPrivateKey);
}
}
}

export default NetworkPrivateKeyMigration;
Loading

0 comments on commit 2418d05

Please sign in to comment.