Skip to content

Commit

Permalink
Merge pull request #3134 from OriginTrail/v6/prerelease/testnet
Browse files Browse the repository at this point in the history
OriginTrail Testnet Release v6.3.0
  • Loading branch information
djordjekovac authored Apr 18, 2024
2 parents d2b43c0 + 7419584 commit feddcb2
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 124 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

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.2.4",
"version": "6.3.0",
"description": "OTNode V6",
"main": "index.js",
"type": "module",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import Command from '../../command.js';
import {
CONTENT_ASSET_HASH_FUNCTION_ID,
EXPECTED_TRANSACTION_ERRORS,
GET_LATEST_SERVICE_AGREEMENT_FREQUENCY_MILLS,
SERVICE_AGREEMENT_SOURCES,
} from '../../../constants/constants.js';

const BATCH_SIZE = 50;

class BlockchainGetLatestServiceAgreement extends Command {
constructor(ctx) {
super(ctx);
this.repositoryModuleManager = ctx.repositoryModuleManager;
this.blockchainModuleManager = ctx.blockchainModuleManager;
this.serviceAgreementService = ctx.serviceAgreementService;
this.ualService = ctx.ualService;
}

/**
* Executes command and produces one or more events
* @param command
*/
async execute(command) {
const { blockchain } = command.data;

const assetStorageContractAddresses =
this.blockchainModuleManager.getAssetStorageContractAddresses(blockchain);

await Promise.all(
assetStorageContractAddresses.map((contract) =>
this.updateAgreementDataForAssetContract(contract, blockchain),
),
);

return Command.repeat();
}

async updateAgreementDataForAssetContract(contract, blockchain) {
this.logger.info(
`Get latest service agreement: Starting get latest service agreement command for blockchain: ${blockchain}`,
);
let latestBlockchainTokenId;
try {
latestBlockchainTokenId = await this.blockchainModuleManager.getLatestTokenId(
blockchain,
contract,
);
} catch (error) {
if (error.message.includes(EXPECTED_TRANSACTION_ERRORS.NO_MINTED_ASSETS)) {
this.logger.info(
`Get latest service agreement: No minted assets on blockchain: ${blockchain}`,
);
return;
}
throw error;
}

const latestDbTokenId =
(await this.repositoryModuleManager.getLatestServiceAgreementTokenId(blockchain)) ?? 0;

this.logger.debug(
`Get latest service agreement: Latest token id on chain: ${latestBlockchainTokenId}, latest token id in database: ${latestDbTokenId} on blockchain: ${blockchain}`,
);

let tokenIdDifference = latestBlockchainTokenId - latestDbTokenId;
let getAgreementDataPromise = [];
for (
let tokenIdToBeFetched = latestDbTokenId + 1;
tokenIdToBeFetched <= latestBlockchainTokenId;
tokenIdToBeFetched += 1
) {
getAgreementDataPromise.push(
this.getAgreementDataForToken(tokenIdToBeFetched, blockchain, contract),
);
if (
getAgreementDataPromise.length === tokenIdDifference ||
getAgreementDataPromise.length === BATCH_SIZE
) {
// eslint-disable-next-line no-await-in-loop
const missingAgreements = await Promise.all(getAgreementDataPromise);

// eslint-disable-next-line no-await-in-loop
await this.repositoryModuleManager.bulkCreateServiceAgreementRecords(
missingAgreements.filter((agreement) => agreement != null),
);
getAgreementDataPromise = [];
tokenIdDifference -= BATCH_SIZE;
}
}
if (latestBlockchainTokenId - latestDbTokenId !== 0) {
this.logger.debug(
`Get latest service agreement: Successfully fetched ${
latestBlockchainTokenId - latestDbTokenId
} on blockchain: ${blockchain}`,
);
}
}

async getAgreementDataForToken(
tokenId,
blockchain,
contract,
hashFunctionId = CONTENT_ASSET_HASH_FUNCTION_ID,
) {
this.logger.debug(
`Get latest service agreement: Getting agreement data for token id: ${tokenId} on blockchain: ${blockchain}`,
);
const assertionIds = await this.blockchainModuleManager.getAssertionIds(
blockchain,
contract,
tokenId,
);
const keyword = await this.ualService.calculateLocationKeyword(
blockchain,
contract,
tokenId,
assertionIds[0],
);
const agreementId = await this.serviceAgreementService.generateId(
blockchain,
contract,
tokenId,
keyword,
hashFunctionId,
);
const agreementData = await this.blockchainModuleManager.getAgreementData(
blockchain,
agreementId,
);

if (!agreementData) {
this.logger.warn(
`Unable to fetch agreement data while processing asset created event for agreement id: ${agreementId}, blockchain id: ${blockchain}`,
);
}

const latestStateIndex = assertionIds.length - 1;

return {
blockchainId: blockchain,
assetStorageContractAddress: contract,
tokenId,
agreementId,
startTime: agreementData.startTime,
epochsNumber: agreementData.epochsNumber,
epochLength: agreementData.epochLength,
scoreFunctionId: agreementData.scoreFunctionId,
stateIndex: latestStateIndex,
assertionId: assertionIds[latestStateIndex],
hashFunctionId,
keyword,
proofWindowOffsetPerc: agreementData.proofWindowOffsetPerc,
dataSource: SERVICE_AGREEMENT_SOURCES.NODE,
};
}

/**
* Recover system from failure
* @param error
*/
async recover() {
return Command.repeat();
}

/**
* Builds default command
* @param map
* @returns {{add, data: *, delay: *, deadline: *}}
*/
default(map) {
const command = {
name: 'blockchainGetLatestServiceAgreement',
data: {},
period: GET_LATEST_SERVICE_AGREEMENT_FREQUENCY_MILLS,
transactional: false,
};
Object.assign(command, map);
return command;
}
}

