Skip to content

Commit 3e1b46a

Browse files
authored
* Added handler for older version of EventSupplierSlashed (#43)
* Added handler for older version of `EventSupplierSlashed` * Fixed `getEntityIdArg` when calling `indexStakeEntity` for the following events: `poktroll.service.EventRelayMiningDifficultyUpdated` and `poktroll.tokenomics.EventSupplierSlashed`
1 parent 673b0cb commit 3e1b46a

File tree

2 files changed

+74
-13
lines changed

2 files changed

+74
-13
lines changed

src/mappings/indexer.manager.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,7 @@ async function indexService(msgByType: MessageByType, eventByType: EventByType):
214214
], {
215215
"/poktroll.service.MsgAddService": "service.id",
216216
"poktroll.service.EventRelayMiningDifficultyUpdated": (attributes) => {
217-
for (const attribute of attributes) {
218-
if (attribute.key === "service_id") {
219-
return JSON.parse(attribute.value as string).service_id
220-
}
221-
}
222-
223-
return null
217+
return attributes.find(({key}) => key === "service_id")?.value as string
224218
}
225219
})
226220
}
@@ -319,7 +313,6 @@ async function indexGateway(msgByType: MessageByType, eventByType: EventByType):
319313
"poktroll.gateway.EventGatewayUnstaked": getIdOfUnbondingEvents,
320314
"poktroll.gateway.EventGatewayUnbondingBegin": getIdOfUnbondingEvents,
321315
"poktroll.gateway.EventGatewayUnbondingEnd": getIdOfUnbondingEvents,
322-
323316
})
324317
}
325318

