Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: EraConversions support #6

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ var epochStartTime = converters.epoch().beginningOfEpochToUTCTime(445);
var utcTime = converters.slot().slotToTime(109090938L);
var epochNo = converters.time().utcTimeToEpochNo(LocalDateTime.of(2023, 11, 22, 9, 48, 58));
var lastAlonzoAbsoluteSlot = converters.epoch().endingOfEpochToAbsoluteSlot(364);
var firstRealAbsoluteSlotBabbage = converters.era().firstRealSlot(EraType.Babbage);
var firstRealTimeBabbage = converters.era().firstRealEraTime(EraType.Babbage);

System.out.println(epochStartTime); // LocalDateTime.of(2023, 10, 27, 21, 44, 51)
System.out.println(utcTime); // LocalDateTime.of(2023, 11, 22, 12, 47, 9)
System.out.println(epochNo); // 450
System.out.println(lastAlonzoAbsoluteSlot); // 72316799L
System.out.println(firstRealAbsoluteSlotBabbage); // 72316896L
System.out.println(firstRealTimeBabbage); // LocalDateTime.of(2022, 9, 22, 21, 46, 27)
```

## Additional Docs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.cardanofoundation.conversions;

import org.cardanofoundation.conversions.converters.EpochConversions;
import org.cardanofoundation.conversions.converters.EraConversions;
import org.cardanofoundation.conversions.converters.SlotConversions;
import org.cardanofoundation.conversions.converters.TimeConversions;

public record CardanoConverters(
ConversionsConfig conversionsConfig,
GenesisConfig genesisConfig,
EpochConversions epoch,
SlotConversions slot,
TimeConversions time) {}
ConversionsConfig conversionsConfig,
GenesisConfig genesisConfig,
EpochConversions epoch,
SlotConversions slot,
TimeConversions time,
EraConversions era) {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import org.cardanofoundation.conversions.converters.EpochConversions;
import org.cardanofoundation.conversions.converters.EraConversions;
import org.cardanofoundation.conversions.converters.SlotConversions;
import org.cardanofoundation.conversions.converters.TimeConversions;
import org.cardanofoundation.conversions.domain.EraType;
Expand Down Expand Up @@ -45,9 +46,16 @@ public static CardanoConverters createConverters(
var slotConversions = new SlotConversions(genesisConfig);
var epochConversions = new EpochConversions(genesisConfig, slotConversions);
var timeConversions = new TimeConversions(genesisConfig, slotConversions);
var eraConversions = new EraConversions(genesisConfig, slotConversions);

return new CardanoConverters(
conversionsConfig, genesisConfig, epochConversions, slotConversions, timeConversions);
conversionsConfig,
genesisConfig,
epochConversions,
slotConversions,
timeConversions,
eraConversions
);
}

private static String getGenesisEraClasspathLink(EraType era, NetworkType networkType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.cardanofoundation.conversions.converters;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.conversions.ConversionRuntimeException;
import org.cardanofoundation.conversions.GenesisConfig;
import org.cardanofoundation.conversions.domain.EraHistoryItem;
import org.cardanofoundation.conversions.domain.EraType;

import java.time.LocalDateTime;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
public class EraConversions {

private final GenesisConfig genesisConfig;
private final SlotConversions slotConversions;

public long firstRealSlot(EraType eraType) {
return getEraHistoryItem(eraType).firstRealSlotNo();
}

public long firstTheoreticalSlot(EraType eraType) {
return getEraHistoryItem(eraType).firstTheoreticalSlotNo();
}

public Optional<Long> lastTheoreticalSlot(EraType eraType) {
return getEraHistoryItem(eraType).lastTheoreticalSlotNo();
}

public Optional<Long> lastRealSlot(EraType eraType) {
return getEraHistoryItem(eraType).lastRealSlotNo();
}

public LocalDateTime firstRealEraTime(EraType eraType) {
var absoluteSlot = firstRealSlot(eraType);

return slotConversions.slotToTime(absoluteSlot);
}

public LocalDateTime firstTheoreticalEraTime(EraType eraType) {
var absoluteSlot = firstTheoreticalSlot(eraType);

return slotConversions.slotToTime(absoluteSlot);
}

public Optional<LocalDateTime> lastRealEraTime(EraType eraType) {
return lastRealSlot(eraType)
.map(slotConversions::slotToTime);
}

public Optional<LocalDateTime> lastTheoreticalEraTime(EraType eraType) {
return lastTheoreticalSlot(eraType)
.map(slotConversions::slotToTime);
}

private EraHistoryItem getEraHistoryItem(EraType eraType) {
return genesisConfig.getEraHistory()
.findFirstByEra(eraType)
.orElseThrow(() -> new ConversionRuntimeException("Era details not found!, era: " + eraType));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.conversions.ClasspathConversionsFactory;
import org.cardanofoundation.conversions.GenesisConfig;
import org.cardanofoundation.conversions.domain.EraType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -34,7 +35,8 @@ public void testIntraByronFork() {
assertThat(epochConversions.epochToAbsoluteSlot(176, START)).isEqualTo(slot);
}

@Test

@Test
public void testConvertByronEpochToSlot() {
assertThat(epochConversions.epochToAbsoluteSlot(207, START)).isEqualTo(4471200);
assertThat(epochConversions.epochToAbsoluteSlot(207, END)).isEqualTo(4492799L);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.cardanofoundation.conversions.converters;

import org.cardanofoundation.conversions.ClasspathConversionsFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import static org.assertj.core.api.Assertions.assertThat;
import static org.cardanofoundation.conversions.domain.EraType.Babbage;
import static org.cardanofoundation.conversions.domain.EraType.Shelley;
import static org.cardanofoundation.conversions.domain.NetworkType.MAINNET;

class EraConversionsMainNetTest {
private EraConversions eraConversions;

@BeforeEach
public void setup() {
var converters = ClasspathConversionsFactory.createConverters(MAINNET);
eraConversions = converters.era();
}

@Test
void firstRealSlotShelley() {
var absoluteSlot = eraConversions.firstRealSlot(Shelley);
assertThat(absoluteSlot).isEqualTo(4492800L);
}

@Test
void firstRealSlotBabbage() {
var absoluteSlot = eraConversions.firstRealSlot(Babbage);
assertThat(absoluteSlot).isEqualTo(72316896L);
}

@Test
void firstTheoreticalSlotShelley() {
var absoluteSlot = eraConversions.firstTheoreticalSlot(Shelley);
assertThat(absoluteSlot).isEqualTo(4492800L);
}

@Test
void firstTheoreticalSlotBabbage() {
var absoluteSlot = eraConversions.firstTheoreticalSlot(Babbage);
assertThat(absoluteSlot).isEqualTo(72316800L);
}

@Test
void lastRealSlotBabbage() {
assertThat(eraConversions.lastRealEraTime(Babbage)).isEmpty();
}

@Test
void lastTheoreticalSlotShelley() {
var absoluteSlot = eraConversions.lastTheoreticalSlot(Shelley).orElseThrow();
assertThat(absoluteSlot).isEqualTo(16588799L);
}

@Test
void firstRealEraTimeShelley() {
var time = eraConversions.firstRealEraTime(Shelley);
assertThat(time).isEqualTo(LocalDateTime.of(2020, 7, 29, 21, 44, 51));
}

@Test
void lastRealEraTimeShelley() {
var time = eraConversions.lastRealEraTime(Shelley).orElseThrow();
assertThat(time).isEqualTo(LocalDateTime.of(2020, 12, 16, 21, 43, 48));
}

@Test
void firstRealEraTimeBabbage() {
var time = eraConversions.firstRealEraTime(Babbage);
assertThat(time).isEqualTo(LocalDateTime.of(2022, 9, 22, 21, 46, 27));
}

@Test
void lastRealEraTimeBabbage() {
assertThat(eraConversions.lastRealEraTime(Babbage)).isEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.cardanofoundation.conversions.converters;

import org.cardanofoundation.conversions.ClasspathConversionsFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import static org.assertj.core.api.Assertions.assertThat;
import static org.cardanofoundation.conversions.domain.EraType.Shelley;
import static org.cardanofoundation.conversions.domain.NetworkType.MAINNET;
import static org.cardanofoundation.conversions.domain.NetworkType.PREPROD;

class EraConversionsPreProdTest {
private EraConversions eraConversions;

@BeforeEach
public void setup() {
var converters = ClasspathConversionsFactory.createConverters(PREPROD);
eraConversions = converters.era();
}

@Test
void firstTheoreticalSlot() {
var shelleySlot = eraConversions.firstTheoreticalSlot(Shelley);
assertThat(shelleySlot).isEqualTo(86400L);
}

@Test
void lastTheoreticalSlot() {
var shelleySlot = eraConversions.lastTheoreticalSlot(Shelley).orElseThrow();
assertThat(shelleySlot).isEqualTo(518399L);
}

}
Loading