Skip to content

Commit

Permalink
Merge branch 'v6/develop' of https://github.com/OriginTrail/ot-node i…
Browse files Browse the repository at this point in the history
…nto v6/develop
  • Loading branch information
zeroxbt committed Aug 14, 2023
2 parents 4235e03 + b72ea67 commit b9a694b
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2 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
14 changes: 13 additions & 1 deletion 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 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: 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
2 changes: 1 addition & 1 deletion test/bdd/steps/api/publish.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ When('I wait for latest Publish to finalize', { timeout: 80000 }, async function
assert.fail('Unable to fetch publish result');
}
// eslint-disable-next-line no-await-in-loop
await setTimeout(4000);
await setTimeout(6000);
}
});

Expand Down
3 changes: 3 additions & 0 deletions test/unit/service/validation-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ describe('Validation service test', async () => {
validationModuleManager: new ValidationModuleManagerMock(),
blockchainModuleManager: new BlockchainModuleManagerMock(),
logger: new Logger(),
config: {
maximumAssertionSizeInKb: 2500,
},
});
});

Expand Down

0 comments on commit b9a694b

Please sign in to comment.