Fix update in sync status (#489)

This commit is contained in:
Nabarun Gogoi 2023-11-24 11:54:45 +05:30 committed by GitHub
parent 6decf61d4c
commit 9ee423f38e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -139,31 +139,50 @@ export class Database {
} }
async updateSyncStatusIndexedBlock (repo: Repository<SyncStatusInterface>, blockHash: string, blockNumber: number, force = false): Promise<SyncStatusInterface> { async updateSyncStatusIndexedBlock (repo: Repository<SyncStatusInterface>, blockHash: string, blockNumber: number, force = false): Promise<SyncStatusInterface> {
const entity = await repo.findOne(); let entity = await repo.findOne();
assert(entity); assert(entity);
if (force || blockNumber >= entity.latestIndexedBlockNumber) { if (force || blockNumber >= entity.latestIndexedBlockNumber) {
entity.latestIndexedBlockHash = blockHash; const updateData = {
entity.latestIndexedBlockNumber = blockNumber; latestIndexedBlockHash: blockHash,
latestIndexedBlockNumber: blockNumber
};
await repo.update(entity.id, updateData);
entity = {
...entity,
...updateData
};
} }
return await repo.save(entity); return entity;
} }
async updateSyncStatusCanonicalBlock (repo: Repository<SyncStatusInterface>, blockHash: string, blockNumber: number, force = false): Promise<SyncStatusInterface> { async updateSyncStatusCanonicalBlock (repo: Repository<SyncStatusInterface>, blockHash: string, blockNumber: number, force = false): Promise<SyncStatusInterface> {
const entity = await repo.findOne(); let entity = await repo.findOne();
assert(entity); assert(entity);
if (force || blockNumber >= entity.latestCanonicalBlockNumber) { if (force || blockNumber >= entity.latestCanonicalBlockNumber) {
entity.latestCanonicalBlockHash = blockHash; const updateData = {
entity.latestCanonicalBlockNumber = blockNumber; latestCanonicalBlockHash: blockHash,
latestCanonicalBlockNumber: blockNumber
};
await repo.update(entity.id, updateData);
entity = {
...entity,
...updateData
};
} }
return await repo.save(entity); return entity;
} }
async updateSyncStatusChainHead (repo: Repository<SyncStatusInterface>, blockHash: string, blockNumber: number, force = false): Promise<SyncStatusInterface> { async updateSyncStatusChainHead (repo: Repository<SyncStatusInterface>, blockHash: string, blockNumber: number, force = false): Promise<SyncStatusInterface> {
let entity = await repo.findOne(); let entity = await repo.findOne();
if (!entity) { if (!entity) {
entity = repo.create({ entity = repo.create({
chainHeadBlockHash: blockHash, chainHeadBlockHash: blockHash,
@ -177,47 +196,81 @@ export class Database {
initialIndexedBlockHash: blockHash, initialIndexedBlockHash: blockHash,
initialIndexedBlockNumber: blockNumber initialIndexedBlockNumber: blockNumber
}); });
return repo.save(entity);
} }
if (force || blockNumber >= entity.chainHeadBlockNumber) { if (force || blockNumber >= entity.chainHeadBlockNumber) {
entity.chainHeadBlockHash = blockHash; const updateData = {
entity.chainHeadBlockNumber = blockNumber; chainHeadBlockHash: blockHash,
chainHeadBlockNumber: blockNumber
};
await repo.update(entity.id, updateData);
entity = {
...entity,
...updateData
};
} }
return await repo.save(entity); return entity;
} }
async updateSyncStatusProcessedBlock (repo: Repository<SyncStatusInterface>, blockHash: string, blockNumber: number, force = false): Promise<SyncStatusInterface> { async updateSyncStatusProcessedBlock (repo: Repository<SyncStatusInterface>, blockHash: string, blockNumber: number, force = false): Promise<SyncStatusInterface> {
const entity = await repo.findOne(); let entity = await repo.findOne();
assert(entity); assert(entity);
if (force || blockNumber >= entity.latestProcessedBlockNumber) { if (force || blockNumber >= entity.latestProcessedBlockNumber) {
entity.latestProcessedBlockHash = blockHash; const updateData = {
entity.latestProcessedBlockNumber = blockNumber; latestProcessedBlockHash: blockHash,
latestProcessedBlockNumber: blockNumber
};
await repo.update(entity.id, updateData);
entity = {
...entity,
...updateData
};
} }
return await repo.save(entity); return entity;
} }
async updateSyncStatusIndexingError (repo: Repository<SyncStatusInterface>, hasIndexingError: boolean): Promise<SyncStatusInterface | undefined> { async updateSyncStatusIndexingError (repo: Repository<SyncStatusInterface>, hasIndexingError: boolean): Promise<SyncStatusInterface | undefined> {
const entity = await repo.findOne(); let entity = await repo.findOne();
if (!entity) { if (!entity) {
return; return;
} }
entity.hasIndexingError = hasIndexingError; const updateData = {
hasIndexingError: hasIndexingError
};
return repo.save(entity); await repo.update(entity.id, updateData);
entity = {
...entity,
...updateData
};
return entity;
} }
async updateSyncStatus (repo: Repository<SyncStatusInterface>, syncStatus: DeepPartial<SyncStatusInterface>): Promise<SyncStatusInterface> { async updateSyncStatus (repo: Repository<SyncStatusInterface>, syncStatus: DeepPartial<SyncStatusInterface>): Promise<SyncStatusInterface> {
const entity = await repo.findOne(); let entity = await repo.findOne();
assert(entity);
return await repo.save({ await repo.update(entity.id, syncStatus);
entity = {
...entity, ...entity,
...syncStatus ...syncStatus
}); };
return entity;
} }
async getBlockProgress (repo: Repository<BlockProgressInterface>, blockHash: string): Promise<BlockProgressInterface | undefined> { async getBlockProgress (repo: Repository<BlockProgressInterface>, blockHash: string): Promise<BlockProgressInterface | undefined> {