export default BlockchainGetLatestServiceAgreement;
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Command from '../../command.js';
import { GET_LATEST_SERVICE_AGREEMENT_FREQUENCY_MILLS } from '../../../constants/constants.js';

class GetLatestServiceAgreement extends Command {
constructor(ctx) {
super(ctx);
this.commandExecutor = ctx.commandExecutor;
this.shardingTableService = ctx.shardingTableService;
this.repositoryModuleManager = ctx.repositoryModuleManager;
this.blockchainModuleManager = ctx.blockchainModuleManager;
}

/**
* Executes command and produces one or more events
* @param command
*/
async execute() {
const operationId = this.operationIdService.generateId();

this.logger.info(
`Get latest service agreement: Starting get latest service agreement command for operation id: ${operationId}`,
);

await this.commandExecutor.delete('blockchainGetLatestServiceAgreement');

await Promise.all(
this.blockchainModuleManager.getImplementationNames().map(async (blockchain) => {
const commandData = {
blockchain,
operationId,
};

return this.commandExecutor.add({
name: 'blockchainGetLatestServiceAgreement',
data: commandData,
period: GET_LATEST_SERVICE_AGREEMENT_FREQUENCY_MILLS,
});
}),
);

return Command.empty();
}

/**
* Recover system from failure
* @param command
* @param error
*/
async recover(command) {
this.logger.warn(`Failed to execute ${command.name}. Error: ${command.message}`);

return Command.repeat();
}

/**
* Builds default command
* @param map
* @returns {{add, data: *, delay: *, deadline: *}}
*/
default(map) {
const command = {
name: 'getLatestServiceAgreement',
data: {},
transactional: false,
};
Object.assign(command, map);
return command;
}
}

