Skip to content

Commit 097d308

Browse files
committed
fix: resolve bug in method to fetch the pool owner from the latest update id
1 parent d7d7794 commit 097d308

File tree

7 files changed

+31
-19
lines changed

7 files changed

+31
-19
lines changed

src/main/java/org/cardanofoundation/rewards/calculation/domain/PoolHistory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.*;
44

55
import java.math.BigInteger;
6+
import java.util.HashSet;
67
import java.util.List;
78

89
@Getter
@@ -21,7 +22,7 @@ public class PoolHistory {
2122
private Double margin;
2223
private BigInteger fixedCost;
2324
private BigInteger pledge;
24-
private List<Delegator> delegators;
25+
private HashSet<Delegator> delegators;
2526
private int blockCount;
2627
private int epoch;
2728
}

src/main/java/org/cardanofoundation/rewards/validation/data/provider/DbSyncDataProvider.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.List;
2121
import java.util.stream.Collectors;
2222

23+
import static org.cardanofoundation.rewards.calculation.constants.RewardConstants.MAINNET_SHELLEY_START_EPOCH;
24+
2325
@Service
2426
@Slf4j
2527
@Profile("db-sync")
@@ -68,6 +70,10 @@ public AdaPots getAdaPotsForEpoch(int epoch) {
6870

6971
@Override
7072
public Epoch getEpochInfo(int epoch) {
73+
if (epoch < MAINNET_SHELLEY_START_EPOCH) {
74+
return null;
75+
}
76+
7177
DbSyncEpoch dbSyncEpoch = dbSyncEpochRepository.findByNumber(epoch);
7278
Epoch epochInfo = EpochMapper.fromDbSyncEpoch(dbSyncEpoch);
7379

@@ -103,7 +109,7 @@ public List<PoolHistory> fetchPoolHistoryInBatches(Integer epoch, int batchSize,
103109
.distinct()
104110
.toList();
105111

106-
List<LatestPoolUpdate> latestUpdates = dbSyncPoolUpdateRepository.findLatestActiveUpdatesInEpoch(epoch, poolIds);
112+
HashSet<LatestPoolUpdate> latestUpdates = dbSyncPoolUpdateRepository.findLatestActiveUpdatesInEpoch(epoch, poolIds);
107113

108114
List<Long> updateIds = latestUpdates.stream()
109115
.map(LatestPoolUpdate::getId)
@@ -122,15 +128,15 @@ public List<PoolHistory> fetchPoolHistoryInBatches(Integer epoch, int batchSize,
122128
int batches = poolIdBatches.size();
123129
for (List<String> poolIdBatch : poolIdBatches) {
124130
log.info("fetching pool history batch " + i + " / " + batches + " for epoch " + epoch + " with " + poolIdBatch.size() + " pools");
125-
List<PoolEpochStake> poolEpochStakes = dbSyncEpochStakeRepository.getAllPoolsActiveStakesInEpoch(epoch, poolIdBatch);
131+
HashSet<PoolEpochStake> poolEpochStakes = dbSyncEpochStakeRepository.getAllPoolsActiveStakesInEpoch(epoch, poolIdBatch);
126132

127133
for (String poolId : poolIdBatch) {
128134
PoolHistory poolHistory = new PoolHistory();
129135

130-
List<Delegator> delegators = poolEpochStakes.stream()
136+
HashSet<Delegator> delegators = poolEpochStakes.stream()
131137
.filter(epochStake -> epochStake.getPoolId().equals(poolId))
132138
.map(DelegatorMapper::fromPoolEpochStake)
133-
.toList();
139+
.collect(Collectors.toCollection(HashSet::new));
134140

135141
if (!delegators.isEmpty()) {
136142
BigInteger activeStake = delegators.stream()
@@ -198,7 +204,7 @@ public PoolHistory getPoolHistory(String poolId, int epoch) {
198204
PoolHistory poolHistory = new PoolHistory();
199205
List<PoolEpochStake> poolEpochStakes = dbSyncEpochStakeRepository.getPoolActiveStakeInEpoch(poolId, epoch);
200206
BigInteger activeStake = BigInteger.ZERO;
201-
List<Delegator> delegators = new ArrayList<>();
207+
HashSet<Delegator> delegators = new HashSet<>();
202208
for (PoolEpochStake poolEpochStake : poolEpochStakes) {
203209
activeStake = activeStake.add(poolEpochStake.getAmount());
204210
Delegator delegator = Delegator.builder()

src/main/java/org/cardanofoundation/rewards/validation/data/provider/KoiosDataProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public PoolHistory getPoolHistory(String poolId, int epoch) {
119119

120120
if (history == null) return null;
121121

122-
List<Delegator> poolMemberInEpoch = getPoolMemberInEpoch(poolId, epoch);
122+
HashSet<Delegator> poolMemberInEpoch = getPoolMemberInEpoch(poolId, epoch);
123123
history.setDelegators(poolMemberInEpoch);
124124
return history;
125125
}
@@ -316,8 +316,8 @@ public HashSet<String> getRegisteredAccountsUntilNow(Integer epoch, HashSet<Stri
316316
return null;
317317
}
318318

319-
private List<Delegator> getPoolMemberInEpoch(String poolId, int epoch) {
320-
List<Delegator> delegators = new ArrayList<>();
319+
private HashSet<Delegator> getPoolMemberInEpoch(String poolId, int epoch) {
320+
HashSet<Delegator> delegators = new HashSet<>();
321321
try {
322322
List<PoolDelegatorHistory> poolDelegatorsHistory = koiosBackendService
323323
.getPoolService().getPoolDelegatorsHistory(poolId, epoch, Options.EMPTY).getValue();

src/main/java/org/cardanofoundation/rewards/validation/repository/DbSyncEpochStakeRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.data.jpa.repository.Query;
77

88
import java.math.BigInteger;
9+
import java.util.HashSet;
910
import java.util.List;
1011

1112
public interface DbSyncEpochStakeRepository extends ReadOnlyRepository<DbSyncEpochStake, Long> {
@@ -22,7 +23,7 @@ public interface DbSyncEpochStakeRepository extends ReadOnlyRepository<DbSyncEpo
2223
JOIN pool_hash ON pool_hash.id=epoch_stake.pool_id
2324
JOIN stake_address ON stake_address.id = epoch_stake.addr_id
2425
WHERE epoch_no=:epoch AND pool_hash.view IN :poolIds AND amount > 0""")
25-
List<PoolEpochStake> getAllPoolsActiveStakesInEpoch(Integer epoch, List<String> poolIds);
26+
HashSet<PoolEpochStake> getAllPoolsActiveStakesInEpoch(Integer epoch, List<String> poolIds);
2627

2728
@Query("SELECT SUM(amount) FROM DbSyncEpochStake WHERE epoch=:epoch")
2829
BigInteger getEpochStakeByEpoch(Integer epoch);

src/main/java/org/cardanofoundation/rewards/validation/repository/DbSyncPoolUpdateRepository.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ public interface DbSyncPoolUpdateRepository extends ReadOnlyRepository<DbSyncPoo
2222

2323
@Query(nativeQuery = true, value = """
2424
SELECT pool_update.id, pool_hash.view AS poolId, pledge, margin,
25-
fixed_cost AS fixedCost, stake_address.view AS rewardAddress
25+
fixed_cost AS fixedCost, stake_address.view AS rewardAddress
2626
FROM pool_update
27-
JOIN stake_address ON stake_address.id=pool_update.reward_addr_id
28-
JOIN pool_hash ON pool_hash.id=hash_id
29-
WHERE pool_hash.view IN :poolIds AND pool_update.registered_tx_id IN (
30-
SELECT MAX(registered_tx_id) FROM pool_update WHERE active_epoch_no <= :epoch GROUP BY hash_id
31-
);""")
32-
List<LatestPoolUpdate> findLatestActiveUpdatesInEpoch(Integer epoch, List<String> poolIds);
27+
JOIN stake_address ON stake_address.id=pool_update.reward_addr_id
28+
JOIN pool_hash ON pool_hash.id=pool_update.hash_id AND pool_hash.view IN :poolIds
29+
JOIN (SELECT MAX(registered_tx_id) AS registered_tx_id, hash_id
30+
FROM pool_update WHERE active_epoch_no <= :epoch
31+
GROUP BY hash_id
32+
) AS latest_update ON latest_update.hash_id=pool_update.hash_id AND latest_update.registered_tx_id=pool_update.registered_tx_id
33+
WHERE pool_update.active_epoch_no <= :epoch
34+
""")
35+
HashSet<LatestPoolUpdate> findLatestActiveUpdatesInEpoch(Integer epoch, List<String> poolIds);
3336

3437
@Query(nativeQuery = true, value = """
3538
SELECT COUNT(*) FROM (

src/test/java/org/cardanofoundation/rewards/validation/EpochValidationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void testCalculateEpochPots(final int epoch, DataProvider dataProvider, b
4343
}
4444

4545
static Stream<Integer> dataProviderEpochRange() {
46-
return IntStream.range(236, 460).boxed();
46+
return IntStream.range(208, 460).boxed();
4747
}
4848

4949
@ParameterizedTest

src/test/java/org/cardanofoundation/rewards/validation/PoolRewardValidationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ void calculateAutoStakeIIPoolRewardInEpoch215() {
111111
}
112112

113113
@Test
114+
@EnabledIf(expression = "#{environment.acceptsProfiles('db-sync')}", loadContext = true, reason = "DB Sync data provider must be available for this test")
114115
void calculateIOG1PoolRewardInEpoch241() {
115116
String poolId = "pool1mxqjlrfskhd5kql9kak06fpdh8xjwc76gec76p3taqy2qmfzs5z";
116117
int epoch = 241;
117-
Test_calculatePoolReward(poolId, epoch, DataProviderType.JSON);
118+
Test_calculatePoolReward(poolId, epoch, DataProviderType.DB_SYNC);
118119
}
119120
@Test
120121
void calculateXYZPoolRewardInEpoch213() {

0 commit comments

Comments
 (0)