Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
🐛 Index stakes from PoS genesis assets
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersubudhi committed Oct 29, 2023
1 parent f11f076 commit f30c63e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,16 @@ const getStakers = async params => {
}

// Fetch list of stakes
const stakes = await stakesTable.find(
{
...stakerAddressQueryFilter,
validatorAddress: params.validatorAddress,
limit: params.limit,
offset: params.offset,
sort: 'amount:desc',
order: 'stakerAddress:asc', // Amount sorting tie-breaker
},
['stakerAddress', 'amount'],
);
const stakesQueryParams = Object.freeze({
...stakerAddressQueryFilter,
validatorAddress: params.validatorAddress,
propBetweens: [{ property: 'amount', greaterThan: BigInt('0') }],
limit: params.limit,
offset: params.offset,
sort: 'amount:desc',
order: 'stakerAddress:asc', // Amount sorting tie-breaker
});
const stakes = await stakesTable.find(stakesQueryParams, ['stakerAddress', 'amount']);

// Populate stakers name and prepare response
stakersResponse.data.stakers = await BluebirdPromise.map(
Expand Down Expand Up @@ -164,10 +163,7 @@ const getStakers = async params => {

stakersResponse.meta.count = stakersResponse.data.stakers.length;
stakersResponse.meta.offset = params.offset;
stakersResponse.meta.total = await stakesTable.count({
...stakerAddressQueryFilter,
validatorAddress: params.validatorAddress,
});
stakersResponse.meta.total = await stakesTable.count(stakesQueryParams);

return stakersResponse;
};
Expand Down
31 changes: 24 additions & 7 deletions services/blockchain-indexer/shared/indexer/genesisBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ const { updateTotalLockedAmounts } = require('./utils/blockchainIndex');

const requestAll = require('../utils/requestAll');
const config = require('../../config');
const stakesTableSchema = require('../database/schema/stakes');
const commissionsTableSchema = require('../database/schema/commissions');
const { getIndexStats } = require('./indexStatus');

const logger = Logger();

const MYSQL_ENDPOINT = config.endpoints.mysql;

const getStakesTable = () => getTableInstance(stakesTableSchema, MYSQL_ENDPOINT);
const getCommissionsTable = () => getTableInstance(commissionsTableSchema, MYSQL_ENDPOINT);

const allAccountsAddresses = [];
Expand Down Expand Up @@ -101,10 +103,12 @@ const indexPosValidatorsInfo = async (numValidators, dbTrx) => {
};

const indexPosStakesInfo = async (numStakers, dbTrx) => {
let totalStakeChange = BigInt(0);
let totalSelfStakeChange = BigInt(0);
let totalStake = BigInt(0);
let totalSelfStake = BigInt(0);

if (numStakers > 0) {
const stakesTable = await getStakesTable();

const posModuleData = await requestAll(
requestConnector,
'getGenesisAssetByModule',
Expand All @@ -113,21 +117,34 @@ const indexPosStakesInfo = async (numStakers, dbTrx) => {
);
const stakers = posModuleData[MODULE_SUB_STORE.POS.STAKERS];

const allStakes = [];
stakers.forEach(staker => {
const { address: stakerAddress, stakes } = staker;

stakes.forEach(stake => {
const { validatorAddress, amount } = stake;
totalStakeChange += BigInt(amount);

allStakes.push({
stakerAddress,
validatorAddress,
amount: BigInt(amount),
});

totalStake += BigInt(amount);
if (stakerAddress === validatorAddress) {
totalSelfStakeChange += BigInt(amount);
totalSelfStake += BigInt(amount);
}
});
});

await stakesTable.upsert(allStakes, dbTrx);
logger.info(`Updated ${allStakes.length} stakes from the genesis block.`);
}

await updateTotalStake(totalStakeChange, dbTrx);
await updateTotalSelfStake(totalSelfStakeChange, dbTrx);
await updateTotalStake(totalStake, dbTrx);
logger.info(`Updated total stakes at genesis: ${totalStake.toString()}.`);

await updateTotalSelfStake(totalSelfStake, dbTrx);
logger.info(`Updated total self-stakes information at genesis: ${totalSelfStake.toString()}.`);
};

const indexPosModuleAssets = async dbTrx => {
Expand Down
6 changes: 0 additions & 6 deletions services/blockchain-indexer/shared/indexer/indexStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ const {
Signals,
} = require('lisk-service-framework');

const { indexValidatorCommissionInfo, indexStakersInfo } = require('./validatorIndex');

const { getCurrentHeight, getGenesisHeight } = require('../constants');

const logger = Logger();
Expand Down Expand Up @@ -115,10 +113,6 @@ const init = async () => {

// Register event listeners
Signals.get('newBlock').add(checkIndexReadiness);

// Index stakers and commission information available in genesis block
await indexValidatorCommissionInfo();
await indexStakersInfo();
};

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const getStakeIndexingInfo = async tx => {

stakeEntry.stakerAddress = getLisk32AddressFromPublicKey(tx.senderPublicKey);
stakeEntry.validatorAddress = stake.validatorAddress;
stakeEntry.amount = stake.amount;
stakeEntry.amount = BigInt(stake.amount);
return stakeEntry;
},
{ concurrency: tx.params.stakes.length },
Expand Down Expand Up @@ -158,6 +158,7 @@ const revertTransaction = async (blockHeader, tx, events, dbTrx) => {
stakes,
async stake => {
await decrementStakeTrx(stake, dbTrx);

// Subtract to reverse the impact
if (stake.stakerAddress === stake.validatorAddress) {
totalSelfStakeChange -= BigInt(stake.amount);
Expand Down
68 changes: 0 additions & 68 deletions services/blockchain-indexer/shared/indexer/validatorIndex.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ const mockEventTopicsQueryParams = {
'03',
'c8ee3933cc841287d834f74f278ac12f145f320d0593a612e11b67b4a58cd17b',
'lskw68y3kyus7ota9mykr726aby44mw574m8dkngu',
'04c8ee3933cc841287d834f74f278ac12f145f320d0593a612e11b67b4a58cd17b',
'05c8ee3933cc841287d834f74f278ac12f145f320d0593a612e11b67b4a58cd17b',
],
},
groupBy: 'eventID',
Expand Down

0 comments on commit f30c63e

Please sign in to comment.