Skip to content

Commit

Permalink
feat: a legacy pegin transaction below the minimum should fail and th…
Browse files Browse the repository at this point in the history
…e funds not refunded
  • Loading branch information
julianlen committed Dec 26, 2024
1 parent c9394d9 commit a0d4adc
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,30 @@ void registerBtcTransaction_whenLegacyBtcTransactionWithNegativeHeight_shouldNot
assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver));
}

@Test
void registerBtcTransaction_whenLegacyBtcTransactionWithBalanceBelowMinimum_shouldNotRefundFunds() throws Exception {
// Arrange
Coin valueBelowMinimumPegin = minimumPeginValue.subtract(Coin.SATOSHI);
BtcTransaction bitcoinTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), valueBelowMinimumPegin, btcPublicKey);
setupChainWithBtcTransaction(bitcoinTransaction);
bridgeStorageProvider.save();

co.rsk.core.Coin expectedReceiverBalance = repository.getBalance(rskReceiver);
List<UTXO> expectedFederationUTXOs = List.copyOf(federationSupport.getActiveFederationBtcUTXOs());

// Act
bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize());
bridgeSupport.save();

// Assert
assertEquals(expectedFederationUTXOs, federationSupport.getActiveFederationBtcUTXOs());
assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver));

assertRejectedPeginTransaction(bitcoinTransaction, BridgeEvents.UNREFUNDABLE_PEGIN.getEvent(), RejectedPeginReason.INVALID_AMOUNT.getValue());
Optional<Long> heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(bitcoinTransaction.getHash());
assertFalse(heightIfBtcTxHashIsAlreadyProcessed.isPresent());
}

@Test
void registerBtcTransaction_whenLegacyBtcTransactionFromAMultiSig_shouldRefundTheFunds() throws Exception {
// Arrange
Expand Down Expand Up @@ -209,7 +233,7 @@ void registerBtcTransaction_whenLegacyBtcTransactionFromAMultiSig_shouldRefundTh
Address pegoutReceiver = pegOutOutput.getAddressFromP2SH(btcNetworkParams);
assertEquals(pegInTxSender, pegoutReceiver);

assertRejectedPeginTransaction(bitcoinTransaction);
assertRejectedPeginTransaction(bitcoinTransaction, BridgeEvents.REJECTED_PEGIN.getEvent(), RejectedPeginReason.LEGACY_PEGIN_MULTISIG_SENDER.getValue());
assertReleaseBtcRequested(rskTx.getHash().getBytes(), pegOut, minimumPeginValue);
assertPegoutTransactionCreated(pegOut.getHash(), UtxoUtils.extractOutpointValues(pegOut));

Expand Down Expand Up @@ -275,11 +299,10 @@ private void assertLogPegInBtc(BtcTransaction bitcoinTransaction) {
assertEventWasEmittedWithExpectedData(encodedData, logs);
}

private void assertRejectedPeginTransaction(BtcTransaction bitcoinTransaction) {
CallTransaction.Function rejectedPeginEvent = BridgeEvents.REJECTED_PEGIN.getEvent();
private void assertRejectedPeginTransaction(BtcTransaction bitcoinTransaction, CallTransaction.Function rejectionEvent, int rejectionReason) {
Sha256Hash peginTransactionHash = bitcoinTransaction.getHash();
List<DataWord> encodedTopics = getEncodedTopics(rejectedPeginEvent, peginTransactionHash.getBytes());
byte[] encodedData = getEncodedData(rejectedPeginEvent, RejectedPeginReason.LEGACY_PEGIN_MULTISIG_SENDER.getValue());
List<DataWord> encodedTopics = getEncodedTopics(rejectionEvent, peginTransactionHash.getBytes());
byte[] encodedData = getEncodedData(rejectionEvent, rejectionReason);

assertEventWasEmittedWithExpectedTopics(encodedTopics, logs);
assertEventWasEmittedWithExpectedData(encodedData, logs);
Expand Down

0 comments on commit a0d4adc

Please sign in to comment.