Skip to content

Commit

Permalink
feat: removing overhead and unused methods. clean up and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianbormann committed Mar 9, 2024
1 parent 1060673 commit da1379c
Show file tree
Hide file tree
Showing 80 changed files with 143 additions and 971 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@

import static org.cardanofoundation.rewards.calculation.PoolRewardsCalculation.calculatePoolRewardInEpoch;
import static org.cardanofoundation.rewards.calculation.constants.RewardConstants.*;
import static org.cardanofoundation.rewards.calculation.constants.RewardConstants.MAINNET_BOOTSTRAP_ADDRESS_AMOUNT;
import static org.cardanofoundation.rewards.calculation.util.BigNumberUtils.*;
import static org.cardanofoundation.rewards.calculation.util.CurrencyConverter.lovelaceToAda;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EpochCalculation {

public static EpochCalculationResult calculateEpochRewardPots(final int epoch, final AdaPots adaPotsForPreviousEpoch,
final ProtocolParameters protocolParameters, final Epoch epochInfo,
final List<PoolDeregistration> retiredPools,
final HashSet<String> rewardAddressesOfRetiredPools,
final HashSet<String> deregisteredAccounts,
final List<MirCertificate> mirCertificates,
final List<String> poolsThatProducedBlocksInEpoch,
Expand Down Expand Up @@ -83,8 +81,7 @@ public static EpochCalculationResult calculateEpochRewardPots(final int epoch, f

// The sum of all the refunds attached to unregistered reward accounts are added to the
// treasury (see: Pool Reap Transition, p.53, figure 40, shelley-ledger.pdf)
if (retiredPools.size() > 0) {
List<String> rewardAddressesOfRetiredPools = retiredPools.stream().map(PoolDeregistration::getRewardAddress).toList();
if (rewardAddressesOfRetiredPools.size() > 0) {
List<String> deregisteredOwnerAccounts = deregisteredAccountsOnEpochBoundary.stream()
.filter(rewardAddressesOfRetiredPools::contains).toList();
List<String> ownerAccountsRegisteredInThePast = registeredAccountsUntilNow.stream()
Expand All @@ -93,8 +90,7 @@ public static EpochCalculationResult calculateEpochRewardPots(final int epoch, f
/* Check if the reward address of the retired pool has been unregistered before
or if the reward address has been unregistered after the randomness stabilization window
or if the reward address has not been registered at all */
for (PoolDeregistration retiredPool : retiredPools) {
String rewardAddress = retiredPool.getRewardAddress();
for (String rewardAddress : rewardAddressesOfRetiredPools) {
if (deregisteredOwnerAccounts.contains(rewardAddress) ||
!ownerAccountsRegisteredInThePast.contains(rewardAddress)) {
// If the reward address has been unregistered, the deposit can not be returned
Expand Down Expand Up @@ -181,7 +177,7 @@ public static EpochCalculationResult calculateEpochRewardPots(final int epoch, f
calculatedReserve = calculatedReserve.add(MAINNET_BOOTSTRAP_ADDRESS_AMOUNT);
}

log.debug("Unspendable earned rewards: " + lovelaceToAda(unspendableEarnedRewards.longValue()) + " ADA");
log.debug("Unspendable earned rewards: " + unspendableEarnedRewards.longValue() + " Lovelace");
treasuryForCurrentEpoch = add(treasuryForCurrentEpoch, unspendableEarnedRewards);

TreasuryCalculationResult treasuryCalculationResult = TreasuryCalculationResult.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static BigInteger calculateMemberReward(BigInteger poolReward, double mar
public static PoolRewardCalculationResult calculatePoolRewardInEpoch(final String poolId, final PoolHistory poolHistoryCurrentEpoch,
final int totalBlocksInEpoch, final ProtocolParameters protocolParameters,
final BigInteger adaInCirculation, final BigInteger activeStakeInEpoch, BigInteger stakePoolRewardsPot,
final BigInteger totalActiveStakeOfOwners, final List<String> poolOwnerStakeAddresses,
final BigInteger totalActiveStakeOfOwners, final HashSet<String> poolOwnerStakeAddresses,
final HashSet<String> deregisteredAccounts, final boolean ignoreLeaderReward,
final HashSet<String> lateDeregisteredAccounts,
final HashSet<String> accountsRegisteredInThePast) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TreasuryCalculation {

public static TreasuryCalculationResult calculateTreasuryInEpoch(int epoch, ProtocolParameters protocolParameters,
AdaPots adaPotsForPreviousEpoch, Epoch epochInfo,
List<PoolDeregistration> retiredPools,
HashSet<String> rewardAddressesOfRetiredPools,
List<MirCertificate> mirCertificates,
final HashSet<String> deregisteredAccounts,
final HashSet<String> registeredAccountsUntilNow,
Expand Down Expand Up @@ -58,14 +58,13 @@ public static TreasuryCalculationResult calculateTreasuryInEpoch(int epoch, Prot
final BigInteger treasuryCut = multiplyAndFloor(totalRewardPot, treasuryGrowthRate);
BigInteger treasuryForCurrentEpoch = treasuryInPreviousEpoch.add(treasuryCut);

if (retiredPools.size() > 0) {
List<String> rewardAddressesOfRetiredPools = retiredPools.stream().map(PoolDeregistration::getRewardAddress).toList();
if (rewardAddressesOfRetiredPools.size() > 0) {
HashSet<String> deregisteredRewardAccounts = deregisteredAccounts.stream()
.filter(rewardAddressesOfRetiredPools::contains).collect(Collectors.toCollection(HashSet::new));
List<String> ownerAccountsRegisteredInThePast = registeredAccountsUntilNow.stream()
.filter(rewardAddressesOfRetiredPools::contains).toList();

BigInteger unclaimedRefunds = calculateUnclaimedRefundsForRetiredPools(retiredPools, deregisteredRewardAccounts, ownerAccountsRegisteredInThePast);
BigInteger unclaimedRefunds = calculateUnclaimedRefundsForRetiredPools(rewardAddressesOfRetiredPools, deregisteredRewardAccounts, ownerAccountsRegisteredInThePast);
treasuryForCurrentEpoch = treasuryForCurrentEpoch.add(unclaimedRefunds);
}

Expand Down Expand Up @@ -138,16 +137,15 @@ private static BigDecimal calculateEta(int totalBlocksInEpochByPools, BigDecimal
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,
public static BigInteger calculateUnclaimedRefundsForRetiredPools(HashSet<String> rewardAddressesOfRetiredPools,
HashSet<String> deregisteredRewardAccounts,
List<String> ownerAccountsRegisteredInThePast) {
BigInteger unclaimedRefunds = BigInteger.ZERO;
if (retiredPools.size() > 0) {
if (rewardAddressesOfRetiredPools.size() > 0) {
/* Check if the reward address of the retired pool has been unregistered before
or if the reward address has been unregistered after the randomness stabilization window
or if the reward address has not been registered at all */
for (PoolDeregistration retiredPool : retiredPools) {
String rewardAddress = retiredPool.getRewardAddress();
for (String rewardAddress : rewardAddressesOfRetiredPools) {
if (deregisteredRewardAccounts.contains(rewardAddress) ||
!ownerAccountsRegisteredInThePast.contains(rewardAddress)) {
// If the reward address has been unregistered, the deposit can not be returned
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@
@AllArgsConstructor
public class Epoch {
private int number;
private BigInteger output;
private BigInteger fees;
private int blockCount;
private BigInteger activeStake;
private List<String> poolsMadeBlocks;
private int nonOBFTBlockCount;
private int OBFTBlockCount;
private long unixTimeFirstBlock;
private long unixTimeLastBlock;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
@NoArgsConstructor
@AllArgsConstructor
public class MirCertificate {
private long blockTime;
private MirPot pot;
private int totalStakeKeys;
private BigInteger totalRewards;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
public class PoolHistory {
private String poolId;
private BigInteger activeStake;
private BigInteger delegatorRewards;
private String rewardAddress;
private List<String> owners;
private HashSet<String> owners;
private BigInteger ownerActiveStake;
private BigInteger poolFees;
private Double margin;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public class PoolRewardCalculationResult {
BigInteger distributedPoolReward;
Double poolMargin;
BigInteger poolCost;
List<String> poolOwnerStakeAddresses;
HashSet<String> poolOwnerStakeAddresses;
BigInteger unspendableEarnedRewards;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.*;

import java.math.BigInteger;
import java.util.HashSet;
import java.util.List;

@Getter
Expand All @@ -19,5 +20,5 @@ public class PoolUpdate {
private BigInteger fixedCost;
private BigInteger pledge;
private String rewardAddress;
private List<String> owners;
private HashSet<String> owners;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public static BigInteger add(BigInteger a, BigInteger b) {
return a.add(b);
}

public static BigInteger multiplyAndFloor(BigInteger a, double b, BigDecimal c) {
return new BigDecimal(a).multiply(BigDecimal.valueOf(b).multiply(c))
.round(new MathContext(0, RoundingMode.FLOOR)).toBigInteger();
}

public static BigInteger multiplyAndFloor(BigInteger a, BigDecimal b, BigDecimal c) {
return new BigDecimal(a).multiply(b).multiply(c).round(new MathContext(0, RoundingMode.FLOOR)).toBigInteger();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import org.cardanofoundation.rewards.validation.data.provider.DataProvider;
import org.cardanofoundation.rewards.calculation.domain.AdaPots;
import org.cardanofoundation.rewards.calculation.domain.PoolDeregistration;

import java.math.BigInteger;
import java.util.List;
import java.util.HashSet;

import static org.cardanofoundation.rewards.calculation.DepositsCalculation.calculateDepositsInEpoch;

Expand All @@ -15,11 +13,11 @@ public static BigInteger computeDepositsInEpoch(int epoch, DataProvider dataProv
AdaPots adaPots = dataProvider.getAdaPotsForEpoch(epoch);
BigInteger depositsInPreviousEpoch = adaPots.getDeposits();
BigInteger transactionDepositsInEpoch = BigInteger.ZERO;
List<PoolDeregistration> retiredPoolsInEpoch = List.of();
HashSet<String> retiredPoolsInEpoch = new HashSet<>();

if (epoch > 207) {
transactionDepositsInEpoch = dataProvider.getTransactionDepositsInEpoch(epoch);
retiredPoolsInEpoch = dataProvider.getRetiredPoolsInEpoch(epoch + 1);
retiredPoolsInEpoch = dataProvider.getRewardAddressesOfRetiredPoolsInEpoch(epoch + 1);
}

return calculateDepositsInEpoch(depositsInPreviousEpoch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static EpochCalculationResult calculateEpochRewardPots(int epoch, DataPro
AdaPots adaPotsForPreviousEpoch = dataProvider.getAdaPotsForEpoch(epoch - 1);
ProtocolParameters protocolParameters = dataProvider.getProtocolParametersForEpoch(epoch - 2);
Epoch epochInfo = dataProvider.getEpochInfo(epoch - 2);
List<PoolDeregistration> retiredPools = dataProvider.getRetiredPoolsInEpoch(epoch);
HashSet<String> rewardAddressesOfRetiredPoolsInEpoch = dataProvider.getRewardAddressesOfRetiredPoolsInEpoch(epoch);
List<MirCertificate> mirCertificates = dataProvider.getMirCertificatesInEpoch(epoch - 1);
List<PoolBlock> blocksMadeByPoolsInEpoch = dataProvider.getBlocksMadeByPoolsInEpoch(epoch - 2);
List<String> poolIds = blocksMadeByPoolsInEpoch.stream().map(PoolBlock::getPoolId).distinct().toList();
Expand All @@ -82,7 +82,7 @@ public static EpochCalculationResult calculateEpochRewardPots(int epoch, DataPro
sharedPoolRewardAddressesWithoutReward = dataProvider.findSharedPoolRewardAddressWithoutReward(epoch - 2);
}
HashSet<String> poolRewardAddresses = poolHistories.stream().map(PoolHistory::getRewardAddress).collect(Collectors.toCollection(HashSet::new));
poolRewardAddresses.addAll(retiredPools.stream().map(PoolDeregistration::getRewardAddress).collect(Collectors.toSet()));
poolRewardAddresses.addAll(rewardAddressesOfRetiredPoolsInEpoch);

long stabilityWindow = RANDOMNESS_STABILISATION_WINDOW;
// Since the Vasil hard fork, the unregistered accounts will not filter out before the
Expand All @@ -107,7 +107,7 @@ public static EpochCalculationResult calculateEpochRewardPots(int epoch, DataPro

start = System.currentTimeMillis();
EpochCalculationResult epochCalculationResult = EpochCalculation.calculateEpochRewardPots(
epoch, adaPotsForPreviousEpoch, protocolParameters, epochInfo, retiredPools, deregisteredAccounts,
epoch, adaPotsForPreviousEpoch, protocolParameters, epochInfo, rewardAddressesOfRetiredPoolsInEpoch, deregisteredAccounts,
mirCertificates, poolIds, poolHistories, lateDeregisteredAccounts,
registeredAccountsSinceLastEpoch, registeredAccountsUntilNow, sharedPoolRewardAddressesWithoutReward,
deregisteredAccountsOnEpochBoundary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import org.cardanofoundation.rewards.calculation.TreasuryCalculation;
import org.cardanofoundation.rewards.calculation.domain.*;
import org.cardanofoundation.rewards.validation.data.provider.DataProvider;
import org.cardanofoundation.rewards.calculation.enums.AccountUpdateAction;
import org.cardanofoundation.rewards.validation.domain.TreasuryValidationResult;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -20,17 +17,17 @@ public static TreasuryValidationResult calculateTreasuryForEpoch(int epoch, Data
AdaPots adaPotsForPreviousEpoch = dataProvider.getAdaPotsForEpoch(epoch - 1);
ProtocolParameters protocolParameters = dataProvider.getProtocolParametersForEpoch(epoch - 2);
Epoch epochInfo = dataProvider.getEpochInfo(epoch - 2);
List<PoolDeregistration> retiredPools = dataProvider.getRetiredPoolsInEpoch(epoch);
HashSet<String> rewardAddressesOfRetiredPoolsInEpoch = dataProvider.getRewardAddressesOfRetiredPoolsInEpoch(epoch);
List<MirCertificate> mirCertificates = dataProvider.getMirCertificatesInEpoch(epoch - 1);
List<PoolBlock> blocksMadeByPoolsInEpoch = dataProvider.getBlocksMadeByPoolsInEpoch(epoch - 2);
List<PoolHistory> poolHistories = dataProvider.getHistoryOfAllPoolsInEpoch(epoch - 2, blocksMadeByPoolsInEpoch);
HashSet<String> deregisteredAccountsOnEpochBoundary = dataProvider.getDeregisteredAccountsInEpoch(epoch - 1, EXPECTED_SLOTS_PER_EPOCH);

HashSet<String> poolRewardAddresses = poolHistories.stream().map(PoolHistory::getRewardAddress).collect(Collectors.toCollection(HashSet::new));
poolRewardAddresses.addAll(retiredPools.stream().map(PoolDeregistration::getRewardAddress).collect(Collectors.toSet()));
poolRewardAddresses.addAll(rewardAddressesOfRetiredPoolsInEpoch);
HashSet<String> registeredAccountsUntilNow = dataProvider.getRegisteredAccountsUntilNow(epoch, poolRewardAddresses, RANDOMNESS_STABILISATION_WINDOW);

TreasuryCalculationResult treasuryCalculationResult = TreasuryCalculation.calculateTreasuryInEpoch(epoch, protocolParameters, adaPotsForPreviousEpoch, epochInfo, retiredPools, mirCertificates, deregisteredAccountsOnEpochBoundary, registeredAccountsUntilNow, BigInteger.ZERO);
TreasuryCalculationResult treasuryCalculationResult = TreasuryCalculation.calculateTreasuryInEpoch(epoch, protocolParameters, adaPotsForPreviousEpoch, epochInfo, rewardAddressesOfRetiredPoolsInEpoch, mirCertificates, deregisteredAccountsOnEpochBoundary, registeredAccountsUntilNow, BigInteger.ZERO);

TreasuryValidationResult treasuryValidationResult = TreasuryValidationResult.fromTreasuryCalculationResult(treasuryCalculationResult);
AdaPots adaPotsForCurrentEpoch = dataProvider.getAdaPotsForEpoch(epoch);
Expand Down

0 comments on commit da1379c

Please sign in to comment.