From 6a3fd9ff24f3dd307aaa72dc689a4b004fa399e3 Mon Sep 17 00:00:00 2001 From: julia-zack Date: Mon, 16 Sep 2024 10:40:32 -0300 Subject: [PATCH] Add test for svp transaction without output change --- .../java/co/rsk/peg/BridgeSupportTest.java | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java index 904db71d3ff..a5c177fe3e5 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java @@ -823,7 +823,7 @@ void registerBtcTransaction_forSvpFundTransaction_whenProposedFederationDoesNotE bridgeStorageProvider.save(); // assert - assertNewUtxoWasSaved(activeFederationUtxosSizeBeforeRegisteringTx); + assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1); assertTransactionWasProcessed(svpFundTransaction.getHash()); assertSvpFundTransactionValuesWereNotUpdated(); } @@ -851,7 +851,7 @@ void registerBtcTransaction_forSvpFundTransaction_whenValidationPeriodEnded_shou bridgeStorageProvider.save(); // assert - assertNewUtxoWasSaved(activeFederationUtxosSizeBeforeRegisteringTx); + assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1); assertTransactionWasProcessed(svpFundTransaction.getHash()); assertSvpFundTransactionValuesWereNotUpdated(); } @@ -870,11 +870,29 @@ void registerBtcTransaction_forNormalPegout_whenSvpPeriodIsOngoing_shouldRegiste bridgeStorageProvider.save(); // assert - assertNewUtxoWasSaved(activeFederationUtxosSizeBeforeRegisteringTx); + assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1); assertTransactionWasProcessed(pegout.getHash()); assertSvpFundTransactionValuesWereNotUpdated(); } + @Test + void registerBtcTransaction_forSvpFundTransactionWithoutChangeOutput_whenSvpPeriodIsOngoing_shouldRegisterTransactionAndUpdateSvpFundTransactionValuesButNotSaveNewUtxos() throws Exception { + // Arrange + BtcTransaction svpFundTransaction = createAndSaveSvpFundTransactionUnsignedWithoutChangeOutput(); + signInputs(svpFundTransaction); // a transaction trying to be registered should be signed + setUpForTransactionRegistration(svpFundTransaction); + + // Act + int activeFederationUtxosSizeBeforeRegisteringTx = federationSupport.getActiveFederationBtcUTXOs().size(); + bridgeSupport.registerBtcTransaction(rskTx, svpFundTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); + bridgeStorageProvider.save(); + + // Assert + assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx); + assertTransactionWasProcessed(svpFundTransaction.getHash()); + assertSvpFundTransactionValuesWereUpdated(); + } + @Test void registerBtcTransaction_forSvpFundTransaction_whenSvpPeriodIsOngoing_shouldRegisterTransactionAndUpdateSvpFundTransactionValues() throws Exception { // Arrange @@ -888,13 +906,17 @@ void registerBtcTransaction_forSvpFundTransaction_whenSvpPeriodIsOngoing_shouldR bridgeStorageProvider.save(); // Assert - assertNewUtxoWasSaved(activeFederationUtxosSizeBeforeRegisteringTx); + assertActiveFederationUtxosSize(activeFederationUtxosSizeBeforeRegisteringTx + 1); assertTransactionWasProcessed(svpFundTransaction.getHash()); assertSvpFundTransactionValuesWereUpdated(); } - private void assertNewUtxoWasSaved(int activeFederationUtxosSizeBeforeRegisteringTx) { - assertEquals(activeFederationUtxosSizeBeforeRegisteringTx + 1, federationSupport.getActiveFederationBtcUTXOs().size()); + private void addOutputToProposedFederation(BtcTransaction svpFundTransaction) { + svpFundTransaction.addOutput(spendableValueFromProposedFederation, proposedFederation.getAddress()); + } + + private void assertActiveFederationUtxosSize(int expectedActiveFederationUtxosSize) { + assertEquals(expectedActiveFederationUtxosSize, federationSupport.getActiveFederationBtcUTXOs().size()); } private void assertTransactionWasProcessed(Sha256Hash transactionHash) throws IOException { @@ -917,10 +939,22 @@ private void assertSvpFundTransactionValuesWereUpdated() { assertFalse(bridgeStorageProvider.getSvpFundTxHashUnsigned().isPresent()); } + private BtcTransaction createAndSaveSvpFundTransactionUnsignedWithoutChangeOutput() { + BtcTransaction svpFundTransaction = new BtcTransaction(btcMainnetParams); + Sha256Hash parentTxHash = BitcoinTestUtils.createHash(1); + addInput(svpFundTransaction, parentTxHash); + addOutputToProposedFederation(svpFundTransaction); + savePegoutIndex(svpFundTransaction); + saveSvpFundTransactionHashUnsigned(svpFundTransaction.getHash()); + + return svpFundTransaction; + } + private BtcTransaction createAndSaveSvpFundTransactionUnsigned() { BtcTransaction svpFundTransaction = new BtcTransaction(btcMainnetParams); Sha256Hash parentTxHash = BitcoinTestUtils.createHash(1); addInput(svpFundTransaction, parentTxHash); + addOutputToProposedFederation(svpFundTransaction); addOutputChange(svpFundTransaction); savePegoutIndex(svpFundTransaction); saveSvpFundTransactionHashUnsigned(svpFundTransaction.getHash());