@@ -395,6 +388,8 @@ async function indexStakeEntity(data: Array<CosmosEvent | CosmosMessage>, getEnt
395388
if (!getEntityId) throw new Error(`getIdFromEventAttribute not found for event type ${datum.event.type}`)
396389
let entityId = getEntityId(datum.event.attributes)
397390

391+
if (!entityId) throw new Error(`entityId not found for event type ${datum.event.type}`)
392+
398393
if (Array.isArray(entityId)) {
399394
entitiesUpdatedAtSameDatum.push(entityId)
400395
// we are only taking the first one because later we will get the data for every entity in this group
@@ -516,6 +511,11 @@ async function indexSupplier(msgByType: MessageByType, eventByType: EventByType)
516511
"poktroll.supplier.EventSupplierUnbondingEnd": eventGetId,
517512
"poktroll.tokenomics.EventSupplierSlashed": (attributes) => {
518513
for (const attribute of attributes) {
514+
// in the previous version of this event this is the key to get the supplierId
515+
if (attribute.key === "supplier_operator_addr") {
516+
return attribute.value as string
517+
}
518+
519519
if (attribute.key === "claim") {
520520
return JSON.parse(attribute.value as string).supplier_operator_address
521521
}

src/mappings/poktroll/relays.ts

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,12 @@ function getSettlementOpReasonFromSDK(item: typeof SettlementOpReasonSDKType | n
133133
}
134134

135135
function parseAttribute(attribute: unknown): string {
136-
return (attribute as string).replaceAll("\"", "");
136+
return (attribute as string).replaceAll("\"", "");
137137
}
138138

139+
// eslint-disable-next-line complexity
139140
function getAttributes(attributes: CosmosEvent["event"]["attributes"]) {
141+
140142
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
141143
// @ts-ignore
142144
let proof: ProofSDKType = {},
@@ -171,6 +173,11 @@ function getAttributes(attributes: CosmosEvent["event"]["attributes"]) {
171173
numRelays = BigInt(parseAttribute(attribute.value));
172174
}
173175

176+
// in older versions this is the key to get the number of claimed compute units
177+
if (attribute.key === "num_compute_units") {
178+
numClaimedComputedUnits = BigInt(parseAttribute(attribute.value));
179+
}
180+
174181
if (attribute.key === "num_claimed_compute_units") {
175182
numClaimedComputedUnits = BigInt(parseAttribute(attribute.value));
176183
}
@@ -578,11 +585,11 @@ function _handleEventApplicationOverserviced(event: CosmosEvent): EventApplicati
578585

579586
for (const attribute of event.event.attributes) {
580587
if (attribute.key === "application_addr") {
581-
applicationAddress = (attribute.value as string).replaceAll('"', '');
588+
applicationAddress = parseAttribute(attribute.value)
582589
}
583590

584591
if (attribute.key === "supplier_operator_addr") {
585-
supplierAddress = (attribute.value as string).replaceAll('"', '');
592+
supplierAddress = parseAttribute(attribute.value as string)
586593
}
587594

588595
if (attribute.key === "expected_burn") {
@@ -668,18 +675,72 @@ function _handleEventApplicationReimbursementRequest(event: CosmosEvent): EventA
668675
}
669676
}
670677

678+
async function _handleOldEventSupplierSlashed(event: CosmosEvent) {
679+
let slashingCoin: CoinSDKType | null = null, operatorAddress = '';
680+
681+
for (const attribute of event.event.attributes) {
682+
if (attribute.key === "slashing_amount") {
683+
slashingCoin = JSON.parse(attribute.value as string);
684+
}
685+
686+
if (attribute.key === "supplier_operator_addr") {
687+
operatorAddress = parseAttribute(attribute.value);
688+
}
689+
}
690+
691+
if (!slashingCoin) {
692+
throw new Error(`[handleEventSupplierSlashed] slashingCoin not found in event`);
693+
}
694+
695+
if (!operatorAddress) {
696+
throw new Error(`[handleEventSupplierSlashed] operatorAddress not found in event`);
697+
}
698+
699+
const supplier = await Supplier.get(operatorAddress)
700+
701+
if (!supplier) {
702+
throw new Error(`[handleEventSupplierSlashed] supplier not found for operator address ${operatorAddress}`)
703+
}
704+
705+
supplier.stakeAmount -= BigInt(slashingCoin.amount)
706+
707+
await Promise.all([
708+
supplier.save(),
709+
EventSupplierSlashed.create({
710+
id: getEventId(event),
711+
supplierId: operatorAddress,
712+
blockId: getBlockId(event.block),
713+
eventId: getEventId(event),
714+
proofMissingPenalty: BigInt(slashingCoin.amount),
715+
proofMissingPenaltyDenom: slashingCoin.denom,
716+
previousStakeAmount: supplier.stakeAmount,
717+
afterStakeAmount: supplier.stakeAmount,
718+
// in alpha this event does not have the values below, so we are setting them to empty values for now
719+
applicationId: '',
720+
serviceId: '',
721+
sessionId: '',
722+
sessionStartHeight: BigInt(0),
723+
sessionEndHeight: BigInt(0),
724+
}).save(),
725+
])
726+
}
727+
671728
async function _handleEventSupplierSlashed(event: CosmosEvent) {
672729
const {
673730
claim,
674731
proofMissingPenalty
675732
} = getAttributes(event.event.attributes);
676733

677734
if (!claim) {
678-
throw new Error(`[handleEventSupplierSlashed] claim not found in event`);
735+
logger.warn(`[handleEventSupplierSlashed] claim not found in event, trying to handle with previous version`);
736+
await _handleOldEventSupplierSlashed(event)
737+
return
679738
}
680739

681740
if (!claim.session_header) {
682-
throw new Error(`[handleEventSupplierSlashed] session_header not found in event`);
741+
logger.warn(`[handleEventSupplierSlashed] session_header not found in event, trying to handle with previous version`);
742+
await _handleOldEventSupplierSlashed(event)
743+
return
683744
}
684745

685746
const supplier = await Supplier.get(claim.supplier_operator_address)

0 commit comments

Comments
 (0)