Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update validator as UNRECOGNIZED when not found onchain (develop) #290

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/models/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export class Validator extends BaseModel {
return {
BONDED: 'BOND_STATUS_BONDED',
UNBONDED: 'BOND_STATUS_UNBONDED',
UNSPECIFIED: 'BOND_STATUS_UNSPECIFIED',
UNBONDING: 'BOND_STATUS_UNBONDING',
UNRECOGNIZED: 'UNRECOGNIZED',
};
}

Expand Down
30 changes: 27 additions & 3 deletions src/services/crawl-validator/crawl_validator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export default class CrawlValidatorService extends BullableService {
.select('value')
.limit(1)
.offset(0);

await knex.transaction(async (trx) => {
if (resultTx.length > 0) {
await this.updateValidators(trx);
Expand Down Expand Up @@ -110,8 +109,10 @@ export default class CrawlValidatorService extends BullableService {
}
}

const validatorInDB: Validator[] = await knex('validator').select('*');

const validatorInDB: Validator[] = await knex('validator')
.select('*')
.whereNot('status', Validator.STATUS.UNRECOGNIZED);
const offchainMapped: Map<string, boolean> = new Map();
await Promise.all(
validators.map(async (validator) => {
const foundValidator = validatorInDB.find(
Expand All @@ -123,6 +124,9 @@ export default class CrawlValidatorService extends BullableService {
if (!foundValidator) {
validatorEntity = Validator.createNewValidator(validator);
} else {
// mark this offchain validator is mapped with onchain
offchainMapped.set(validator.operator_address, true);

validatorEntity = foundValidator;
validatorEntity.jailed = validator.jailed;
validatorEntity.status = validator.status;
Expand All @@ -146,6 +150,26 @@ export default class CrawlValidatorService extends BullableService {

updateValidators = await this.loadCustomInfo(updateValidators);

// loop all validator not found onchain, update status is UNRECOGNIZED
validatorInDB
.filter((val: any) => !offchainMapped.get(val.operator_address))
.forEach(async (validatorNotOnchain: any) => {
this.logger.debug(
'Account not found onchain: ',
validatorNotOnchain.operator_address
);
validatorNotOnchain.status = Validator.STATUS.UNRECOGNIZED;
validatorNotOnchain.tokens = 0;
validatorNotOnchain.delegator_shares = 0;
validatorNotOnchain.percent_voting_power = 0;

validatorNotOnchain.jailed_until =
validatorNotOnchain.jailed_until.toISOString();
validatorNotOnchain.unbonding_time =
validatorNotOnchain.unbonding_time.toISOString();
updateValidators.push(validatorNotOnchain);
});

await Validator.query()
.insert(updateValidators)
.onConflict('operator_address')
Expand Down
47 changes: 44 additions & 3 deletions test/unit/services/crawl-validator/crawl_validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AfterAll, BeforeAll, Describe, Test } from '@jest-decorated/core';
import { AfterEach, BeforeEach, Describe, Test } from '@jest-decorated/core';
import { ServiceBroker } from 'moleculer';
import { BULL_JOB_NAME } from '../../../../src/common';
import {
Expand Down Expand Up @@ -75,7 +75,7 @@ export default class CrawlValidatorTest {

crawlSigningInfoService?: CrawlSigningInfoService;

@BeforeAll()
@BeforeEach()
async initSuite() {
await this.broker.start();
this.crawlSigningInfoService = this.broker.createService(
Expand All @@ -97,7 +97,7 @@ export default class CrawlValidatorTest {
await BlockCheckpoint.query().insert(this.blockCheckpoint);
}

@AfterAll()
@AfterEach()
async tearDown() {
await Promise.all([
Validator.query().delete(true),
Expand Down Expand Up @@ -128,4 +128,45 @@ export default class CrawlValidatorTest {
)?.consensus_address
).toEqual('auravalcons1rvq6km74pua3pt9g7u5svm4r6mrw8z08walfep');
}

@Test('Set validator not found onchain is UNRECOGNIZED')
public async testCrawlValidatorNotFoundOnchain() {
await Validator.query().insert(
Validator.fromJson({
operator_address: 'xxx',
account_address: 'xxx',
consensus_address: 'xxx',
consensus_hex_address: 'xxx',
consensus_pubkey: {},
jailed: false,
status: Validator.STATUS.UNBONDED,
tokens: 100,
delegator_shares: 100,
description: {},
unbonding_height: 0,
unbonding_time: '1970-01-01 00:00:00+00',
commission: {},
min_self_delegation: 0,
uptime: 0,
self_delegation_balance: 0,
percent_voting_power: 100,
start_height: 0,
index_offset: 0,
jailed_until: '1970-01-01 00:00:00+00',
tombstoned: false,
missed_blocks_counter: 0,
delegators_count: 0,
delegators_last_height: 0,
image_url: 'xxx',
})
);

await this.crawlValidatorService?.handleCrawlAllValidator({});

const validator = await Validator.query().findOne({
operator_address: 'xxx',
});
expect(validator?.status).toEqual(Validator.STATUS.UNRECOGNIZED);
expect(validator?.tokens).toEqual('0');
}
}
Loading