From 33be157065132c95389579b7878d913df3ebb8aa Mon Sep 17 00:00:00 2001 From: Michal Bajer Date: Wed, 13 Dec 2023 14:02:21 +0000 Subject: [PATCH] fix(persistence-fabric): hide not critical API - Make not critical connector API private. This will allow easier refactors in the future. Most of the fields were used for testing purposes anyway. - Change naming of some methods to match persistence-ethereum plugin. Signed-off-by: Michal Bajer --- .../README.md | 16 ---- .../typescript/plugin-persistence-fabric.ts | 74 +++++++------------ 2 files changed, 28 insertions(+), 62 deletions(-) diff --git a/packages/cactus-plugin-persistence-fabric/README.md b/packages/cactus-plugin-persistence-fabric/README.md index f6ef80ef5c7..a31cace9ad1 100644 --- a/packages/cactus-plugin-persistence-fabric/README.md +++ b/packages/cactus-plugin-persistence-fabric/README.md @@ -37,22 +37,6 @@ yarn run configure Instantiate a new `PluginPersistenceFabrickBlock` instance: -There is few ways to use this plugin: - -1. Using Watch block might cause infinite loop - and function migrateBlockNrWithTransactions -2. Individually using migrateBlockNrWithTransactions - if you know which exactly which blocks you want to analyze -3. Using plugin function continueBlocksSynchronization in some periodical calls -4. Using plugin function continuousBlocksSynchronization in some reasonable time with changeSynchronization - which should break flow - this function might continue work for long period - -5) Best to start synchronization with initialBlocksSynchronization , which will transfer and parse into database number of blocks equal to edgeOfLedger - -6) If there were some issues in network connection or you think that your database might be corrupted and lost some data you might use whichBlocksAreMissingInDdSimple - and then showHowManyBlocksMissing if more than 0 - then use synchronizeOnlyMissedBlocks - ```typescript import { PluginPersistenceFabric } from "../../../main/typescript/plugin-fabric-persistence-block"; import { v4 as uuidv4 } from "uuid"; diff --git a/packages/cactus-plugin-persistence-fabric/src/main/typescript/plugin-persistence-fabric.ts b/packages/cactus-plugin-persistence-fabric/src/main/typescript/plugin-persistence-fabric.ts index 90b30f77e22..7aae96952ed 100644 --- a/packages/cactus-plugin-persistence-fabric/src/main/typescript/plugin-persistence-fabric.ts +++ b/packages/cactus-plugin-persistence-fabric/src/main/typescript/plugin-persistence-fabric.ts @@ -58,8 +58,6 @@ export class PluginPersistenceFabric private isConnected = false; private isWebServicesRegistered = false; - private failedBlocks = new Set(); - // = > private lastSeenBlock = 0; private lastSeenBlock = 0; // Last Block in Ledger @@ -69,12 +67,11 @@ export class PluginPersistenceFabric private missedBlocks: string[] = []; private howManyBlocksMissing = 0; - public ledgerChannelName = "mychannel"; - public ledgerContractName = "basic"; + private ledgerChannelName = "mychannel"; // gateway options - public gatewayOptions: GatewayOptions; + private gatewayOptions: GatewayOptions; // synchronization ongoing - public synchronizationGo = true; + private synchronizationGo = true; constructor(public readonly options: IPluginPersistenceFabricOptions) { const level = this.options.logLevel || "INFO"; @@ -128,12 +125,6 @@ export class PluginPersistenceFabric public getInstanceId(): string { return this.instanceId; } - // this is just test function to check if you correctly created instance of plugin - public async helloWorldTest(): Promise { - return new Promise((resolve) => { - resolve("hello World test"); - }); - } public getPackageName(): string { return `@hyperledger/cactus-plugin-persistence-fabric`; @@ -154,6 +145,7 @@ export class PluginPersistenceFabric webServices.forEach((ws) => ws.registerExpress(app)); return webServices; } + // For future development public async getOrCreateWebServices(): Promise { const pkgName = this.getPackageName(); @@ -173,27 +165,11 @@ export class PluginPersistenceFabric return endpoints; } - // current last block from ledger ( not in database ) - public currentLastBlock(): number { - return this.lastBlock; - } - // this is greatest block number successfully migrated to database - public currentLastSeenBlock(): number { - return this.lastSeenBlock; - } - // Additional check if ledger is in synchronization - public isLastBlockGreatherThenLastSeen(): boolean { - if (this.lastSeenBlock >= this.lastBlock) { - return false; - } else { - return true; - } - } /** * lastBlockInLedger * @returns this.lastBlock which is last block in ledger assuming using getBlock and node js SDK */ - public async lastBlockInLedger( + private async lastBlockInLedger( signingCredentialInput: FabricSigningCredential, ): Promise { const lastBlockInChainTest = await this.apiClient.runTransactionV1({ @@ -216,12 +192,13 @@ export class PluginPersistenceFabric const lastBlock = lastBlockPreview.height; return lastBlock; } + //test /** * lastBlockInLedgerWithoutFabricProto * @returns this.lastBlock which is last block in ledger assuming using getBlock and node js SDK */ - public async lastBlockInLedgerWithoutFabricProto(): Promise { + private async lastBlockInLedgerWithoutFabricProto(): Promise { let tempBlockNumber = this.lastBlock; let blockNumber = tempBlockNumber.toString(); let block: AxiosResponse = @@ -254,6 +231,7 @@ export class PluginPersistenceFabric } while (moreBlocks); return this.lastBlock; } + /** Synchronization of blocks * - Synchronize entire first edgeOfLedger blocks of ledger state * @param edgeOfLedger defines which part of blockchain do we want to include in database @@ -262,7 +240,9 @@ export class PluginPersistenceFabric * // future changes - parameter to set which part of blokchain to move to database */ - async initialBlocksSynchronization(edgeOfLedger: number): Promise { + private async initialBlocksSynchronization( + edgeOfLedger: number, + ): Promise { let tempBlockNumber = 0; let blockNumber = tempBlockNumber.toString(); let block: AxiosResponse = @@ -312,10 +292,9 @@ export class PluginPersistenceFabric * @returns string promise lastBlock number after finishing the process * */ - - async continueBlocksSynchronization( + public async syncAll( signingCredential: FabricSigningCredential, - ): Promise { + ): Promise { this.lastSeenBlock = await this.dbClient.getMaxBlockNumber(); let tempBlockNumber = this.lastSeenBlock; let blockNumber = tempBlockNumber.toString(); @@ -351,7 +330,7 @@ export class PluginPersistenceFabric this.synchronizationGo = false; } } while (this.synchronizationGo); - return "done"; + return 1; } /** Synchronization of blocks @@ -362,8 +341,9 @@ export class PluginPersistenceFabric */ // NOTE: this function can loop into very long almost infinite loop or even // infinite loop - async continuousBlocksSynchronization( + public async startMonitor( signingCredential: FabricSigningCredential, + onError?: (err: unknown) => void, ): Promise { this.lastSeenBlock = await this.dbClient.getMaxBlockNumber(); this.synchronizationGo = true; @@ -384,7 +364,9 @@ export class PluginPersistenceFabric }, }); } catch (error) { - this.log.info("Last block in ledger", tempBlockNumber - 1); + if (onError) { + onError(error); + } } if (block?.status == 200) { @@ -417,7 +399,7 @@ export class PluginPersistenceFabric return "stopped"; } - async changeSynchronization(): Promise { + private async changeSynchronization(): Promise { if (this.synchronizationGo) { this.synchronizationGo = false; } else { @@ -426,7 +408,7 @@ export class PluginPersistenceFabric return this.synchronizationGo; } - async getBlockFromLedger(blockNumber: string): Promise { + private async getBlockFromLedger(blockNumber: string): Promise { const block: AxiosResponse = await this.apiClient.getBlockV1({ channelName: this.ledgerChannelName, @@ -450,7 +432,7 @@ export class PluginPersistenceFabric * block number to be moved from ledger to database * @returns true a boolean which indicates successfull migration */ - public async migrateBlockNrWithTransactions( + private async migrateBlockNrWithTransactions( blockNumber: string, ): Promise { const block: AxiosResponse = @@ -711,7 +693,7 @@ export class PluginPersistenceFabric If some blocks above this number are already in database they will not be removed. * @returns number which is this.lastBlock , artificially set lastBlock in ledger */ - public setLastBlockConsidered(limitLastBlockConsidered: number): number { + private setLastBlockConsidered(limitLastBlockConsidered: number): number { this.lastBlock = limitLastBlockConsidered; return this.lastBlock; } @@ -721,7 +703,7 @@ If some blocks above this number are already in database they will not be remove * @returns number blocks missing according to last run of function which checks missing blocks * whichBlocksAreMissingInDdSimple */ - public showHowManyBlocksMissing(): number { + private showHowManyBlocksMissing(): number { return this.howManyBlocksMissing; } @@ -730,7 +712,7 @@ If some blocks above this number are already in database they will not be remove * that could not be synchronized with the DB for some reasons and list them * @returns number of missing blocks */ - public async whichBlocksAreMissingInDdSimple(): Promise { + private async whichBlocksAreMissingInDdSimple(): Promise { this.howManyBlocksMissing = 0; for (let iterator: number = this.lastBlock; iterator >= 0; iterator--) { @@ -750,7 +732,7 @@ If some blocks above this number are already in database they will not be remove * run function whichBlocksAreMissingInDdSimple before using this one * @returns number of missing blocks if any , should return 0 */ - async synchronizeOnlyMissedBlocks(): Promise { + public async syncFailedBlocks(): Promise { if (this.howManyBlocksMissing > 0) { let missedIndex = 0; let blockNumber: string = this.missedBlocks[missedIndex]; @@ -791,7 +773,7 @@ If some blocks above this number are already in database they will not be remove /** migrateNextBlock * tries to migrate next block according to lastBlock information stored in plugin */ - public async migrateNextBlock(): Promise { + private async migrateNextBlock(): Promise { const toMigrate: number = this.lastBlock + 1; try { await this.migrateBlockNrWithTransactions(toMigrate.toString()); @@ -804,7 +786,7 @@ If some blocks above this number are already in database they will not be remove return 0; } - public async insertBlockDataEntry( + private async insertBlockDataEntry( data: InsertBlockDataEntryInterface, ): Promise { console.log("insert Block Data Entry");