Skip to content

Commit

Permalink
Merge branch 'main' into doc/RA-37-main-page
Browse files Browse the repository at this point in the history
  • Loading branch information
shleger committed May 8, 2024
2 parents 2dcbf2c + cf30a02 commit e932182
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 101 deletions.
57 changes: 9 additions & 48 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,6 @@
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -108,22 +92,10 @@
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>


<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.openapitools/jackson-databind-nullable -->
<dependency>
Expand Down Expand Up @@ -176,19 +148,19 @@
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.7</version>
<version>1.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.swagger.parser.v3/swagger-parser -->
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser</artifactId>
<version>2.1.14</version>
<version>2.1.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
<version>20231013</version>
</dependency>
<dependency>
<groupId>com.bloxbean.cardano</groupId>
Expand All @@ -198,23 +170,6 @@
<groupId>com.bloxbean.cardano</groupId>
<artifactId>yaci</artifactId>
</dependency>
<dependency>
<groupId>org.purejava</groupId>
<artifactId>tweetnacl-java</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
<version>3.41.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.hypersistence</groupId>
<artifactId>hypersistence-utils-hibernate-63</artifactId>
Expand Down Expand Up @@ -376,6 +331,12 @@
<exclude>**/org/openapitools/**</exclude>
<exclude>**/model/**</exclude>
<exclude>**/entity/**</exclude>
<exclude>**/rosetta/config/**</exclude>
<exclude>**/rosetta/common/enumeration/NetworkId.java</exclude>
<exclude>**/rosetta/common/enumeration/OperationTypeStatus.java</exclude>
<exclude>**/rosetta/common/enumeration/RewardType.java</exclude>
<exclude>**/rosetta/common/enumeration/ScriptPurposetype.java</exclude>
<exclude>**/rosetta/common/enumeration/SyncStateType.java</exclude>
</excludes>
</configuration>
<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ public interface AddressBalanceRepository extends
// "WHERE b.address = :address AND b.blockNumber <= :number " +
// "GROUP BY b.address, b.unit, b.quantity, b.addrFull, b.policy, b.assetName, b.paymentCredential, b.stakeAddress, b.blockHash, b.epoch")
@Query(value =
"SELECT b from AddressBalanceEntity b WHERE b.blockNumber in (SELECT MAX(c.blockNumber) FROM AddressBalanceEntity c WHERE c.address = :address AND c.blockNumber <= :number GROUP BY c.unit) AND b.address = :address")
"""
SELECT b FROM AddressBalanceEntity b
WHERE (b.blockNumber, b.unit) in
(SELECT MAX(c.blockNumber), c.unit FROM AddressBalanceEntity c
WHERE c.address = :address AND c.blockNumber <= :number
GROUP BY c.unit)
AND b.address = :address
""")
List<AddressBalanceEntity> findAddressBalanceByAddressAndBlockNumber(
@Param("address") String address, @Param("number") Long number);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public String getValue() {

@Override
public String toString() {
return String.valueOf(value);
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public String getLabel() {

@Override
public String toString() {
return String.valueOf(label);
return label;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@

public enum NetworkId {
TESTNET,
MAINNET;

private NetworkId() {
}
MAINNET
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ public enum NetworkIdentifierType {
CARDANO_TESTNET_NETWORK("testnet", 0, 1097911063L),
CARDANO_PREPROD_NETWORK("preprod", 0, 1);

private String networkId;
private int value;
private long protocolMagic;
private final String networkId;
private final int value;
private final long protocolMagic;

NetworkIdentifierType(String networkId, int value, long protocolMagic) {
this.networkId = networkId;
this.value = value;
this.protocolMagic = protocolMagic;
}

NetworkIdentifierType() {
}

/**
* This method will return the NetworkIdentifierType based on the network value
* Thus, if the network value is 0,
* then it will return CARDANO_TESTNET_NETWORK or CARDANO_PREPROD_NETWORK
* (as both of them have the same network value)
*
* @param network - network value identifier
* @return a NetworkIdentifierType Enum instance
*/
public static NetworkIdentifierType find(int network) {
for (NetworkIdentifierType networkIdentifierType : NetworkIdentifierType.values()) {
if (network == networkIdentifierType.getValue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public String getValue() {

@Override
public String toString() {
return String.valueOf(value);
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,38 @@ public static Amount mapAmount(String value, String symbol, Integer decimals,

/**
* Maps a list of AddressBalanceDTOs to a Rosetta compatible AccountBalanceResponse.
*
* @param block The block from where the balances are calculated into the past
* @param balances The balances of the addresses
* @param balances The list of filtered balances up to {@code block} number.
* Each unit should occur only one time with the latest balance.
* Native assets should be present only as a lovelace unit.
* @return The Rosetta compatible AccountBalanceResponse
*/
public static AccountBalanceResponse mapToAccountBalanceResponse(Block block, List<AddressBalance> balances) {
List<AddressBalance> nonLovelaceBalances = balances.stream().filter(balance -> !balance.unit().equals(Constants.LOVELACE) && !balance.unit().equals(Constants.ADA)).toList();
long sum = balances.stream().filter(balance -> balance.unit().equals(Constants.LOVELACE) || balance.unit().equals(Constants.ADA)).mapToLong(value -> value.quantity().longValue()).sum();
BigInteger lovelaceAmount = balances.stream()
.filter(b -> Constants.LOVELACE.equals(b.unit()))
.map(AddressBalance::quantity)
.findFirst()
.orElse(BigInteger.ZERO);
List<Amount> amounts = new ArrayList<>();
if (sum > 0) {
amounts.add(mapAmount(String.valueOf(sum)));
if (lovelaceAmount.compareTo(BigInteger.ZERO) > 0) {
amounts.add(mapAmount(String.valueOf(lovelaceAmount)));
}
nonLovelaceBalances.forEach(balance -> amounts.add(mapAmount(balance.quantity().toString(), balance.unit().substring(Constants.POLICY_ID_LENGTH), Constants.MULTI_ASSET_DECIMALS, new CurrencyMetadata(
balance.unit().substring(0, Constants.POLICY_ID_LENGTH)))));
balances.stream()
.filter(b -> !Constants.LOVELACE.equals(b.unit()))
.forEach(b -> amounts.add(
mapAmount(b.quantity().toString(),
b.unit().substring(Constants.POLICY_ID_LENGTH),
Constants.MULTI_ASSET_DECIMALS,
new CurrencyMetadata(b.unit().substring(0, Constants.POLICY_ID_LENGTH)))
));
return AccountBalanceResponse.builder()
.blockIdentifier(BlockIdentifier.builder()
.hash(block.getHash())
.index(block.getNumber())
.build())
.balances(amounts)
.build();
.blockIdentifier(BlockIdentifier.builder()
.hash(block.getHash())
.index(block.getNumber())
.build())
.balances(amounts)
.build();
}

public static AccountBalanceResponse mapToStakeAddressBalanceResponse(Block block, StakeAddressBalance balance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class SpringWebSecurityConfig {
@Bean
public SecurityFilterChain filterChain(final HttpSecurity http) throws Exception {
http
// we don't need CSRF protection yet, since Rosetta API is used by non-browser clients only
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(matcherRegistry -> matcherRegistry
.requestMatchers("/**").permitAll())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void getAccountBalanceNoStakeAddressPositiveTest() {
AccountIdentifier accountIdentifier = getMockedAccountIdentifierAndMockAccountBalanceRequest(
accountBalanceRequest, blockIdentifier, accountAddress);
Block block = getMockBlock();
AddressBalance addressBalance = new AddressBalance(accountAddress, ADA, 1L,
AddressBalance addressBalance = new AddressBalance(accountAddress, LOVELACE, 1L,
BigInteger.valueOf(1000L), 1L);
when(ledgerBlockService.findBlock(1L, HASH)).thenReturn(block);
when(ledgerDataProviderService.findBalanceByAddressAndBlock(accountAddress, 1L))
Expand All @@ -74,6 +74,8 @@ void getAccountBalanceNoStakeAddressPositiveTest() {
assertNotNull(actual);
assertEquals("1000", actual.getBalances().getFirst().getValue());
assertNotNull(actual.getBalances().getFirst().getCurrency().getSymbol());
assertEquals(Constants.ADA, actual.getBalances().getFirst().getCurrency().getSymbol());
assertEquals(Constants.ADA_DECIMALS, actual.getBalances().getFirst().getCurrency().getDecimals());
assertEquals(blockIdentifier.getIndex(), actual.getBlockIdentifier().getIndex());
assertEquals(blockIdentifier.getHash(), actual.getBlockIdentifier().getHash());
verify(ledgerBlockService).findBlock(1L, HASH);
Expand Down Expand Up @@ -124,7 +126,7 @@ void getAccountBalanceNoStakeAddressNullBlockIdentifierPositiveTest() {
when(accountBalanceRequest.getAccountIdentifier()).thenReturn(accountIdentifier);
when(accountIdentifier.getAddress()).thenReturn(accountAddress);
Block block = getMockBlock();
AddressBalance addressBalance = new AddressBalance(accountAddress, ADA, 1L,
AddressBalance addressBalance = new AddressBalance(accountAddress, LOVELACE, 1L,
BigInteger.valueOf(1000L), 1L);
when(ledgerBlockService.findLatestBlock()).thenReturn(block);
when(ledgerDataProviderService.findBalanceByAddressAndBlock(accountAddress, 1L))
Expand All @@ -135,6 +137,8 @@ void getAccountBalanceNoStakeAddressNullBlockIdentifierPositiveTest() {
assertNotNull(actual);
assertEquals("1000", actual.getBalances().getFirst().getValue());
assertNotNull(actual.getBalances().getFirst().getCurrency().getSymbol());
assertEquals(Constants.ADA, actual.getBalances().getFirst().getCurrency().getSymbol());
assertEquals(Constants.ADA_DECIMALS, actual.getBalances().getFirst().getCurrency().getDecimals());
assertEquals(block.getNumber(), actual.getBlockIdentifier().getIndex());
assertEquals(block.getHash(), actual.getBlockIdentifier().getHash());
verify(ledgerBlockService).findLatestBlock();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.cardanofoundation.rosetta.common.enumeration;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class AddressTypeTest {

final AddressType[] addressTypes = AddressType.values();

@Test
void findByValue() {
for (AddressType addressType : addressTypes) {
AddressType actual = AddressType.findByValue(addressType.getValue());
assertEquals(addressType, actual);
// check toString() method to return the value
assertEquals(addressType.getValue(), actual.toString());
}
assertNull(AddressType.findByValue("Invalid"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.cardanofoundation.rosetta.common.enumeration;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class CatalystDataIndexesTest {

final CatalystDataIndexes[] catalystDataIndexes = CatalystDataIndexes.values();

@Test
void findByValue() {
for (CatalystDataIndexes catalystDataIndex : catalystDataIndexes) {
CatalystDataIndexes actual = CatalystDataIndexes.findByValue(catalystDataIndex.getValue());
assertEquals(catalystDataIndex, actual);
assertEquals(String.valueOf(catalystDataIndex.getValue()), actual.toString());
}
assertNull(CatalystDataIndexes.findByValue(0L));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.cardanofoundation.rosetta.common.enumeration;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class CatalystLabelsTest {

final CatalystLabels[] catalystLabels = CatalystLabels.values();

@Test
void findByValue() {
for (CatalystLabels catalystLabel : catalystLabels) {
CatalystLabels actual = CatalystLabels.findByValue(catalystLabel.getLabel());
assertEquals(catalystLabel, actual);
assertEquals(catalystLabel.getLabel(), actual.toString());
}
assertNull(CatalystLabels.findByValue("Invalid"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.cardanofoundation.rosetta.common.enumeration;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class NetworkEnumTest {

final NetworkEnum[] networkEnums = NetworkEnum.values();

@Test
void fromValue() {
for (NetworkEnum networkEnum : networkEnums) {
NetworkEnum actual = NetworkEnum.fromValue(networkEnum.getValue());
assertEquals(networkEnum, actual);
}
assertNull(NetworkEnum.fromValue("Invalid"));
}

@Test
void fromProtocolMagic() {
for (NetworkEnum networkEnum : networkEnums) {
NetworkEnum actual = NetworkEnum.fromProtocolMagic(
networkEnum.getNetwork().getProtocolMagic());
assertEquals(networkEnum, actual);
}
assertNull(NetworkEnum.fromProtocolMagic(0L));
}
}

0 comments on commit e932182

Please sign in to comment.