mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 20:36:19 +00:00
Update ipld-status table in reset and fill state CLI (#193)
This commit is contained in:
parent
00a9c247f5
commit
6149690126
@ -120,10 +120,10 @@ export class Database implements IPLDDatabaseInterface {
|
||||
await this._baseDatabase.removeIPLDBlocks(repo, blockNumber, kind);
|
||||
}
|
||||
|
||||
async removeIPLDBlocksInRange (dbTx: QueryRunner, startBlock: number, endBlock: number): Promise<void> {
|
||||
async removeIPLDBlocksAfterBlock (dbTx: QueryRunner, blockNumber: number): Promise<void> {
|
||||
const repo = dbTx.manager.getRepository(IPLDBlock);
|
||||
|
||||
await this._baseDatabase.removeIPLDBlocksInRange(repo, startBlock, endBlock);
|
||||
await this._baseDatabase.removeIPLDBlocksAfterBlock(repo, blockNumber);
|
||||
}
|
||||
|
||||
async getIPLDStatus (): Promise<IpldStatus | undefined> {
|
||||
|
@ -15,21 +15,13 @@ export const command = 'ipld-state';
|
||||
export const desc = 'Reset IPLD state in the given range';
|
||||
|
||||
export const builder = {
|
||||
startBlock: {
|
||||
type: 'number'
|
||||
},
|
||||
endBlock: {
|
||||
blockNumber: {
|
||||
type: 'number'
|
||||
}
|
||||
};
|
||||
|
||||
export const handler = async (argv: any): Promise<void> => {
|
||||
const { startBlock, endBlock } = argv;
|
||||
if (startBlock > endBlock) {
|
||||
log('endBlock should be greater than or equal to startBlock');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const { blockNumber } = argv;
|
||||
const config = await getConfig(argv.configFile);
|
||||
|
||||
// Initialize database
|
||||
@ -39,9 +31,27 @@ export const handler = async (argv: any): Promise<void> => {
|
||||
// Create a DB transaction
|
||||
const dbTx = await db.createTransactionRunner();
|
||||
|
||||
console.time('time:reset-ipld-state');
|
||||
try {
|
||||
// Delete all IPLDBlock entries in the given range
|
||||
await db.removeIPLDBlocksInRange(dbTx, startBlock, endBlock);
|
||||
await db.removeIPLDBlocksAfterBlock(dbTx, blockNumber);
|
||||
|
||||
// Reset the IPLD status.
|
||||
const ipldStatus = await db.getIPLDStatus();
|
||||
|
||||
if (ipldStatus) {
|
||||
if (ipldStatus.latestHooksBlockNumber > blockNumber) {
|
||||
await db.updateIPLDStatusHooksBlock(dbTx, blockNumber, true);
|
||||
}
|
||||
|
||||
if (ipldStatus.latestCheckpointBlockNumber > blockNumber) {
|
||||
await db.updateIPLDStatusCheckpointBlock(dbTx, blockNumber, true);
|
||||
}
|
||||
|
||||
if (ipldStatus.latestIPFSBlockNumber > blockNumber) {
|
||||
await db.updateIPLDStatusIPFSBlock(dbTx, blockNumber, true);
|
||||
}
|
||||
}
|
||||
|
||||
dbTx.commitTransaction();
|
||||
} catch (error) {
|
||||
@ -50,6 +60,7 @@ export const handler = async (argv: any): Promise<void> => {
|
||||
} finally {
|
||||
await dbTx.release();
|
||||
}
|
||||
console.timeEnd('time:reset-ipld-state');
|
||||
|
||||
log(`Reset ipld-state successfully for range [${startBlock}, ${endBlock}]`);
|
||||
log(`Reset ipld-state successfully to block ${blockNumber}`);
|
||||
};
|
||||
|
@ -15,21 +15,13 @@ export const command = 'ipld-state';
|
||||
export const desc = 'Reset IPLD state in the given range';
|
||||
|
||||
export const builder = {
|
||||
startBlock: {
|
||||
type: 'number'
|
||||
},
|
||||
endBlock: {
|
||||
blockNumber: {
|
||||
type: 'number'
|
||||
}
|
||||
};
|
||||
|
||||
export const handler = async (argv: any): Promise<void> => {
|
||||
const { startBlock, endBlock } = argv;
|
||||
if (startBlock > endBlock) {
|
||||
log('endBlock should be greater than or equal to startBlock');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const { blockNumber } = argv;
|
||||
const config = await getConfig(argv.configFile);
|
||||
|
||||
// Initialize database
|
||||
@ -42,7 +34,24 @@ export const handler = async (argv: any): Promise<void> => {
|
||||
console.time('time:reset-ipld-state');
|
||||
try {
|
||||
// Delete all IPLDBlock entries in the given range
|
||||
await db.removeIPLDBlocksInRange(dbTx, startBlock, endBlock);
|
||||
await db.removeIPLDBlocksAfterBlock(dbTx, blockNumber);
|
||||
|
||||
// Reset the IPLD status.
|
||||
const ipldStatus = await db.getIPLDStatus();
|
||||
|
||||
if (ipldStatus) {
|
||||
if (ipldStatus.latestHooksBlockNumber > blockNumber) {
|
||||
await db.updateIPLDStatusHooksBlock(dbTx, blockNumber, true);
|
||||
}
|
||||
|
||||
if (ipldStatus.latestCheckpointBlockNumber > blockNumber) {
|
||||
await db.updateIPLDStatusCheckpointBlock(dbTx, blockNumber, true);
|
||||
}
|
||||
|
||||
if (ipldStatus.latestIPFSBlockNumber > blockNumber) {
|
||||
await db.updateIPLDStatusIPFSBlock(dbTx, blockNumber, true);
|
||||
}
|
||||
}
|
||||
|
||||
dbTx.commitTransaction();
|
||||
} catch (error) {
|
||||
@ -53,5 +62,5 @@ export const handler = async (argv: any): Promise<void> => {
|
||||
}
|
||||
console.timeEnd('time:reset-ipld-state');
|
||||
|
||||
log(`Reset ipld-state successfully for range [${startBlock}, ${endBlock}]`);
|
||||
log(`Reset ipld-state successfully to block ${blockNumber}`);
|
||||
};
|
||||
|
@ -80,10 +80,10 @@ export class Database implements IPLDDatabaseInterface {
|
||||
await this._baseDatabase.removeIPLDBlocks(repo, blockNumber, kind);
|
||||
}
|
||||
|
||||
async removeIPLDBlocksInRange (dbTx: QueryRunner, startBlock: number, endBlock: number): Promise<void> {
|
||||
async removeIPLDBlocksAfterBlock (dbTx: QueryRunner, blockNumber: number): Promise<void> {
|
||||
const repo = dbTx.manager.getRepository(IPLDBlock);
|
||||
|
||||
await this._baseDatabase.removeIPLDBlocksInRange(repo, startBlock, endBlock);
|
||||
await this._baseDatabase.removeIPLDBlocksAfterBlock(repo, blockNumber);
|
||||
}
|
||||
|
||||
async getIPLDStatus (): Promise<IpldStatus | undefined> {
|
||||
|
@ -97,16 +97,31 @@ export const fillState = async (
|
||||
|
||||
// Persist subgraph state to the DB
|
||||
await indexer.dumpSubgraphState(blockHash, true);
|
||||
await indexer.updateIPLDStatusHooksBlock(blockNumber);
|
||||
|
||||
// Create checkpoints
|
||||
await indexer.processCheckpoint(blockHash);
|
||||
await indexer.updateIPLDStatusCheckpointBlock(blockNumber);
|
||||
|
||||
// TODO: Push state to IPFS in separate process.
|
||||
if (indexer.isIPFSConfigured()) {
|
||||
// Get IPLDBlocks for the given blocHash.
|
||||
const ipldBlocks = await indexer.getIPLDBlocksByHash(blockHash);
|
||||
|
||||
// Push all the IPLDBlocks to IPFS.
|
||||
for (const ipldBlock of ipldBlocks) {
|
||||
const data = indexer.getIPLDData(ipldBlock);
|
||||
await indexer.pushToIPFS(data);
|
||||
}
|
||||
|
||||
// Update the IPLD status.
|
||||
await indexer.updateIPLDStatusIPFSBlock(blockNumber);
|
||||
}
|
||||
|
||||
console.timeEnd(`time:fill-state-${blockNumber}`);
|
||||
}
|
||||
|
||||
console.timeEnd('time:fill-state');
|
||||
|
||||
// TODO: Push state to IPFS
|
||||
|
||||
log(`Filled state for subgraph entities in range: [${startBlock}, ${endBlock}]`);
|
||||
};
|
||||
|
@ -210,7 +210,7 @@ export class IPLDDatabase extends Database {
|
||||
}
|
||||
}
|
||||
|
||||
async removeIPLDBlocksInRange (repo: Repository<IPLDBlockInterface>, startBlock: number, endBlock: number): Promise<void> {
|
||||
async removeIPLDBlocksAfterBlock (repo: Repository<IPLDBlockInterface>, blockNumber: number): Promise<void> {
|
||||
// Use raw SQL as TypeORM curently doesn't support delete via 'join' or 'using'
|
||||
const deleteQuery = `
|
||||
DELETE FROM
|
||||
@ -218,10 +218,10 @@ export class IPLDDatabase extends Database {
|
||||
USING block_progress
|
||||
WHERE
|
||||
ipld_block.block_id = block_progress.id
|
||||
AND block_progress.block_number BETWEEN $1 AND $2;
|
||||
AND block_progress.block_number > $1;
|
||||
`;
|
||||
|
||||
await repo.query(deleteQuery, [startBlock, endBlock]);
|
||||
await repo.query(deleteQuery, [blockNumber]);
|
||||
}
|
||||
|
||||
async getIPLDStatus (repo: Repository<IpldStatusInterface>): Promise<IpldStatusInterface | undefined> {
|
||||
|
Loading…
Reference in New Issue
Block a user