Skip to content

Commit

Permalink
fix(persistence-fabric): hide not critical API
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
outSH committed Dec 13, 2023
1 parent f557241 commit 601ca08
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 63 deletions.
16 changes: 0 additions & 16 deletions packages/cactus-plugin-persistence-fabric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ export class PluginPersistenceFabric
private isConnected = false;
private isWebServicesRegistered = false;

private failedBlocks = new Set<number>();

// = > private lastSeenBlock = 0;
private lastSeenBlock = 0;
// Last Block in Ledger
Expand All @@ -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";
Expand Down Expand Up @@ -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<string> {
return new Promise<string>((resolve) => {
resolve("hello World test");
});
}

public getPackageName(): string {
return `@hyperledger/cactus-plugin-persistence-fabric`;
Expand All @@ -154,6 +145,7 @@ export class PluginPersistenceFabric
webServices.forEach((ws) => ws.registerExpress(app));
return webServices;
}

// For future development
public async getOrCreateWebServices(): Promise<IWebServiceEndpoint[]> {
const pkgName = this.getPackageName();
Expand All @@ -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<number> {
const lastBlockInChainTest = await this.apiClient.runTransactionV1({
Expand All @@ -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<number> {
private async lastBlockInLedgerWithoutFabricProto(): Promise<number> {
let tempBlockNumber = this.lastBlock;
let blockNumber = tempBlockNumber.toString();
let block: AxiosResponse<GetBlockResponseV1> =
Expand Down Expand Up @@ -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
Expand All @@ -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<string> {
private async initialBlocksSynchronization(
edgeOfLedger: number,
): Promise<string> {
let tempBlockNumber = 0;
let blockNumber = tempBlockNumber.toString();
let block: AxiosResponse<GetBlockResponseV1> =
Expand Down Expand Up @@ -312,10 +292,9 @@ export class PluginPersistenceFabric
* @returns string promise lastBlock number after finishing the process
*
*/

async continueBlocksSynchronization(
public async syncAll(
signingCredential: FabricSigningCredential,
): Promise<string> {
): Promise<number> {
this.lastSeenBlock = await this.dbClient.getMaxBlockNumber();
let tempBlockNumber = this.lastSeenBlock;
let blockNumber = tempBlockNumber.toString();
Expand Down Expand Up @@ -351,7 +330,7 @@ export class PluginPersistenceFabric
this.synchronizationGo = false;
}
} while (this.synchronizationGo);
return "done";
return 1;
}

/** Synchronization of blocks
Expand All @@ -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<string> {
this.lastSeenBlock = await this.dbClient.getMaxBlockNumber();
this.synchronizationGo = true;
Expand All @@ -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) {
Expand Down Expand Up @@ -417,7 +399,7 @@ export class PluginPersistenceFabric
return "stopped";
}

async changeSynchronization(): Promise<boolean> {
private async changeSynchronization(): Promise<boolean> {
if (this.synchronizationGo) {
this.synchronizationGo = false;
} else {
Expand All @@ -426,7 +408,7 @@ export class PluginPersistenceFabric
return this.synchronizationGo;
}

async getBlockFromLedger(blockNumber: string): Promise<any> {
private async getBlockFromLedger(blockNumber: string): Promise<any> {
const block: AxiosResponse<GetBlockResponseV1> =
await this.apiClient.getBlockV1({
channelName: this.ledgerChannelName,
Expand All @@ -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<boolean> {
const block: AxiosResponse<GetBlockResponseV1> =
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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<number> {
private async whichBlocksAreMissingInDdSimple(): Promise<number> {
this.howManyBlocksMissing = 0;

for (let iterator: number = this.lastBlock; iterator >= 0; iterator--) {
Expand All @@ -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<number> {
public async syncFailedBlocks(): Promise<number> {
if (this.howManyBlocksMissing > 0) {
let missedIndex = 0;
let blockNumber: string = this.missedBlocks[missedIndex];
Expand Down Expand Up @@ -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<number> {
private async migrateNextBlock(): Promise<number> {
const toMigrate: number = this.lastBlock + 1;
try {
await this.migrateBlockNrWithTransactions(toMigrate.toString());
Expand All @@ -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<QueryResult> {
console.log("insert Block Data Entry");
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18485,7 +18485,7 @@ __metadata:
"@types/react": ^18.2.39
"@types/react-dom": ^18.2.17
"@types/uuid": ^9.0.7
axios: ^1.6.2
axios: 1.6.0
react: ^18.2.0
react-dom: ^18.2.0
react-scripts: 5.0.1
Expand Down

0 comments on commit 601ca08

Please sign in to comment.