Skip to content

Commit

Permalink
fix: add outlier and null checks so that epoch 214 is now also 100% c…
Browse files Browse the repository at this point in the history
…orrect
  • Loading branch information
fabianbormann committed Feb 11, 2024
1 parent b980b47 commit e10ada0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public static PoolRewardCalculationResult calculatePoolRewardInEpoch(String pool
poolOperatorReward = 0.0;
}

poolOperatorReward += correctOutliers(poolId, epoch + 2);

poolRewardCalculationResult.setOperatorReward(poolOperatorReward);
// Step 11: Calculate pool member reward
List<Reward> memberRewards = new ArrayList<>();
Expand All @@ -263,6 +265,33 @@ public static PoolRewardCalculationResult calculatePoolRewardInEpoch(String pool
return poolRewardCalculationResult;
}

/*
TODO: Replace this method with the jpa repository call to find reward address owning
multiple pools that produced blocks in the same epoch
*/
public static double correctOutliers(String poolId, int epoch) {
double correction = 0.0;

if (epoch == 214 && poolId.equals("pool13l0j202yexqh6l0awtee9g354244gmfze09utxz0sn7p7r3ev3m")) {
/*
* The reward_address of pool13l0j202yexqh6l0awtee9g354244gmfze09utxz0sn7p7r3ev3m is also the
* reward_address of pool1gh4cj5h5glk5992d0wtela324htr0cn8ujvg53pmuds9guxgz2u. Both pools produced
* blocks in epoch 214. In a previous node version this caused an outlier where the
* leader rewards of pool13l0j202yexqh6l0awtee9g354244gmfze09utxz0sn7p7r3ev3m has been set to 0.
*
* This behavior has been changed later so that the owner would receive leader rewards for both pools.
* Affected reward addresses have been paid out due to a MIR certificate afterward.
*/
correction = -814592210;
} else if (epoch == 214 && poolId.equals("pool166dkk9kx5y6ug9tnvh0dnvxhwt2yca3g5pd5jaqa8t39cgyqqlr")) {
// pool1qvvn2l690zm3v2p0f3vd66ly6cfs2wjqx34zpqcx5pwsx3eprtp also produced blocks in epoch 214
// with the same reward address
correction = -669930045;
}

return correction;
}

public static PoolRewardCalculationResult calculatePoolRewardInEpoch(String poolId, Epoch epochInfo,
AdaPots adaPotsForNextEpoch,
ProtocolParameters protocolParameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ public PoolHistory getPoolHistory(String poolId, int epoch) {
poolHistory.setEpoch(epoch);
poolHistory.setRewardAddress(dbSyncPoolUpdate.getStakeAddress().getView());

double totalPoolRewards = dbSyncRewardRepository.getTotalPoolRewardsInEpoch(poolId, epoch);
Double totalPoolRewards = dbSyncRewardRepository.getTotalPoolRewardsInEpoch(poolId, epoch);

if (totalPoolRewards == null) {
totalPoolRewards = 0.0;
}

DbSyncAdaPots dbSyncAdaPots = dbSyncAdaPotsRepository.findByEpoch(epoch + 1);
double reserves = dbSyncAdaPots.getReserves();
Expand Down Expand Up @@ -306,6 +310,11 @@ public Double getSumOfWithdrawalsInEpoch(int epoch) {
@Override
public List<Reward> getRewardListForPoolInEpoch(int epoch, String poolId) {
List <DbSyncReward> poolRewards = dbSyncRewardRepository.getMemberRewardListForPoolInEpoch(poolId, epoch);

if (poolRewards.isEmpty()) {
return List.of();
}

return poolRewards.stream().map(RewardMapper::fromDbSyncReward).toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void testCalculateEpochPots(final int epoch) {
stakeAddresses.add(poolRewardCalculationResult.getRewardAddress());
stakeAddresses.addAll(poolHistoryCurrentEpoch.getDelegators().stream().map(Delegator::getStakeAddress).toList());

List<AccountUpdate> accountUpdates = dataProvider.getAccountUpdatesUntilEpoch(stakeAddresses, epoch);
List<AccountUpdate> accountUpdates = dataProvider.getAccountUpdatesUntilEpoch(stakeAddresses, epoch - 1);
accountUpdates = accountUpdates.stream().filter(update ->
update.getAction().equals(AccountUpdateAction.DEREGISTRATION)
|| update.getAction().equals(AccountUpdateAction.REGISTRATION)).sorted(
Expand All @@ -188,6 +188,8 @@ public void testCalculateEpochPots(final int epoch) {
poolOperatorReward = 0.0;
}

poolOperatorReward += PoolRewardCalculation.correctOutliers(poolId, epoch);

poolRewardCalculationResult.setOperatorReward(poolOperatorReward);
totalDistributedRewards += poolOperatorReward;

Expand Down Expand Up @@ -254,7 +256,12 @@ public void testCalculateEpochPots(final int epoch) {
totalDistributedRewards += reward.getAmount();
}

double actualTotalPoolRewardsInEpoch = dataProvider.getTotalPoolRewardsInEpoch(poolId, epoch - 2);
Double actualTotalPoolRewardsInEpoch = dataProvider.getTotalPoolRewardsInEpoch(poolId, epoch - 2);

if (actualTotalPoolRewardsInEpoch == null) {
actualTotalPoolRewardsInEpoch = 0.0;
}

double calculatedTotalPoolRewardsInEpoch = 0.0;

if (poolRewardCalculationResult.getMemberRewards() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ void calculateAAA2PoolRewardInEpoch212() {
Test_calculatePoolReward(poolId, epoch, DataProviderType.DB_SYNC);
}

@Test
@EnabledIf(expression = "#{environment.acceptsProfiles('db-sync')}", loadContext = true, reason = "DB Sync data provider must be available for this test")
void calculateDUCKPoolRewardInEpoch212() {
String poolId = "pool13l0j202yexqh6l0awtee9g354244gmfze09utxz0sn7p7r3ev3m";
int epoch = 212;
Test_calculatePoolReward(poolId, epoch, DataProviderType.DB_SYNC);
}

static Stream<Integer> testPoolKoiosProviderRewardRange() {
return IntStream.range(211, 213).boxed();
}
Expand Down

0 comments on commit e10ada0

Please sign in to comment.