-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migration/fix service agreement #3402
Open
u-hubar
wants to merge
8
commits into
release/v6.5.2
Choose a base branch
from
migration/fix-service-agreement
base: release/v6.5.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a260f56
Migration blueprint
Mihajlo-Pavlovic 85a1d68
wip
Mihajlo-Pavlovic 7711b8b
Migration done
Mihajlo-Pavlovic f6a5b62
fix typo
Mihajlo-Pavlovic 5f32d26
Testing done
Mihajlo-Pavlovic 319f876
revert testing changes
Mihajlo-Pavlovic 4867e69
Update src/migration/service-agreement-fix-migration.js
Mihajlo-Pavlovic 9cf8787
fixes
Mihajlo-Pavlovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import BaseMigration from './base-migration.js'; | ||
import { NODE_ENVIRONMENTS, CONTENT_ASSET_HASH_FUNCTION_ID } from '../constants/constants.js'; | ||
|
||
const NUMBER_OF_ASSETS_FROM_DB = 1_000_000; | ||
const BATCH_FOR_RPC_CALLS = 25; | ||
|
||
class ServiceAgreementFixMigration extends BaseMigration { | ||
constructor( | ||
migrationName, | ||
logger, | ||
config, | ||
repositoryModuleManager, | ||
blockchainModuleManager, | ||
serviceAgreementService, | ||
) { | ||
super(migrationName, logger, config); | ||
this.repositoryModuleManager = repositoryModuleManager; | ||
this.blockchainModuleManager = blockchainModuleManager; | ||
this.serviceAgreementService = serviceAgreementService; | ||
} | ||
|
||
async executeMigration() { | ||
let blockchainId; | ||
switch (process.env.NODE_ENV) { | ||
case NODE_ENVIRONMENTS.DEVNET: | ||
blockchainId = 'otp:2160'; | ||
break; | ||
case NODE_ENVIRONMENTS.TESTNET: | ||
blockchainId = 'otp:20430'; | ||
break; | ||
case NODE_ENVIRONMENTS.MAINENET: | ||
default: | ||
blockchainId = 'otp:2043'; | ||
} | ||
|
||
// Get count of service agreement for neuroweb | ||
const serviceAgreementCount = | ||
await this.repositoryModuleManager.getCountOfServiceAgreementsByBlockchain( | ||
blockchainId, | ||
); | ||
|
||
// In batches | ||
const numberOfIteration = Math.ceil(serviceAgreementCount / NUMBER_OF_ASSETS_FROM_DB); | ||
for (let i = 0; i < numberOfIteration; i += 1) { | ||
let serviceAgreementToBeUpdated = []; | ||
const serviceAgreementBatch = | ||
// eslint-disable-next-line no-await-in-loop | ||
await this.repositoryModuleManager.getServiceAgreementsByBlockchainInBatches( | ||
blockchainId, | ||
NUMBER_OF_ASSETS_FROM_DB, | ||
i * NUMBER_OF_ASSETS_FROM_DB, | ||
); | ||
for (let j = 0; j < serviceAgreementBatch.length; j += BATCH_FOR_RPC_CALLS) { | ||
const currentBatch = serviceAgreementBatch.slice(j, j + BATCH_FOR_RPC_CALLS); | ||
|
||
const batchPromises = currentBatch.map((serviceAgreement) => | ||
this.compareDataWithOnChainData(serviceAgreement), | ||
); | ||
// eslint-disable-next-line no-await-in-loop | ||
const results = await Promise.all(batchPromises); | ||
|
||
const mismatches = results.filter((result) => result !== null); | ||
|
||
if (mismatches.length > 0) { | ||
this.logger.trace(`Mismatches found: ${mismatches.length}`); | ||
serviceAgreementToBeUpdated = serviceAgreementToBeUpdated.concat(mismatches); | ||
} | ||
} | ||
if (serviceAgreementToBeUpdated.length > 0) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await this.repositoryModuleManager.updateAssertionIdServiceAgreement( | ||
blockchainId, | ||
serviceAgreementToBeUpdated, | ||
); | ||
} | ||
} | ||
} | ||
|
||
async compareDataWithOnChainData(serviceAgreement) { | ||
let assertionId; | ||
try { | ||
assertionId = await this.blockchainModuleManager.getAssertionIdByIndex( | ||
serviceAgreement.blockchainId, | ||
serviceAgreement.assetStorageContractAddress, | ||
serviceAgreement.tokenId, | ||
0, | ||
); | ||
} catch (error) { | ||
this.logger.warn( | ||
`Unable to fetch assertionIdfor token id: ${serviceAgreement.tokenId}`, | ||
); | ||
return null; | ||
} | ||
|
||
if (serviceAgreement.asssertionId !== assertionId) { | ||
const serviceAgreementId = this.serviceAgreementService.generateId( | ||
serviceAgreement.blockchainId, | ||
serviceAgreement.assetStorageContractAddress, | ||
serviceAgreement.tokenId, | ||
assertionId, | ||
CONTENT_ASSET_HASH_FUNCTION_ID, // 1 - sha256 | ||
); | ||
return { | ||
tokenId: serviceAgreement.tokenId, | ||
assertionId, | ||
serviceAgreementId, | ||
}; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
export default ServiceAgreementFixMigration; |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this would work, nodes will start executing this simultaneously, will be a huge RPC load
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't do it in batches it will last too long, we can make batches smaller