Skip to content

Commit

Permalink
Merge branch 'v6/develop' into v6/api-versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
u-hubar authored Aug 17, 2023
2 parents 181ce0f + cbb81b3 commit 1b3a21c
Show file tree
Hide file tree
Showing 22 changed files with 253 additions and 46 deletions.
4 changes: 4 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
}
}
},
"maximumAssertionSizeInKb": 2500,
"commandExecutorVerboseLoggingEnabled": false,
"appDataPath": "data",
"logLevel": "info",
Expand Down Expand Up @@ -302,6 +303,7 @@
}
}
},
"maximumAssertionSizeInKb": 2500,
"commandExecutorVerboseLoggingEnabled": false,
"appDataPath": "data",
"logLevel": "trace",
Expand Down Expand Up @@ -449,6 +451,7 @@
}
}
},
"maximumAssertionSizeInKb": 2500,
"commandExecutorVerboseLoggingEnabled": false,
"appDataPath": "data",
"logLevel": "trace",
Expand Down Expand Up @@ -597,6 +600,7 @@
}
}
},
"maximumAssertionSizeInKb": 2500,
"commandExecutorVerboseLoggingEnabled": false,
"appDataPath": "data",
"logLevel": "trace",
Expand Down
6 changes: 6 additions & 0 deletions ot-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class OTNode {

await this.initializeCommandExecutor();
await this.initializeRouters();
await this.startNetworkModule();
this.logger.info('Node is up and running!');
}

Expand Down Expand Up @@ -254,6 +255,11 @@ class OTNode {
}
}

async startNetworkModule() {
const networkModuleManager = this.container.resolve('networkModuleManager');
await networkModuleManager.start();
}

