From 8ff1cc7c7dba8e8faf297ed04cb5e65d8d8094ac Mon Sep 17 00:00:00 2001 From: Sameh Abouel-saad Date: Wed, 1 Apr 2026 15:41:45 +0200 Subject: [PATCH] cleanup: remove dead code, unused imports, and extract certification helpers I1: Remove unused import `allowedNodeEnvironmentFlags` from nodes.ts. I2: Remove unused imports `Store`, `In` from tftPrice.ts. I3: Remove unused debug variables `h`, `r` from nodes.ts. I5: Remove dead function `twinCreateOrUpdateOrDelete` from twins.ts (never called from processor, contained dormant bugs). I6: Remove dead function `contractBilled` from contracts.ts (superseded by `collectContractBillReports`). I8: Extract `parseNodeCertification()` and `parseFarmCertification()` helpers, replacing 8 duplicated switch blocks across nodes.ts, farms.ts, and policies.ts. I9: Remove dead `savedNode.interfaces.push()` in nodeUpdated (OneToMany is read-only on the "one" side). I10: Remove dead `newNode.interfaces = []` in nodeStored. I11: Remove dead `newFarm.publicIPs = []` in farmStored and `savedFarm.publicIPs.push()` guard in farmUpdated. Refs: #211 Co-Authored-By: Claude Opus 4.6 (1M context) --- src/mappings/contracts.ts | 32 ------------- src/mappings/farms.ts | 31 ++++--------- src/mappings/nodes.ts | 97 ++++++++------------------------------- src/mappings/policies.ts | 55 ++++------------------ src/mappings/tftPrice.ts | 2 - src/mappings/twins.ts | 64 -------------------------- 6 files changed, 37 insertions(+), 244 deletions(-) diff --git a/src/mappings/contracts.ts b/src/mappings/contracts.ts index 2cadf0a..9a58ef1 100644 --- a/src/mappings/contracts.ts +++ b/src/mappings/contracts.ts @@ -394,38 +394,6 @@ export function collectContractBillReports(ctx: Ctx): ContractBillReport[] { return list } -export async function contractBilled( - ctx: Ctx, - item: EventItem<'SmartContractModule.ContractBilled', { event: { args: true } }>, -) { - const contract_billed_event = new SmartContractModuleContractBilledEvent(ctx, item.event).asV9 - - const newContractBilledReport = new ContractBillReport() - - newContractBilledReport.id = item.event.id - newContractBilledReport.contractID = contract_billed_event.contractId - - let level = DiscountLevel.None - switch (contract_billed_event.discountLevel.__kind) { - case 'None': break - case 'Default': - level = DiscountLevel.Default - break - case 'Bronze': - level = DiscountLevel.Bronze - break - case 'Silver': - level = DiscountLevel.Silver - break - case 'Gold': level = DiscountLevel.Gold - } - newContractBilledReport.discountReceived = level - newContractBilledReport.amountBilled = contract_billed_event.amountBilled - newContractBilledReport.timestamp = contract_billed_event.timestamp - - await ctx.store.save(newContractBilledReport) -} - export async function contractUpdateUsedResources( ctx: Ctx, item: EventItem<'SmartContractModule.UpdatedUsedResources', { event: { args: true } }>, diff --git a/src/mappings/farms.ts b/src/mappings/farms.ts index 6018f6f..cebf922 100644 --- a/src/mappings/farms.ts +++ b/src/mappings/farms.ts @@ -8,6 +8,13 @@ import { validateString } from "./nodes" import * as ipaddr from 'ipaddr.js'; +export function parseFarmCertification(kind: string): FarmCertification { + switch (kind) { + case 'Gold': return FarmCertification.Gold + default: return FarmCertification.NotCertified + } +} + export class FarmWithIPs { constructor(farmID: number, ips: PublicIp[]) { this.farmID = farmID @@ -58,8 +65,6 @@ export async function farmStored( : false newFarm.certification = FarmCertification.NotCertified - newFarm.publicIPs = [] - await ctx.store.save(newFarm) const ipPromises = farmStoredEventParsed.publicIps.map((ip, index) => { @@ -146,11 +151,7 @@ export async function farmUpdated( const rawArgs = item.event.args as any rawArgs.dedicatedFarm = rawArgs['dedicatedFarm:'] ?? rawArgs.dedicatedFarm ?? false farmUpdatedEventParsed = farmUpdatedEvent.asV63 - switch (farmUpdatedEvent.asV63.certification.__kind) { - case "Gold": { - certification = FarmCertification.Gold - } - } + certification = parseFarmCertification(farmUpdatedEvent.asV63.certification.__kind) } if (!farmUpdatedEventParsed) { @@ -196,10 +197,6 @@ export async function farmUpdated( newIP.contractId = ip.contractId newIP.farm = savedFarm await ctx.store.save(newIP) - if (!savedFarm.publicIPs) { - savedFarm.publicIPs = [] - } - savedFarm.publicIPs.push(newIP) ctx.log.debug({ eventName: item.name, ip: ip.ip.toString() }, `PublicIP: ${ip.ip.toString()} added with farm id: ${savedFarm.farmID}`); } } @@ -267,16 +264,6 @@ export async function farmCertificationSet( return } - let certType = FarmCertification.NotCertified - switch (certification.__kind.toString()) { - case 'NotCertified': - certType = FarmCertification.NotCertified - break - case 'Gold': - certType = FarmCertification.Gold - break - } - - savedFarm.certification = certType + savedFarm.certification = parseFarmCertification(certification.__kind.toString()) await ctx.store.save(savedFarm) } diff --git a/src/mappings/nodes.ts b/src/mappings/nodes.ts index 3c83688..993119d 100644 --- a/src/mappings/nodes.ts +++ b/src/mappings/nodes.ts @@ -13,7 +13,13 @@ import { PublicConfig as V105PublicConfig } from '../types/v105' import { Ctx } from '../processor' import assert from "assert"; -import { allowedNodeEnvironmentFlags } from "process"; + +export function parseNodeCertification(kind: string): NodeCertification { + switch (kind) { + case 'Certified': return NodeCertification.Certified + default: return NodeCertification.Diy + } +} export async function nodeStored( ctx: Ctx, @@ -137,8 +143,6 @@ export async function nodeStored( resourcesTotal.cru = nodeEvent.resources.cru await ctx.store.save(resourcesTotal) - newNode.interfaces = [] - const interfacesPromisses = nodeEvent.interfaces.map(async (intf, index) => { const newInterface = new Interfaces() newInterface.id = item.event.id + '-' + index @@ -226,21 +230,9 @@ export async function nodeUpdated( savedNode.country = validateString(ctx, nodeAsV28.country.toString()) savedNode.city = validateString(ctx, nodeAsV28.city.toString()) - if (nodeAsV28.certificationType) { - const certificationTypeAsString = nodeAsV28.certificationType.__kind.toString() - let certType = NodeCertification.Diy - switch (certificationTypeAsString) { - case 'Diy': - certType = NodeCertification.Diy - break - case 'Certified': - certType = NodeCertification.Certified - break - } - savedNode.certification = certType - } else { - savedNode.certification = NodeCertification.Diy - } + savedNode.certification = nodeAsV28.certificationType + ? parseNodeCertification(nodeAsV28.certificationType.__kind.toString()) + : NodeCertification.Diy } if (node.isV43) { @@ -250,21 +242,9 @@ export async function nodeUpdated( savedNode.secure = nodeAsV43.secureBoot ? true : false savedNode.virtualized = nodeAsV43.virtualized ? true : false savedNode.serialNumber = validateString(ctx, nodeAsV43.serialNumber.toString()) - if (nodeAsV43.certificationType) { - const certificationTypeAsString = nodeAsV43.certificationType.__kind.toString() - let certType = NodeCertification.Diy - switch (certificationTypeAsString) { - case 'Diy': - certType = NodeCertification.Diy - break - case 'Certified': - certType = NodeCertification.Certified - break - } - savedNode.certification = certType - } else { - savedNode.certification = NodeCertification.Diy - } + savedNode.certification = nodeAsV43.certificationType + ? parseNodeCertification(nodeAsV43.certificationType.__kind.toString()) + : NodeCertification.Diy } if (node.isV63 || node.isV105) { @@ -281,21 +261,9 @@ export async function nodeUpdated( savedNode.secure = nodeEvent.secureBoot ? true : false savedNode.virtualized = nodeEvent.virtualized ? true : false savedNode.serialNumber = validateString(ctx, nodeEvent.serialNumber.toString()) - if (nodeEvent.certification) { - const certificationTypeAsString = nodeEvent.certification.__kind.toString() - let certType = NodeCertification.Diy - switch (certificationTypeAsString) { - case 'Diy': - certType = NodeCertification.Diy - break - case 'Certified': - certType = NodeCertification.Certified - break - } - savedNode.certification = certType - } else { - savedNode.certification = NodeCertification.Diy - } + savedNode.certification = nodeEvent.certification + ? parseNodeCertification(nodeEvent.certification.__kind.toString()) + : NodeCertification.Diy } if (node.isV118) { @@ -306,21 +274,9 @@ export async function nodeUpdated( savedNode.secure = nodeEvent.secureBoot ? true : false savedNode.virtualized = nodeEvent.virtualized ? true : false savedNode.serialNumber = nodeEvent.serialNumber ? validateString(ctx, nodeEvent.serialNumber.toString()) : 'Unknown' - if (nodeEvent.certification) { - const certificationTypeAsString = nodeEvent.certification.__kind.toString() - let certType = NodeCertification.Diy - switch (certificationTypeAsString) { - case 'Diy': - certType = NodeCertification.Diy - break - case 'Certified': - certType = NodeCertification.Certified - break - } - savedNode.certification = certType - } else { - savedNode.certification = NodeCertification.Diy - } + savedNode.certification = nodeEvent.certification + ? parseNodeCertification(nodeEvent.certification.__kind.toString()) + : NodeCertification.Diy } // First remove all ifs @@ -337,7 +293,6 @@ export async function nodeUpdated( newInterface.node = savedNode await ctx.store.save(newInterface) - savedNode.interfaces.push(newInterface) })) await ctx.store.save(savedNode) @@ -499,17 +454,7 @@ export async function nodeCertificationSet( const savedNode = await ctx.store.get(Node, { where: { nodeID: nodeID }, relations: { location: true, interfaces: true } }) if (!savedNode) return - let certType = NodeCertification.Diy - switch (certification.__kind.toString()) { - case 'Diy': - certType = NodeCertification.Diy - break - case 'Certified': - certType = NodeCertification.Certified - break - } - - savedNode.certification = certType + savedNode.certification = parseNodeCertification(certification.__kind.toString()) await ctx.store.save(savedNode) } @@ -669,8 +614,6 @@ function getNodePublicConfig(ctx: Ctx, node: TfgridModuleNodeStoredEvent): NodeP if (nodeEvent.publicConfig.domain) { domain = nodeEvent.publicConfig.domain.toString() } - let h = nodeEvent.publicConfig?.ip6?.ip.toString(); - let r = nodeEvent.publicConfig?.ip4.ip.toString(); return { ip4: validateString(ctx, nodeEvent.publicConfig?.ip4.ip.toString()), gw4: validateString(ctx, nodeEvent.publicConfig?.ip4.gw.toString()), diff --git a/src/mappings/policies.ts b/src/mappings/policies.ts index a2fe476..7c142e5 100644 --- a/src/mappings/policies.ts +++ b/src/mappings/policies.ts @@ -11,7 +11,8 @@ import { TfgridModulePricingPolicyStoredEvent, TfgridModuleFarmingPolicyStoredEvent, TfgridModuleFarmingPolicyUpdatedEvent } from "../types/events"; -import { validateString } from "./nodes" +import { validateString, parseNodeCertification } from "./nodes" +import { parseFarmCertification } from "./farms" export async function pricingPolicyStored( ctx: Ctx, @@ -109,29 +110,9 @@ export async function farmingPolicyStored( newFarmingPolicy.immutable = farmingPolicyStoredEvent.immutable newFarmingPolicy.default = farmingPolicyStoredEvent.default - const certificationTypeAsString = farmingPolicyStoredEvent.nodeCertification.__kind.toString() - let nodeCertType = NodeCertification.Diy - switch (certificationTypeAsString) { - case 'Diy': - nodeCertType = NodeCertification.Diy - break - case 'Certified': - nodeCertType = NodeCertification.Certified - break - } - newFarmingPolicy.nodeCertification = nodeCertType - - const farmCertificationTypeAsString = farmingPolicyStoredEvent.farmCertification.__kind.toString() - let farmCertType = FarmCertification.NotCertified - switch (farmCertificationTypeAsString) { - case 'NotCertified': - farmCertType = FarmCertification.NotCertified - break - case 'Gold': - farmCertType = FarmCertification.Gold - break - } - newFarmingPolicy.farmCertification = farmCertType + newFarmingPolicy.nodeCertification = parseNodeCertification(farmingPolicyStoredEvent.nodeCertification.__kind.toString()) + + newFarmingPolicy.farmCertification = parseFarmCertification(farmingPolicyStoredEvent.farmCertification.__kind.toString()) await ctx.store.save(newFarmingPolicy) } @@ -165,29 +146,9 @@ export async function farmingPolicyUpdated( savedPolicy.immutable = farmingPolicyUpdatedEvent.immutable savedPolicy.default = farmingPolicyUpdatedEvent.default - const certificationTypeAsString = farmingPolicyUpdatedEvent.nodeCertification.__kind.toString() - let nodeCertType = NodeCertification.Diy - switch (certificationTypeAsString) { - case 'Diy': - nodeCertType = NodeCertification.Diy - break - case 'Certified': - nodeCertType = NodeCertification.Certified - break - } - savedPolicy.nodeCertification = nodeCertType - - const farmCertificationTypeAsString = farmingPolicyUpdatedEvent.farmCertification.__kind.toString() - let farmCertType = FarmCertification.NotCertified - switch (farmCertificationTypeAsString) { - case 'NotCertified': - farmCertType = FarmCertification.NotCertified - break - case 'Gold': - farmCertType = FarmCertification.Gold - break - } - savedPolicy.farmCertification = farmCertType + savedPolicy.nodeCertification = parseNodeCertification(farmingPolicyUpdatedEvent.nodeCertification.__kind.toString()) + + savedPolicy.farmCertification = parseFarmCertification(farmingPolicyUpdatedEvent.farmCertification.__kind.toString()) await ctx.store.save(savedPolicy) } \ No newline at end of file diff --git a/src/mappings/tftPrice.ts b/src/mappings/tftPrice.ts index eee0375..97ea936 100644 --- a/src/mappings/tftPrice.ts +++ b/src/mappings/tftPrice.ts @@ -1,8 +1,6 @@ -import { Store } from '@subsquid/typeorm-store' import { Ctx } from '../processor' import { SubstrateBlock } from '@subsquid/substrate-processor'; import { EventItem } from '@subsquid/substrate-processor/lib/interfaces/dataSelection' -import { In } from 'typeorm' import { TftPriceModulePriceStoredEvent, TftPriceModuleAveragePriceStoredEvent } from '../types/events'; import { PriceStored, AveragePriceStored } from '../model'; import { ParserFixPointFn, parseI16F16 } from '@encointer/util' diff --git a/src/mappings/twins.ts b/src/mappings/twins.ts index 9e9d8d2..3f94c3b 100644 --- a/src/mappings/twins.ts +++ b/src/mappings/twins.ts @@ -54,70 +54,6 @@ export async function twinDeleted( await ctx.store.remove(savedTwin) } -export async function twinCreateOrUpdateOrDelete(ctx: Ctx): Promise<[Twin[], Twin[], Twin[]]> { - let newTwins = [] - let updatedTwins = [] - let deletedTwins = [] - for (let block of ctx.blocks) { - for (let item of block.items) { - if (item.name === "TfgridModule.TwinStored") { - const twin = getTwinCreate(ctx, item) - - const newTwin = new Twin() - - newTwin.id = item.event.id - newTwin.gridVersion = twin.version - newTwin.twinID = twin.twinID - newTwin.accountID = twin.accountID - newTwin.relay = twin.relay - newTwin.publicKey = twin.pk - - newTwins.push(newTwin) - } - if (item.name === "TfgridModule.TwinUpdated") { - const twin = getTwinUpdate(ctx, item) - - const foundInNewListIndex: number = newTwins.findIndex(t => t.twinID == twin.twinID); - if (foundInNewListIndex != -1) { - const savedTwin: Twin = newTwins[foundInNewListIndex] - savedTwin.relay = twin.relay - savedTwin.publicKey = twin.pk - savedTwin.gridVersion = twin.version - newTwins[foundInNewListIndex] = savedTwin - continue - } - - const foundInUpdatedListIndex: number = updatedTwins.findIndex(t => t.twinID == twin.twinID); - if (foundInUpdatedListIndex != -1) { - let savedTwin: Twin = updatedTwins[foundInUpdatedListIndex] - savedTwin.relay = twin.relay - savedTwin.publicKey = twin.pk - savedTwin.gridVersion = twin.version - updatedTwins[foundInUpdatedListIndex] = savedTwin - continue - } - - const savedTwin: any = await ctx.store.get(Twin, { where: { twinID: twin.twinID } }) - if (!savedTwin) continue - savedTwin.relay = twin.relay - savedTwin.publicKey = twin.pk - savedTwin.gridVersion = twin.version - updatedTwins.push(savedTwin) - } - if (item.name === "TfgridModule.TwinDeleted") { - const twinDeletedEvent = new TfgridModuleTwinDeletedEvent(ctx, item.event).asV9 - - const savedTwin = await ctx.store.get(Twin, { where: { twinID: twinDeletedEvent } }) - if (savedTwin) { - deletedTwins.push(savedTwin) - } - } - } - } - - return [newTwins, updatedTwins, deletedTwins] -} - function getTwinCreate( ctx: Ctx, item: EventItem<'TfgridModule.TwinStored', { event: { args: true } }>