-
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.
Merge pull request #185 from cardano-foundation/vda-ada-pots-verifica…
…tion feat: compare data of ada_pots table
- Loading branch information
Showing
14 changed files
with
400 additions
and
63 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
102 changes: 51 additions & 51 deletions
102
...ava/org/cardanofoundation/ledgersync/verifier/data/app/config/DbSyncDatasourceConfig.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 |
---|---|---|
@@ -1,51 +1,51 @@ | ||
//package org.cardanofoundation.ledgersync.verifier.data.app.config; | ||
// | ||
//import lombok.RequiredArgsConstructor; | ||
//import org.springframework.beans.factory.annotation.Qualifier; | ||
//import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
//import org.springframework.context.annotation.Bean; | ||
//import org.springframework.context.annotation.Configuration; | ||
//import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
//import org.springframework.orm.jpa.JpaTransactionManager; | ||
//import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
//import org.springframework.transaction.PlatformTransactionManager; | ||
//import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
// | ||
//import javax.sql.DataSource; | ||
//import java.util.Objects; | ||
// | ||
//@Configuration | ||
//@EnableTransactionManagement | ||
//@EnableJpaRepositories( | ||
// entityManagerFactoryRef = "dbSyncEntityManagerFactory", | ||
// transactionManagerRef = "dbSyncTransactionManager", | ||
// basePackages = {"org.cardanofoundation.ledgersync.verifier.data.app.repository.dbsync"}) | ||
//@RequiredArgsConstructor | ||
//public class DbSyncDatasourceConfig { | ||
// | ||
// private final MultiDataSourceProperties multiDataSourceProperties; | ||
// | ||
// @Bean(name = "dbSyncDataSource") | ||
// public DataSource ledgerSyncDataSource() { | ||
// return multiDataSourceProperties.buildDataSource( | ||
// multiDataSourceProperties.getDatasourceDbSync()); | ||
// } | ||
// | ||
// @Bean(name = "dbSyncEntityManagerFactory") | ||
// public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean( | ||
// EntityManagerFactoryBuilder builder, @Qualifier("dbSyncDataSource") DataSource dataSource) { | ||
// return builder | ||
// .dataSource(dataSource) | ||
// .packages( | ||
// "org.cardanofoundation.ledgersync.verifier.data.app.entity.dbsync") | ||
// .build(); | ||
// } | ||
// | ||
// @Bean(name = "dbSyncTransactionManager") | ||
// public PlatformTransactionManager dbSyncTransactionManager( | ||
// @Qualifier("dbSyncEntityManagerFactory") | ||
// LocalContainerEntityManagerFactoryBean ledgerSyncEntityManagerFactory) { | ||
// return new JpaTransactionManager( | ||
// Objects.requireNonNull(ledgerSyncEntityManagerFactory.getObject())); | ||
// } | ||
//} | ||
package org.cardanofoundation.ledgersync.verifier.data.app.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Objects; | ||
|
||
@Configuration | ||
@EnableTransactionManagement | ||
@EnableJpaRepositories( | ||
entityManagerFactoryRef = "dbSyncEntityManagerFactory", | ||
transactionManagerRef = "dbSyncTransactionManager", | ||
basePackages = {"org.cardanofoundation.ledgersync.verifier.data.app.repository.dbsync"}) | ||
@RequiredArgsConstructor | ||
public class DbSyncDatasourceConfig { | ||
|
||
private final MultiDataSourceProperties multiDataSourceProperties; | ||
|
||
@Bean(name = "dbSyncDataSource") | ||
public DataSource ledgerSyncDataSource() { | ||
return multiDataSourceProperties.buildDataSource( | ||
multiDataSourceProperties.getDatasourceDbSync()); | ||
} | ||
|
||
@Bean(name = "dbSyncEntityManagerFactory") | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean( | ||
EntityManagerFactoryBuilder builder, @Qualifier("dbSyncDataSource") DataSource dataSource) { | ||
return builder | ||
.dataSource(dataSource) | ||
.packages( | ||
"org.cardanofoundation.ledgersync.verifier.data.app.entity.dbsync") | ||
.build(); | ||
} | ||
|
||
@Bean(name = "dbSyncTransactionManager") | ||
public PlatformTransactionManager dbSyncTransactionManager( | ||
@Qualifier("dbSyncEntityManagerFactory") | ||
LocalContainerEntityManagerFactoryBean ledgerSyncEntityManagerFactory) { | ||
return new JpaTransactionManager( | ||
Objects.requireNonNull(ledgerSyncEntityManagerFactory.getObject())); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...in/java/org/cardanofoundation/ledgersync/verifier/data/app/config/JdbcTemplateConfig.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,25 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.config; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class JdbcTemplateConfig { | ||
@Bean(name = "ledgerSyncJdbcTemplate") | ||
public JdbcTemplate ledgerSyncJdbcTemplate( | ||
@Qualifier("ledgerSyncDataSource") DataSource dataSource) { | ||
return new JdbcTemplate(dataSource); | ||
} | ||
|
||
@Bean(name = "dbSyncJdbcTemplate") | ||
public JdbcTemplate dbSyncJdbcTemplate(@Qualifier("dbSyncDataSource") DataSource dataSource) { | ||
return new JdbcTemplate(dataSource); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...va/org/cardanofoundation/ledgersync/verifier/data/app/mapper/AdaPotsComparisonMapper.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,21 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.mapper; | ||
|
||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparison; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparisonKey; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.sql.SQLException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Component | ||
public interface AdaPotsComparisonMapper<T> { | ||
/** | ||
* Builds a map of Ada pots comparisons. | ||
* | ||
* @param source The source data list. | ||
* @return A map of Ada pots comparisons. | ||
*/ | ||
Map<AdaPotsComparisonKey, AdaPotsComparison> buildMap(List<T> source) throws SQLException; | ||
} |
35 changes: 35 additions & 0 deletions
35
...rdanofoundation/ledgersync/verifier/data/app/mapper/impl/AdaPotsComparisonMapperImpl.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,35 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.mapper.impl; | ||
|
||
import org.cardanofoundation.ledgersync.verifier.data.app.mapper.AdaPotsComparisonMapper; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparison; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparisonKey; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.projection.AdaPotsProjection; | ||
|
||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class AdaPotsComparisonMapperImpl implements AdaPotsComparisonMapper<AdaPotsProjection> { | ||
@Override | ||
public Map<AdaPotsComparisonKey, AdaPotsComparison> buildMap(List<AdaPotsProjection> source) | ||
throws SQLException { | ||
Map<AdaPotsComparisonKey, AdaPotsComparison> map = new HashMap<>(); | ||
for (AdaPotsProjection adaPotsProjection : source) { | ||
AdaPotsComparisonKey key = | ||
AdaPotsComparisonKey.builder() | ||
.slotNo(adaPotsProjection.getSlotNo()) | ||
.epochNo(adaPotsProjection.getEpochNo()) | ||
.build(); | ||
AdaPotsComparison value = | ||
AdaPotsComparison.builder() | ||
.adaPotsComparisonKey(key) | ||
.treasury(adaPotsProjection.getTreasury()) | ||
.reserves(adaPotsProjection.getReserves()) | ||
.rewards(adaPotsProjection.getRewards()) | ||
.build(); | ||
map.put(key, value); | ||
} | ||
return map; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...main/java/org/cardanofoundation/ledgersync/verifier/data/app/model/AdaPotsComparison.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,20 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.model; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
import java.math.BigInteger; | ||
|
||
@Builder | ||
@Setter | ||
@Getter | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
public class AdaPotsComparison { | ||
AdaPotsComparisonKey adaPotsComparisonKey; | ||
BigInteger treasury; | ||
BigInteger reserves; | ||
BigInteger rewards; | ||
} |
31 changes: 31 additions & 0 deletions
31
...n/java/org/cardanofoundation/ledgersync/verifier/data/app/model/AdaPotsComparisonKey.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,31 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.model; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
import java.util.Objects; | ||
|
||
@Builder | ||
@Setter | ||
@Getter | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
public class AdaPotsComparisonKey { | ||
Long slotNo; | ||
Long epochNo; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
AdaPotsComparisonKey that = (AdaPotsComparisonKey) o; | ||
return slotNo.equals(that.slotNo) && epochNo.equals(that.epochNo); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(slotNo, epochNo); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...java/org/cardanofoundation/ledgersync/verifier/data/app/projection/AdaPotsProjection.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,19 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.projection; | ||
|
||
import lombok.*; | ||
|
||
import java.math.BigInteger; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@Getter | ||
@Setter | ||
public class AdaPotsProjection { | ||
Long slotNo; | ||
Long epochNo; | ||
BigInteger treasury; | ||
BigInteger reserves; | ||
BigInteger rewards; | ||
} |
22 changes: 22 additions & 0 deletions
22
...cardanofoundation/ledgersync/verifier/data/app/projection/AdaPotsProjectionRowMapper.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,22 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.projection; | ||
|
||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class AdaPotsProjectionRowMapper implements RowMapper<AdaPotsProjection> { | ||
@Override | ||
public AdaPotsProjection mapRow(@NotNull ResultSet rs, int rowNum) throws SQLException { | ||
AdaPotsProjection adaPotsProjection = new AdaPotsProjection(); | ||
adaPotsProjection.setSlotNo(rs.getLong("slot_no")); | ||
adaPotsProjection.setEpochNo(rs.getLong("epoch_no")); | ||
adaPotsProjection.setTreasury(rs.getBigDecimal("treasury").toBigInteger()); | ||
adaPotsProjection.setReserves(rs.getBigDecimal("reserves").toBigInteger()); | ||
adaPotsProjection.setRewards(rs.getBigDecimal("rewards").toBigInteger()); | ||
|
||
return adaPotsProjection; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../main/java/org/cardanofoundation/ledgersync/verifier/data/app/service/AdaPotsService.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,46 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.service; | ||
|
||
import org.cardanofoundation.ledgersync.verifier.data.app.mapper.impl.AdaPotsComparisonMapperImpl; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparison; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.model.AdaPotsComparisonKey; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.projection.AdaPotsProjection; | ||
import org.cardanofoundation.ledgersync.verifier.data.app.projection.AdaPotsProjectionRowMapper; | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
|
||
import java.sql.SQLException; | ||
import java.util.*; | ||
|
||
public abstract class AdaPotsService { | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public AdaPotsService(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public List<Long> getRandomEpochNos(int maxAdaPots) { | ||
List<Long> epochNos = | ||
jdbcTemplate.query( | ||
"SELECT epoch_no FROM ada_pots ORDER BY RANDOM() LIMIT ?", | ||
new Object[] {maxAdaPots}, | ||
(rs, rowNum) -> rs.getLong("epoch_no")); | ||
return epochNos; | ||
} | ||
|
||
public List<AdaPotsProjection> getAdaPotsByEpochNos(List<Long> epochNos) { | ||
Map<String, Object> params = Collections.singletonMap("epochNos", epochNos); | ||
AdaPotsProjectionRowMapper rowMapper = new AdaPotsProjectionRowMapper(); | ||
return new NamedParameterJdbcTemplate(jdbcTemplate) | ||
.query( | ||
"SELECT slot_no, epoch_no, treasury, reserves, rewards FROM ada_pots WHERE epoch_no IN (:epochNos)", | ||
params, | ||
(rs, rowNum) -> rowMapper.mapRow(rs, rowNum)); | ||
} | ||
|
||
public Map<AdaPotsComparisonKey, AdaPotsComparison> getMapAdaPotsFromEpochNos(List<Long> epochNos) | ||
throws SQLException { | ||
List<AdaPotsProjection> adaPotsProjections = getAdaPotsByEpochNos(epochNos); | ||
return new AdaPotsComparisonMapperImpl().buildMap(adaPotsProjections); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...cardanofoundation/ledgersync/verifier/data/app/service/impl/AdaPotsDbSyncServiceImpl.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,16 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.service.impl; | ||
|
||
import org.cardanofoundation.ledgersync.verifier.data.app.service.AdaPotsService; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AdaPotsDbSyncServiceImpl extends AdaPotsService { | ||
|
||
public AdaPotsDbSyncServiceImpl( | ||
@Qualifier("dbSyncJdbcTemplate") JdbcTemplate dbSyncJdbcTemplate) { | ||
super(dbSyncJdbcTemplate); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...org/cardanofoundation/ledgersync/verifier/data/app/service/impl/AdaPotsLSServiceImpl.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,18 @@ | ||
package org.cardanofoundation.ledgersync.verifier.data.app.service.impl; | ||
|
||
import org.cardanofoundation.ledgersync.verifier.data.app.service.AdaPotsService; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Primary | ||
public class AdaPotsLSServiceImpl extends AdaPotsService { | ||
|
||
public AdaPotsLSServiceImpl( | ||
@Qualifier("ledgerSyncJdbcTemplate") JdbcTemplate ledgerSyncJdbcTemplate) { | ||
super(ledgerSyncJdbcTemplate); | ||
} | ||
} |
Oops, something went wrong.