Skip to content

Commit

Permalink
feat: add evm_address to validator story
Browse files Browse the repository at this point in the history
  • Loading branch information
fibonacci998 committed Sep 13, 2024
1 parent 3c44725 commit 0d8687f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 15 deletions.
3 changes: 2 additions & 1 deletion ci/config.json.ci
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"crawlValidator": {
"millisecondCrawl": null,
"queryPageLimit": 100,
"patternCrawl": "30 */2 * * * *"
"patternCrawl": "30 */2 * * * *",
"chunkSizeInsert": 500
},
"cw721": {
"key": "cw721",
Expand Down
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"crawlValidator": {
"millisecondCrawl": null,
"queryPageLimit": 100,
"patternCrawl": "30 */2 * * * *"
"patternCrawl": "30 */2 * * * *",
"chunkSizeInsert": 500
},
"cw721": {
"key": "cw721",
Expand Down
13 changes: 13 additions & 0 deletions migrations/20240913070037_add_evm_address_to_validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('validator', (table) => {
table.string('evm_address').index();
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('validator', (table) => {
table.dropColumn('evm_address');
});
}
17 changes: 15 additions & 2 deletions network.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"redisDBNumber": 4,
"moleculerNamespace": "namespace-ancient8",
"EVMJSONRPC": ["https://rpc.ancient8.gg"],
"EVMchainId": 888888888
"EVMchainId": 888888888
},
{
"chainId": "ancient-8-testnet",
Expand All @@ -88,7 +88,7 @@
"redisDBNumber": 4,
"moleculerNamespace": "namespace-auratestnet",
"EVMJSONRPC": ["https://rpcv2-testnet.ancient8.gg"],
"EVMchainId": 28122024
"EVMchainId": 28122024
},
{
"chainId": "ethereum",
Expand All @@ -103,5 +103,18 @@
"RPC": [],
"LCD": [],
"EVMchainId": 11155111
},
{
"chainId": "1513",
"RPC": [],
"LCD": ["https://story-testnet-lcd.aura.network"],
"databaseName": "local_evm",
"redisDBNumber": 0,
"EVMJSONRPC": [
"https://story-testnet.aura.network",
"https://testnet.storyrpc.io"
],
"EVMchainId": 1513,
"moleculerNamespace": "erascope-dev-story-testnet"
}
]
2 changes: 2 additions & 0 deletions src/models/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export class Validator extends BaseModel {

image_url!: string;

evm_address!: string;

static get tableName() {
return 'validator';
}
Expand Down
41 changes: 30 additions & 11 deletions src/services/evm/story/crawl-validator/crawl_validator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
toBech32,
toHex,
} from '@cosmjs/encoding';
import { Secp256k1 } from '@cosmjs/crypto';
import axios from 'axios';
import { pubkeyToRawAddress } from '@cosmjs/tendermint-rpc';
import { keccak256 } from 'viem';
import { Validator } from '../../../../models/validator';
import BullableService, {
QueueHandler,
Expand Down Expand Up @@ -43,15 +45,20 @@ export default class CrawlValidatorService extends BullableService {
public async handleCrawlAllValidator(_payload: object): Promise<void> {
this.logger.info('Crawl validator info in story protocol');
const updateValidators: Validator[] = await this.getFullInfoValidators();
await Validator.query()
.insert(updateValidators)
.onConflict('operator_address')
.merge()
.returning('id')
.catch((error) => {
this.logger.error('Error insert or update validators');
this.logger.error(error);
});
if (updateValidators.length > 0) {
for (let i = 0; i < updateValidators.length; i += 500) {
const chunk = updateValidators.slice(i, i + 500);
await Validator.query()
.insert(chunk)
.onConflict('operator_address')
.merge()
.returning('id')
.catch((error) => {
this.logger.error('Error insert or update validators');
this.logger.error(error);
});
}
}
}

private async getFullInfoValidators(): Promise<Validator[]> {
Expand Down Expand Up @@ -124,8 +131,14 @@ export default class CrawlValidatorService extends BullableService {
} else {
// mark this offchain validator is mapped with onchain
offchainMapped.set(validator.operator_address, true);

const unCompressPubKey = Secp256k1.uncompressPubkey(
fromBase64(validator.consensus_pubkey.value)
);
const evmAddress = `0x${keccak256(unCompressPubKey.slice(1)).slice(
-40
)}`;
validatorEntity = foundValidator;
validatorEntity.evm_address = evmAddress;
validatorEntity.consensus_pubkey = validator.consensus_pubkey;
validatorEntity.jailed = validator.jailed ?? false;
validatorEntity.status = validator.status;
Expand Down Expand Up @@ -193,15 +206,21 @@ export default class CrawlValidatorService extends BullableService {
);
const consensusPubkey = {
type: validator.consensus_pubkey.type,
key: validator.consensus_pubkey.value,
value: validator.consensus_pubkey.value,
};

const unCompressPubKey = Secp256k1.uncompressPubkey(
fromBase64(consensusPubkey.value)
);
const evmAddress = `0x${keccak256(unCompressPubKey.slice(1)).slice(-40)}`;

const validatorEntity = Validator.fromJson({
operator_address: validator.operator_address,
account_address: accountAddress,
consensus_address: consensusAddress,
consensus_hex_address: consensusHexAddress,
consensus_pubkey: consensusPubkey,
evm_address: evmAddress,
jailed: validator.jailed ?? false,
status: validator.status,
tokens: validator.tokens,
Expand Down

0 comments on commit 0d8687f

Please sign in to comment.