Skip to content
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

Purge data in explorer based on a parameter #341

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ WORKDIR $EXPLORER_APP_PATH

COPY . .

RUN sed -i -e 's/http:/https:/' /etc/apk/repositories

# install required dependencies by NPM packages:
# current dependencies are: python, make, g++
RUN apk add --no-cache --virtual npm-deps python3 make g++ curl bash && \
Expand All @@ -25,7 +27,7 @@ RUN apk add --no-cache --virtual npm-deps python3 make g++ curl bash && \
rm -r /root/.cache

# install node-prune (https://github.com/tj/node-prune)
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
RUN curl -sf https://gobinaries.com/tj/node-prune | sh

# install NPM dependencies
RUN npm install && npm run build && npm prune --production
Expand Down
36 changes: 36 additions & 0 deletions app/persistence/fabric/CRUDService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,42 @@ export class CRUDService {
return false;
}

/**
*
* @param network_name
* @param channel_genesis_hash
* @param blockCount
* @returns
* @memberof CRUDService
*/
async deleteBlock(network_name, channel_genesis_hash, blockCount) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ongoing we can add types also in the function args

const count: any = await this.sql.getRowsBySQlCase(
' select count(*) as count from blocks where channel_genesis_hash=$1 and network_name = $2 ',
[channel_genesis_hash, network_name]
);

const rowCount: number = count.count;
var rowsToDelete: number = 0;
if (rowCount > blockCount) {
rowsToDelete = rowCount - blockCount;
}

if (rowsToDelete > 0) {
await this.sql.updateBySql(
`delete from transactions where blockid in ( select blocknum from blocks where channel_genesis_hash=$1 and network_name = $2 order by blocknum limit $3) `,
[channel_genesis_hash, network_name, rowsToDelete]
);

await this.sql.updateBySql(
`delete from blocks where id in ( select id from blocks where channel_genesis_hash=$1 and network_name = $2 order by blocknum limit $3) `,
[channel_genesis_hash, network_name, rowsToDelete]
);

return true;
}
return false;
}

/* eslint-enable */

/**
Expand Down
19 changes: 15 additions & 4 deletions app/platform/fabric/sync/SyncPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class SyncPlatform {
syncService: any;
blocksSyncTime: number;
network_config: Record<string, any>;
blockCount: number;

/**
* Creates an instance of SyncPlatform.
Expand All @@ -54,6 +55,7 @@ export class SyncPlatform {
this.syncService = new SyncServices(this, this.persistence);
this.blocksSyncTime = 60000;
this.network_config = null;
this.blockCount = 1000;
}

/**
Expand Down Expand Up @@ -86,6 +88,8 @@ export class SyncPlatform {
this.network_name = args[1];
}

this.blockCount = network_configs[this.network_id].blockCount;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blockCount needs to be defined in config.json in (app/platform/fabric/config.json)


logger.info(explorerError.MESSAGE_1002, this.network_id, this.network_name);

logger.debug('Blocks synch interval time >> %s', this.blocksSyncTime);
Expand Down Expand Up @@ -119,7 +123,7 @@ export class SyncPlatform {
*/
// During initial sync-up phase, disable discovery request
(function validateMissingBlocks(sync: SyncPlatform, noDiscovery: boolean) {
sync.isChannelEventHubConnected(noDiscovery);
sync.isChannelEventHubConnected(noDiscovery, sync.blockCount);
setTimeout(validateMissingBlocks, sync.blocksSyncTime, sync, false);
})(this, true);

Expand All @@ -134,12 +138,19 @@ export class SyncPlatform {
*
* @memberof SyncPlatform
*/
async isChannelEventHubConnected(noDiscovery: boolean) {
async isChannelEventHubConnected(noDiscovery: boolean, blockCount: number) {
for (const channel_name of this.client.getChannels()) {
// Validate channel event is connected
const status = this.eventHub.isChannelEventHubConnected(channel_name);
const status: boolean = this.eventHub.isChannelEventHubConnected(
channel_name
);
if (status) {
await this.syncService.syncBlocks(this.client, channel_name, noDiscovery);
await this.syncService.syncBlocks(
this.client,
channel_name,
noDiscovery,
blockCount
);
} else {
// Channel client is not connected then it will reconnect
this.eventHub.connectChannelEventHub(channel_name);
Expand Down
8 changes: 7 additions & 1 deletion app/platform/fabric/sync/SyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export class SyncServices {
.saveChaincodPeerRef(network_id, chaincode_peer_row);
}

async syncBlocks(client, channel_name, noDiscovery) {
async syncBlocks(client, channel_name, noDiscovery, blockCount) {
const network_id = client.getNetworkId();

// Get channel information from ledger
Expand Down Expand Up @@ -399,6 +399,12 @@ export class SyncServices {
} else {
logger.debug('Missing blocks not found for %s', channel_name);
}

const successDeleteBlock = await this.persistence
.getCrudService()
.deleteBlock(network_id, channel_genesis_hash, blockCount);
logger.info('result of DeleteBlock ', successDeleteBlock);

const index = this.synchInProcess.indexOf(synch_key);
this.synchInProcess.splice(index, 1);
logger.info(`syncBlocks: Finish >> ${synch_key}`);
Expand Down
3 changes: 2 additions & 1 deletion examples/net1/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"network-configs": {
"test-network": {
"name": "Test Network",
"profile": "./connection-profile/test-network.json"
"profile": "./connection-profile/test-network.json",
"blockCount": "5"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick]: update this to numerical value.

}
},
"license": "Apache-2.0"
Expand Down