-
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.
- Loading branch information
1 parent
27b347c
commit 8c066e4
Showing
7 changed files
with
192 additions
and
7 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
12 changes: 7 additions & 5 deletions
12
src/main/java/org/cardanofoundation/conversions/CardanoConverters.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,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) {} |
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
53 changes: 53 additions & 0 deletions
53
src/main/java/org/cardanofoundation/conversions/converters/EraConversions.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,53 @@ | ||
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 Optional<LocalDateTime> lastRealEraTime(EraType eraType) { | ||
return lastRealSlot(eraType) | ||
.map(slotConversions::slotToTime); | ||
} | ||
|
||
private EraHistoryItem getEraHistoryItem(EraType eraType) { | ||
return genesisConfig.getEraHistory() | ||
.findFirstByEra(eraType) | ||
.orElseThrow(() -> new ConversionRuntimeException("Era details not found!, era: " + eraType)); | ||
} | ||
|
||
} |
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
81 changes: 81 additions & 0 deletions
81
src/test/java/org/cardanofoundation/conversions/converters/EraConversionsMainNetTest.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,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(); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/org/cardanofoundation/conversions/converters/EraConversionsPreProdTest.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.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); | ||
} | ||
|
||
} |