export default GetLatestServiceAgreement;
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,9 @@ class BlockchainEpochCheckCommand extends Command {

/**
* Recover system from failure
* @param command
* @param error
*/
async recover(command) {
this.logger.warn(`Failed to execute ${command.name}. Error: ${command.message}`);

async recover() {
return Command.repeat();
}

Expand Down
18 changes: 6 additions & 12 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ export const REMOVE_SESSION_COMMAND_DELAY = 2 * 60 * 1000;

export const OPERATION_IDS_COMMAND_CLEANUP_TIME_MILLS = 24 * 60 * 60 * 1000;

export const GET_LATEST_SERVICE_AGREEMENT_FREQUENCY_MILLS = 30 * 1000;

export const DIAL_PEERS_COMMAND_FREQUENCY_MILLS = 30 * 1000;

export const DIAL_PEERS_CONCURRENCY = 10;
Expand All @@ -174,6 +176,7 @@ export const PERMANENT_COMMANDS = [
'commandsCleanerCommand',
'dialPeersCommand',
'epochCheckCommand',
'getLatestServiceAgreement',
'blockchainEventCleanerCommand',
'getCleanerCommand',
'getResponseCleanerCommand',
Expand Down Expand Up @@ -464,6 +467,7 @@ export const EXPECTED_TRANSACTION_ERRORS = {
PROOF_WINDOW_CLOSED: 'ProofWindowClosed',
NODE_NOT_AWARDED: 'NodeNotAwarded',
WRONG_MERKLE_PROOF: 'WrongMerkleProof',
NO_MINTED_ASSETS: 'NoMintedAssets',
};

/**
Expand Down Expand Up @@ -661,24 +665,14 @@ export const CONTRACT_EVENTS = {
SHARDING_TABLE: ['NodeAdded', 'NodeRemoved'],
STAKING: ['StakeIncreased', 'StakeWithdrawalStarted'],
PROFILE: ['AskUpdated'],
CONTENT_ASSET: ['AssetMinted'],
COMMIT_MANAGER_V1: ['StateFinalized'],
SERVICE_AGREEMENT_V1: [
'ServiceAgreementV1Created',
'ServiceAgreementV1Extended',
'ServiceAgreementV1Terminated',
],
SERVICE_AGREEMENT_V1: ['ServiceAgreementV1Extended', 'ServiceAgreementV1Terminated'],
PARAMETERS_STORAGE: ['ParameterChanged'],
LOG2PLDSF: ['ParameterChanged'],
LINEAR_SUM: ['ParameterChanged'],
};

export const GROUPED_CONTRACT_EVENTS = {
AssetCreatedGroup: {
events: ['AssetMinted', 'ServiceAgreementV1Created'],
groupingKey: 'tokenId',
},
};
export const GROUPED_CONTRACT_EVENTS = {};

export const CONTRACT_EVENT_TO_GROUP_MAPPING = (() => {
const mapping = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,10 @@ class BidSuggestionController extends BaseController {
firstAssertionId,
hashFunctionId,
} = req.query;
let { proximityScoreFunctionsPairId, bidSuggestionRange } = req.query;
let { bidSuggestionRange } = req.query;
try {
// TODO: ADD-DOCS
if (!proximityScoreFunctionsPairId) {
if (blockchain.startsWith('otp') || blockchain.startsWith('hardhat1'))
proximityScoreFunctionsPairId = 1;
else if (blockchain.startsWith('gnosis') || blockchain.startsWith('hardhat2'))
proximityScoreFunctionsPairId = 2;
}
const proximityScoreFunctionsPairId = 2;

if (!bidSuggestionRange) {
bidSuggestionRange = LOW_BID_SUGGESTION;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class HardhatService extends Web3Service {
}

async getAgreementScoreFunctionId() {
if (this.getBlockchainId() === 'hardhat1:31337') {
return 1;
}
return 2;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,6 @@ class OtParachainService extends Web3Service {
});
return wallets;
}

async getAgreementScoreFunctionId() {
return 1;
}
}

export default OtParachainService;
Loading

0 comments on commit feddcb2

Please sign in to comment.