Skip to content

Commit

Permalink
cache the last submitted block in parentchain
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrjerome committed Jul 15, 2023
1 parent 4f071ec commit c5f8a7a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions backend/src/interfaces/output/blocksResponse.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface BlockResponse extends BaseBlockResponse {
parentHash: string;
committedInSubnet: boolean;
committedInParentChain: boolean;
submittedInParentChain: boolean;
}

export interface BaseBlockResponse {
Expand Down
31 changes: 18 additions & 13 deletions backend/src/services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,14 @@ export class BlockService {
}

const sameChainBlocks = this.filterOutForksBeforeStartingBlock(allBlocks, lastCommittedBlock);
const { committed } = await this.getLastParentchainSubnetBlock();
const { committed, submitted } = await this.getLastParentchainSubnetBlock();

return sameChainBlocks.map(b => {
let committedInSubnet = false;
let committedInParentChain = false;
if (b.number <= lastCommittedBlockInfo.number) {
committedInSubnet = true;
}
if (b.number <= committed.height) {
committedInParentChain = true;
}
return {
...b,
committedInSubnet,
committedInParentChain,
committedInSubnet: b.number <= lastCommittedBlockInfo.number,
committedInParentChain: b.number <= committed.height,
submittedInParentChain: b.number <= submitted.height,
};
});
}
Expand All @@ -104,7 +97,7 @@ export class BlockService {

public async getLastParentchainSubnetBlock() {
const { smartContractCommittedHash, smartContractCommittedHeight, smartContractHash, smartContractHeight } =
await this.parentChainClient.getLastAudittedBlock();
await this.getAndSetLastSubmittedBlockInfo();
return {
committed: {
height: smartContractCommittedHeight,
Expand Down Expand Up @@ -194,7 +187,7 @@ export class BlockService {
}

private async getSmartContractProcessingInfo(): Promise<{ processedUntil: number; isProcessing: boolean }> {
const { smartContractHeight, smartContractCommittedHash } = await this.parentChainClient.getLastAudittedBlock();
const { smartContractHeight, smartContractCommittedHash } = await this.getAndSetLastSubmittedBlockInfo();
const { timestamp } = await this.parentChainClient.getParentChainBlockBySubnetHash(smartContractCommittedHash);
let isProcessing = true;
const timeDiff = new Date().getTime() / 1000 - parseInt(timestamp.toString());
Expand All @@ -206,6 +199,17 @@ export class BlockService {
};
}

private async getAndSetLastSubmittedBlockInfo() {
const lastSubmittedBlockInfo = await this.blockStorage.getLastSubmittedBlockInfo();
// Not exist, we need to call the parentchain node and set the value
if (!lastSubmittedBlockInfo) {
const result = await this.parentChainClient.getLastAudittedBlock();
await this.blockStorage.setLastSubmittedToParentchainBlockInfo(result);
return result;
}
return lastSubmittedBlockInfo;
}

/**
In order to insert into the queue. we need it satisfy any of the below condition
Expand Down Expand Up @@ -263,6 +267,7 @@ export class BlockService {
...b,
committedInSubnet: false,
committedInParentChain: false,
submittedInParentChain: false,
};
});
}
Expand Down
11 changes: 11 additions & 0 deletions backend/src/storage/block.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { SmartContractAuditedBlockInfo } from '../client/parentchain';
import { MAX_NUM_OF_BLOCKS_IN_HISTORY } from '../config';
import { Db } from './base';

const TYPE = 'BLOCK';
// In case of no new blocks in 60s, we declare the block componenet of the system is not operational
const STATUS_KEY = 'BLOCK_STATUS';
const TTL = 60;
const CHECK_PARENTCHAIN_TTL = 2;

const LATEST_COMMITTEDBLOCK_KEY = 'LATEST_COMMITTED_BLOCK';
const LATEST_PARENTCHAIN_SUBMITTED_KEY = 'LATEST_PARENTCHAIN_SUBMITTED_BLOCKINFO';

/**
This class is created so that we can easily swap with real DB without making changes to any other files.
Expand Down Expand Up @@ -58,6 +61,14 @@ export class BlockStorage {
async getStatus(): Promise<boolean> {
return this.db.get(STATUS_KEY) || false;
}

async getLastSubmittedBlockInfo(): Promise<SmartContractAuditedBlockInfo> {
return this.db.get(LATEST_PARENTCHAIN_SUBMITTED_KEY);
}

async setLastSubmittedToParentchainBlockInfo(blockInfo: SmartContractAuditedBlockInfo) {
this.db.set(LATEST_PARENTCHAIN_SUBMITTED_KEY, blockInfo, CHECK_PARENTCHAIN_TTL);
}
}

export interface StoredBlock {
Expand Down
4 changes: 4 additions & 0 deletions backend/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,15 @@ definitions:
committedInParentChain:
description: This boolean value is to indicate whether this block has been confirmed in the parent chain smart contract
type: boolean
submittedInParentChain:
description: This boolean value is to indicate whether this block has been submitted in the parent chain smart contract and waiting for the confirmation
type: boolean
required:
- hash
- number
- committedInParentChain
- committedInSubnet
- submittedInParentChain

schemes:
- https
Expand Down

0 comments on commit c5f8a7a

Please sign in to comment.