Skip to content

Commit

Permalink
Merge pull request #1851 from OriginTrail/v6/prerelease/testnet
Browse files Browse the repository at this point in the history
OriginTrail 6.0.0-beta.1.31 Testnet Release
  • Loading branch information
kotlarmilos authored Mar 24, 2022
2 parents ca1ff29 + 6ce9639 commit 836b158
Show file tree
Hide file tree
Showing 29 changed files with 2,087 additions and 490 deletions.
1 change: 0 additions & 1 deletion external/BytesUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class BytesUtilities {
}

static fromNat(bn) {
// eslint-disable-next-line no-nested-ternary
return bn === '0x0' ? '0x' : bn.length % 2 === 0 ? bn : `0x0${bn.slice(2)}`;
}

Expand Down
5 changes: 4 additions & 1 deletion external/graphdb-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class GraphdbService {
.setReadTimeout(readTimeout)
.setWriteTimeout(writeTimeout);

this.repository = await this.server.getRepository(this.config.repositoryName, repositoryServerConfig);
this.repository = await this.server.getRepository(
this.config.repositoryName,
repositoryServerConfig,
);
this.repository.registerParser(new SparqlXmlResultParser());
this.logger.info('GraphDB module initialized successfully');
}
Expand Down
2 changes: 1 addition & 1 deletion external/kad-identity-ranking-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class KadIdentityRanking {

nodes.sort((first_node, second_node) => this.distance(id, first_node._id) - this.distance(id, second_node._id));
for (const node of nodes) {
this.logger.info(`XOR distance between topic ${topic} and node ${node._idB58String}: ` + this.distance(id, node._id));
this.logger.info(`XOR distance between topic ${topic} and node ${node._idB58String}: ${this.distance(id, node._id)}`);
}

return nodes.slice(0, replicationFactor);
Expand Down
65 changes: 46 additions & 19 deletions external/libp2p-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ const MPLEX = require('libp2p-mplex');
const TCP = require('libp2p-tcp');
const pipe = require('it-pipe');
const {sha256} = require('multiformats/hashes/sha2');
const {v1: uuidv1} = require("uuid");
const PeerId = require("peer-id");
const fs = require('fs');
const {time} = require("streaming-iterables");
const { BufferList } = require('bl')
const { InMemoryRateLimiter } = require("rolling-rate-limiter");
const constants = require('../modules/constants');



const initializationObject = {
addresses: {
listen: ['/ip4/0.0.0.0/tcp/9000'],
Expand Down Expand Up @@ -74,6 +71,10 @@ class Libp2pService {

initializationObject.peerId = this.config.peerId;
this.workerPool = this.config.workerPool;
this.limiter = new InMemoryRateLimiter({
interval: constants.NETWORK_API_RATE_LIMIT_TIME_WINDOW_MILLS,
maxInInterval: constants.NETWORK_API_RATE_LIMIT_MAX_NUMBER,
});

Libp2p.create(initializationObject).then((node) => {
this.node = node;
Expand Down Expand Up @@ -115,13 +116,13 @@ class Libp2pService {
// Creates a DHT ID by hashing a given Uint8Array
const id = (await sha256.digest(encodedKey)).digest;
const nodes = this.node._dht.peerRouting.getClosestPeers(id);
const result = [];
const result = new Set();
for await (const node of nodes) {
result.push(node);
result.add(node);
}
this.logger.info(`Found ${result.length} nodes`);
this.logger.info(`Found ${result.size} nodes`);

return result;
return [...result];
}

getPeers() {
Expand All @@ -144,18 +145,36 @@ class Libp2pService {
return rec.serialize();
}

async prepareForSending(data) {
if(constants.NETWORK_RESPONSES[data]) {
data = constants.STRINGIFIED_NETWORK_RESPONSES[data];
} else {
data = await this.workerPool.exec('JSONStringify', [data]);
}
return Buffer.from(data);
}

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

let async = false, timeout = 60e3;
let async = false, timeout = constants.NETWORK_HANDLER_TIMEOUT;
if (options) {
async = options.async;
timeout = options.timeout;
}
this.node.handle(eventName, async (handlerProps) => {
const {stream} = handlerProps;
let timestamp = Date.now();

const blocked = await this.limiter.limit(handlerProps.connection.remotePeer.toB58String());
if(blocked) {
const preparedBlockedResponse = await this.prepareForSending(constants.NETWORK_RESPONSES.BLOCKED);
await pipe(
[preparedBlockedResponse],
stream
);
this.logger.info(`Blocking request from ${handlerProps.connection.remotePeer._idB58String}. Max number of requests exceeded.`);
return;
}
let data = await pipe(
stream,
async function (source) {
Expand All @@ -173,14 +192,15 @@ class Libp2pService {
if (!async) {
const result = await handler(data);
this.logger.info(`Sending response from ${this.config.id} to ${handlerProps.connection.remotePeer._idB58String}: event=${eventName};`);
const stringifiedData = await this.workerPool.exec('JSONStringify', [result]);
const preparedData = await this.prepareForSending(result);
await pipe(
[Buffer.from(stringifiedData)],
[Buffer.from(preparedData)],
stream,
)
} else {
const preparedAckResponse = await this.prepareForSending(constants.NETWORK_RESPONSES.ACK);
await pipe(
[constants.NETWORK_RESPONSES.ACK],
[preparedAckResponse],
stream
)

Expand All @@ -198,21 +218,21 @@ class Libp2pService {
msg: `Error: ${e}, stack: ${e.stack} \n Data received: ${stringifiedData}`,
Event_name: constants.ERROR_TYPE.LIBP2P_HANDLE_MSG_ERROR,
});
const preparedErrorResponse = await this.prepareForSending(constants.NETWORK_RESPONSES.ERROR);
await pipe(
[constants.NETWORK_RESPONSES.ACK],
[preparedErrorResponse],
stream
)

);
}
});
}

async sendMessage(eventName, data, peerId) {
this.logger.info(`Sending message from ${this.config.id} to ${peerId._idB58String}: event=${eventName};`);
const {stream} = await this.node.dialProtocol(peerId, eventName);
const stringifiedData = await this.workerPool.exec('JSONStringify', [data]);
const preparedData = await this.prepareForSending(data);
const response = await pipe(
[Buffer.from(stringifiedData)],
[Buffer.from(preparedData)],
stream,
async function (source) {
const bl = new BufferList()
Expand All @@ -224,11 +244,18 @@ class Libp2pService {
},
)

// TODO: Remove - Backwards compatibility check with 1.30 and lower
if(response.toString() === constants.NETWORK_RESPONSES.ACK) {
return null;
}

return JSON.parse(response);
const parsedData = await this.workerPool.exec('JSONParse', [response.toString()]);
const suppressedResponses = [constants.NETWORK_RESPONSES.ACK, constants.NETWORK_RESPONSES.BLOCKED, constants.NETWORK_RESPONSES.ERROR];
if(suppressedResponses.includes(parsedData)) {
return null;
}

return parsedData;
}

healthCheck() {
Expand Down
3 changes: 0 additions & 3 deletions external/merkle-validation-service.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
const MerkleTools = require('merkle-tools');
const SHA256 = require('crypto-js/sha256');

// eslint-disable-next-line import/no-extraneous-dependencies
const elliptic = require('elliptic');

// eslint-disable-next-line new-cap
const secp256k1 = new elliptic.ec('secp256k1');
const sha3 = require('js-sha3');

const BytesUtilities = require('./BytesUtilities');

// eslint-disable-next-line no-cond-assign,func-names,no-unsafe-finally,no-undef-init,no-undef
const _slicedToArray = (function () {
function sliceIterator(arr, i) {
const _arr = [];
Expand Down
32 changes: 20 additions & 12 deletions external/web3-blockchain-service.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const Web3 = require('web3');
const BigNumber = require('big-number');
const axios = require('axios');
const DKGContract = require('../build/contracts/DKGcontract.json').abi;
const UAIRegistry = require('../build/contracts/UAIRegistry.json').abi;
const constants = require('../modules/constants');
const axios = require('axios');

class Web3BlockchainService {
constructor(config) {
this.config = config;
this.gasStationLink = 'https://gasstation-mumbai.matic.today/';

}

initialize(logger) {
Expand Down Expand Up @@ -56,9 +55,12 @@ class Web3BlockchainService {
gas: '500000',
};

const createdTransaction = await this.web3.eth.accounts.signTransaction(tx, this.config.privateKey);
const createdTransaction = await this.web3.eth.accounts.signTransaction(
tx,
this.config.privateKey,
);
const result = await this.web3.eth.sendSignedTransaction(createdTransaction.rawTransaction);
return {transactionHash: result.transactionHash, blockchain: this.config.networkId};
return { transactionHash: result.transactionHash, blockchain: this.config.networkId };
}

async registerAsset(uai, type, alsoKnownAs, stateCommitHash, rootHash, tokenAmount) {
Expand All @@ -77,9 +79,12 @@ class Web3BlockchainService {
gas: '900000',
};

const createdTransaction = await this.web3.eth.accounts.signTransaction(tx, this.config.privateKey);
const createdTransaction = await this.web3.eth.accounts.signTransaction(
tx,
this.config.privateKey,
);
const result = await this.web3.eth.sendSignedTransaction(createdTransaction.rawTransaction);
return {transactionHash: result.transactionHash, blockchain: this.config.networkId};
return { transactionHash: result.transactionHash, blockchain: this.config.networkId };
}

async updateAsset(UAI, newStateCommitHash, rootHash) {
Expand All @@ -98,9 +103,12 @@ class Web3BlockchainService {
gas: '500000',
};

const createdTransaction = await this.web3.eth.accounts.signTransaction(tx, this.config.privateKey);
const createdTransaction = await this.web3.eth.accounts.signTransaction(
tx,
this.config.privateKey,
);
const result = await this.web3.eth.sendSignedTransaction(createdTransaction.rawTransaction);
return {transactionHash: result.transactionHash, blockchain: this.config.networkId};
return { transactionHash: result.transactionHash, blockchain: this.config.networkId };
}

async getAssertionProofs(assertionId) {
Expand All @@ -118,14 +126,14 @@ class Web3BlockchainService {

const issuer = await contractInstance.methods.getAssetController(`0x${ual}`).call();
let assertionId = await contractInstance.methods.getAssetStateCommitHash(`0x${ual}`).call();
if (assertionId === '0x0000000000000000000000000000000000000000000000000000000000000000')
if (assertionId === '0x0000000000000000000000000000000000000000000000000000000000000000') {
assertionId = undefined;
else
} else {
assertionId = assertionId.slice(2);
return {issuer, assertionId};
}
return { issuer, assertionId };
}


async getAssertionRegistryAddress() {
const contractAddress = this.config.hubContractAddress;
const contractInstance = new this.web3.eth.Contract(UAIRegistry, contractAddress);
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const fs = require('fs-extra');
const path = require('path');
const appRootPath = require('app-root-path');
const { exec, execSync } = require('child_process');
const rc = require('rc');
const OTNode = require('./ot-node');
const pjson = require('./package.json');
const rc = require('rc');

const configjson = require('./config/config.json');

Expand Down
2 changes: 1 addition & 1 deletion migrations/20211117005505-update-handler_ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ module.exports = {
allowNull: true,
},
),
};
};
2 changes: 0 additions & 2 deletions models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const path = require('path');
const Sequelize = require('sequelize');

const basename = path.basename(__filename);
// eslint-disable-next-line import/no-dynamic-require
const config = require(`${__dirname}/../config/sequelizeConfig`);
const db = {};
let sequelize = {};
Expand All @@ -20,7 +19,6 @@ fs
.readdirSync(__dirname)
.filter((file) => (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'))
.forEach((file) => {
// eslint-disable-next-line global-require,import/no-dynamic-require
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Expand Down
1 change: 0 additions & 1 deletion modules/command/command-executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class CommandExecutor {
that.logger.trace('Command executor has not been started yet. Hibernating...');
}

// eslint-disable-next-line
await sleep.sleep(1000);
}
await this._execute(command);
Expand Down
16 changes: 11 additions & 5 deletions modules/command/common/keep-alive-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ class KeepAliveCommand extends Command {
['created_at', 'DESC'],
],
attributes: ['hash', 'topics', 'created_at'],
})).map(x => ({assertionId: x.dataValues.hash, keyword: x.dataValues.topics, publishTimestamp: x.dataValues.created_at}));
})).map((x) => (
{
assertionId: x.dataValues.hash,
keyword: x.dataValues.topics,
publishTimestamp: x.dataValues.created_at,
}
));
} catch (e) {
this.logger.error({
msg: `An error has occurred with signaling data error: ${e}, stack: ${e.stack}`,
Expand All @@ -67,14 +73,14 @@ class KeepAliveCommand extends Command {
method: 'post',
url: 'https://signum.origintrail.io:3000/signal',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
data: JSON.stringify(signalingMessage)
data: JSON.stringify(signalingMessage),
};

const that = this;
axios(config).catch(e=>{
that.handleError(uuidv1(), e, constants.ERROR_TYPE.KEEP_ALIVE_ERROR, false)
axios(config).catch((e) => {
that.handleError(uuidv1(), e, constants.ERROR_TYPE.KEEP_ALIVE_ERROR, false);
});
return Command.repeat();
}
Expand Down
9 changes: 6 additions & 3 deletions modules/command/common/otnode-update-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ class OtnodeUpdateCommand extends Command {
}
try {
this.logger.info('Checking for new updates...');
const { upToDate, currentVersion, remoteVersion } = await this.updater.compareVersions();
const {
upToDate,
currentVersion,
remoteVersion,
} = await this.updater.compareVersions();
if (!upToDate) {
if (semver.major(currentVersion) < semver.major(remoteVersion)) {
this.logger.info(`New major update available. Please run update to version ${remoteVersion} manually.`);
return Command.repeat();
} else {
await this.updater.autoUpdate();
}
await this.updater.autoUpdate();
}
} catch (e) {
await this.handleError(e);
Expand Down
4 changes: 2 additions & 2 deletions modules/command/publish/insert-assertion-command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const Command = require('../command');
const Models = require('../../../models/index');
const constants = require('../../constants');

class InsertAssertionCommand extends Command {
Expand All @@ -16,13 +15,14 @@ class InsertAssertionCommand extends Command {
*/
async execute(command) {
const { documentPath, handlerId, operationId } = command.data;

this.logger.emit({
msg: 'Started measuring execution of storing publishing data into local triple store',
Event_name: 'publish_local_store_start',
Operation_name: 'publish_local_store',
Id_operation: operationId,
});
let { nquads, assertion } = await this.fileService.loadJsonFromFile(documentPath);
const { nquads, assertion } = await this.fileService.loadJsonFromFile(documentPath);

try {
await this.dataService.insert(nquads.join('\n'), `${constants.DID_PREFIX}:${assertion.id}`);
Expand Down
Loading

0 comments on commit 836b158

Please sign in to comment.