From 9e9c791283fe069c0915404c8f485f59ba0b97f7 Mon Sep 17 00:00:00 2001 From: hailin Date: Wed, 21 Jan 2026 23:33:58 -0800 Subject: [PATCH] =?UTF-8?q?fix(batch-mining):=20=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E9=98=B6=E6=AE=B5=E5=88=92=E5=88=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit preMineDays 表示该批次比最后批次提前多少天开始挖矿 阶段天数 = 当前批次的preMineDays - 下一批次的preMineDays(差值) 例如:批次1(3天)、批次2(2天)、批次3(1天)、批次4(0天) - 阶段1:只有批次1,持续 3-2=1 天 - 阶段2:批次1+2,持续 2-1=1 天 - 阶段3:批次1+2+3,持续 1-0=1 天 - 最后阶段:所有批次一起挖剩余天数 Co-Authored-By: Claude Opus 4.5 --- .../services/batch-mining.service.ts | 61 ++++++++++++------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/backend/services/mining-service/src/application/services/batch-mining.service.ts b/backend/services/mining-service/src/application/services/batch-mining.service.ts index bf23d481..1a745149 100644 --- a/backend/services/mining-service/src/application/services/batch-mining.service.ts +++ b/backend/services/mining-service/src/application/services/batch-mining.service.ts @@ -350,18 +350,18 @@ export class BatchMiningService { /** * 构建挖矿阶段 * - * 根据批次的"提前天数"构建各挖矿阶段: - * - preMineDays 表示该批次比下一批次提前开始的天数 - * - 批次1的 preMineDays=3 意味着批次1比批次2提前3天,所以批次1先独挖3天 - * - 批次2的 preMineDays=2 意味着批次2比批次3提前2天,所以批次1+2一起挖2天 - * - 批次3的 preMineDays=1 意味着批次3比批次4提前1天,所以批次1+2+3一起挖1天 - * - 批次4的 preMineDays=0 意味着批次4是最后一批,所有批次一起挖剩余天数 + * preMineDays 的含义:该批次比最后一批提前多少天开始挖矿 + * - 批次1的 preMineDays=3 意味着批次1比最后批次提前3天开始 + * - 批次2的 preMineDays=2 意味着批次2比最后批次提前2天开始 + * - 批次3的 preMineDays=1 意味着批次3比最后批次提前1天开始 + * - 批次4的 preMineDays=0 意味着批次4是最后一批 * - * 正确的阶段划分: - * - 阶段1: 只有批次1,持续批次1的 preMineDays 天(在批次2加入之前) - * - 阶段2: 批次1+2,持续批次2的 preMineDays 天(在批次3加入之前) - * - 阶段3: 批次1+2+3,持续批次3的 preMineDays 天(在批次4加入之前) - * - 最后阶段: 所有批次一起挖 (totalMiningDays - 所有提前天数之和) + * 阶段划分(按 preMineDays 从大到小排序批次,计算相邻批次的差值): + * - 假设批次按 preMineDays 排序后为: 批次1(3), 批次2(2), 批次3(1), 批次4(0) + * - 阶段1: 只有批次1,持续 3-2=1 天 + * - 阶段2: 批次1+2,持续 2-1=1 天 + * - 阶段3: 批次1+2+3,持续 1-0=1 天 + * - 最后阶段: 所有批次一起挖剩余天数 */ private buildMiningPhases( items: BatchMiningItem[], @@ -395,22 +395,37 @@ export class BatchMiningService { return phases; } + // 按 preMineDays 从大到小排序批次(提前天数多的批次先开始挖) + const batchesByPreMineDays = [...sortedBatches].sort((a, b) => { + const preA = batchPreMineDays.get(a) || 0; + const preB = batchPreMineDays.get(b) || 0; + return preB - preA; // 降序 + }); + + this.logger.log(`[buildMiningPhases] 按提前天数排序后的批次: ${batchesByPreMineDays.join(', ')}`); + let currentPhase = 1; let participatingBatches: number[] = []; - let usedDays = 0; // 已分配的天数 + let usedDays = 0; - // 按批次顺序添加阶段 - // 关键:先用当前批次的 preMineDays 创建阶段,然后下一批次加入 - for (let i = 0; i < sortedBatches.length; i++) { - const currentBatch = sortedBatches[i]; + // 按 preMineDays 从大到小处理批次 + for (let i = 0; i < batchesByPreMineDays.length; i++) { + const currentBatch = batchesByPreMineDays[i]; const currentPreMineDays = batchPreMineDays.get(currentBatch) || 0; - // 先把当前批次加入参与列表 + // 计算当前阶段的天数 = 当前批次的 preMineDays - 下一批次的 preMineDays + let nextPreMineDays = 0; + if (i + 1 < batchesByPreMineDays.length) { + const nextBatch = batchesByPreMineDays[i + 1]; + nextPreMineDays = batchPreMineDays.get(nextBatch) || 0; + } + + const phaseDays = currentPreMineDays - nextPreMineDays; + + // 当前批次加入参与列表 participatingBatches.push(currentBatch); - // 该阶段持续天数 = 当前批次的 preMineDays(当前批次比下一批次提前的天数) - // 如果 preMineDays > 0,说明当前参与批次要在下一批次加入前独挖这些天 - if (currentPreMineDays > 0) { + if (phaseDays > 0) { // 计算该阶段参与用户的总算力 let participatingContribution = new Decimal(0); for (const batch of participatingBatches) { @@ -421,14 +436,14 @@ export class BatchMiningService { phaseNumber: currentPhase, startDate: new Date(), endDate: new Date(), - daysInPhase: currentPreMineDays, + daysInPhase: phaseDays, participatingContribution, participatingBatches: [...participatingBatches], }); - this.logger.log(`[buildMiningPhases] 阶段${currentPhase}: ${currentPreMineDays}天, 批次[${participatingBatches.join(',')}], 参与算力=${participatingContribution.toFixed(2)}`); + this.logger.log(`[buildMiningPhases] 阶段${currentPhase}: ${phaseDays}天, 批次[${participatingBatches.join(',')}], 参与算力=${participatingContribution.toFixed(2)}`); currentPhase++; - usedDays += currentPreMineDays; + usedDays += phaseDays; } }