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

Slot to Epoch, and date to slot conversion #8

Merged
merged 7 commits into from
Jul 4, 2024
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.cardanofoundation.conversions.converters;

import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.cardanofoundation.conversions.GenesisConfig;
import org.cardanofoundation.conversions.domain.EraType;

import java.time.LocalDateTime;

@RequiredArgsConstructor
public class SlotConversions {

@@ -26,4 +27,14 @@ public LocalDateTime slotToTime(long absoluteSlot) {
// for now post byron we have 1 slot = 1 second
return genesisConfig.blockTime(EraType.Shelley, absoluteSlot);
}

public Long slotToEpoch(long absoluteSlot) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we somehow indicate that this is partial function in Java?

https://www.pgrs.net/2015/04/23/partial-function-application-in-java-8/

like if this is byron then function doesn't work right..

What do you think?


var shelleyRelativeSlot = absoluteSlot - genesisConfig.firstShelleySlot();
var numFullEpochs = shelleyRelativeSlot / genesisConfig.getShelleyEpochLength();

return genesisConfig.firstShelleyEpochNo() + numFullEpochs;

}

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

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

import java.time.LocalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.conversions.ClasspathConversionsFactory;
import org.cardanofoundation.conversions.GenesisConfig;
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.NetworkType.MAINNET;

@Slf4j
class SlotConversionsMainNetTest {

@@ -47,4 +48,28 @@ public void testSlot109090938() {
assertThat(slotConversions.slotToTime(slot))
.isEqualTo(LocalDateTime.of(2023, 11, 22, 12, 47, 9));
}

@Test
public void testFirstShelleySlot() {
var slot = 4492800L;
assertThat(slotConversions.slotToEpoch(slot)).isEqualTo(208);
}
@Test
public void testEpoch208() {
var slot = 4492801L;
assertThat(slotConversions.slotToEpoch(slot)).isEqualTo(208);
}
@Test
public void testEpoch209() {
var slot = 4492800L + genesisConfig.getShelleyEpochLength();
assertThat(slotConversions.slotToEpoch(slot)).isEqualTo(209);
}
@Test
public void testEpoch300() {
var slot = 44237054L;
assertThat(slotConversions.slotToEpoch(slot)).isEqualTo(300);
}



}
Loading