-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add epoch calculation and refactor validation class
- Loading branch information
1 parent
fdf116f
commit 9fcadab
Showing
18 changed files
with
235 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/org/cardanofoundation/rewards/calculation/TreasuryCalculation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.cardanofoundation.rewards.calculation; | ||
|
||
import org.cardanofoundation.rewards.calculation.domain.*; | ||
import org.cardanofoundation.rewards.calculation.enums.AccountUpdateAction; | ||
|
||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
import static org.cardanofoundation.rewards.calculation.constants.RewardConstants.EXPECTED_SLOT_PER_EPOCH; | ||
import static org.cardanofoundation.rewards.calculation.constants.RewardConstants.POOL_DEPOSIT_IN_LOVELACE; | ||
import static org.cardanofoundation.rewards.calculation.util.BigNumberUtils.multiplyAndFloor; | ||
|
||
public class TreasuryCalculation { | ||
|
||
/* | ||
* Calculate the reward pot for epoch e with the formula: | ||
* | ||
* rewards(e) = floor(monetary_expand_rate * eta * reserve(e - 1) + fee(e - 1)) | ||
* rewards(e) = 0, if e < 209 | ||
*/ | ||
public static BigInteger calculateTotalRewardPotWithEta(double monetaryExpandRate, int totalBlocksInEpochByPools, | ||
double decentralizationParameter, BigInteger reserve, BigInteger fee) { | ||
double eta = calculateEta(totalBlocksInEpochByPools, decentralizationParameter); | ||
return multiplyAndFloor(reserve, monetaryExpandRate, eta).add(fee); | ||
} | ||
|
||
/* | ||
* Calculate eta using the decentralisation parameter and the formula: | ||
* | ||
* eta(totalBlocksInEpochMadeByPools, decentralisation) = 1, if decentralisation >= 0.8, otherwise | ||
* eta(totalBlocksInEpochMadeByPools, decentralisation) = | ||
* min(1, totalBlocksInEpochMadeByPools / ((1 - decentralisation) * expectedSlotPerEpoch * activeSlotsCoeff)) | ||
* | ||
* See: https://github.com/input-output-hk/cardano-ledger/commit/c4f10d286faadcec9e4437411bce9c6c3b6e51c2 | ||
*/ | ||
private static double calculateEta(int totalBlocksInEpochByPools, double decentralizationParameter) { | ||
// shelley-delegation.pdf 5.4.3 | ||
|
||
if (decentralizationParameter >= 0.8) { | ||
return 1.0; | ||
} | ||
|
||
// The number of expected blocks will be the number of slots per epoch times the active slots coefficient | ||
double activeSlotsCoeff = 0.05; // See: Non-Updatable Parameters: https://cips.cardano.org/cips/cip9/ | ||
|
||
// decentralizationParameter is the proportion of blocks that are expected to be produced by stake pools | ||
// instead of the OBFT (Ouroboros Byzantine Fault Tolerance) nodes. It was introduced close before the Shelley era: | ||
// https://github.com/input-output-hk/cardano-ledger/commit/c4f10d286faadcec9e4437411bce9c6c3b6e51c2 | ||
double expectedBlocksInNonOBFTSlots = EXPECTED_SLOT_PER_EPOCH * activeSlotsCoeff * (1 - decentralizationParameter); | ||
|
||
// eta is the ratio between the number of blocks that have been produced during the epoch, and | ||
// the expectation value of blocks that should have been produced during the epoch under | ||
// ideal conditions. | ||
return Math.min(1, totalBlocksInEpochByPools / expectedBlocksInNonOBFTSlots); | ||
} | ||
|
||
/* | ||
"For each retiring pool, the refund for the pool registration deposit is added to the | ||
pool's registered reward account, provided the reward account is still registered." - | ||
https://github.com/input-output-hk/cardano-ledger/blob/9e2f8151e3b9a0dde9faeb29a7dd2456e854427c/eras/shelley/formal-spec/epoch.tex#L546C9-L547C87 | ||
*/ | ||
public static BigInteger calculateUnclaimedRefundsForRetiredPools(List<PoolDeregistration> retiredPools, | ||
List<AccountUpdate> latestAccountUpdates) { | ||
BigInteger refunds = BigInteger.ZERO; | ||
|
||
if (retiredPools.size() > 0) { | ||
for (AccountUpdate lastAccountUpdate : latestAccountUpdates) { | ||
if (lastAccountUpdate.getAction() == AccountUpdateAction.DEREGISTRATION) { | ||
refunds = refunds.add(POOL_DEPOSIT_IN_LOVELACE); | ||
} | ||
} | ||
} | ||
|
||
return refunds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.