-
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.
Merge pull request #8 from cardano-foundation/feat/slot-to-epoch-conv…
…ersion Slot to Epoch, and date to slot conversion
- Loading branch information
Showing
12 changed files
with
233 additions
and
158 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
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
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
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
76 changes: 37 additions & 39 deletions
76
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 |
---|---|---|
@@ -1,64 +1,62 @@ | ||
package org.cardanofoundation.conversions.converters; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
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(); | ||
} | ||
private final GenesisConfig genesisConfig; | ||
private final SlotConversions slotConversions; | ||
|
||
public long firstTheoreticalSlot(EraType eraType) { | ||
return getEraHistoryItem(eraType).firstTheoreticalSlotNo(); | ||
} | ||
public long firstRealSlot(EraType eraType) { | ||
return getEraHistoryItem(eraType).firstRealSlotNo(); | ||
} | ||
|
||
public Optional<Long> lastTheoreticalSlot(EraType eraType) { | ||
return getEraHistoryItem(eraType).lastTheoreticalSlotNo(); | ||
} | ||
public long firstTheoreticalSlot(EraType eraType) { | ||
return getEraHistoryItem(eraType).firstTheoreticalSlotNo(); | ||
} | ||
|
||
public Optional<Long> lastRealSlot(EraType eraType) { | ||
return getEraHistoryItem(eraType).lastRealSlotNo(); | ||
} | ||
public Optional<Long> lastTheoreticalSlot(EraType eraType) { | ||
return getEraHistoryItem(eraType).lastTheoreticalSlotNo(); | ||
} | ||
|
||
public LocalDateTime firstRealEraTime(EraType eraType) { | ||
var absoluteSlot = firstRealSlot(eraType); | ||
public Optional<Long> lastRealSlot(EraType eraType) { | ||
return getEraHistoryItem(eraType).lastRealSlotNo(); | ||
} | ||
|
||
return slotConversions.slotToTime(absoluteSlot); | ||
} | ||
public LocalDateTime firstRealEraTime(EraType eraType) { | ||
var absoluteSlot = firstRealSlot(eraType); | ||
|
||
public LocalDateTime firstTheoreticalEraTime(EraType eraType) { | ||
var absoluteSlot = firstTheoreticalSlot(eraType); | ||
return slotConversions.slotToTime(absoluteSlot); | ||
} | ||
|
||
return slotConversions.slotToTime(absoluteSlot); | ||
} | ||
public LocalDateTime firstTheoreticalEraTime(EraType eraType) { | ||
var absoluteSlot = firstTheoreticalSlot(eraType); | ||
|
||
public Optional<LocalDateTime> lastRealEraTime(EraType eraType) { | ||
return lastRealSlot(eraType) | ||
.map(slotConversions::slotToTime); | ||
} | ||
return slotConversions.slotToTime(absoluteSlot); | ||
} | ||
|
||
public Optional<LocalDateTime> lastTheoreticalEraTime(EraType eraType) { | ||
return lastTheoreticalSlot(eraType) | ||
.map(slotConversions::slotToTime); | ||
} | ||
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)); | ||
} | ||
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)); | ||
} | ||
} |
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
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
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
144 changes: 71 additions & 73 deletions
144
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 |
---|---|---|
@@ -1,81 +1,79 @@ | ||
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(); | ||
} | ||
import java.time.LocalDateTime; | ||
import org.cardanofoundation.conversions.ClasspathConversionsFactory; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
} | ||
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(); | ||
} | ||
} |
Oops, something went wrong.