-
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 #589 from cardano-foundation/rollback-fix
fix: disable rollback handler for good
- Loading branch information
Showing
1 changed file
with
126 additions
and
126 deletions.
There are no files selected for viewing
252 changes: 126 additions & 126 deletions
252
...mitment-app/src/main/java/org/cardano/foundation/voting/service/yaci/RollbackHandler.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,126 +1,126 @@ | ||
package org.cardano.foundation.voting.service.yaci; | ||
|
||
import com.bloxbean.cardano.yaci.core.model.Block; | ||
import com.bloxbean.cardano.yaci.core.model.Era; | ||
import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point; | ||
import com.bloxbean.cardano.yaci.helper.BlockSync; | ||
import com.bloxbean.cardano.yaci.helper.listener.BlockChainDataListener; | ||
import com.bloxbean.cardano.yaci.helper.model.Transaction; | ||
import jakarta.annotation.PostConstruct; | ||
import jakarta.annotation.PreDestroy; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.cardano.foundation.voting.client.ChainFollowerClient; | ||
import org.cardano.foundation.voting.domain.ChainNetwork; | ||
import org.cardano.foundation.voting.domain.WellKnownPointWithProtocolMagic; | ||
import org.cardano.foundation.voting.service.merkle_tree.VoteCommitmentService; | ||
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.List; | ||
import java.util.Optional; | ||
|
||
@Component | ||
@Slf4j | ||
@ConditionalOnProperty(prefix = "rollback.handling", value = "enabled", havingValue = "true") | ||
public class RollbackHandler { | ||
|
||
@Value("${cardano.node.ip}") | ||
private String cardanoNodeIp; | ||
|
||
@Value("${cardano.node.port}") | ||
private int cardanoNodePort; | ||
|
||
@Autowired | ||
private ChainNetwork chainNetwork; | ||
|
||
@Autowired | ||
private ChainFollowerClient chainFollowerClient; | ||
|
||
@Autowired | ||
private VoteMerkleProofService voteMerkleProofService; | ||
|
||
@Autowired | ||
private VoteCommitmentService voteCommitmentService; | ||
|
||
@Autowired | ||
private WellKnownPointWithProtocolMagic wellKnownPointWithProtocolMagic; | ||
|
||
private Optional<BlockSync> blockSync = Optional.empty(); | ||
|
||
@PostConstruct | ||
public void init() { | ||
log.info("Starting cardano block sync on network: {}...", chainNetwork); | ||
|
||
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 | ||
public void onBlock(Era era, Block block, List<Transaction> transactions) { | ||
var headerBody = block.getHeader().getHeaderBody(); | ||
|
||
log.info("Block's slot:{}, hash:{}, blockNo:{}", headerBody.getSlot(), headerBody.getBlockHash(), headerBody.getBlockNumber()); | ||
} | ||
|
||
@Override | ||
public void onRollback(Point point) { | ||
var allCommitmentWindowOpenEventsE = chainFollowerClient.findAllCommitmentWindowOpenEvents(); | ||
|
||
if (allCommitmentWindowOpenEventsE.isEmpty()) { | ||
var issue = allCommitmentWindowOpenEventsE.swap().get(); | ||
log.warn("Failed to get eventSummaries issue: {}, will try again in some time (on next rollback)...", issue.toString()); | ||
|
||
return; | ||
} | ||
|
||
var allCommitmentWindowOpenEvents = allCommitmentWindowOpenEventsE.get(); | ||
|
||
if (allCommitmentWindowOpenEvents.isEmpty()) { | ||
log.info("No commitment window open events found. Skipping rollback handler..."); | ||
|
||
return; | ||
} | ||
|
||
var absoluteSlot = point.getSlot(); | ||
|
||
for (var eventSummary : allCommitmentWindowOpenEvents) { | ||
String eventId = eventSummary.id(); | ||
|
||
log.info("Processing rollback for eventId: {}, absoluteSlot: {}", eventId, absoluteSlot); | ||
|
||
int updatedVoteProofs = voteMerkleProofService.softDeleteAllProofsAfterSlot(eventId, absoluteSlot); | ||
|
||
log.info("Soft deleted {} vote proofs after slot: {} for eventId: {}", updatedVoteProofs, absoluteSlot, eventId); | ||
} | ||
|
||
} | ||
|
||
}); | ||
|
||
return blockSync; | ||
} | ||
|
||
@PreDestroy | ||
public void destroy() { | ||
log.info("Stopping block sync..."); | ||
|
||
blockSync.ifPresent(BlockSync::stop); | ||
} | ||
|
||
} | ||
//package org.cardano.foundation.voting.service.yaci; | ||
// | ||
//import com.bloxbean.cardano.yaci.core.model.Block; | ||
//import com.bloxbean.cardano.yaci.core.model.Era; | ||
//import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point; | ||
//import com.bloxbean.cardano.yaci.helper.BlockSync; | ||
//import com.bloxbean.cardano.yaci.helper.listener.BlockChainDataListener; | ||
//import com.bloxbean.cardano.yaci.helper.model.Transaction; | ||
//import jakarta.annotation.PostConstruct; | ||
//import jakarta.annotation.PreDestroy; | ||
//import lombok.extern.slf4j.Slf4j; | ||
//import org.cardano.foundation.voting.client.ChainFollowerClient; | ||
//import org.cardano.foundation.voting.domain.ChainNetwork; | ||
//import org.cardano.foundation.voting.domain.WellKnownPointWithProtocolMagic; | ||
//import org.cardano.foundation.voting.service.merkle_tree.VoteCommitmentService; | ||
//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.List; | ||
//import java.util.Optional; | ||
// | ||
//@Component | ||
//@Slf4j | ||
//@ConditionalOnProperty(prefix = "rollback.handling", value = "enabled", havingValue = "true") | ||
//public class RollbackHandler { | ||
// | ||
// @Value("${cardano.node.ip}") | ||
// private String cardanoNodeIp; | ||
// | ||
// @Value("${cardano.node.port}") | ||
// private int cardanoNodePort; | ||
// | ||
// @Autowired | ||
// private ChainNetwork chainNetwork; | ||
// | ||
// @Autowired | ||
// private ChainFollowerClient chainFollowerClient; | ||
// | ||
// @Autowired | ||
// private VoteMerkleProofService voteMerkleProofService; | ||
// | ||
// @Autowired | ||
// private VoteCommitmentService voteCommitmentService; | ||
// | ||
// @Autowired | ||
// private WellKnownPointWithProtocolMagic wellKnownPointWithProtocolMagic; | ||
// | ||
// private Optional<BlockSync> blockSync = Optional.empty(); | ||
// | ||
// @PostConstruct | ||
// public void init() { | ||
// log.info("Starting cardano block sync on network: {}...", chainNetwork); | ||
// | ||
// 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 | ||
// public void onBlock(Era era, Block block, List<Transaction> transactions) { | ||
// var headerBody = block.getHeader().getHeaderBody(); | ||
// | ||
// log.info("Block's slot:{}, hash:{}, blockNo:{}", headerBody.getSlot(), headerBody.getBlockHash(), headerBody.getBlockNumber()); | ||
// } | ||
// | ||
// @Override | ||
// public void onRollback(Point point) { | ||
// var allCommitmentWindowOpenEventsE = chainFollowerClient.findAllCommitmentWindowOpenEvents(); | ||
// | ||
// if (allCommitmentWindowOpenEventsE.isEmpty()) { | ||
// var issue = allCommitmentWindowOpenEventsE.swap().get(); | ||
// log.warn("Failed to get eventSummaries issue: {}, will try again in some time (on next rollback)...", issue.toString()); | ||
// | ||
// return; | ||
// } | ||
// | ||
// var allCommitmentWindowOpenEvents = allCommitmentWindowOpenEventsE.get(); | ||
// | ||
// if (allCommitmentWindowOpenEvents.isEmpty()) { | ||
// log.info("No commitment window open events found. Skipping rollback handler..."); | ||
// | ||
// return; | ||
// } | ||
// | ||
// var absoluteSlot = point.getSlot(); | ||
// | ||
// for (var eventSummary : allCommitmentWindowOpenEvents) { | ||
// String eventId = eventSummary.id(); | ||
// | ||
// log.info("Processing rollback for eventId: {}, absoluteSlot: {}", eventId, absoluteSlot); | ||
// | ||
// int updatedVoteProofs = voteMerkleProofService.softDeleteAllProofsAfterSlot(eventId, absoluteSlot); | ||
// | ||
// log.info("Soft deleted {} vote proofs after slot: {} for eventId: {}", updatedVoteProofs, absoluteSlot, eventId); | ||
// } | ||
// | ||
// } | ||
// | ||
// }); | ||
// | ||
// return blockSync; | ||
// } | ||
// | ||
// @PreDestroy | ||
// public void destroy() { | ||
// log.info("Stopping block sync..."); | ||
// | ||
// blockSync.ifPresent(BlockSync::stop); | ||
// } | ||
// | ||
//} |