From efabbb1c182f918101dbebe0824f9f3bdd9be6c6 Mon Sep 17 00:00:00 2001 From: julia-zack Date: Thu, 7 Nov 2024 10:41:34 -0300 Subject: [PATCH] Make updateSvpState private. Call it from updateCollections --- .../main/java/co/rsk/peg/BridgeSupport.java | 4 +- .../java/co/rsk/peg/BridgeSupportSvpTest.java | 41 +++++++++++-------- 2 files changed, 26 insertions(+), 19 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 ef472d0bf2..d06e74e82f 100644 --- a/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java +++ b/rskj-core/src/main/java/co/rsk/peg/BridgeSupport.java @@ -1010,6 +1010,8 @@ public void updateCollections(Transaction rskTx) throws IOException { processConfirmedPegouts(rskTx); updateFederationCreationBlockHeights(); + + updateSvpState(rskTx); } private void logUpdateCollections(Transaction rskTx) { @@ -1017,7 +1019,7 @@ private void logUpdateCollections(Transaction rskTx) { eventLogger.logUpdateCollections(sender); } - protected void updateSvpState(Transaction rskTx) { + private void updateSvpState(Transaction rskTx) { Optional proposedFederationOpt = federationSupport.getProposedFederation(); if (proposedFederationOpt.isEmpty()) { logger.debug("[updateSvpState] Proposed federation does not exist, so there's no svp going on."); 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 016e021e49..fe4be8d2d8 100644 --- a/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java +++ b/rskj-core/src/test/java/co/rsk/peg/BridgeSupportSvpTest.java @@ -44,6 +44,7 @@ import static co.rsk.peg.bitcoin.BitcoinUtils.addInputFromMatchingOutputScript; import static co.rsk.peg.bitcoin.UtxoUtils.extractOutpointValues; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -129,6 +130,10 @@ void setUp() { logs ); + ECKey key = RskTestUtils.getEcKeyFromSeed("key"); + RskAddress address = new RskAddress(key.getAddress()); + when(rskTx.getSender(any())).thenReturn(address); // to not throw when calling update collections + bridgeSupport = bridgeSupportBuilder .withBridgeConstants(bridgeMainNetConstants) .withProvider(bridgeStorageProvider) @@ -173,13 +178,13 @@ void setUp() { } @Test - void updateSvpState_whenSvpFundTxHashUnsigned_shouldLogValidationFailureAndClearValue() { + void updateCollections_whenSvpFundTxHashUnsigned_shouldLogValidationFailureAndClearValue() throws IOException { // arrange svpFundTransactionHashUnsigned = BitcoinTestUtils.createHash(1); bridgeStorageProvider.setSvpFundTxHashUnsigned(svpFundTransactionHashUnsigned); // act - bridgeSupport.updateSvpState(rskTx); + bridgeSupport.updateCollections(rskTx); // assert assertLogCommitFederationFailed(); @@ -188,13 +193,13 @@ void updateSvpState_whenSvpFundTxHashUnsigned_shouldLogValidationFailureAndClear } @Test - void updateSvpState_whenSvpFundTxSigned_shouldLogValidationFailureAndClearValue() { + void updateCollections_whenSvpFundTxSigned_shouldLogValidationFailureAndClearValue() throws IOException { // arrange svpFundTransaction = new BtcTransaction(btcMainnetParams); bridgeStorageProvider.setSvpFundTxSigned(svpFundTransaction); // act - bridgeSupport.updateSvpState(rskTx); + bridgeSupport.updateCollections(rskTx); // assert assertLogCommitFederationFailed(); @@ -203,7 +208,7 @@ void updateSvpState_whenSvpFundTxSigned_shouldLogValidationFailureAndClearValue( } @Test - void updateSvpState_whenSvpSpendTxWFS_shouldLogValidationFailureAndClearSpendTxValues() { + void updateCollections_whenSvpSpendTxWFS_shouldLogValidationFailureAndClearSpendTxValues() throws IOException { // arrange Keccak256 svpSpendTxCreationHash = RskTestUtils.createHash(1); svpSpendTransaction = new BtcTransaction(btcMainnetParams); @@ -211,7 +216,7 @@ void updateSvpState_whenSvpSpendTxWFS_shouldLogValidationFailureAndClearSpendTxV bridgeStorageProvider.setSvpSpendTxWaitingForSignatures(svpSpendTxWFS); // act - bridgeSupport.updateSvpState(rskTx); + bridgeSupport.updateCollections(rskTx); // assert assertLogCommitFederationFailed(); @@ -220,13 +225,13 @@ void updateSvpState_whenSvpSpendTxWFS_shouldLogValidationFailureAndClearSpendTxV } @Test - void updateSvpState_whenSvpSpendTxHashUnsigned_shouldLogValidationFailureAndClearValue() { + void updateCollections_whenSvpSpendTxHashUnsigned_shouldLogValidationFailureAndClearValue() throws IOException { // arrange svpSpendTransactionHashUnsigned = BitcoinTestUtils.createHash(2); bridgeStorageProvider.setSvpSpendTxHashUnsigned(svpSpendTransactionHashUnsigned); // act - bridgeSupport.updateSvpState(rskTx); + bridgeSupport.updateCollections(rskTx); // assert assertLogCommitFederationFailed(); @@ -261,34 +266,34 @@ private void assertNoSVPValues() { @Tag("Fund transaction creation and processing tests") class FundTxCreationAndProcessingTests { @Test - void updateSvpState_whenProposedFederationDoesNotExist_shouldNotCreateFundTransaction() { + void updateCollections_whenProposedFederationDoesNotExist_shouldNotCreateFundTransaction() throws IOException { // arrange when(federationSupport.getProposedFederation()).thenReturn(Optional.empty()); // act - bridgeSupport.updateSvpState(rskTx); + bridgeSupport.updateCollections(rskTx); // assert assertNoSvpFundTxHashUnsigned(); } @Test - void updateSvpState_whenThereAreNoEnoughUTXOs_shouldNotCreateFundTransaction() { + void updateCollections_whenThereAreNoEnoughUTXOs_shouldNotCreateFundTransaction() throws IOException { // arrange List insufficientUtxos = new ArrayList<>(); when(federationSupport.getActiveFederationBtcUTXOs()).thenReturn(insufficientUtxos); // act - bridgeSupport.updateSvpState(rskTx); + bridgeSupport.updateCollections(rskTx); // assert assertNoSvpFundTxHashUnsigned(); } @Test - void updateSvpState_whenFundTxCanBeCreated_createsExpectedFundTxAndSavesTheHashInStorageEntryAndPerformsPegoutActions() throws Exception { + void updateCollections_whenFundTxCanBeCreated_createsExpectedFundTxAndSavesTheHashInStorageEntryAndPerformsPegoutActions() throws Exception { // act - bridgeSupport.updateSvpState(rskTx); + bridgeSupport.updateCollections(rskTx); bridgeStorageProvider.save(); // to save the tx sig hash // assert @@ -593,9 +598,9 @@ private void assertSvpFundTransactionValuesWereUpdated() { class SpendTxCreationAndProcessingTests { @Test - void updateSvpState_whenThereIsNoFundTxSigned_shouldNotCreateNorProcessSpendTx() { + void updateCollections_whenThereIsNoFundTxSigned_shouldNotCreateNorProcessSpendTx() throws IOException { // act - bridgeSupport.updateSvpState(rskTx); + bridgeSupport.updateCollections(rskTx); bridgeStorageProvider.save(); // assert @@ -604,12 +609,12 @@ void updateSvpState_whenThereIsNoFundTxSigned_shouldNotCreateNorProcessSpendTx() } @Test - void updateSvpState_whenSpendTxCanBeCreated_createsExpectedSpendTxAndSavesTheValuesAndLogsExpectedEvents() { + void updateCollections_whenSpendTxCanBeCreated_createsExpectedSpendTxAndSavesTheValuesAndLogsExpectedEvents() throws IOException { // arrange arrangeSvpFundTransactionSigned(); // act - bridgeSupport.updateSvpState(rskTx); + bridgeSupport.updateCollections(rskTx); bridgeStorageProvider.save(); // assert