-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added get latest service agreement data command, removed listening on asset created grouped events * Updated default hash function id, prioritizing commits and proofs for assets with new score function id * Added permanent command * Fixes after testing for latest-service-agreement command * Updated version of contracts that are deployed for hardhat 1 * Added support for neuro * comment removed, logging improved * Default score function id for neuro updated --------- Co-authored-by: Mihajlo Pavlovic <mihajlo.pavlovic@origin-trail.com>
- Loading branch information
1 parent
d3cd15b
commit ef12e8e
Showing
11 changed files
with
289 additions
and
120 deletions.
There are no files selected for viewing
186 changes: 186 additions & 0 deletions
186
src/commands/common/get-latest-service-agreement/blockchain-get-latest-service-agreement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
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}`, | ||
); | ||
|
||
const missingTokenIds = Array.from( | ||
{ length: latestBlockchainTokenId - latestDbTokenId }, | ||
(_, index) => latestDbTokenId + index + 1, | ||
); | ||
this.logger.debug( | ||
`Get latest service agreement: Found ${missingTokenIds.length} on blockchain: ${blockchain}`, | ||
); | ||
let batchNumber = 0; | ||
while (batchNumber * BATCH_SIZE < missingTokenIds.length) { | ||
const promises = []; | ||
for ( | ||
let i = batchNumber * BATCH_SIZE; | ||
i < missingTokenIds.length && i < (batchNumber + 1) * BATCH_SIZE; | ||
i += 1 | ||
) { | ||
const tokenIdToBeFetched = missingTokenIds[i]; | ||
promises.push( | ||
this.getAgreementDataForToken(tokenIdToBeFetched, blockchain, contract), | ||
); | ||
} | ||
|
||
// eslint-disable-next-line no-await-in-loop | ||
const missingAgreements = await Promise.all(promises); | ||
|
||
// eslint-disable-next-line no-await-in-loop | ||
await this.repositoryModuleManager.bulkCreateServiceAgreementRecords( | ||
missingAgreements.filter((agreement) => agreement != null), | ||
); | ||
batchNumber += 1; | ||
} | ||
if (missingTokenIds.length !== 0) { | ||
this.logger.debug( | ||
`Get latest service agreement: Successfully fetched ${missingTokenIds.length} 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; |
71 changes: 71 additions & 0 deletions
71
src/commands/common/get-latest-service-agreement/get-latest-service-agreement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.