diff --git a/src/worker/cccChange.ts b/src/worker/cccChange.ts index c77ea78..111d248 100644 --- a/src/worker/cccChange.ts +++ b/src/worker/cccChange.ts @@ -25,7 +25,14 @@ export async function updateCCCChange( block.number - 1 ); - if (commonParams.termSeconds === null) { + // When a block closes a term, the current term from "chain_getTermMetadata" is changed to the next term. + // To get the "real" current term of the block, we use the parent block number. + const [, currentTerm] = await sdk.rpc.sendRpcRequest( + "chain_getTermMetadata", + [block.number - 1] + ); + + if (currentTerm === 0) { const queries = []; queries.push( staticFeeDistribution.distributeFee( diff --git a/src/worker/dynamicFeeDistribution.ts b/src/worker/dynamicFeeDistribution.ts index 5fb24f8..06352f4 100644 --- a/src/worker/dynamicFeeDistribution.ts +++ b/src/worker/dynamicFeeDistribution.ts @@ -306,13 +306,13 @@ export async function applyPenalty({ for (const validator of validators) { const reward = authorRewards.get(validator) || new U64(0); - const reduced = calculatePunishment( + const penaltiedReward = rewardAfterPenalty( notVoted[validator] || 0, termEndBlockNumber - termStartBlockNumber + 1, reward ); - authorRewards.set(validator, reward.minus(reduced)); - penaltyAmount = penaltyAmount.plus(reduced); + authorRewards.set(validator, penaltiedReward); + penaltyAmount = penaltyAmount.plus(reward).minus(penaltiedReward); } return { @@ -321,29 +321,32 @@ export async function applyPenalty({ }; } -function calculatePunishment( - notVotedCnt: number, +function rewardAfterPenalty( + missedCnt: number, totalCnt: number, - reward: U64 -) { - const x = reward.times(notVotedCnt); - if (notVotedCnt * 3 <= totalCnt) { - // 0.3 * x - return x.times(3).idiv(10 * totalCnt); - } else if (notVotedCnt * 2 <= totalCnt) { - // 4.8 * x - 1.5 - return x - .times(48) - .minus(reward.times(15 * totalCnt)) + authorReward: U64 +): U64 { + const x = authorReward.times(missedCnt); + if (missedCnt * 3 <= totalCnt) { + // 1 - 0.3 * x + return authorReward + .times(10 * totalCnt) + .minus(x.times(3)) .idiv(10 * totalCnt); - } else if (notVotedCnt * 3 <= 2 * totalCnt) { - // 0.6 * x + 0.6 - return x - .times(6) - .plus(reward.times(6 * totalCnt)) + } else if (missedCnt * 2 <= totalCnt) { + // 2.5 - 4.8 * x + return authorReward + .times(25 * totalCnt) + .minus(x.times(48)) + .idiv(10 * totalCnt); + } else if (missedCnt * 3 <= 2 * totalCnt) { + // 0.4 - 0.6 * x + return authorReward + .times(4 * totalCnt) + .minus(x.times(6)) .idiv(10 * totalCnt); } else { - return reward; + return new U64(0); } } @@ -593,7 +596,7 @@ function calculateProposed(termBlocks: BlockAttribute[]) { if (result[block.author]) { result[block.author] += 1; } else { - result[block.author] = 0; + result[block.author] = 1; } } return result;