diff --git a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java index f8c0ef345e..3df3d65152 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -179,6 +179,33 @@ void registerBtcTransaction_whenLegacyBtcTransactionWithNegativeHeight_shouldNot assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); } + @Test + void registerBtcTransaction_whenLegacyBtcTransactionWithBalanceBelowMinimum_shouldNotRefundFunds() throws Exception { + // Arrange + Coin valueBelowMinimumPegin = minimumPeginValue.subtract(Coin.SATOSHI); + BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), valueBelowMinimumPegin, btcPublicKey); + PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); + int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); + int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); + recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, btcNetworkParams); + bridgeStorageProvider.save(); + + co.rsk.core.Coin expectedReceiverBalance = repository.getBalance(rskReceiver); + List expectedFederationUTXOs = List.copyOf(federationSupport.getActiveFederationBtcUTXOs()); + + // Act + bridgeSupport.registerBtcTransaction(rskTx, btcTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); + bridgeSupport.save(); + + // Assert + assertEquals(expectedFederationUTXOs, federationSupport.getActiveFederationBtcUTXOs()); + assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); + + assertRejectedPeginTransaction(btcTransaction, BridgeEvents.UNREFUNDABLE_PEGIN.getEvent(), RejectedPeginReason.INVALID_AMOUNT.getValue()); + Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(btcTransaction.getHash()); + assertFalse(heightIfBtcTxHashIsAlreadyProcessed.isPresent()); + } + @Test void registerBtcTransaction_whenLegacyPeginBtcTransactionFromAMultiSig_shouldRefundTheFunds() throws Exception { // Arrange @@ -219,7 +246,7 @@ void registerBtcTransaction_whenLegacyPeginBtcTransactionFromAMultiSig_shouldRef Address pegoutReceiver = pegOutOutput.getAddressFromP2SH(btcNetworkParams); assertEquals(pegInTxSender, pegoutReceiver); - assertRejectedPeginTransaction(btcTransaction); + assertRejectedPeginTransaction(btcTransaction, BridgeEvents.REJECTED_PEGIN.getEvent(), RejectedPeginReason.LEGACY_PEGIN_MULTISIG_SENDER.getValue()); assertReleaseBtcRequested(rskTx.getHash().getBytes(), pegOut, minimumPeginValue); assertPegoutTransactionCreated(pegOut.getHash(), UtxoUtils.extractOutpointValues(pegOut)); @@ -279,11 +306,10 @@ private void assertLogPegInBtc(BtcTransaction btcTransaction) { assertEventWasEmittedWithExpectedData(encodedData, logs); } - private void assertRejectedPeginTransaction(BtcTransaction btcTransaction) { - CallTransaction.Function rejectedPeginEvent = BridgeEvents.REJECTED_PEGIN.getEvent(); + private void assertRejectedPeginTransaction(BtcTransaction btcTransaction, CallTransaction.Function rejectionEvent, int rejectionReason) { Sha256Hash peginTransactionHash = btcTransaction.getHash(); - List encodedTopics = getEncodedTopics(rejectedPeginEvent, peginTransactionHash.getBytes()); - byte[] encodedData = getEncodedData(rejectedPeginEvent, RejectedPeginReason.LEGACY_PEGIN_MULTISIG_SENDER.getValue()); + List encodedTopics = getEncodedTopics(rejectionEvent, peginTransactionHash.getBytes()); + byte[] encodedData = getEncodedData(rejectionEvent, rejectionReason); assertEventWasEmittedWithExpectedTopics(encodedTopics, logs); assertEventWasEmittedWithExpectedData(encodedData, logs);