Skip to content

Commit

Permalink
feat(verifier-data-app): test address_tx_amount and calculate address…
Browse files Browse the repository at this point in the history
… balance
  • Loading branch information
Sotatek-QuanLeA committed May 3, 2024
1 parent 8c9b43f commit 80cd765
Show file tree
Hide file tree
Showing 30 changed files with 1,013 additions and 75 deletions.
2 changes: 1 addition & 1 deletion verifier-data-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.github.cardano-community:koios-java-client:1.18.1'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
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()));
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.cardanofoundation.ledgersync.verifier.data.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import rest.koios.client.backend.api.address.AddressService;
import rest.koios.client.backend.api.block.BlockService;
import rest.koios.client.backend.api.transactions.TransactionsService;
import rest.koios.client.backend.factory.BackendFactory;
import rest.koios.client.backend.factory.BackendService;

@Configuration
public class KoiosConfig {

/**
* Creates a bean for accessing the Koios preprod backend service.
*
* @return BackendService instance for Koios preprod.
*/
@Bean
public BackendService createBackendServiceBean() {
return BackendFactory.getKoiosPreprodService();
}

/**
* Creates a bean for accessing the address service provided by the backend service.
*
* @param backendService The backend service instance.
* @return AddressService instance for interacting with address-related data.
*/
@Bean
public AddressService createAddressServiceBean(BackendService backendService) {
return backendService.getAddressService();
}

/**
* Creates a bean for accessing the transactions service provided by the backend service.
*
* @param backendService The backend service instance.
* @return TransactionsService instance for interacting with transaction-related data.
*/
@Bean
public TransactionsService createTransactionServiceBean(BackendService backendService) {
return backendService.getTransactionsService();
}

/**
* Creates a bean for accessing the block service provided by the backend service.
*
* @param backendService The backend service instance.
* @return BlockService instance for interacting with block-related data.
*/
@Bean
public BlockService createBlockServiceBean(BackendService backendService) {
return backendService.getBlockService();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import javax.sql.DataSource;
import java.util.Objects;

/**
* Configuration class for setting up the datasource, entity manager factory, and transaction manager
* for the LedgerSync application.
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
Expand All @@ -24,17 +28,34 @@ public class LedgerSyncDatasourceConfig {

private final MultiDataSourceProperties multiDataSourceProperties;

/**
* Constructor for LedgerSyncDatasourceConfig.
*
* @param multiDataSourceProperties The properties for configuring multiple datasources.
*/
public LedgerSyncDatasourceConfig(MultiDataSourceProperties multiDataSourceProperties) {
this.multiDataSourceProperties = multiDataSourceProperties;
}

/**
* Creates the primary datasource bean for the LedgerSync application.
*
* @return The primary datasource for LedgerSync.
*/
@Primary
@Bean(name = "ledgerSyncDataSource")
public DataSource ledgerSyncDataSource() {
return multiDataSourceProperties.buildDataSource(
multiDataSourceProperties.getDatasourceLedgerSync());
}

/**
* Creates the entity manager factory bean for the LedgerSync application.
*
* @param builder The EntityManagerFactoryBuilder.
* @param dataSource The primary datasource for LedgerSync.
* @return The LocalContainerEntityManagerFactoryBean for LedgerSync.
*/
@Primary
@Bean(name = "ledgerSyncEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
Expand All @@ -44,11 +65,18 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
.dataSource(dataSource)
.packages(
"org.cardanofoundation.ledgersync.consumercommon.entity",
"org.cardanofoundation.ledgersync.verifier.data.app.entity.ledgersync",
"org.cardanofoundation.ledgersync.consumercommon.enumeration",
"org.cardanofoundation.ledgersync.consumercommon.validation")
.build();
}

/**
* Creates the transaction manager bean for the LedgerSync application.
*
* @param ledgerSyncEntityManagerFactory The entity manager factory for LedgerSync.
* @return The transaction manager for LedgerSync.
*/
@Primary
@Bean(name = "ledgerSyncTransactionManager")
public PlatformTransactionManager ledgerSyncTransactionManager(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cardanofoundation.ledgersync.verifier.data.app.config;


import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.AccessLevel;
Expand All @@ -13,6 +12,9 @@

import javax.sql.DataSource;

/**
* Configuration class for setting up multiple datasources using HikariCP.
*/
@Getter
@Setter
@FieldDefaults(level = AccessLevel.PRIVATE)
Expand All @@ -21,12 +23,22 @@
public class MultiDataSourceProperties {
DataSourceConfig datasourceLedgerSync;
DataSourceConfig datasourceDbSync;

/**
* Inner class representing the configuration for a single datasource.
*/
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public static class DataSourceConfig {
HikariConfig hikariConfig;
}

/**
* Build and return a datasource using the provided configuration.
*
* @param dataSourceConfig The configuration for the datasource.
* @return The configured datasource.
*/
public DataSource buildDataSource(DataSourceConfig dataSourceConfig) {
return new HikariDataSource(dataSourceConfig.getHikariConfig());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.cardanofoundation.ledgersync.verifier.data.app.constant;

/**
* Constants for unit values.
*/
public class Unit {
/**
* The unit value for Lovelace.
*/
public static final String LOVELACE = "lovelace";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.cardanofoundation.ledgersync.verifier.data.app.entity.ledgersync;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;

import java.math.BigInteger;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "address_tx_amount")
@IdClass(AddressTxAmountId.class)
@DynamicUpdate
public class AddressTxAmount {
@Id
@Column(name = "address")
private String address;

@Id
@Column(name = "unit")
private String unit;

@Id
@Column(name = "tx_hash")
private String txHash;

@Column(name = "slot")
private Long slot;

@Column(name = "quantity")
private BigInteger quantity;

//Only set if address doesn't fit in ownerAddr field. Required for few Byron Era addr
@Column(name = "addr_full")
private String addrFull;

@Column(name = "stake_address")
private String stakeAddress;

@Column(name = "epoch")
private Integer epoch;

@Column(name = "block")
private Long blockNumber;

@Column(name = "block_time")
private Long blockTime;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.cardanofoundation.ledgersync.verifier.data.app.entity.ledgersync;

import jakarta.persistence.Column;
import lombok.*;

import java.io.Serializable;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Builder
public class AddressTxAmountId implements Serializable {
@Column(name = "address")
private String address;
@Column(name = "unit")
private String unit;
@Column(name = "tx_hash")
private String txHash;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.cardanofoundation.ledgersync.verifier.data.app.mapper;

import org.cardanofoundation.ledgersync.verifier.data.app.model.AddressBalanceComparison;
import org.cardanofoundation.ledgersync.verifier.data.app.model.AddressBalanceComparisonKey;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
* Interface for mapping address balance comparisons.
* @param <T> The type of source data.
*/
@Component
public interface AddressBalanceComparisonMapper<T> {
/**
* Builds a map of address balance comparisons.
* @param source The source data list.
* @return A map of address balance comparisons.
*/
Map<AddressBalanceComparisonKey, AddressBalanceComparison> buildMap(List<T> source);
}

0 comments on commit 80cd765

Please sign in to comment.