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:
prathamesh0 2022-09-16 09:23:41 +05:30 committed by GitHub
parent c2bbaa6867
commit b2cf997900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 18 deletions

View File

@ -63,6 +63,9 @@ export const fillState = async (
const blockHash = blocks[0].blockHash; const blockHash = blocks[0].blockHash;
// Create initial state for contracts
await indexer.createInit(blockHash, blockNumber);
// Fill state for each contract in contractEntitiesMap // Fill state for each contract in contractEntitiesMap
const contractStatePromises = Array.from(contractEntitiesMap.entries()) const contractStatePromises = Array.from(contractEntitiesMap.entries())
.map(async ([contractAddress, entities]): Promise<void> => { .map(async ([contractAddress, entities]): Promise<void> => {
@ -90,7 +93,12 @@ export const fillState = async (
// Persist subgraph state to the DB // Persist subgraph state to the DB
await indexer.dumpSubgraphState(blockHash, true); 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}]`); log(`Filled state for subgraph entities in range: [${startBlock}, ${endBlock}]`);
}; };

View File

@ -419,7 +419,7 @@ export class Indexer implements IPLDIndexerInterface {
return this._baseIndexer.createStateCheckpoint(contractAddress, block, data); 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> { async createCheckpoint (contractAddress: string, blockHash: string): Promise<string | undefined> {
const block = await this.getBlockProgress(blockHash); const block = await this.getBlockProgress(blockHash);
assert(block); assert(block);
@ -427,6 +427,12 @@ export class Indexer implements IPLDIndexerInterface {
return this._baseIndexer.createCheckpoint(this, contractAddress, block); 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> { async saveOrUpdateIPLDBlock (ipldBlock: IPLDBlock): Promise<IPLDBlock> {
return this._baseIndexer.saveOrUpdateIPLDBlock(ipldBlock); return this._baseIndexer.saveOrUpdateIPLDBlock(ipldBlock);
} }

View File

@ -67,6 +67,9 @@ export const fillState = async (
const blockHash = blocks[0].blockHash; const blockHash = blocks[0].blockHash;
// Create initial state for contracts
await indexer.createInit(blockHash, blockNumber);
// Fill state for each contract in contractEntitiesMap // Fill state for each contract in contractEntitiesMap
const contractStatePromises = Array.from(contractEntitiesMap.entries()) const contractStatePromises = Array.from(contractEntitiesMap.entries())
.map(async ([contractAddress, entities]): Promise<void> => { .map(async ([contractAddress, entities]): Promise<void> => {
@ -95,10 +98,15 @@ export const fillState = async (
// Persist subgraph state to the DB // Persist subgraph state to the DB
await indexer.dumpSubgraphState(blockHash, true); await indexer.dumpSubgraphState(blockHash, true);
// Create checkpoints
await indexer.processCheckpoint(blockHash);
console.timeEnd(`time:fill-state-${blockNumber}`); console.timeEnd(`time:fill-state-${blockNumber}`);
} }
console.timeEnd('time:fill-state'); console.timeEnd('time:fill-state');
// TODO: Push state to IPFS
log(`Filled state for subgraph entities in range: [${startBlock}, ${endBlock}]`); log(`Filled state for subgraph entities in range: [${startBlock}, ${endBlock}]`);
}; };

View File

@ -349,7 +349,7 @@ export class Indexer implements IPLDIndexerInterface {
return this._baseIndexer.createStateCheckpoint(contractAddress, block, data); 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> { async createCheckpoint (contractAddress: string, blockHash: string): Promise<string | undefined> {
const block = await this.getBlockProgress(blockHash); const block = await this.getBlockProgress(blockHash);
assert(block); assert(block);
@ -357,6 +357,12 @@ export class Indexer implements IPLDIndexerInterface {
return this._baseIndexer.createCheckpoint(this, contractAddress, block); 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> { async saveOrUpdateIPLDBlock (ipldBlock: IPLDBlock): Promise<IPLDBlock> {
return this._baseIndexer.saveOrUpdateIPLDBlock(ipldBlock); return this._baseIndexer.saveOrUpdateIPLDBlock(ipldBlock);
} }

View File

@ -27,27 +27,24 @@ export class IPLDDatabase extends Database {
? queryBuilder.andWhere('ipld_block.kind = :kind', { kind }) ? queryBuilder.andWhere('ipld_block.kind = :kind', { kind })
: queryBuilder.andWhere('ipld_block.kind != :kind', { kind: StateKind.DiffStaged }); : queryBuilder.andWhere('ipld_block.kind != :kind', { kind: StateKind.DiffStaged });
// Get the first two entries. // Get the first three entries.
queryBuilder.limit(2); queryBuilder.limit(3);
const results = await queryBuilder.getMany(); const results = await queryBuilder.getMany();
switch (results.length) { if (results.length) {
case 0: // Sort by (block number desc, id desc) to get the latest entry.
// No result found. // At same height, IPLD blocks are expected in order ['init', 'diff', 'checkpoint'],
return; // and are given preference in order ['checkpoint', 'diff', 'init']
case 1: results.sort((result1, result2) => {
// Return the only IPLD block entry found. if (result1.block.blockNumber === result2.block.blockNumber) {
return results[0]; return (result1.id > result2.id) ? -1 : 1;
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];
} else { } 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];
} }
} }