Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/worker/cccChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
49 changes: 26 additions & 23 deletions src/worker/dynamicFeeDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down