async executePrivateAssetsMetadataMigration() {
if (
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "origintrail_node",
"version": "6.0.12",
"version": "6.0.13",
"description": "OTNode V6",
"main": "index.js",
"type": "module",
Expand Down
17 changes: 14 additions & 3 deletions src/commands/protocols/common/handle-protocol-message-command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Command from '../../command.js';
import { NETWORK_MESSAGE_TYPES } from '../../../constants/constants.js';
import { BYTES_IN_KILOBYTE, NETWORK_MESSAGE_TYPES } from '../../../constants/constants.js';

class HandleProtocolMessageCommand extends Command {
constructor(ctx) {
Expand Down Expand Up @@ -127,6 +127,18 @@ class HandleProtocolMessageCommand extends Command {
this.blockchainModuleManager.getR0(blockchain),
getAsk(),
]);
const blockchainAssertionSizeInKb = blockchainAssertionSize / BYTES_IN_KILOBYTE;
if (blockchainAssertionSizeInKb > this.config.maximumAssertionSizeInKb) {
this.logger.warn(
`The size of the received assertion exceeds the maximum limit allowed.. Maximum allowed assertion size in kb: ${this.config.maximumAssertionSizeInKb}, assertion size read from blockchain in kb: ${blockchainAssertionSizeInKb}`,
);
return {
errorMessage:
'The size of the received assertion exceeds the maximum limit allowed.',
agreementId,
agreementData,
};
}

const now = await this.blockchainModuleManager.getBlockchainTimestamp(blockchain);

Expand All @@ -143,8 +155,7 @@ class HandleProtocolMessageCommand extends Command {
.mul(epochsLeft)
.mul(blockchainAssertionSize);

const serviceAgreementBid = this.blockchainModuleManager
.toBigNumber(blockchain, agreementData.tokenAmount)
const serviceAgreementBid = agreementData.tokenAmount
.add(agreementData.updateTokenAmount)
.mul(1024)
.div(divisor)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ProtocolScheduleMessagesCommand from '../../common/protocol-schedule-messages-command.js';
import Command from '../../../command.js';
import { OPERATION_ID_STATUS, ERROR_TYPE } from '../../../../constants/constants.js';

class PublishScheduleMessagesCommand extends ProtocolScheduleMessagesCommand {
Expand All @@ -10,6 +11,105 @@ class PublishScheduleMessagesCommand extends ProtocolScheduleMessagesCommand {
this.errorType = ERROR_TYPE.PUBLISH.PUBLISH_START_ERROR;
}

async execute(command) {
const {
operationId,
keyword,
leftoverNodes,
numberOfFoundNodes,
blockchain,
minAckResponses,
hashFunctionId,
assertionId,
tokenId,
contract,
} = command.data;
let isValid = true;
// perform check only first time not for every batch
if (leftoverNodes === numberOfFoundNodes) {
isValid = await this.validateBidsForNeighbourhood(
blockchain,
contract,
tokenId,
keyword,
hashFunctionId,
assertionId,
leftoverNodes,
minAckResponses,
operationId,
);
}
if (isValid) {
return super.execute(command);
}
return Command.empty();
}

async validateBidsForNeighbourhood(
blockchain,
contract,
tokenId,
keyword,
hashFunctionId,
assertionId,
nodes,
minAckResponses,
operationId,
) {
const agreementId = await this.serviceAgreementService.generateId(
blockchain,
contract,
tokenId,
keyword,
hashFunctionId,
);

const agreementData = await this.blockchainModuleManager.getAgreementData(
blockchain,
agreementId,
);

const r0 = await this.blockchainModuleManager.getR0(blockchain);

const blockchainAssertionSize = await this.blockchainModuleManager.getAssertionSize(
blockchain,
assertionId,
);

const divisor = this.blockchainModuleManager
.toBigNumber(blockchain, r0)
.mul(Number(agreementData.epochsNumber))
.mul(blockchainAssertionSize);

const serviceAgreementBid = this.blockchainModuleManager
.toBigNumber(blockchain, agreementData.tokenAmount)
.add(agreementData.updateTokenAmount)
.mul(1024)
.div(divisor)
.add(1); // add 1 wei because of the precision loss

let validBids = 0;

nodes.forEach((node) => {
const askNumber = this.blockchainModuleManager.convertToWei(blockchain, node.ask);

const ask = this.blockchainModuleManager.toBigNumber(blockchain, askNumber);

if (ask.lte(serviceAgreementBid)) {
validBids += 1;
}
});
if (validBids < minAckResponses) {
await this.operationService.markOperationAsFailed(
operationId,
'Unable to start publish, not enough nodes in neighbourhood satisfy the bid.',
ERROR_TYPE.PUBLISH.PUBLISH_START_ERROR,
);
return false;
}
return true;
}

/**
* Builds default publishScheduleMessagesCommand
* @param map
Expand Down
3 changes: 3 additions & 0 deletions src/modules/blockchain/implementation/web3-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ class Web3Service {
TRANSACTION_CONFIRMATIONS,
TRANSACTION_POLLING_TIMEOUT_MILLIS,
);
if (result?.status === 0) {
throw Error();
}
} catch (error) {
this.logger.warn(
`Failed executing smart contract function ${functionName}. Error: ${error.message}`,
Expand Down
9 changes: 6 additions & 3 deletions src/modules/network/implementation/libp2p-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,14 @@ class Libp2pService {
*/
this.sessions = {};
this.node = await libp2p.create(initializationObject);
await this.node.start();
const port = parseInt(this.node.multiaddrs.toString().split('/')[4], 10);
const peerId = this.node.peerId.toB58String();
this.config.id = peerId;
this.logger.info(`Network ID is ${peerId}, connection port is ${port}`);
}

async start() {
await this.node.start();
const port = parseInt(this.node.multiaddrs.toString().split('/')[4], 10);
this.logger.info(`Network ID is ${this.config.id}, connection port is ${port}`);
}

async onPeerConnected(listener) {
Expand Down
6 changes: 6 additions & 0 deletions src/modules/network/network-module-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ class NetworkModuleManager extends BaseModuleManager {
return 'network';
}

async start() {
if (this.initialized) {
return this.getImplementation().module.start();
}
}

async onPeerConnected(listener) {
if (this.initialized) {
return this.getImplementation().module.onPeerConnected(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class ShardRepository {
}

async updatePeerRecordLastDialed(peerId, timestamp) {
await this.model.update(
return this.model.update(
{
lastDialed: timestamp,
},
Expand All @@ -119,7 +119,7 @@ class ShardRepository {
}

async updatePeerRecordLastSeenAndLastDialed(peerId, timestamp) {
await this.model.update(
return this.model.update(
{
lastDialed: timestamp,
lastSeen: timestamp,
Expand Down
52 changes: 38 additions & 14 deletions src/service/sharding-table-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ class ShardingTableService {
firstAssertionId,
hashFunctionId,
) {
const kbSize = assertionSize < BYTES_IN_KILOBYTE ? BYTES_IN_KILOBYTE : assertionSize;
const peerRecords = await this.findNeighbourhood(
blockchainId,
this.blockchainModuleManager.encodePacked(
Expand All @@ -164,20 +163,34 @@ class ShardingTableService {
hashFunctionId,
true,
);

const sorted = peerRecords.sort((a, b) => a.ask - b.ask);

const { ask } = sorted[Math.floor(sorted.length * 0.75)];
const r1 = await this.blockchainModuleManager.getR1(blockchainId);
// todo remove this line once we implement logic for storing assertion in publish node if it's in neighbourhood
const myPeerId = this.networkModuleManager.getPeerId().toB58String();
const filteredPeerRecords = peerRecords.filter((peer) => peer.peerId !== myPeerId);
const sorted = filteredPeerRecords.sort((a, b) => a.ask - b.ask);
let ask;
if (sorted.length > r1) {
ask = sorted[r1 - 1].ask;
} else {
ask = sorted[sorted.length - 1].ask;
}

const r0 = await this.blockchainModuleManager.getR0(blockchainId);

return this.blockchainModuleManager
const minBidSuggestion = this.blockchainModuleManager
.toBigNumber(blockchainId, '1')
.mul(epochsNumber)
.mul(r0);

const bidSuggestion = this.blockchainModuleManager
.toBigNumber(blockchainId, this.blockchainModuleManager.convertToWei(blockchainId, ask))
.mul(kbSize)
.mul(assertionSize)
.mul(epochsNumber)
.mul(r0)
.div(BYTES_IN_KILOBYTE)
.toString();
.div(BYTES_IN_KILOBYTE);
return bidSuggestion.lte(minBidSuggestion)
? minBidSuggestion.toString()
: bidSuggestion.toString();
}

async findEligibleNodes(neighbourhood, bid, r1, r0) {
Expand Down Expand Up @@ -214,15 +227,21 @@ class ShardingTableService {
};
}
if (this.memoryCachedPeerIds[peerId].lastUpdated < timestampThreshold) {
await this.repositoryModuleManager.updatePeerRecordLastSeenAndLastDialed(peerId, now);
this.memoryCachedPeerIds[peerId].lastUpdated = now;
const [rowsUpdated] =
await this.repositoryModuleManager.updatePeerRecordLastSeenAndLastDialed(
peerId,
now,
);
if (rowsUpdated) {
this.memoryCachedPeerIds[peerId].lastUpdated = now;
}
}
this.memoryCachedPeerIds[peerId].lastDialed = now;
this.memoryCachedPeerIds[peerId].lastSeen = now;
}

async updatePeerRecordLastDialed(peerId) {
const now = new Date();
const now = Date.now();
const timestampThreshold = now - PEER_RECORD_UPDATE_DELAY;
if (!this.memoryCachedPeerIds[peerId]) {
this.memoryCachedPeerIds[peerId] = {
Expand All @@ -232,8 +251,13 @@ class ShardingTableService {
};
}
if (this.memoryCachedPeerIds[peerId].lastUpdated < timestampThreshold) {
await this.repositoryModuleManager.updatePeerRecordLastDialed(peerId, now);
this.memoryCachedPeerIds[peerId].lastUpdated = now;
const [rowsUpdated] = await this.repositoryModuleManager.updatePeerRecordLastDialed(
peerId,
now,
);
if (rowsUpdated) {
this.memoryCachedPeerIds[peerId].lastUpdated = now;
}
}
this.memoryCachedPeerIds[peerId].lastDialed = now;
}
Expand Down
9 changes: 9 additions & 0 deletions src/service/validation-service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { assertionMetadata } from 'assertion-tools';
import { BYTES_IN_KILOBYTE } from '../constants/constants.js';

class ValidationService {
constructor(ctx) {
this.logger = ctx.logger;
this.config = ctx.config;
this.validationModuleManager = ctx.validationModuleManager;
this.blockchainModuleManager = ctx.blockchainModuleManager;
}
Expand Down Expand Up @@ -45,6 +47,13 @@ class ValidationService {
blockchain,
assertionId,
);

const blockchainAssertionSizeInKb = blockchainAssertionSize / BYTES_IN_KILOBYTE;
if (blockchainAssertionSizeInKb > this.config.maximumAssertionSizeInKb) {
throw Error(
`The size of the received assertion exceeds the maximum limit allowed.. Maximum allowed assertion size in kb: ${this.config.maximumAssertionSizeInKb}, assertion size read from blockchain in kb: ${blockchainAssertionSizeInKb}`,
);
}
const assertionSize = assertionMetadata.getAssertionSizeInBytes(assertion);
if (blockchainAssertionSize !== assertionSize) {
throw Error(
Expand Down
Loading

0 comments on commit 1b3a21c

Please sign in to comment.