mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 20:36:19 +00:00
Update CLI to fill state to create checkpoints (#187)
* Update CLI to fill state to create checkpoints * Accomodate changes in codegen
This commit is contained in:
parent
c2bbaa6867
commit
b2cf997900
@ -63,6 +63,9 @@ export const fillState = async (
|
||||
|
||||
const blockHash = blocks[0].blockHash;
|
||||
|
||||
// Create initial state for contracts
|
||||
await indexer.createInit(blockHash, blockNumber);
|
||||
|
||||
// Fill state for each contract in contractEntitiesMap
|
||||
const contractStatePromises = Array.from(contractEntitiesMap.entries())
|
||||
.map(async ([contractAddress, entities]): Promise<void> => {
|
||||
@ -90,7 +93,12 @@ export const fillState = async (
|
||||
|
||||
// Persist subgraph state to the DB
|
||||
await indexer.dumpSubgraphState(blockHash, true);
|
||||
|
||||
// Create checkpoints
|
||||
await indexer.processCheckpoint(blockHash);
|
||||
}
|
||||
|
||||
// TODO: Push state to IPFS
|
||||
|
||||
log(`Filled state for subgraph entities in range: [${startBlock}, ${endBlock}]`);
|
||||
};
|
||||
|
@ -419,7 +419,7 @@ export class Indexer implements IPLDIndexerInterface {
|
||||
return this._baseIndexer.createStateCheckpoint(contractAddress, block, data);
|
||||
}
|
||||
|
||||
// Method to be used by checkpoint CLI.
|
||||
// Method to be used by export-state CLI.
|
||||
async createCheckpoint (contractAddress: string, blockHash: string): Promise<string | undefined> {
|
||||
const block = await this.getBlockProgress(blockHash);
|
||||
assert(block);
|
||||
@ -427,6 +427,12 @@ export class Indexer implements IPLDIndexerInterface {
|
||||
return this._baseIndexer.createCheckpoint(this, contractAddress, block);
|
||||
}
|
||||
|
||||
// Method to be used by fill-state CLI.
|
||||
async createInit (blockHash: string, blockNumber: number): Promise<void> {
|
||||
// Create initial state for contracts.
|
||||
await this._baseIndexer.createInit(this, blockHash, blockNumber);
|
||||
}
|
||||
|
||||
async saveOrUpdateIPLDBlock (ipldBlock: IPLDBlock): Promise<IPLDBlock> {
|
||||
return this._baseIndexer.saveOrUpdateIPLDBlock(ipldBlock);
|
||||
}
|
||||
|
@ -67,6 +67,9 @@ export const fillState = async (
|
||||
|
||||
const blockHash = blocks[0].blockHash;
|
||||
|
||||
// Create initial state for contracts
|
||||
await indexer.createInit(blockHash, blockNumber);
|
||||
|
||||
// Fill state for each contract in contractEntitiesMap
|
||||
const contractStatePromises = Array.from(contractEntitiesMap.entries())
|
||||
.map(async ([contractAddress, entities]): Promise<void> => {
|
||||
@ -95,10 +98,15 @@ export const fillState = async (
|
||||
// Persist subgraph state to the DB
|
||||
await indexer.dumpSubgraphState(blockHash, true);
|
||||
|
||||
// Create checkpoints
|
||||
await indexer.processCheckpoint(blockHash);
|
||||
|
||||
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}]`);
|
||||
};
|
||||
|
@ -349,7 +349,7 @@ export class Indexer implements IPLDIndexerInterface {
|
||||
return this._baseIndexer.createStateCheckpoint(contractAddress, block, data);
|
||||
}
|
||||
|
||||
// Method to be used by checkpoint CLI.
|
||||
// Method to be used by export-state CLI.
|
||||
async createCheckpoint (contractAddress: string, blockHash: string): Promise<string | undefined> {
|
||||
const block = await this.getBlockProgress(blockHash);
|
||||
assert(block);
|
||||
@ -357,6 +357,12 @@ export class Indexer implements IPLDIndexerInterface {
|
||||
return this._baseIndexer.createCheckpoint(this, contractAddress, block);
|
||||
}
|
||||
|
||||
// Method to be used by fill-state CLI.
|
||||
async createInit (blockHash: string, blockNumber: number): Promise<void> {
|
||||
// Create initial state for contracts.
|
||||
await this._baseIndexer.createInit(this, blockHash, blockNumber);
|
||||
}
|
||||
|
||||
async saveOrUpdateIPLDBlock (ipldBlock: IPLDBlock): Promise<IPLDBlock> {
|
||||
return this._baseIndexer.saveOrUpdateIPLDBlock(ipldBlock);
|
||||
}
|
||||
|
@ -27,27 +27,24 @@ export class IPLDDatabase extends Database {
|
||||
? queryBuilder.andWhere('ipld_block.kind = :kind', { kind })
|
||||
: queryBuilder.andWhere('ipld_block.kind != :kind', { kind: StateKind.DiffStaged });
|
||||
|
||||
// Get the first two entries.
|
||||
queryBuilder.limit(2);
|
||||
// Get the first three entries.
|
||||
queryBuilder.limit(3);
|
||||
|
||||
const results = await queryBuilder.getMany();
|
||||
|
||||
switch (results.length) {
|
||||
case 0:
|
||||
// No result found.
|
||||
return;
|
||||
case 1:
|
||||
// Return the only IPLD block entry found.
|
||||
return results[0];
|
||||
case 2:
|
||||
// If there are two entries in the result and both are at the same block number, give preference to checkpoint kind.
|
||||
if (results[0].block.blockNumber === results[1].block.blockNumber) {
|
||||
return (results[1].kind === StateKind.Checkpoint) ? results[1] : results[0];
|
||||
if (results.length) {
|
||||
// Sort by (block number desc, id desc) to get the latest entry.
|
||||
// At same height, IPLD blocks are expected in order ['init', 'diff', 'checkpoint'],
|
||||
// and are given preference in order ['checkpoint', 'diff', 'init']
|
||||
results.sort((result1, result2) => {
|
||||
if (result1.block.blockNumber === result2.block.blockNumber) {
|
||||
return (result1.id > result2.id) ? -1 : 1;
|
||||
} else {
|
||||
return results[0];
|
||||
return (result1.block.blockNumber > result2.block.blockNumber) ? -1 : 1;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unexpected results length ${results.length}`);
|
||||
});
|
||||
|
||||
return results[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user