Skip to content

Commit

Permalink
Improve switch use to handle pegins
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanieliov committed Jan 7, 2025
1 parent b2a1d48 commit 61bd76f
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,48 +504,52 @@ protected void registerPegIn(

PeginProcessAction peginProcessAction = peginEvaluationResult.getPeginProcessAction();

if (peginProcessAction == PeginProcessAction.CAN_BE_REGISTERED) {
logger.debug("[{}] Peg-in is valid, going to register", METHOD_NAME);
executePegIn(btcTx, peginInformation, totalAmount);
return;
}

// If the peg-in cannot be registered means it should be rejected
RejectedPeginReason rejectedPeginReason = peginEvaluationResult.getRejectedPeginReason()
.map(reason -> {
logger.debug("[{}] Rejected peg-in, reason {}", METHOD_NAME, reason);
eventLogger.logRejectedPegin(btcTx, reason);
return reason;
}).orElseThrow(() -> {
// This flow should never be reached. There should always be a rejected pegin reason.
String message = "Invalid state. No rejected reason was returned for an invalid pegin.";
logger.error("[{}] {}", METHOD_NAME, message);
return new IllegalStateException(message);
});

switch (peginProcessAction) {
case CAN_BE_REGISTERED -> {
logger.debug("[{}] Peg-in is valid, going to register", METHOD_NAME);
executePegIn(btcTx, peginInformation, totalAmount);
}
case CAN_BE_REFUNDED -> {
// If the peg-in cannot be registered means it should be rejected
handleRejectedPegin(btcTx,
peginEvaluationResult);
logger.debug("[{}] Refunding to address {} ", METHOD_NAME,
peginInformation.getBtcRefundAddress());
generateRejectionRelease(btcTx, peginInformation.getBtcRefundAddress(), rskTxHash,
totalAmount);
markTxAsProcessed(btcTx);
}
case CANNOT_BE_REFUNDED -> {
logger.debug("[{}] Nonrefundable transaction {}.", METHOD_NAME, btcTx.getHash());
// If the peg-in cannot be registered means it should be rejected
RejectedPeginReason rejectedPeginReason = handleRejectedPegin(btcTx,
peginEvaluationResult);

handleNonRefundablePegin(btcTx, peginInformation.getProtocolVersion(),
rejectedPeginReason);

if (!activations.isActive(RSKIP459)) {
return;
}

// Since RSKIP459, rejected peg-ins should be marked as processed
markTxAsProcessed(btcTx);
}
}
}

private RejectedPeginReason handleRejectedPegin(BtcTransaction btcTx,
PeginEvaluationResult peginEvaluationResult) {
return peginEvaluationResult.getRejectedPeginReason().map(reason -> {
logger.debug("[{handleRejectedPegin}] Rejected peg-in, reason {}", reason);
eventLogger.logRejectedPegin(btcTx, reason);
return reason;
}).orElseThrow(() -> {
// This flow should never be reached. There should always be a rejected pegin reason.
String message = "Invalid state. No rejected reason was returned for an invalid pegin.";
logger.error("[{handleRejectedPegin}] {}", message);
return new IllegalStateException(message);
});
}

private void handleNonRefundablePegin(
BtcTransaction btcTx,
int protocolVersion,
Expand Down

0 comments on commit 61bd76f

Please sign in to comment.