From 2818e0248c6ecb04fec355bf080ee429577b598e Mon Sep 17 00:00:00 2001 From: Julian Len Date: Thu, 26 Dec 2024 11:12:51 -0300 Subject: [PATCH] feat: testing that a tx with multiple peg ins result in one and correct peg in --- .../co/rsk/peg/RegisterBtcTransactionIT.java | 50 +++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) 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 880e2bba93..a7b238012d 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -127,7 +127,40 @@ void registerBtcTransaction_forALegacyBtcTransaction_shouldRegisterTheNewUtxoAnd co.rsk.core.Coin expectedReceiverBalance = co.rsk.core.Coin.fromBitcoin(output.getValue()); assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); - assertLogPegInBtc(bitcoinTransaction); + assertLogPegInBtc(bitcoinTransaction, minimumPeginValue.getValue()); + } + + @Test + void registerBtcTransaction_forMultipleLegacyBtcTransaction_shouldRegisterTheNewUtxosAndTransferTheRbtcBalance() throws Exception { + // Act + short numberOfOutputs = 3; + BtcTransaction bitcoinTransaction = createTransactionWithMultiplePegIns(federationSupport.getActiveFederation().getAddress(), btcPublicKey, minimumPeginValue, numberOfOutputs); + setupChainWithBtcTransaction(bitcoinTransaction); + bridgeStorageProvider.save(); + bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); + bridgeSupport.save(); + + // Assert + Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(bitcoinTransaction.getHash()); + assertTrue(heightIfBtcTxHashIsAlreadyProcessed.isPresent()); + assertEquals(RSK_EXECUTION_BLOCK_NUMBER, heightIfBtcTxHashIsAlreadyProcessed.get()); + + List expectedFederationUtxos = new ArrayList<>(); + for (short outputIndex = 0; outputIndex < numberOfOutputs; outputIndex++) { + TransactionOutput output = bitcoinTransaction.getOutput(outputIndex); + expectedFederationUtxos.add(utxoOf(bitcoinTransaction, output)); + } + + assertEquals(expectedFederationUtxos, federationSupport.getActiveFederationBtcUTXOs()); + + co.rsk.core.Coin expectedReceiverBalance = co.rsk.core.Coin.ZERO; + for (short outputIndex = 0; outputIndex < numberOfOutputs; outputIndex++) { + TransactionOutput output = bitcoinTransaction.getOutput(outputIndex); + expectedReceiverBalance = expectedReceiverBalance.add(co.rsk.core.Coin.fromBitcoin(output.getValue())); + } + + assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); + assertLogPegInBtc(bitcoinTransaction, minimumPeginValue.getValue() * numberOfOutputs); } @Test @@ -250,6 +283,17 @@ private BtcTransaction createPegInTransaction(Address federationAddress, Coin co return btcTx; } + private BtcTransaction createTransactionWithMultiplePegIns(Address federationAddress, BtcECKey pubKey, Coin value, short numberOfOutputs) { + BtcTransaction btcTx = new BtcTransaction(btcNetworkParams); + int outputIndex = 0; + int nHash = 0; + btcTx.addInput(BitcoinTestUtils.createHash(nHash), outputIndex, ScriptBuilder.createInputScript(null, pubKey)); + for (int i = 0; i < numberOfOutputs; i++) { + btcTx.addOutput(new TransactionOutput(btcNetworkParams, btcTx, value, federationAddress)); + } + return btcTx; + } + private BtcTransaction createMultiSigPegInTransaction(Address federationAddress, Coin coin) { BtcTransaction btcTx = new BtcTransaction(btcNetworkParams); btcTx.addInput( @@ -262,14 +306,14 @@ private BtcTransaction createMultiSigPegInTransaction(Address federationAddress, return btcTx; } - private void assertLogPegInBtc(BtcTransaction bitcoinTransaction) { + private void assertLogPegInBtc(BtcTransaction bitcoinTransaction, long value) { CallTransaction.Function pegInBtcEvent = BridgeEvents.PEGIN_BTC.getEvent(); Sha256Hash peginTransactionHash = bitcoinTransaction.getHash(); List encodedTopics = getEncodedTopics(pegInBtcEvent, rskReceiver.toString(), peginTransactionHash.getBytes()); int protocolVersion = 0; - byte[] encodedData = getEncodedData(pegInBtcEvent, minimumPeginValue.getValue(), protocolVersion); + byte[] encodedData = getEncodedData(pegInBtcEvent, value, protocolVersion); assertEventWasEmittedWithExpectedTopics(encodedTopics, logs); assertEventWasEmittedWithExpectedData(encodedData, logs);