From 3e536115eb5768b109f58f55564700d49b1965e8 Mon Sep 17 00:00:00 2001 From: hailin Date: Sat, 17 Jan 2026 01:19:18 -0800 Subject: [PATCH] fix(mining): add defensive checks for network sync undefined values - Handle missing currentContributionPerTree with default value - Add null checks for all network progress fields - Prevent DecimalError when contribution service returns incomplete data Co-Authored-By: Claude Opus 4.5 --- .../services/network-sync.service.ts | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/backend/services/mining-service/src/application/services/network-sync.service.ts b/backend/services/mining-service/src/application/services/network-sync.service.ts index 895be42c..37c47ec8 100644 --- a/backend/services/mining-service/src/application/services/network-sync.service.ts +++ b/backend/services/mining-service/src/application/services/network-sync.service.ts @@ -73,10 +73,12 @@ export class NetworkSyncService { */ async handleNetworkProgressUpdated(data: NetworkProgressUpdatedData): Promise { try { + // 防御性检查 + const contributionPerTree = data.currentContributionPerTree || '20'; + const treeCount = data.totalTreeCount || 0; + // 计算全网理论算力 - const networkTotalContribution = new Decimal(data.totalTreeCount).mul( - data.currentContributionPerTree, - ); + const networkTotalContribution = new Decimal(treeCount).mul(contributionPerTree); // 更新 MiningConfig 中的全网理论算力 const config = await this.prisma.miningConfig.findFirst(); @@ -89,14 +91,14 @@ export class NetworkSyncService { where: { id: config.id }, data: { networkTotalContribution: networkTotalContribution, - totalTreeCount: data.totalTreeCount, - contributionPerTree: new Decimal(data.currentContributionPerTree), + totalTreeCount: treeCount, + contributionPerTree: new Decimal(contributionPerTree), networkLastSyncedAt: new Date(), }, }); this.logger.log( - `Updated network progress: trees=${data.totalTreeCount}, contribution=${networkTotalContribution.toString()}`, + `Updated network progress: trees=${treeCount}, contribution=${networkTotalContribution.toString()}`, ); } catch (error) { this.logger.error('Failed to update network progress', error); @@ -144,14 +146,23 @@ export class NetworkSyncService { } const progressResult = await progressResponse.json(); + // 防御性检查:确保必要字段存在 + if (!progressResult.currentContributionPerTree) { + this.logger.warn( + `Network progress missing currentContributionPerTree, response: ${JSON.stringify(progressResult)}`, + ); + // 使用默认值继续 + progressResult.currentContributionPerTree = '20'; + } + await this.handleNetworkProgressUpdated({ - totalTreeCount: progressResult.totalTreeCount, - totalAdoptionOrders: progressResult.totalAdoptionOrders, - totalAdoptedUsers: progressResult.totalAdoptedUsers, - currentUnit: progressResult.currentUnit, - currentMultiplier: progressResult.currentMultiplier, + totalTreeCount: progressResult.totalTreeCount || 0, + totalAdoptionOrders: progressResult.totalAdoptionOrders || 0, + totalAdoptedUsers: progressResult.totalAdoptedUsers || 0, + currentUnit: progressResult.currentUnit || 1, + currentMultiplier: progressResult.currentMultiplier || '1', currentContributionPerTree: progressResult.currentContributionPerTree, - nextUnitTreeCount: progressResult.nextUnitTreeCount, + nextUnitTreeCount: progressResult.nextUnitTreeCount || 100000, }); // 3. 获取最新的 MiningConfig 来返回结果