Skip to content

Commit

Permalink
feat: initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Czeladka authored and matiwinnetou committed Nov 21, 2023
1 parent 97da651 commit b42b92b
Show file tree
Hide file tree
Showing 27 changed files with 15,594 additions and 13 deletions.
35 changes: 35 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<java.version>17</java.version>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
<lombok.version>1.18.26</lombok.version>
<jackson.version>2.16.0</jackson.version>
<ccl.version>0.5.0</ccl.version>
<assertj.version>3.24.2</assertj.version>

<slf4j.version>2.0.7</slf4j.version>

Expand All @@ -36,6 +39,30 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>com.bloxbean.cardano</groupId>
<artifactId>cardano-client-crypto</artifactId>
<version>${ccl.version}</version>
</dependency>

<!-- test scope -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -50,6 +77,14 @@
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<developers>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/cardanofoundation/ConversionsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.cardanofoundation;

import java.net.URL;

public record ConversionsConfig(URL byronGenesisFile, URL shelleyGenesisFile) {}
9 changes: 0 additions & 9 deletions src/main/java/org/cardanofoundation/Example.java

This file was deleted.

This file was deleted.

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

public class ConversionRuntimeException extends RuntimeException {

public ConversionRuntimeException() {}

public ConversionRuntimeException(String message) {
super(message);
}

public ConversionRuntimeException(Throwable cause) {
super(cause);
}

public ConversionRuntimeException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,104 @@
package org.cardanofoundation.conversions;

import static org.cardanofoundation.conversions.domain.EpochOffset.END;
import static org.cardanofoundation.conversions.domain.EpochOffset.START;
import static org.cardanofoundation.conversions.domain.Era.Byron;
import static org.cardanofoundation.conversions.domain.Era.Shelley;

import java.time.LocalDateTime;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.conversions.domain.EpochOffset;
import org.cardanofoundation.conversions.domain.Era;

@AllArgsConstructor
@Slf4j
public final class EpochConversions {

private final GenesisConfig genesisConfig;

/**
* @param epochNo
* @return
*/
public long beginningOfEpochToAbsoluteSlot(int epochNo) {
return epochToAbsoluteSlot(epochNo, START);
}

/**
* @param epochNo
* @return
*/
public long endingOfEpochToAbsoluteSlot(int epochNo) {
return epochToAbsoluteSlot(epochNo, END);
}

/**
* Converts epoch to absolute slot.
*
* @param epochNo
* @param epochOffset - either epoch's start or epoch's end
* @return
*/
public long epochToAbsoluteSlot(int epochNo, EpochOffset epochOffset) {
return switch (epochOffset) {
case START -> {
if (epochNo <= genesisConfig.lastByronEpochNo()) {
yield absoluteSlotAssumingEra(Byron, epochNo, END)
- genesisConfig.slotsPerEpoch(Byron)
+ 1;
}

yield epochToAbsoluteSlot(epochNo, END) - genesisConfig.slotsPerEpoch(Shelley);
}
case END -> {
if (epochNo <= genesisConfig.lastByronEpochNo()) {
yield absoluteSlotAssumingEra(Byron, epochNo, END);
}

var lastByronSlot = genesisConfig.lastByronSlot();
var countLastByronSlot = lastByronSlot + 1;

var postByronEpochs = (epochNo - genesisConfig.lastByronEpochNo() - 1);

yield 1 + countLastByronSlot + absoluteSlotAssumingEra(Shelley, postByronEpochs, END);
}
};
}

public LocalDateTime epochToTime(int epochNo,
EpochOffset epochOffset) {
// if (epochNo < 208) {
// return genesisConfig.getByronSlotLength() * epochNo;
// }
//

return null;
}

/**
* Returns absolute slot but assuming one is in particular era.
*
* @param era
* @param epochNo
* @param epochOffset - start or end of a given epoch
* @return
*/
long absoluteSlotAssumingEra(Era era, int epochNo, EpochOffset epochOffset) {
long allSlotsPerEra = genesisConfig.slotsPerEpoch(era) * epochNo;

return switch (epochOffset) {
case START -> allSlotsPerEra;
case END -> allSlotsPerEra + ((genesisConfig.slotsPerEpoch(era)) - 1);
};
}

long firstEpochSlot(Era era, int epochNo) {
return absoluteSlotAssumingEra(era, epochNo, START);
}

long lastEpochSlot(Era era, int epochNo) {
return absoluteSlotAssumingEra(era, epochNo, END);
}
}
70 changes: 70 additions & 0 deletions src/main/java/org/cardanofoundation/conversions/EraService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.cardanofoundation.conversions;

import static org.cardanofoundation.conversions.domain.Consensus.*;
import static org.cardanofoundation.conversions.domain.LedgerProtocol.Praos;
import static org.cardanofoundation.conversions.domain.ProtocolVersion.VER_0_0;

import java.util.List;
import java.util.Optional;
import lombok.AllArgsConstructor;
import org.cardanofoundation.conversions.domain.*;

@AllArgsConstructor
public class EraService {

private final GenesisConfig genesisConfig;

public List<EraLine> all(NetworkType networkType) {
return switch (networkType) {
case MAINNET -> mainnet();
case PREPROD -> preprod();
default -> List.of();
};
}

private List<EraLine> mainnet() {
return List.of(
new EraLine(Phase.Byron, Era.Byron, 0, VER_0_0, Praos, Ouroboros_Classic),
new EraLine(Phase.Byron, Era.Byron, 3801600, ProtocolVersion.VER_1_0, Praos, Ouroboros_BFT),
new EraLine(
Phase.Shelley, Era.Shelley, 4492800, ProtocolVersion.VER_2_0, Praos, Ouroboros_Praos),
new EraLine(
Phase.Gougen, Era.Allegra, 16588800, ProtocolVersion.VER_3_0, Praos, Ouroboros_Praos),
new EraLine(
Phase.Gougen, Era.Mary, 23068800, ProtocolVersion.VER_4_0, Praos, Ouroboros_Praos),
new EraLine(
Phase.Gougen, Era.Alonzo, 39916975, ProtocolVersion.VER_5_0, Praos, Ouroboros_Praos),
new EraLine(
Phase.Gougen, Era.Alonzo, 43372972, ProtocolVersion.VER_6_0, Praos, Ouroboros_Praos),
new EraLine(
Phase.Gougen, Era.Babbage, 72316896, ProtocolVersion.VER_7_0, Praos, Ouroboros_Praos),
new EraLine(
Phase.Gougen, Era.Babbage, 84844885, ProtocolVersion.VER_8_0, Praos, Ouroboros_Praos));
}

private List<EraLine> preprod() {
return List.of(
new EraLine(Phase.Byron, Era.Byron, 0, ProtocolVersion.VER_1_0, Praos, Ouroboros_BFT),
new EraLine(
Phase.Shelley, Era.Shelley, 84242 + 1, ProtocolVersion.VER_2_0, Praos, Ouroboros_Praos),
new EraLine(
Phase.Gougen, Era.Allegra, 518360 + 1, ProtocolVersion.VER_3_0, Praos, Ouroboros_Praos),
new EraLine(
Phase.Gougen, Era.Mary, 950340 + 1, ProtocolVersion.VER_4_0, Praos, Ouroboros_Praos),
new EraLine(
Phase.Gougen, Era.Alonzo, 1382348 + 1, ProtocolVersion.VER_6_0, Praos, Ouroboros_Praos),
new EraLine(
Phase.Gougen,
Era.Babbage,
3542390 + 1,
ProtocolVersion.VER_8_0,
Praos,
Ouroboros_Praos));
}

public Optional<EraLine> findShelleyEraLine(NetworkType networkType) {
return all(networkType).stream().filter(eraLine -> eraLine.era() == Era.Shelley).findFirst();
}
}

// last byron block on pre-prod: 84242
Loading

0 comments on commit b42b92b

Please sign in to comment.