Skip to content

Commit f2a98c0

Browse files
authored
Feat/get local params from n2c (#236)
* chore: upgraded to node 9.0 * chore:updated yaci store * feat: implemented get local params from n2c connection * feat: added backup solution to get params Epoch Table if local params not availalbe + adjusted tests * feat: spotless * feat: updated db * feat: added n2c socat host and port to yaci indexer
1 parent 188c152 commit f2a98c0

32 files changed

+439
-120
lines changed

.env.IntegrationTest

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ CARDANO_NODE_PORT=3001
2929
CARDANO_NODE_SUBMIT_HOST=yaci-cli
3030
NODE_SUBMIT_API_PORT=8090
3131
CARDANO_NODE_VERSION=0.0.0
32-
CARDANO_NODE_DIR=/node
32+
CARDANO_NODE_DIR=/Users/thkammer/Documents/dev/cardano/cardano-rosetta-java/node
3333
CARDANO_NODE_SOCKET_PATH=${CARDANO_NODE_DIR}/node.socket
3434
CARDANO_NODE_DB=${CARDANO_NODE_DIR}/db
3535
CARDANO_CONFIG=./config/${NETWORK}
@@ -59,14 +59,15 @@ GENESIS_CONWAY_PATH=/config/conway-genesis.json
5959
INDEXER_DOCKER_IMAGE_TAG=main
6060
PRUNING_ENABLED=false
6161

62-
YACI_SPRING_PROFILES=postgres
62+
YACI_SPRING_PROFILES=postgres,n2c-socat
6363
# database profiles: h2, h2-testData, postgres
6464
MEMPOOL_ENABLED=false
6565

6666
## Ports
6767
HOST_N2N_PORT=${CARDANO_NODE_PORT}
6868
HOST_SUBMIT_API_PORT=${NODE_SUBMIT_API_PORT}
6969
HOST_N2C_SOCAT_PORT=3333
70+
HOST_N2C_SOCAT_HOST=${CARDANO_NODE_HOST}
7071
HOST_STORE_API_PORT=8080
7172
HOST_CLUSTER_API_PORT=10000
7273
HOST_OGMIOS_PORT=1337

.env.docker-compose

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ GENESIS_CONWAY_PATH=/config/conway-genesis.json
5858
INDEXER_DOCKER_IMAGE_TAG=main
5959
PRUNING_ENABLED=false
6060

61-
YACI_SPRING_PROFILES=postgres
61+
YACI_SPRING_PROFILES=postgres,n2c-socket
6262
# database profiles: h2, h2-testData, postgres
6363
MEMPOOL_ENABLED=false
6464

.env.h2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ GENESIS_CONWAY_PATH=./config/${NETWORK}/conway-genesis.json
4141
INDEXER_DOCKER_IMAGE_TAG=main
4242
PRUNING_ENABLED=false
4343

44-
YACI_SPRING_PROFILES=h2
44+
YACI_SPRING_PROFILES=h2,n2c-socket
4545
# database profiles: h2, h2-testData, postgres
4646
MEMPOOL_ENABLED=false
4747

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The default config is focused on mainnet. If you want to test this on other Card
4141
git clone https://github.com/cardano-foundation/cardano-rosetta-java
4242
cd cardano-rosetta-java
4343
docker build -t rosetta-java -f ./docker/Dockerfile .
44-
docker run --name rosetta --env-file ./docker/.env.dockerfile -p 8082:8082 -d rosetta-java:latest
44+
docker run --name rosetta -v {CUSTOM_MOUNT_PATH}:/node --env-file ./docker/.env.dockerfile -p 8082:8082 -d rosetta-java:latest
4545
```
4646
Detailed explanation can be found in the [Wiki](https://github.com/cardano-foundation/cardano-rosetta-java/wiki/3.-Getting-Started-with-Docker).
4747

api/src/main/java/org/cardanofoundation/rosetta/api/block/model/domain/ProtocolParams.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010

1111
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1212
import com.fasterxml.jackson.annotation.JsonInclude;
13+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
14+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
1315

1416
@Data
1517
@NoArgsConstructor
1618
@AllArgsConstructor
1719
@Builder
1820
@JsonIgnoreProperties(ignoreUnknown = true)
1921
@JsonInclude(JsonInclude.Include.NON_NULL)
22+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
2023
public class ProtocolParams {
2124

2225
private Integer minFeeA; //0
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.cardanofoundation.rosetta.api.block.model.entity;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.Id;
6+
import jakarta.persistence.Table;
7+
8+
import lombok.AllArgsConstructor;
9+
import lombok.Builder;
10+
import lombok.Data;
11+
import lombok.NoArgsConstructor;
12+
13+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
14+
import com.fasterxml.jackson.annotation.JsonInclude;
15+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
16+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
17+
import io.hypersistence.utils.hibernate.type.json.JsonType;
18+
import org.hibernate.annotations.Type;
19+
20+
import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
21+
22+
@Data
23+
@NoArgsConstructor
24+
@AllArgsConstructor
25+
@Builder
26+
@Entity
27+
@Table(name = "local_epoch_param")
28+
@JsonIgnoreProperties(ignoreUnknown = true)
29+
@JsonInclude(JsonInclude.Include.NON_NULL)
30+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
31+
public class LocalProtocolParamsEntity {
32+
@Id
33+
private Long epoch;
34+
35+
@Type(JsonType.class)
36+
@Column(name = "params")
37+
private ProtocolParams protocolParams;
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.cardanofoundation.rosetta.api.block.model.repository;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
7+
8+
import org.cardanofoundation.rosetta.api.block.model.entity.LocalProtocolParamsEntity;
9+
10+
public interface LocalProtocolParamsRepository extends JpaRepository<LocalProtocolParamsEntity, Long> {
11+
12+
@Query(value = """
13+
SELECT p FROM LocalProtocolParamsEntity p ORDER BY p.epoch DESC LIMIT 1
14+
"""
15+
)
16+
Optional<LocalProtocolParamsEntity> getLocalProtocolParams();
17+
}

api/src/main/java/org/cardanofoundation/rosetta/common/services/ProtocolParamServiceImpl.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.cardanofoundation.rosetta.common.services;
22

3+
import java.util.Optional;
4+
35
import lombok.RequiredArgsConstructor;
46
import lombok.extern.slf4j.Slf4j;
57

@@ -11,28 +13,35 @@
1113
import org.springframework.transaction.annotation.Transactional;
1214

1315
import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
16+
import org.cardanofoundation.rosetta.api.block.model.entity.LocalProtocolParamsEntity;
1417
import org.cardanofoundation.rosetta.api.block.model.entity.ProtocolParamsEntity;
1518
import org.cardanofoundation.rosetta.api.block.model.repository.EpochParamRepository;
19+
import org.cardanofoundation.rosetta.api.block.model.repository.LocalProtocolParamsRepository;
1620
import org.cardanofoundation.rosetta.common.mapper.ProtocolParamsMapper;
1721

1822
@Service
1923
@Slf4j
2024
@RequiredArgsConstructor
2125
public class ProtocolParamServiceImpl implements ProtocolParamService {
2226

23-
private final EpochParamRepository epochParamRepository;
27+
private final LocalProtocolParamsRepository localProtocolParamsRepository;
2428
private final ProtocolParamsMapper mapperProtocolParams;
29+
private final EpochParamRepository epochParamRepository;
2530

2631
@Override
2732
@Cacheable(value = "protocolParamsCache")
2833
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
2934
public ProtocolParams findProtocolParametersFromIndexer() {
3035
log.info("Fetching protocol parameters from the indexer");
31-
ProtocolParamsEntity paramsEntity = epochParamRepository.findLatestProtocolParams();
32-
ProtocolParams protocolParams = mapperProtocolParams.mapProtocolParamsToEntity(paramsEntity);
36+
Optional<LocalProtocolParamsEntity> protocolParams = localProtocolParamsRepository.getLocalProtocolParams();
3337
log.debug("Protocol parameters fetched from the indexer: {} \nand saved in cachedProtocolParams",
34-
paramsEntity);
35-
return protocolParams;
38+
protocolParams);
39+
if(protocolParams.isEmpty()) {
40+
ProtocolParamsEntity paramsEntity = epochParamRepository.findLatestProtocolParams();
41+
return mapperProtocolParams.mapProtocolParamsToEntity(paramsEntity);
42+
} else {
43+
return protocolParams.get().getProtocolParams();
44+
}
3645
}
3746

3847
@Scheduled(fixedRate = 3600000) // 1 hour

api/src/main/resources/config/application.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ server:
66

77
spring:
88
profiles:
9-
active: ${API_SPRING_PROFILES_ACTIVE:staging, standalone}
9+
active: ${API_SPRING_PROFILES_ACTIVE:dev}
1010
jackson:
1111
default-property-inclusion: NON_NULL
1212
serialization:

api/src/test/java/org/cardanofoundation/rosetta/api/data/account/AccountBalanceApiTest.java renamed to api/src/test/java/org/cardanofoundation/rosetta/api/account/controller/AccountBalanceApiTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.cardanofoundation.rosetta.api.data.account;
1+
package org.cardanofoundation.rosetta.api.account.controller;
22

33
import org.springframework.http.MediaType;
44
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@@ -35,7 +35,6 @@ class AccountBalanceApiTest extends BaseSpringMvcSetup {
3535

3636
private final String currentAdaBalance = "3636394";
3737
private final String previousAdaBalance = "1636394";
38-
private final String currentLovelaceBalance = "1939500";
3938

4039
@Test
4140
void accountBalance2Ada_Test() {
@@ -71,7 +70,7 @@ void accountBalanceMintedTokenAndEmptyName_Test() {
7170
accountBalanceResponse.getBalances().get(1).getValue());
7271
assertNotEquals(accountBalanceResponse.getBalances().getFirst().getCurrency().getSymbol(),
7372
accountBalanceResponse.getBalances().get(1).getCurrency().getSymbol());
74-
assertEquals("", accountBalanceResponse.getBalances().get(2).getCurrency().getSymbol());
73+
assertEquals("", accountBalanceResponse.getBalances().get(1).getCurrency().getSymbol());
7574
}
7675

7776
@Test

api/src/test/java/org/cardanofoundation/rosetta/api/data/account/AccountCoinsApiTest.java renamed to api/src/test/java/org/cardanofoundation/rosetta/api/account/controller/AccountCoinsApiTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.cardanofoundation.rosetta.api.data.account;
1+
package org.cardanofoundation.rosetta.api.account.controller;
22

33
import java.util.Arrays;
44
import java.util.List;
@@ -36,7 +36,7 @@
3636

3737
class AccountCoinsApiTest extends BaseSpringMvcSetup {
3838

39-
private final String myAssetPolicyId = "7fb3df13910c057bd9254e847c076fb02de78503b9fa0ecdd70b566c";
39+
private final String myAssetPolicyId = "d97e36383ae494e72b736ace04080f2953934626376ee06cf84adeb4";
4040
private final String latestTxHashOnZeroSlot = generatedDataMap.get(
4141
TestTransactionNames.SIMPLE_NEW_EMPTY_NAME_COINS_TRANSACTION.getName()).txHash() + ":0";
4242
private final String expectedTestAccountCoinAmount = "1636394";

api/src/test/java/org/cardanofoundation/rosetta/api/construction/service/MetadataApiTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void combineWithMetadataTest() throws IOException {
4343
assertEquals(ADA, constructionMetadataResponse.getSuggestedFee().getFirst().getCurrency().getSymbol());
4444
assertEquals(ADA_DECIMALS, constructionMetadataResponse.getSuggestedFee().getFirst().getCurrency().getDecimals());
4545

46-
assertEquals(BigDecimal.valueOf(68), constructionMetadataResponse.getMetadata().getTtl());
46+
assertEquals(BigDecimal.valueOf(79), constructionMetadataResponse.getMetadata().getTtl());
4747
assertEquals("4310", constructionMetadataResponse.getMetadata().getProtocolParameters().getCoinsPerUtxoSize());
4848
assertEquals("2000000", constructionMetadataResponse.getMetadata().getProtocolParameters().getKeyDeposit());
4949
assertEquals("500000000", constructionMetadataResponse.getMetadata().getProtocolParameters().getPoolDeposit());

api/src/test/java/org/cardanofoundation/rosetta/common/cache/ProtocolParamsCacheTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.cardanofoundation.rosetta.common.cache;
22

3+
import java.util.Optional;
4+
35
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.boot.test.context.SpringBootTest;
57
import org.springframework.boot.test.mock.mockito.MockBean;
@@ -12,8 +14,10 @@
1214
import org.junit.jupiter.api.Test;
1315

1416
import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
17+
import org.cardanofoundation.rosetta.api.block.model.entity.LocalProtocolParamsEntity;
1518
import org.cardanofoundation.rosetta.api.block.model.entity.ProtocolParamsEntity;
1619
import org.cardanofoundation.rosetta.api.block.model.repository.EpochParamRepository;
20+
import org.cardanofoundation.rosetta.api.block.model.repository.LocalProtocolParamsRepository;
1721
import org.cardanofoundation.rosetta.common.mapper.ProtocolParamsMapper;
1822
import org.cardanofoundation.rosetta.common.services.ProtocolParamServiceImpl;
1923

@@ -32,6 +36,8 @@ class ProtocolParamsCacheTest {
3236

3337
@MockBean
3438
private EpochParamRepository epochParamRepository;
39+
@MockBean
40+
private LocalProtocolParamsRepository localProtocolParamsRepository;
3541

3642
@MockBean
3743
private ProtocolParamsMapper protocolParamsToEntity;
@@ -56,16 +62,14 @@ void protocolParamsCacheTest() {
5662
paramsEntity.setMinFeeA(1);
5763
ProtocolParams protocolParams = new ProtocolParams();
5864
protocolParams.setMinFeeA(1);
59-
60-
when(epochParamRepository.findLatestProtocolParams()).thenReturn(paramsEntity);
61-
when(protocolParamsToEntity.mapProtocolParamsToEntity(paramsEntity)).thenReturn(protocolParams);
65+
LocalProtocolParamsEntity localProtocolParamsEntity = LocalProtocolParamsEntity.builder().protocolParams(protocolParams).build();
66+
when(localProtocolParamsRepository.getLocalProtocolParams()).thenReturn(Optional.of(localProtocolParamsEntity));
6267

6368
ProtocolParams result1 = genesisService.findProtocolParametersFromIndexer();
6469
ProtocolParams result2 = genesisService.findProtocolParametersFromIndexer();
6570

6671
// Assert that the repository & mapper method is only called once
67-
verify(epochParamRepository, times(1)).findLatestProtocolParams();
68-
verify(protocolParamsToEntity, times(1)).mapProtocolParamsToEntity(paramsEntity);
72+
verify(localProtocolParamsRepository, times(1)).getLocalProtocolParams();
6973
assertEquals(result1, result2);
7074

7175
Cache cache = cacheManager.getCache(PROTOCOL_PARAMS_CACHE);

api/src/test/java/org/cardanofoundation/rosetta/common/services/ProtocolParamServiceImplTest.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.cardanofoundation.rosetta.common.services;
22

3+
import java.util.Optional;
4+
35
import org.mockito.InjectMocks;
46
import org.mockito.Mock;
57
import org.mockito.junit.jupiter.MockitoExtension;
@@ -8,9 +10,8 @@
810
import org.junit.jupiter.api.extension.ExtendWith;
911

1012
import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
11-
import org.cardanofoundation.rosetta.api.block.model.entity.ProtocolParamsEntity;
12-
import org.cardanofoundation.rosetta.api.block.model.repository.EpochParamRepository;
13-
import org.cardanofoundation.rosetta.common.mapper.ProtocolParamsMapper;
13+
import org.cardanofoundation.rosetta.api.block.model.entity.LocalProtocolParamsEntity;
14+
import org.cardanofoundation.rosetta.api.block.model.repository.LocalProtocolParamsRepository;
1415

1516
import static org.junit.jupiter.api.Assertions.assertNotNull;
1617
import static org.mockito.Mockito.when;
@@ -19,24 +20,19 @@
1920
class ProtocolParamServiceImplTest {
2021

2122
@Mock
22-
EpochParamRepository epochParamRepository;
23-
@Mock
24-
ProtocolParamsMapper protocolParamsMapper;
23+
LocalProtocolParamsRepository protocolParamsRepository;
2524
@InjectMocks
2625
ProtocolParamServiceImpl genesisService;
2726

28-
@Test
29-
void getProtocolParameters() {
30-
when(genesisService.findProtocolParametersFromIndexer()).thenReturn(new ProtocolParams());
31-
assertNotNull(genesisService.findProtocolParametersFromIndexer());
32-
}
33-
3427
@Test
3528
void findProtocolParametersFromIndexerTest() {
3629
//given
37-
ProtocolParamsEntity protocolParams = new ProtocolParamsEntity();
38-
when(epochParamRepository.findLatestProtocolParams()).thenReturn(protocolParams);
39-
when(protocolParamsMapper.mapProtocolParamsToEntity(protocolParams)).thenReturn(new ProtocolParams());
30+
when(protocolParamsRepository.getLocalProtocolParams()).thenReturn(Optional.of(
31+
LocalProtocolParamsEntity.builder()
32+
.epoch(5L)
33+
.protocolParams(
34+
ProtocolParams.builder().build())
35+
.build()));
4036
//when
4137
ProtocolParams protocolParameters = genesisService.findProtocolParametersFromIndexer();
4238
//then

api/src/test/resources/config/application-test-integration.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ spring:
1616
max-request-size: 16MB
1717
datasource:
1818
driver-class-name: org.h2.Driver
19-
username: ${DB_USER:rosetta_db_admin}
20-
password: ${DB_SECRET:weakpwd#123_d}
19+
username: rosetta_db_admin
20+
password: weakpwd#123_d
2121
url: jdbc:h2:file:../testData/devkit.db
2222
# url: jdbc:h2:mem:${DB_NAME:rosetta-java};INIT=RUNSCRIPT FROM 'classpath:init.sql;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH
2323
sql:

config/mainnet/config.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33
"AlonzoGenesisHash": "7e94a15f55d1e82d10f09203fa1d40f8eede58fd8066542cf6566008068ed874",
44
"ByronGenesisFile": "byron-genesis.json",
55
"ByronGenesisHash": "5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb",
6-
"ConwayGenesisFile": "conway-genesis.json",
7-
"ConwayGenesisHash": "de609b281cb3d8ae91a9d63a00c87092975612d603aa54c0f1c6a781e33d6e1e",
86
"EnableP2P": true,
97
"LastKnownBlockVersion-Alt": 0,
108
"LastKnownBlockVersion-Major": 3,
119
"LastKnownBlockVersion-Minor": 0,
1210
"MaxKnownMajorProtocolVersion": 2,
13-
"MinNodeVersion": "8.9.2",
11+
"MinNodeVersion": "8.12.0",
1412
"PeerSharing": true,
1513
"Protocol": "Cardano",
1614
"RequiresNetworkMagic": "RequiresNoMagic",
1715
"ShelleyGenesisFile": "shelley-genesis.json",
1816
"ShelleyGenesisHash": "1a3be38bcbb7911969283716ad7aa550250226b76a61fc51cc9a9a35d9276d81",
1917
"TargetNumberOfActivePeers": 20,
2018
"TargetNumberOfEstablishedPeers": 50,
21-
"TargetNumberOfKnownPeers": 100,
19+
"TargetNumberOfKnownPeers": 150,
2220
"TargetNumberOfRootPeers": 60,
2321
"TraceAcceptPolicy": true,
2422
"TraceBlockFetchClient": false,

config/mainnet/topology.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
}
2929
],
3030
"useLedgerAfterSlot": 116812831
31-
}
31+
}

0 commit comments

Comments
 (0)