Skip to content

Commit

Permalink
feat: create an overall epoch calculation for all pots
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianbormann committed Feb 8, 2024
1 parent 493b66e commit 3cdee2e
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,9 @@ public static double calculateDepositInEpoch(int epoch, DataProvider dataProvide
if (epoch > 207) {
double transactionDepositsInEpoch = dataProvider.getTransactionDepositsInEpoch(epoch);
List<PoolDeregistration> retiredPoolsInEpoch = dataProvider.getRetiredPoolsInEpoch(epoch + 1);
int actualPoolDeregistrationsInEpoch = 0;

for (PoolDeregistration poolDeregistration : retiredPoolsInEpoch) {
boolean poolDeregistrationLaterInEpoch = retiredPoolsInEpoch.stream().anyMatch(
deregistration -> deregistration.getPoolId().equals(poolDeregistration.getPoolId()) &&
deregistration.getAnnouncedTransactionId() > poolDeregistration.getAnnouncedTransactionId()
);

// To prevent double counting, we only count the pool deregistration if there is no other deregistration
// for the same pool later in the epoch
if (poolDeregistrationLaterInEpoch) {
continue;
}

List<PoolUpdate> poolUpdates = dataProvider.getPoolUpdateAfterTransactionIdInEpoch(poolDeregistration.getPoolId(),
poolDeregistration.getAnnouncedTransactionId(), epoch);

// There is an update after the deregistration, so the pool was not retired
if (poolUpdates.size() == 0) {
PoolDeregistration latestPoolRetirementUntilEpoch = dataProvider.latestPoolRetirementUntilEpoch(poolDeregistration.getPoolId(), epoch);
if (latestPoolRetirementUntilEpoch != null && latestPoolRetirementUntilEpoch.getRetiringEpoch() != epoch + 1) {
// The pool was retired in a previous epoch for the next epoch, but another deregistration was announced and changed the
// retirement epoch to something else. This means the pool was not retired in this epoch.
continue;
}

actualPoolDeregistrationsInEpoch += 1;
}
}

deposit += transactionDepositsInEpoch;
deposit -= actualPoolDeregistrationsInEpoch * DEPOSIT_POOL_REGISTRATION_IN_LOVELACE;
deposit -= retiredPoolsInEpoch.size() * DEPOSIT_POOL_REGISTRATION_IN_LOVELACE;
}

return depositsInPreviousEpoch + deposit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public PoolHistory getPoolHistory(String poolId, int epoch) {
Integer blockCount = dbSyncBlockRepository.getBlocksMadeByPoolInEpoch(poolId, epoch);
poolHistory.setBlockCount(blockCount);

DbSyncPoolUpdate dbSyncPoolUpdate = dbSyncPoolUpdateRepository.findLastestUpdateForEpoch(poolId, epoch);
DbSyncPoolUpdate dbSyncPoolUpdate = dbSyncPoolUpdateRepository.findLastestActiveUpdateInEpoch(poolId, epoch);
poolHistory.setFixedCost(dbSyncPoolUpdate.getFixedCost());
poolHistory.setMargin(dbSyncPoolUpdate.getMargin());
poolHistory.setEpoch(epoch);
Expand Down Expand Up @@ -157,7 +157,7 @@ public PoolHistory getPoolHistory(String poolId, int epoch) {

@Override
public Double getPoolPledgeInEpoch(String poolId, int epoch) {
DbSyncPoolUpdate dbSyncPoolUpdate = dbSyncPoolUpdateRepository.findLastestUpdateForEpoch(poolId, epoch);
DbSyncPoolUpdate dbSyncPoolUpdate = dbSyncPoolUpdateRepository.findLastestActiveUpdateInEpoch(poolId, epoch);
return dbSyncPoolUpdate.getPledge();
}

Expand All @@ -167,7 +167,7 @@ public PoolOwnerHistory getHistoryOfPoolOwnersInEpoch(String poolId, int epoch)
poolOwnerHistory.setEpoch(epoch);

List<PoolEpochStake> poolEpochStakes = dbSyncEpochStakeRepository.getPoolActiveStakeInEpoch(poolId, epoch);
DbSyncPoolUpdate dbSyncPoolUpdate = dbSyncPoolUpdateRepository.findLastestUpdateForEpoch(poolId, epoch);
DbSyncPoolUpdate dbSyncPoolUpdate = dbSyncPoolUpdateRepository.findLastestActiveUpdateInEpoch(poolId, epoch);
List<DbSyncPoolOwner> owners = dbSyncPoolOwnerRepository.getByPoolUpdateId(dbSyncPoolUpdate.getId());

List<String> stakeAddresses = owners.stream()
Expand All @@ -190,7 +190,40 @@ public PoolOwnerHistory getHistoryOfPoolOwnersInEpoch(String poolId, int epoch)
@Override
public List<PoolDeregistration> getRetiredPoolsInEpoch(int epoch) {
List<DbSyncPoolRetirement> dbSyncPoolRetirements = dbSyncPoolRetirementRepository.getPoolRetirementsByEpoch(epoch);
return dbSyncPoolRetirements.stream().map(PoolDeregistrationMapper::fromDbSyncPoolRetirement).toList();
List<PoolDeregistration> poolDeregistrations = dbSyncPoolRetirements.stream().map(PoolDeregistrationMapper::fromDbSyncPoolRetirement).toList();
List<PoolDeregistration> retiredPools = new ArrayList<>();

for (PoolDeregistration poolDeregistration : poolDeregistrations) {
boolean poolDeregistrationLaterInEpoch = poolDeregistrations.stream().anyMatch(
deregistration -> deregistration.getPoolId().equals(poolDeregistration.getPoolId()) &&
deregistration.getAnnouncedTransactionId() > poolDeregistration.getAnnouncedTransactionId()
);

// To prevent double counting, we only count the pool deregistration if there is no other deregistration
// for the same pool later in the epoch
if (poolDeregistrationLaterInEpoch) {
continue;
}

List<PoolUpdate> poolUpdates = this.getPoolUpdateAfterTransactionIdInEpoch(poolDeregistration.getPoolId(),
poolDeregistration.getAnnouncedTransactionId(), epoch - 1);

// There is an update after the deregistration, so the pool has not been retired
if (poolUpdates.size() == 0) {
PoolDeregistration latestPoolRetirementUntilEpoch = this.latestPoolRetirementUntilEpoch(poolDeregistration.getPoolId(), epoch - 1);
if (latestPoolRetirementUntilEpoch != null && latestPoolRetirementUntilEpoch.getRetiringEpoch() != epoch) {
// The pool was retired in a previous epoch for the next epoch, but another deregistration was announced and changed the
// retirement epoch to something else. This means the pool was not retired in this epoch.
continue;
}

DbSyncPoolUpdate dbSyncPoolUpdate = dbSyncPoolUpdateRepository.findLatestUpdateInEpoch(poolDeregistration.getPoolId(), epoch);
poolDeregistration.setRewardAddress(dbSyncPoolUpdate.getStakeAddress().getView());
retiredPools.add(poolDeregistration);
}
}

return retiredPools;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface DbSyncPoolUpdateRepository extends ReadOnlyRepository<DbSyncPoo
WHERE update.pool.bech32PoolId = :poolId
AND update.activeEpochNumber <= :epoch
ORDER BY update.registeredTransaction.id DESC LIMIT 1""")
DbSyncPoolUpdate findLastestUpdateForEpoch(String poolId, Integer epoch);
DbSyncPoolUpdate findLastestActiveUpdateInEpoch(String poolId, Integer epoch);

@Query(nativeQuery = true, value = """
SELECT COUNT(*) FROM (
Expand All @@ -35,4 +35,10 @@ SELECT hash_id, min(registered_tx_id) as registered_tx_id
ORDER BY update.registeredTransaction.id DESC""")
List<DbSyncPoolUpdate> findByBech32PoolIdAfterTransactionIdInEpoch(String poolId, long transactionId, int epoch);

@Query("""
SELECT update FROM DbSyncPoolUpdate AS update
WHERE update.pool.bech32PoolId = :poolId
AND update.registeredTransaction.block.epochNo <= :epoch
ORDER BY update.registeredTransaction.id DESC LIMIT 1""")
DbSyncPoolUpdate findLatestUpdateInEpoch(String poolId, int epoch);
}

0 comments on commit 3cdee2e

Please sign in to comment.