Skip to content

Commit d3aca6c

Browse files
committed
update get_voters.ts api
1 parent b73f7f6 commit d3aca6c

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

src/api/routes/v2-state/get_voters/get_voters.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
2-
import { timedQuery } from "../../../helpers/functions.js";
3-
import { getSkipLimit } from "../../v2-history/get_actions/functions.js";
1+
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
2+
import {timedQuery} from "../../../helpers/functions.js";
3+
import {getSkipLimit} from "../../v2-history/get_actions/functions.js";
44

5-
import { estypes } from "@elastic/elasticsearch";
6-
import { IVoter } from "../../../../interfaces/table-voter.js";
5+
import {estypes} from "@elastic/elasticsearch";
6+
import {IVoter} from "../../../../interfaces/table-voter.js";
77

88
async function getVoters(fastify: FastifyInstance, request: FastifyRequest) {
99
const query: any = request.query;
10-
const { skip, limit } = getSkipLimit(request.query);
10+
const {skip, limit} = getSkipLimit(request.query);
1111
const maxDocs = fastify.manager.config.api.limits.get_voters ?? 100;
1212

1313
const response: any = {
@@ -17,46 +17,58 @@ async function getVoters(fastify: FastifyInstance, request: FastifyRequest) {
1717

1818
let stateResult: IVoter[];
1919

20-
if (fastify.manager.config.indexer.experimental_mongodb_state && fastify.manager.conn.mongodb && query.useMongo === 'true') {
20+
if (fastify.manager.config.indexer.experimental_mongodb_state && fastify.manager.conn.mongodb && query.useMongo === 'true') {
2121
const dbName = `${fastify.manager.conn.mongodb.database_prefix}_${fastify.manager.chain}`;
2222
const collection = fastify.mongo.client.db(dbName).collection<IVoter>('voters');
2323

2424
const mongoQuery: any = {};
2525
if (query.producer) {
26-
mongoQuery.producers = { $all: query.producer.split(",") };
26+
mongoQuery.producers = {$all: query.producer.split(",")};
2727
}
28-
if (query.proxy === 'true') {
28+
29+
if (query.is_proxy === 'true') {
2930
mongoQuery.is_proxy = true;
3031
}
3132

3233
response.voter_count = await collection.countDocuments(mongoQuery);
3334

3435
stateResult = await collection
35-
.find(mongoQuery, { projection: { _id: 0, voter: 1, last_vote_weight: 1, block_num: 1 } })
36+
.find(mongoQuery, {
37+
projection: {
38+
_id: 0,
39+
voter: 1,
40+
last_vote_weight: 1,
41+
is_proxy: 1,
42+
block_num: 1,
43+
staked: 1,
44+
producers: query.listProducers === 'true' ? 1 : undefined
45+
}
46+
})
3647
.skip(skip || 0)
3748
.limit(limit || 50)
3849
.toArray();
3950

4051
} else {
41-
let queryStruct: any = { bool: { must: [] } };
52+
53+
let queryStruct: any = {bool: {must: []}};
4254
if (query.producer) {
4355
for (const bp of query.producer.split(",")) {
44-
queryStruct.bool.must.push({ term: { producers: bp } });
56+
queryStruct.bool.must.push({term: {producers: bp}});
4557
}
4658
}
4759
if (query.proxy === 'true') {
48-
queryStruct.bool.must.push({ term: { is_proxy: true } });
60+
queryStruct.bool.must.push({term: {is_proxy: true}});
4961
}
5062
if (queryStruct.bool.must.length === 0) {
51-
queryStruct = { match_all: {} };
63+
queryStruct = {match_all: {}};
5264
}
5365

5466
const esResult = await fastify.elastic.search<any>({
5567
index: `${fastify.manager.chain}-table-voters-*`,
5668
from: skip || 0,
5769
size: (limit > maxDocs ? maxDocs : limit) || 10,
5870
query: queryStruct,
59-
sort: [{ last_vote_weight: "desc" }]
71+
sort: [{last_vote_weight: "desc"}]
6072
});
6173
stateResult = esResult.hits.hits.map((hit: any) => hit._source);
6274
response.voter_count = (esResult.hits.total as estypes.SearchTotalHits).value;
@@ -66,7 +78,10 @@ async function getVoters(fastify: FastifyInstance, request: FastifyRequest) {
6678
response.voters.push({
6779
account: voter.voter,
6880
weight: voter.last_vote_weight,
69-
last_vote: voter.block_num
81+
staked: voter.staked,
82+
is_proxy: voter.is_proxy,
83+
last_vote_block: voter.block_num,
84+
producers: voter.producers || undefined
7085
});
7186
}
7287

src/api/routes/v2-state/get_voters/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ export default function (fastify: FastifyInstance, opts: any, next) {
2424
type: "object",
2525
properties: {
2626
account: {type: "string"},
27+
is_proxy: {type: "boolean"},
2728
weight: {type: "number"},
28-
last_vote: {type: "number"},
29-
data: {
30-
additionalProperties: true
29+
staked: {type: "number"},
30+
last_vote_block: {type: "number"},
31+
producers: {
32+
type: "array",
33+
items: {
34+
type: "string"
35+
}
3136
}
3237
}
3338
}

src/indexer/modules/master.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ import {getTotalValue} from "../../api/helpers/functions.js";
4949
import {ShipServer, StateHistorySocket} from "../connections/state-history.js";
5050
import {updateByBlock} from "../definitions/updateByBlock.painless.js";
5151
import {IAccount} from "../../interfaces/table-account.js";
52-
import Timeout = NodeJS.Timeout;
5352
import {IProposal} from "../../interfaces/table-proposal.js";
53+
import {IVoter} from "../../interfaces/table-voter.js";
54+
import Timeout = NodeJS.Timeout;
5455

5556
interface RevBlock {
5657
num: number;
@@ -2442,7 +2443,17 @@ export class HyperionMaster {
24422443
{key: {"provided_approvals.actor": 1}},
24432444
{key: {"requested_approvals.actor": 1}}
24442445
]);
2445-
2446+
// voters table indices
2447+
const voters = db.collection<IVoter>("voters");
2448+
await voters.createIndexes([
2449+
{key: {voter: 1}},
2450+
{key: {block_num: 1}},
2451+
{key: {staked: 1}},
2452+
{key: {last_vote_weight: 1}},
2453+
{key: {proxied_vote_weight: 1}},
2454+
{key: {producers: 1}},
2455+
{key: {is_proxy: 1}}
2456+
]);
24462457
}
24472458
}
24482459
} catch (e: any) {

0 commit comments

Comments
 (0)