Skip to content

Commit 25467ac

Browse files
authored
Refactored supplier-related handlers and logic (#65)
## Summary Recreated index supplier logic to handle the messages and events that updates suppliers in parallel: - Removed unused supplier handlers and related code (e.g., `_handleOldEventSupplierSlashed`). - Consolidated supplier processing into `indexSupplier`. - Exported internal utility methods (`parseAttribute`, `getAttributes`, etc.) for broader use. - Deprecated event/message handling for suppliers now processed via `indexSupplier`. - Enhanced modularity for supplier-related functions. ## Issue Our previous logic was indexing every supplier update sequentially, one by one. This caused node runners updating their suppliers' stakes (i.e., updating their services) to take an extremely long time, causing the indexer to get stuck on that block and fall out of sync. ## Type of change Select one or more: - [x] New feature, functionality or library - [ ] Bug fix - [ ] Code health or cleanup - [ ] Documentation - [ ] Other (specify) ## Sanity Checklist - [x] I have tested my changes using the available tooling - [x] I have commented my code - [x] I have performed a self-review of my own code; both comments & source code - [ ] I create and reference any new tickets, if applicable - [ ] I have left TODOs throughout the codebase, if applicable
1 parent a10c265 commit 25467ac

File tree

5 files changed

+957
-538
lines changed

5 files changed

+957
-538
lines changed

src/mappings/handlers.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,10 @@ import {
3131
handleEventClaimUpdated,
3232
handleEventProofUpdated,
3333
handleEventProofValidityChecked,
34-
handleEventSupplierSlashed,
3534
handleMsgCreateClaim,
3635
handleMsgSubmitProof,
3736
} from "./pocket/relays";
3837
import { handleEventRelayMiningDifficultyUpdated, handleMsgAddService } from "./pocket/services";
39-
import {
40-
handleEventSupplierServiceConfigActivated,
41-
handleMsgClaimMorseSupplier,
42-
handleSupplierStakeMsg,
43-
handleSupplierUnbondingBeginEvent,
44-
handleSupplierUnbondingEndEvent,
45-
handleUnstakeSupplierMsg,
46-
} from "./pocket/suppliers";
4738
import {
4839
handleValidatorCommission,
4940
handleValidatorMsgCreate,
@@ -68,7 +59,6 @@ export const MsgHandlers: Record<string, (messages: Array<CosmosMessage>) => Pro
6859
// this is currently being handle inside Authz handler
6960
"/pocket.migration.MsgRecoverMorseAccount": noOp,
7061
"/pocket.migration.MsgClaimMorseApplication": handleMsgClaimMorseApplication,
71-
"/pocket.migration.MsgClaimMorseSupplier": handleMsgClaimMorseSupplier,
7262
// bank
7363
"/cosmos.bank.v1beta1.MsgSend": handleNativeTransfer,
7464
// validator
@@ -83,9 +73,10 @@ export const MsgHandlers: Record<string, (messages: Array<CosmosMessage>) => Pro
8373
"/pocket.application.MsgTransferApplication": handleTransferApplicationMsg,
8474
// service
8575
"/pocket.service.MsgAddService": handleMsgAddService,
86-
// supplier
87-
"/pocket.supplier.MsgStakeSupplier": handleSupplierStakeMsg,
88-
"/pocket.supplier.MsgUnstakeSupplier": handleUnstakeSupplierMsg,
76+
// supplier - handled by batch processing in indexSupplier (called from indexStake)
77+
"/pocket.supplier.MsgStakeSupplier": noOp,// - now handled in indexSupplier
78+
"/pocket.supplier.MsgUnstakeSupplier": noOp,// - now handled in indexSupplier
79+
"/pocket.migration.MsgClaimMorseSupplier": noOp,// - now handled in indexSupplier
8980
// gateway
9081
"/pocket.gateway.MsgStakeGateway": handleGatewayMsgStake,
9182
"/pocket.gateway.MsgUnstakeGateway": handleGatewayMsgUnstake,
@@ -117,10 +108,10 @@ export const EventHandlers: Record<string, (events: Array<CosmosEvent>) => Promi
117108
"pocket.application.EventTransferError": handleTransferApplicationErrorEvent,
118109
"pocket.application.EventApplicationUnbondingBegin": handleApplicationUnbondingBeginEvent,
119110
"pocket.application.EventApplicationUnbondingEnd": handleApplicationUnbondingEndEvent,
120-
// supplier
121-
"pocket.supplier.EventSupplierServiceConfigActivated": handleEventSupplierServiceConfigActivated,
122-
"pocket.supplier.EventSupplierUnbondingBegin": handleSupplierUnbondingBeginEvent,
123-
"pocket.supplier.EventSupplierUnbondingEnd": handleSupplierUnbondingEndEvent,
111+
// supplier - handled by batch processing in indexSupplier (called from indexStake)
112+
"pocket.supplier.EventSupplierServiceConfigActivated": noOp, // - now handled in indexSupplier
113+
"pocket.supplier.EventSupplierUnbondingBegin": noOp, // - now handled in indexSupplier
114+
"pocket.supplier.EventSupplierUnbondingEnd": noOp, // - now handled in indexSupplier
124115
// service
125116
"pocket.service.EventRelayMiningDifficultyUpdated": handleEventRelayMiningDifficultyUpdated,
126117
// gateway
@@ -130,7 +121,7 @@ export const EventHandlers: Record<string, (events: Array<CosmosEvent>) => Promi
130121
// relay
131122
"pocket.tokenomics.EventClaimSettled": handleEventClaimSettled,
132123
"pocket.tokenomics.EventClaimExpired": handleEventClaimExpired,
133-
"pocket.tokenomics.EventSupplierSlashed": handleEventSupplierSlashed,
124+
"pocket.tokenomics.EventSupplierSlashed": noOp, // - now handled in indexSupplier
134125
"pocket.tokenomics.EventApplicationOverserviced": handleEventApplicationOverserviced,
135126
"pocket.tokenomics.EventApplicationReimbursementRequest": handleEventApplicationReimbursementRequest,
136127
"pocket.proof.EventClaimUpdated": handleEventClaimUpdated,

src/mappings/indexer.manager.ts

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
MsgHandlers,
2626
} from "./handlers";
2727
import { handleAddBlockReports } from "./pocket/reports";
28+
import { indexSupplier } from "./pocket/suppliers";
2829
import {
2930
handleBlock,
3031
handleGenesis,
@@ -501,80 +502,6 @@ async function indexStakeEntity(allData: Array<CosmosEvent | CosmosMessage>, get
501502
)
502503
}
503504

504-
// any supplier msg or event
505-
async function indexSupplier(msgByType: MessageByType, eventByType: EventByType): Promise<void> {
506-
const msgTypes = [
507-
"/pocket.supplier.MsgUnstakeSupplier",
508-
"/pocket.migration.MsgClaimMorseSupplier",
509-
"/pocket.supplier.MsgStakeSupplier",
510-
];
511-
const eventTypes = [
512-
"pocket.supplier.EventSupplierUnbondingBegin",
513-
"pocket.supplier.EventSupplierUnbondingEnd",
514-
"pocket.supplier.EventSupplierServiceConfigActivated",
515-
// this is here because it modifies the staked tokens of the supplier
516-
"pocket.tokenomics.EventSupplierSlashed"
517-
];
518-
519-
520-
const eventGetId = (attributes: CosmosEvent["event"]["attributes"]) => {
521-
for (const attribute of attributes) {
522-
if (attribute.key === "supplier") {
523-
return JSON.parse(attribute.value as string).operator_address
524-
}
525-
526-
if (attribute.key === "operator_address") {
527-
return attribute.value as string
528-
}
529-
}
530-
531-
return null
532-
}
533-
534-
await indexStakeEntity(
535-
[
536-
...msgTypes.map(type => msgByType[type]).flat(),
537-
...eventTypes.map(type => eventByType[type]).flat()
538-
],
539-
{
540-
"/pocket.supplier.MsgUnstakeSupplier": "operatorAddress",
541-
"/pocket.supplier.MsgStakeSupplier": "operatorAddress",
542-
"/pocket.migration.MsgClaimMorseSupplier": "shannonOperatorAddress",
543-
"pocket.supplier.EventSupplierUnbondingBegin": eventGetId,
544-
"pocket.supplier.EventSupplierUnbondingEnd": eventGetId,
545-
"pocket.supplier.EventSupplierServiceConfigActivated": eventGetId,
546-
"pocket.tokenomics.EventSupplierSlashed": (attributes) => {
547-
/*
548-
[
549-
{"key":"application_address","value":"\"pokt16wwc45wjc4ulne7wmaawxhju00vwf900lscfld\""},
550-
{"key":"claim_proof_status_int","value":"2"},
551-
{"key":"proof_missing_penalty","value":"\"1upokt\""},
552-
{"key":"service_id","value":"\"hey\""},
553-
{"key":"session_end_block_height","value":"\"363540\""},
554-
{"key":"supplier_operator_address","value":"\"pokt1wua234ulad3vkcsqmasu845mn4ugu9aa6jcv23\""},
555-
{"key":"mode","value":"EndBlock"}
556-
]
557-
*/
558-
for (const attribute of attributes) {
559-
// in the previous version of this event this is the key to get the supplierId
560-
if (attribute.key === "supplier_operator_addr" || attribute.key === "supplier_operator_address") {
561-
return attribute.value as string
562-
}
563-
564-
if (attribute.key === "claim") {
565-
return JSON.parse(attribute.value as string).supplier_operator_address
566-
}
567-
568-
if (attribute.key === "supplier_operator_address") {
569-
return attribute.value as string
570-
}
571-
}
572-
573-
return null
574-
}
575-
})
576-
}
577-
578505
// any message or event related to stake (supplier, gateway, application, service)
579506
async function indexStake(msgByType: MessageByType, eventByType: EventByType): Promise<void> {
580507
await Promise.all([

src/mappings/pocket/relays.ts

Lines changed: 2 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import {
5757
import { getDenomAndAmount } from "../utils/primitives";
5858

5959
// this can return undefined because older events do not have this attribute
60-
function getClaimProofStatusFromSDK(item: typeof ClaimProofStatusSDKType | string | number): ClaimProofStatus | undefined {
60+
export function getClaimProofStatusFromSDK(item: typeof ClaimProofStatusSDKType | string | number): ClaimProofStatus | undefined {
6161
if (!item) return undefined;
6262

6363
switch (item) {
@@ -151,7 +151,7 @@ function parseAttribute(attribute: unknown = ""): string {
151151
}
152152

153153
// eslint-disable-next-line complexity
154-
function getAttributes(attributes: CosmosEvent["event"]["attributes"]) {
154+
export function getAttributes(attributes: CosmosEvent["event"]["attributes"]) {
155155

156156
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
157157
// @ts-ignore
@@ -890,121 +890,6 @@ function _handleEventApplicationReimbursementRequest(event: CosmosEvent): EventA
890890
}
891891
}
892892

893-
async function _handleOldEventSupplierSlashed(event: CosmosEvent) {
894-
let slashingCoin: CoinSDKType | null = null, operatorAddress = "";
895-
896-
for (const attribute of event.event.attributes) {
897-
if (attribute.key === "slashing_amount") {
898-
slashingCoin = getDenomAndAmount(attribute.value as string);
899-
if (!slashingCoin) {
900-
throw new Error(`[handleEventSupplierSlashed] event attribute key=${attribute.key} value=${attribute.value} is not a valid coin`);
901-
}
902-
}
903-
904-
if (attribute.key === "proof_missing_penalty") {
905-
/*
906-
[
907-
{"key":"application_address","value":"\"pokt16wwc45wjc4ulne7wmaawxhju00vwf900lscfld\""},
908-
{"key":"claim_proof_status_int","value":"2"},
909-
{"key":"proof_missing_penalty","value":"\"1upokt\""},
910-
{"key":"service_id","value":"\"hey\""},
911-
{"key":"session_end_block_height","value":"\"363540\""},
912-
{"key":"supplier_operator_address","value":"\"pokt1wua234ulad3vkcsqmasu845mn4ugu9aa6jcv23\""},
913-
{"key":"mode","value":"EndBlock"}
914-
]
915-
*/
916-
const coins = parseCoins(parseAttribute(attribute.value));
917-
if (!coins.length) {
918-
throw new Error(`[handleEventSupplierSlashed] event attribute key=${attribute.key} value=${attribute.value} is not a valid coin`);
919-
}
920-
}
921-
922-
if (attribute.key === "supplier_operator_addr" || attribute.key === "supplier_operator_address") {
923-
operatorAddress = parseAttribute(attribute.value);
924-
}
925-
}
926-
927-
if (!slashingCoin) {
928-
throw new Error(`[handleEventSupplierSlashed] slashingCoin not found in event`);
929-
}
930-
931-
if (!operatorAddress) {
932-
logger.error(`[handleEventSupplierSlashed] operatorAddress not found in event=${event.kind} attributes=${stringify(event.event.attributes)}`);
933-
throw new Error(`[handleEventSupplierSlashed] operatorAddress not found in event`);
934-
}
935-
936-
const supplier = await Supplier.get(operatorAddress);
937-
938-
if (!supplier) {
939-
throw new Error(`[handleEventSupplierSlashed] supplier not found for operator address ${operatorAddress}`);
940-
}
941-
942-
supplier.stakeAmount -= BigInt(slashingCoin.amount);
943-
944-
await Promise.all([
945-
supplier.save(),
946-
EventSupplierSlashed.create({
947-
id: getEventId(event),
948-
supplierId: operatorAddress,
949-
blockId: getBlockId(event.block),
950-
eventId: getEventId(event),
951-
proofMissingPenalty: BigInt(slashingCoin.amount),
952-
proofMissingPenaltyDenom: slashingCoin.denom,
953-
previousStakeAmount: supplier.stakeAmount,
954-
afterStakeAmount: supplier.stakeAmount,
955-
// in alpha this event does not have the values below, so we are setting them to empty values for now
956-
applicationId: "",
957-
serviceId: "",
958-
sessionId: "",
959-
sessionStartHeight: BigInt(0),
960-
sessionEndHeight: BigInt(0),
961-
}).save(),
962-
]);
963-
}
964-
965-
async function _handleEventSupplierSlashed(event: CosmosEvent) {
966-
const {
967-
claim,
968-
proofMissingPenalty,
969-
} = getAttributes(event.event.attributes);
970-
971-
if (!claim || !claim.session_header || Object.keys(claim).length === 0) {
972-
logger.warn(`[handleEventSupplierSlashed] claim not found in event, trying to handle with previous version`);
973-
await _handleOldEventSupplierSlashed(event);
974-
return;
975-
}
976-
977-
const supplier = await Supplier.get(claim.supplier_operator_address);
978-
979-
if (!supplier) {
980-
throw new Error(`[handleEventSupplierSlashed] supplier not found for address: ${claim.supplier_operator_address}`);
981-
}
982-
983-
const previousStakeAmount = supplier.stakeAmount.valueOf();
984-
supplier.stakeAmount -= BigInt(proofMissingPenalty.amount);
985-
986-
await Promise.all([
987-
supplier.save(),
988-
EventSupplierSlashed.create({
989-
id: getEventId(event),
990-
supplierId: claim.supplier_operator_address,
991-
// TODO: Create entity for session header
992-
applicationId: claim.session_header.application_address,
993-
serviceId: claim.session_header.service_id,
994-
sessionId: claim.session_header.session_id || "",
995-
sessionEndHeight: BigInt(claim.session_header.session_end_block_height || "0"),
996-
sessionStartHeight: BigInt(claim.session_header.session_start_block_height || "0"),
997-
blockId: getBlockId(event.block),
998-
eventId: getEventId(event),
999-
proofMissingPenalty: BigInt(proofMissingPenalty.amount),
1000-
proofMissingPenaltyDenom: proofMissingPenalty.denom,
1001-
previousStakeAmount,
1002-
afterStakeAmount: supplier.stakeAmount,
1003-
proofValidationStatus: getClaimProofStatusFromSDK(claim.proof_validation_status),
1004-
}).save(),
1005-
]);
1006-
}
1007-
1008893
// eslint-disable-next-line complexity
1009894
function _handleEventProofValidityChecked(event: CosmosEvent): EventProofValidityCheckedProps {
1010895
let supplierOperatorAddress = "",
@@ -1158,10 +1043,6 @@ export async function handleEventApplicationReimbursementRequest(events: Array<C
11581043
await optimizedBulkCreate("EventApplicationReimbursementRequest", eventsUpdated);
11591044
}
11601045

1161-
export async function handleEventSupplierSlashed(events: Array<CosmosEvent>): Promise<void> {
1162-
await Promise.all(events.map(_handleEventSupplierSlashed));
1163-
}
1164-
11651046
export async function handleEventProofValidityChecked(events: Array<CosmosEvent>): Promise<void> {
11661047
await optimizedBulkCreate("EventProofValidityChecked", events.map(_handleEventProofValidityChecked));
11671048
}

0 commit comments

Comments
 (0)