From d55617bf52a96ef327d5be1781284ea4805905e0 Mon Sep 17 00:00:00 2001 From: julia-zack Date: Thu, 21 Nov 2024 11:57:43 -0300 Subject: [PATCH] Refactor tests and improve comments --- .../peg/federation/FederationSupportImpl.java | 16 ++- .../java/co/rsk/peg/BridgeSupportSvpTest.java | 123 +++++++++++++----- .../federation/VoteFederationChangeTest.java | 60 +++++---- 3 files changed, 132 insertions(+), 67 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/peg/federation/FederationSupportImpl.java b/rskj-core/src/main/java/co/rsk/peg/federation/FederationSupportImpl.java index b07d83186f..6c6e2ae82e 100644 --- a/rskj-core/src/main/java/co/rsk/peg/federation/FederationSupportImpl.java +++ b/rskj-core/src/main/java/co/rsk/peg/federation/FederationSupportImpl.java @@ -693,7 +693,7 @@ private FederationChangeResponseCode commitPendingFederationAccordingToActivatio */ private FederationChangeResponseCode legacyCommitPendingFederation(PendingFederation currentPendingFederation, BridgeEventLogger eventLogger) { Federation newFederation = buildFederationFromPendingFederation(currentPendingFederation); - handoverFromOldToNewFederation(getActiveFederation(), newFederation); + handoverToNewFederation(newFederation); clearPendingFederationVoting(); @@ -708,14 +708,14 @@ public void commitProposedFederation() { Federation proposedFederation = provider.getProposedFederation(constants, activations) .orElseThrow(IllegalStateException::new); - handoverFromOldToNewFederation(getActiveFederation(), proposedFederation); + handoverToNewFederation(proposedFederation); clearProposedFederation(); } - private void handoverFromOldToNewFederation(Federation oldFederation, Federation newFederation) { + private void handoverToNewFederation(Federation newFederation) { moveUTXOsFromNewToOldFederation(); - setOldAndNewFederations(oldFederation, newFederation); + setOldAndNewFederations(getActiveFederation(), newFederation); if (activations.isActive(RSKIP186)) { saveLastRetiredFederationScript(); @@ -729,15 +729,17 @@ private void setOldAndNewFederations(Federation oldFederation, Federation newFed } private void moveUTXOsFromNewToOldFederation() { - List utxosToMove = new ArrayList<>(provider.getNewFederationBtcUTXOs(constants.getBtcParams(), activations)); + // since the current active fed reference will change from being 'new' to 'old', + // we have to change the UTXOs reference to match it + List activeFederationUTXOs = new ArrayList<>(provider.getNewFederationBtcUTXOs(constants.getBtcParams(), activations)); // Clear new and old federation's UTXOs provider.getNewFederationBtcUTXOs(constants.getBtcParams(), activations).clear(); List oldFederationUTXOs = provider.getOldFederationBtcUTXOs(); oldFederationUTXOs.clear(); - // Move UTXOs from the new federation into the old federation - oldFederationUTXOs.addAll(utxosToMove); + // Move UTXOs reference to the old federation + oldFederationUTXOs.addAll(activeFederationUTXOs); } /** diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java index 23e476004b..c4ef1bf58c 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java @@ -963,8 +963,9 @@ void registerBtcTransaction_whenValidationPeriodEnded_shouldNotProcessSpendTx() arrangeSvpSpendTransaction(); setUpForTransactionRegistration(svpSpendTransaction); - // act int activeFederationUtxosSizeBeforeRegisteringTx = federationSupport.getActiveFederationBtcUTXOs().size(); + + // act bridgeSupport.registerBtcTransaction( rskTx, svpSpendTransaction.bitcoinSerialize(), @@ -974,9 +975,14 @@ void registerBtcTransaction_whenValidationPeriodEnded_shouldNotProcessSpendTx() bridgeStorageProvider.save(); // assert + // tx was not registered assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx); assertTransactionWasNotProcessed(svpSpendTransaction.getHash()); + + // svp success was not processed assertSvpSpendTxHashUnsignedIsSavedInStorage(); + assertNoHandoverToNewFederation(); + assertProposedFederation(); } @Test @@ -990,8 +996,9 @@ void registerBtcTransaction_whenIsNotTheSpendTransaction_shouldNotProcessSpendTx signInputs(pegout); setUpForTransactionRegistration(pegout); - // act int activeFederationUtxosSizeBeforeRegisteringTx = federationSupport.getActiveFederationBtcUTXOs().size(); + + // act bridgeSupport.registerBtcTransaction( rskTx, pegout.bitcoinSerialize(), @@ -1001,9 +1008,16 @@ void registerBtcTransaction_whenIsNotTheSpendTransaction_shouldNotProcessSpendTx bridgeStorageProvider.save(); // assert + // pegout was registered assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1); + assertTransactionWasProcessed(pegout.getHash()); + // but spend tx was not assertTransactionWasNotProcessed(svpSpendTransaction.getHash()); + + // svp success was not processed assertSvpSpendTxHashUnsignedIsSavedInStorage(); + assertNoHandoverToNewFederation(); + assertProposedFederation(); } @Test @@ -1012,8 +1026,9 @@ void registerBtcTransaction_whenSpendTransactionHashIsNotSaved_shouldNotProcessS recreateSvpSpendTransactionUnsigned(); setUpForTransactionRegistration(svpSpendTransaction); - // act int activeFederationUtxosSizeBeforeRegisteringTx = federationSupport.getActiveFederationBtcUTXOs().size(); + + // act bridgeSupport.registerBtcTransaction( rskTx, svpSpendTransaction.bitcoinSerialize(), @@ -1023,8 +1038,17 @@ void registerBtcTransaction_whenSpendTransactionHashIsNotSaved_shouldNotProcessS bridgeStorageProvider.save(); // assert + // spend tx was not registered assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx); assertTransactionWasNotProcessed(svpSpendTransaction.getHash()); + + // svp success was not processed + assertNoHandoverToNewFederation(); + assertProposedFederation(); + } + + private void assertProposedFederation() { + assertTrue(federationSupport.getProposedFederation().isPresent()); } @Test @@ -1033,8 +1057,9 @@ void registerBtcTransaction_whenIsTheSpendTransaction_shouldProcessSpendTxAndSvp arrangeSvpSpendTransaction(); setUpForTransactionRegistration(svpSpendTransaction); + List activeFederationUtxosBeforeRegisteringTx = new ArrayList<>(federationSupport.getActiveFederationBtcUTXOs()); + // act - List activeFedUtxosBeforeRegisteringTx = new ArrayList<>(federationSupport.getActiveFederationBtcUTXOs()); bridgeSupport.registerBtcTransaction( rskTx, svpSpendTransaction.bitcoinSerialize(), @@ -1045,18 +1070,13 @@ void registerBtcTransaction_whenIsTheSpendTransaction_shouldProcessSpendTxAndSvp // assert // tx registration - assertActiveFederationUtxosSize(activeFedUtxosBeforeRegisteringTx.size() + 1); + assertActiveFederationUtxosSize(activeFederationUtxosBeforeRegisteringTx.size() + 1); assertTransactionWasProcessed(svpSpendTransaction.getHash()); - assertNoSvpSpendTxHash(); // svp success + assertNoSvpSpendTxHash(); assertNoProposedFederation(); - assertNewActiveFederationCreationBlockHeightWasSet(); - assertLastRetiredFederationScriptWasSet(); - - assertNewAndOldFederationsWereSet(); - int outputSentToActiveFedIndex = 0; TransactionOutput spendTxOutputSentToActiveFed = svpSpendTransaction.getOutput(outputSentToActiveFedIndex); UTXO utxoSentToActiveFederation = new UTXO( @@ -1067,21 +1087,43 @@ void registerBtcTransaction_whenIsTheSpendTransaction_shouldProcessSpendTxAndSvp svpSpendTransaction.isCoinBase(), spendTxOutputSentToActiveFed.getScriptPubKey() ); - activeFedUtxosBeforeRegisteringTx.add(utxoSentToActiveFederation); - assertUTXOsWereMovedFromNewToOldFederation(activeFedUtxosBeforeRegisteringTx); + activeFederationUtxosBeforeRegisteringTx.add(utxoSentToActiveFederation); + + assertHandoverToNewFederation(activeFederationUtxosBeforeRegisteringTx); } - private void assertNewActiveFederationCreationBlockHeightWasSet() { - Optional nextFederationCreationBlockHeight = federationStorageProvider.getNextFederationCreationBlockHeight(allActivations); - assertTrue(nextFederationCreationBlockHeight.isPresent()); - assertEquals(proposedFederation.getCreationBlockNumber(), nextFederationCreationBlockHeight.get()); + private void assertHandoverToNewFederation(List utxosToMove) { + assertUTXOsWereMovedFromNewToOldFederation(utxosToMove); + assertNewAndOldFederationsWereSet(); + assertLastRetiredFederationScriptWasSet(); + assertNewActiveFederationCreationBlockHeightWasSet(); } - private void assertLastRetiredFederationScriptWasSet() { - Script activeFederationMembersP2SHScript = getFederationMembersP2SHScript(allActivations, federationSupport.getActiveFederation()); - Optional