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 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-17 01:19:18 -08:00
parent 68a583508b
commit 3e536115eb
1 changed files with 23 additions and 12 deletions

View File

@ -73,10 +73,12 @@ export class NetworkSyncService {
*/
async handleNetworkProgressUpdated(data: NetworkProgressUpdatedData): Promise<void> {
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 来返回结果