Skip to content

Commit

Permalink
Refactor clearSvpValues to make code more functional
Browse files Browse the repository at this point in the history
  • Loading branch information
julia-zack committed Oct 22, 2024
1 parent 92328e3 commit c2688c4
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -1005,43 +1005,38 @@ protected void processValidationFailure(Federation proposedFederation) {
private void clearSvpValues() {
federationSupport.clearProposedFederation();

String methodName = "[clearSvpValues]";

Optional<Sha256Hash> svpFundTxHashUnsigned = provider.getSvpFundTxHashUnsigned();
if (svpFundTxHashUnsigned.isPresent()) {
logger.warn("{} Fund tx {} was never registered.", methodName, svpFundTxHashUnsigned.get());
provider.setSvpFundTxHashUnsigned(null);
return;
}

Optional<BtcTransaction> svpFundTxSigned = provider.getSvpFundTxSigned();
if (svpFundTxSigned.isPresent()) {
logger.warn("{} Spend tx was never created. Fund tx hash: {}", methodName, svpFundTxSigned.get().getHash());
provider.setSvpFundTxSigned(null);
return;
}
provider.getSvpFundTxHashUnsigned().ifPresent(
svpFundTxHashUnsigned -> {
logger.warn("[clearSvpValues] Fund tx change {} was never registered.", svpFundTxHashUnsigned);
provider.setSvpFundTxHashUnsigned(null);
}
);

Optional<Map.Entry<Keccak256, BtcTransaction>> svpSpendTxWFSOpt = provider.getSvpSpendTxWaitingForSignatures();
if (svpSpendTxWFSOpt.isPresent()) {
Map.Entry<Keccak256, BtcTransaction> svpSpendTxWFS = svpSpendTxWFSOpt.get();
Keccak256 rskCreationHash = svpSpendTxWFS.getKey();
BtcTransaction svpSpendTx = svpSpendTxWFS.getValue();
provider.getSvpFundTxSigned().ifPresent(
svpFundTxSigned -> {
logger.warn("[clearSvpValues] Spend tx was never created. Fund tx hash: {}", svpFundTxSigned.getHash());
provider.setSvpFundTxSigned(null);
}
);

logger.warn("{} Spend tx {} was not fully signed. Rsk creation hash: {}.",
methodName, svpSpendTx.getHash(), rskCreationHash);
provider.setSvpSpendTxWaitingForSignatures(null);
provider.setSvpSpendTxHashUnsigned(null);
return;
}
provider.getSvpSpendTxWaitingForSignatures().ifPresent(
svpSpendTxWFS -> {
Keccak256 rskCreationHash = svpSpendTxWFS.getKey();
BtcTransaction svpSpendTx = svpSpendTxWFS.getValue();

Optional<Sha256Hash> svpSpendTxHashUnsigned = provider.getSvpSpendTxHashUnsigned();
if (svpSpendTxHashUnsigned.isPresent()) {
logger.warn("{} Spend tx {} was not registered.", methodName, svpSpendTxHashUnsigned.get());
provider.setSvpSpendTxHashUnsigned(null);
return;
}
logger.warn("[clearSvpValues] Spend tx {} was not fully signed. Rsk creation hash: {}.",
svpSpendTx.getHash(), rskCreationHash);
provider.setSvpSpendTxWaitingForSignatures(null);
provider.setSvpSpendTxHashUnsigned(null);
}
);

logger.error("{} All SVP values were already clear, so validation should have been successful.", methodName);
provider.getSvpSpendTxHashUnsigned().ifPresent(
svpSpendTxHashUnsigned -> {
logger.warn("[clearSvpValues] Spend tx {} was not registered.", svpSpendTxHashUnsigned);
provider.setSvpSpendTxHashUnsigned(null);
}
);
}

private boolean svpIsOngoing() {
Expand Down

0 comments on commit c2688c4

Please sign in to comment.