From c2688c4e033bd961514b33e8cbbbf51e069958f7 Mon Sep 17 00:00:00 2001 From: julia-zack Date: Tue, 22 Oct 2024 11:01:44 -0300 Subject: [PATCH] Refactor clearSvpValues to make code more functional --- .../main/java/co/rsk/peg/BridgeSupport.java | 61 +++++++++---------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java b/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java index 48c31156a9..e8244ba6e7 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java @@ -1005,43 +1005,38 @@ protected void processValidationFailure(Federation proposedFederation) { private void clearSvpValues() { federationSupport.clearProposedFederation(); - String methodName = "[clearSvpValues]"; - - Optional svpFundTxHashUnsigned = provider.getSvpFundTxHashUnsigned(); - if (svpFundTxHashUnsigned.isPresent()) { - logger.warn("{} Fund tx {} was never registered.", methodName, svpFundTxHashUnsigned.get()); - provider.setSvpFundTxHashUnsigned(null); - return; - } - - Optional 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> svpSpendTxWFSOpt = provider.getSvpSpendTxWaitingForSignatures(); - if (svpSpendTxWFSOpt.isPresent()) { - Map.Entry 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 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() {