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> { async handleNetworkProgressUpdated(data: NetworkProgressUpdatedData): Promise<void> {
try { try {
// 防御性检查
const contributionPerTree = data.currentContributionPerTree || '20';
const treeCount = data.totalTreeCount || 0;
// 计算全网理论算力 // 计算全网理论算力
const networkTotalContribution = new Decimal(data.totalTreeCount).mul( const networkTotalContribution = new Decimal(treeCount).mul(contributionPerTree);
data.currentContributionPerTree,
);
// 更新 MiningConfig 中的全网理论算力 // 更新 MiningConfig 中的全网理论算力
const config = await this.prisma.miningConfig.findFirst(); const config = await this.prisma.miningConfig.findFirst();
@ -89,14 +91,14 @@ export class NetworkSyncService {
where: { id: config.id }, where: { id: config.id },
data: { data: {
networkTotalContribution: networkTotalContribution, networkTotalContribution: networkTotalContribution,
totalTreeCount: data.totalTreeCount, totalTreeCount: treeCount,
contributionPerTree: new Decimal(data.currentContributionPerTree), contributionPerTree: new Decimal(contributionPerTree),
networkLastSyncedAt: new Date(), networkLastSyncedAt: new Date(),
}, },
}); });
this.logger.log( this.logger.log(
`Updated network progress: trees=${data.totalTreeCount}, contribution=${networkTotalContribution.toString()}`, `Updated network progress: trees=${treeCount}, contribution=${networkTotalContribution.toString()}`,
); );
} catch (error) { } catch (error) {
this.logger.error('Failed to update network progress', error); this.logger.error('Failed to update network progress', error);
@ -144,14 +146,23 @@ export class NetworkSyncService {
} }
const progressResult = await progressResponse.json(); 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({ await this.handleNetworkProgressUpdated({
totalTreeCount: progressResult.totalTreeCount, totalTreeCount: progressResult.totalTreeCount || 0,
totalAdoptionOrders: progressResult.totalAdoptionOrders, totalAdoptionOrders: progressResult.totalAdoptionOrders || 0,
totalAdoptedUsers: progressResult.totalAdoptedUsers, totalAdoptedUsers: progressResult.totalAdoptedUsers || 0,
currentUnit: progressResult.currentUnit, currentUnit: progressResult.currentUnit || 1,
currentMultiplier: progressResult.currentMultiplier, currentMultiplier: progressResult.currentMultiplier || '1',
currentContributionPerTree: progressResult.currentContributionPerTree, currentContributionPerTree: progressResult.currentContributionPerTree,
nextUnitTreeCount: progressResult.nextUnitTreeCount, nextUnitTreeCount: progressResult.nextUnitTreeCount || 100000,
}); });
// 3. 获取最新的 MiningConfig 来返回结果 // 3. 获取最新的 MiningConfig 来返回结果