Skip to content

Commit

Permalink
Feature: improved rollback handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Czeladka committed Sep 12, 2023
1 parent 1541c50 commit f2ece3c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
package org.cardano.foundation.voting.config;

import com.bloxbean.cardano.yaci.core.common.Constants;
import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point;
import org.cardano.foundation.voting.domain.CardanoNetwork;
import org.cardano.foundation.voting.domain.ProtocolMagic;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.cardano.foundation.voting.domain.WellKnownPointWithProtocolMagic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Nullable;
import java.util.Optional;

import static com.bloxbean.cardano.yaci.core.common.Constants.*;
import static com.bloxbean.cardano.yaci.core.common.NetworkType.*;

@Configuration
@ConditionalOnProperty(name = "rollback.handling.enabled", havingValue = "true")
public class YaciConfig {

@Bean
public Point wellKnownPointForNetwork(CardanoNetwork cardanoNetwork) {
return switch(cardanoNetwork) {
case MAIN -> Constants.WELL_KNOWN_MAINNET_POINT;
case PREPROD -> Constants.WELL_KNOWN_PREPROD_POINT;
case PREVIEW -> Constants.WELL_KNOWN_PREVIEW_POINT;
case DEV -> null;
};
}

@Bean
public ProtocolMagic protocolMagic(CardanoNetwork cardanoNetwork) {
@Nullable
public WellKnownPointWithProtocolMagic wellKnownPointForNetwork(CardanoNetwork cardanoNetwork) {
return switch(cardanoNetwork) {
case MAIN -> new ProtocolMagic(MAINNET.getProtocolMagic());
case PREPROD -> new ProtocolMagic(PREPROD.getProtocolMagic());
case PREVIEW -> new ProtocolMagic(PREVIEW.getProtocolMagic());
case DEV -> new ProtocolMagic(42);
case MAIN -> new WellKnownPointWithProtocolMagic(Optional.of(WELL_KNOWN_MAINNET_POINT), MAINNET.getProtocolMagic());
case PREPROD -> new WellKnownPointWithProtocolMagic(Optional.of(WELL_KNOWN_PREPROD_POINT), PREPROD.getProtocolMagic());
case PREVIEW -> new WellKnownPointWithProtocolMagic(Optional.of(WELL_KNOWN_PREVIEW_POINT), PREVIEW.getProtocolMagic());
case DEV -> new WellKnownPointWithProtocolMagic(Optional.empty(), 42);
};
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.cardano.foundation.voting.domain;

import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point;

import java.util.Optional;

public record WellKnownPointWithProtocolMagic(Optional<Point> wellKnownPointForNetwork,
long protocolMagic) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.cardano.foundation.voting.domain.CardanoNetwork;
import org.cardano.foundation.voting.domain.ProtocolMagic;
import org.cardano.foundation.voting.domain.WellKnownPointWithProtocolMagic;
import org.cardano.foundation.voting.service.merkle_tree.VoteMerkleProofService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Slf4j
@ConditionalOnProperty(name = "rollback.handling.enabled", havingValue = "true")
public class RollbackHandler {

@Value("${cardano.node.ip}")
Expand All @@ -35,19 +33,29 @@ public class RollbackHandler {
private VoteMerkleProofService voteMerkleProofService;

@Autowired
private ProtocolMagic protocolMagic;

@Autowired
private Point wellKnownPoint;
private WellKnownPointWithProtocolMagic wellKnownPointWithProtocolMagic;

private Optional<BlockSync> blockSync = Optional.empty();

@PostConstruct
public void init() {
log.info("Starting cardano block sync on network: {}...", cardanoNetwork);

var networkMagic = protocolMagic.magic();
var blockSync = new BlockSync(cardanoNodeIp, cardanoNodePort, networkMagic, wellKnownPoint);
if (wellKnownPointWithProtocolMagic.wellKnownPointForNetwork().isEmpty()) {
log.warn("Well known point is not known. Skipping rollback handler / sync...");
return;
}

var wellKnownPoint = wellKnownPointWithProtocolMagic.wellKnownPointForNetwork().orElseThrow();

var protocolMagic = wellKnownPointWithProtocolMagic.protocolMagic();
var blockSync = startBlockSync(protocolMagic, wellKnownPoint);

this.blockSync = Optional.of(blockSync);
}

private BlockSync startBlockSync(long protocolMagic, Point wellKnownPoint) {
var blockSync = new BlockSync(cardanoNodeIp, cardanoNodePort, protocolMagic, wellKnownPoint);
blockSync.startSyncFromTip(new BlockChainDataListener() {

@Override
Expand All @@ -63,8 +71,7 @@ public void onRollback(Point point) {
}

});

this.blockSync = Optional.of(blockSync);
return blockSync;
}

@PreDestroy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.cardano.foundation.voting.handlers;

import com.bloxbean.cardano.yaci.store.metadata.domain.TxMetadataEvent;
import com.bloxbean.cardano.yaci.store.metadata.domain.TxMetadataLabel;
import lombok.extern.slf4j.Slf4j;
import org.cardano.foundation.voting.service.metadata.CustomMetadataProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

@Service
Expand Down

0 comments on commit f2ece3c

Please sign in to comment.