Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: a legacy pegin transaction below the minimum should fail and th… #2905

Open
wants to merge 1 commit into
base: pegin-it-differentSenders
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<UTXO> 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<Long> heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(btcTransaction.getHash());
assertFalse(heightIfBtcTxHashIsAlreadyProcessed.isPresent());
}

@Test
void registerBtcTransaction_whenLegacyPeginBtcTransactionFromAMultiSig_shouldRefundTheFunds() throws Exception {
// Arrange
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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<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
Loading