From 769a86eef3a77f9ac9fd0c30e9362a193b5367dc Mon Sep 17 00:00:00 2001 From: Julian Len Date: Thu, 16 Jan 2025 12:00:40 -0300 Subject: [PATCH 1/3] feat: create a test such that for multiple inputs it takes the first one to get the rsk receiver address --- .../co/rsk/peg/RegisterBtcTransactionIT.java | 91 +++++++++++++++---- 1 file changed, 72 insertions(+), 19 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 47bdc81b2c..7528b2afac 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -33,6 +33,8 @@ import org.ethereum.vm.PrecompiledContracts; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + +import java.io.IOException; import java.util.*; class RegisterBtcTransactionIT { @@ -104,7 +106,9 @@ void setUp() { @Test void registerBtcTransaction_forALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception { // Arrange - BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey); + int spendTxHashSeed = 0; + int outputIndex = 0; + BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey, spendTxHashSeed, outputIndex); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); @@ -116,12 +120,10 @@ void registerBtcTransaction_forALegacyBtcTransaction_shouldRegisterTheNewUtxoAnd bridgeSupport.save(); // Assert - Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(btcTransaction.getHash()); - assertTrue(heightIfBtcTxHashIsAlreadyProcessed.isPresent()); - assertEquals(RSK_EXECUTION_BLOCK_NUMBER, heightIfBtcTxHashIsAlreadyProcessed.get()); + assertItWasProcessed(btcTransaction); - int outputIndex = 0; - TransactionOutput output = btcTransaction.getOutput(outputIndex); + int newOutputIndex = 0; + TransactionOutput output = btcTransaction.getOutput(newOutputIndex); List expectedFederationUtxos = List.of(utxoOf(btcTransaction, output)); assertEquals(expectedFederationUtxos, federationSupport.getActiveFederationBtcUTXOs()); @@ -147,9 +149,7 @@ void registerBtcTransaction_forMultipleLegacyBtcTransaction_shouldRegisterTheNew bridgeSupport.save(); // Assert - Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(btcTransaction.getHash()); - assertTrue(heightIfBtcTxHashIsAlreadyProcessed.isPresent()); - assertEquals(RSK_EXECUTION_BLOCK_NUMBER, heightIfBtcTxHashIsAlreadyProcessed.get()); + assertItWasProcessed(btcTransaction); List expectedFederationUtxos = new ArrayList<>(); for (short outputIndex = 0; outputIndex < numberOfOutputs; outputIndex++) { @@ -202,7 +202,9 @@ void registerBtcTransaction_forMultipleLegacyBtcTransactionBelowMinimumWithSumAb @Test void registerBtcTransaction_forARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throws Exception { // Arrange - BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey); + int outputIndex = 0; + int spendTxHashSeed = 0; + BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey, spendTxHashSeed, outputIndex); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); @@ -227,7 +229,9 @@ void registerBtcTransaction_forARepeatedLegacyBtcTransaction_shouldNotPerformAny @Test void registerBtcTransaction_whenLegacyBtcTransactionWithNegativeHeight_shouldNotPerformAnyChange() throws Exception { // Arrange - BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey); + int spendTxHashSeed = 0; + int outputIndex = 0; + BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey, spendTxHashSeed, outputIndex); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); @@ -251,7 +255,9 @@ void registerBtcTransaction_whenLegacyBtcTransactionWithNegativeHeight_shouldNot void registerBtcTransaction_whenLegacyBtcTransactionWithBalanceBelowMinimum_shouldNotRefundFunds() throws Exception { // Arrange Coin valueBelowMinimumPegin = minimumPeginValue.subtract(Coin.SATOSHI); - BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), valueBelowMinimumPegin, btcPublicKey); + int spendTxHashSeed = 0; + int outputIndex = 0; + BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), valueBelowMinimumPegin, btcPublicKey, spendTxHashSeed, outputIndex); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); @@ -321,6 +327,54 @@ void registerBtcTransaction_whenLegacyPeginBtcTransactionFromAMultiSig_shouldRef assertEquals(expectedFederationUTXOs, federationSupport.getActiveFederationBtcUTXOs()); assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); + assertItWasProcessed(btcTransaction); + } + + @Test + void registerBtcTransaction_forALegacyBtcTransactionWithMultipleInputs_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception { + // Arrange + int outputIndex = 0; + int nHash = 0; + BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey, outputIndex, nHash); + + List fedKeys = BitcoinTestUtils.getBtcEcKeysFromSeeds(new String[]{"seed1", "seed2"}, true); + for (BtcECKey fedKey : fedKeys) { + nHash++; + outputIndex++; + btcTransaction.addInput(BitcoinTestUtils.createHash(nHash), outputIndex, ScriptBuilder.createInputScript(null, fedKey)); + } + + 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(); + + // Act + bridgeSupport.registerBtcTransaction(rskTx, btcTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize()); + bridgeSupport.save(); + + // Assert + assertItWasProcessed(btcTransaction); + + int newOutputIndex = 0; + TransactionOutput output = btcTransaction.getOutput(newOutputIndex); + List expectedFederationUtxos = List.of(utxoOf(btcTransaction, output)); + assertEquals(expectedFederationUtxos, federationSupport.getActiveFederationBtcUTXOs()); + + co.rsk.core.Coin expectedReceiverBalance = co.rsk.core.Coin.fromBitcoin(output.getValue()); + assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver)); + + for (BtcECKey fedKey : fedKeys) { + ECKey ecKey = ECKey.fromPublicOnly(fedKey.getPubKey()); + co.rsk.core.Coin expectedNonReceiverBalance = co.rsk.core.Coin.ZERO; + assertEquals(expectedNonReceiverBalance, repository.getBalance(new RskAddress(ecKey.getAddress()))); + } + + assertLogPegInBtc(btcTransaction, minimumPeginValue.getValue()); + } + + private void assertItWasProcessed(BtcTransaction btcTransaction) throws IOException { Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(btcTransaction.getHash()); assertTrue(heightIfBtcTxHashIsAlreadyProcessed.isPresent()); assertEquals(RSK_EXECUTION_BLOCK_NUMBER, heightIfBtcTxHashIsAlreadyProcessed.get()); @@ -338,11 +392,9 @@ private static UTXO utxoOf(BtcTransaction btcTransaction, TransactionOutput outp ); } - private BtcTransaction createPegInTransaction(Address federationAddress, Coin coin, BtcECKey pubKey) { + private BtcTransaction createPegInTransaction(Address federationAddress, Coin coin, BtcECKey pubKey, int spendTxHashSeed, int outputIndex) { BtcTransaction btcTx = new BtcTransaction(btcNetworkParams); - int outputIndex = 0; - int nHash = 0; - btcTx.addInput(BitcoinTestUtils.createHash(nHash), outputIndex, ScriptBuilder.createInputScript(null, pubKey)); + btcTx.addInput(BitcoinTestUtils.createHash(spendTxHashSeed), outputIndex, ScriptBuilder.createInputScript(null, pubKey)); btcTx.addOutput(new TransactionOutput(btcNetworkParams, btcTx, coin, federationAddress)); return btcTx; @@ -361,10 +413,11 @@ private BtcTransaction createTransactionWithMultiplePegIns(Address federationAdd private BtcTransaction createMultiSigPegInTransaction(Address federationAddress, Coin coin) { BtcTransaction btcTx = new BtcTransaction(btcNetworkParams); - + int outputIndex = 0; + int nHash = 0; btcTx.addInput( - BitcoinTestUtils.createHash(1), - 0, + BitcoinTestUtils.createHash(nHash), + outputIndex, ScriptBuilder.createP2SHMultiSigInputScript(null, federationSupport.getActiveFederation().getRedeemScript()) ); btcTx.addOutput(new TransactionOutput(btcNetworkParams, btcTx, coin, federationAddress)); From a30de8c6cef1e19074ba3b72980ec70bff4471ae Mon Sep 17 00:00:00 2001 From: Julian Len Date: Thu, 16 Jan 2025 12:16:37 -0300 Subject: [PATCH 2/3] fix: extracted the parameters to create a new btc tx input to the class instantiation --- .../co/rsk/peg/RegisterBtcTransactionIT.java | 60 ++++++++----------- 1 file changed, 24 insertions(+), 36 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 7528b2afac..61930683eb 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -40,6 +40,8 @@ class RegisterBtcTransactionIT { public static final long RSK_EXECUTION_BLOCK_NUMBER = 1000L; public static final long RSK_EXECUTION_BLOCK_TIMESTAMP = 10L; + private static int spendTxHashSeed = 0; + private static int outputIndex = 0; private final BridgeConstants bridgeConstants = BridgeMainNetConstants.getInstance(); private final NetworkParameters btcNetworkParams = bridgeConstants.getBtcParams(); private final BridgeSupportBuilder bridgeSupportBuilder = BridgeSupportBuilder.builder(); @@ -106,8 +108,6 @@ void setUp() { @Test void registerBtcTransaction_forALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception { // Arrange - int spendTxHashSeed = 0; - int outputIndex = 0; BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey, spendTxHashSeed, outputIndex); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); @@ -137,7 +137,7 @@ void registerBtcTransaction_forALegacyBtcTransaction_shouldRegisterTheNewUtxoAnd void registerBtcTransaction_forMultipleLegacyBtcTransaction_shouldRegisterTheNewUtxosAndTransferTheRbtcBalance() throws Exception { // Arrange short numberOfOutputs = 3; - BtcTransaction btcTransaction = createTransactionWithMultiplePegIns(federationSupport.getActiveFederation().getAddress(), btcPublicKey, minimumPeginValue, numberOfOutputs); + BtcTransaction btcTransaction = createTransactionWithMultiplePegIns(federationSupport.getActiveFederation().getAddress(), btcPublicKey, minimumPeginValue, numberOfOutputs, outputIndex, spendTxHashSeed); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); @@ -152,16 +152,16 @@ void registerBtcTransaction_forMultipleLegacyBtcTransaction_shouldRegisterTheNew assertItWasProcessed(btcTransaction); List expectedFederationUtxos = new ArrayList<>(); - for (short outputIndex = 0; outputIndex < numberOfOutputs; outputIndex++) { - TransactionOutput output = btcTransaction.getOutput(outputIndex); + for (short newOutputIndex = 0; newOutputIndex < numberOfOutputs; newOutputIndex++) { + TransactionOutput output = btcTransaction.getOutput(newOutputIndex); expectedFederationUtxos.add(utxoOf(btcTransaction, output)); } assertEquals(expectedFederationUtxos, federationSupport.getActiveFederationBtcUTXOs()); co.rsk.core.Coin expectedReceiverBalance = co.rsk.core.Coin.ZERO; - for (short outputIndex = 0; outputIndex < numberOfOutputs; outputIndex++) { - TransactionOutput output = btcTransaction.getOutput(outputIndex); + for (short newOutputIndex = 0; newOutputIndex < numberOfOutputs; newOutputIndex++) { + TransactionOutput output = btcTransaction.getOutput(newOutputIndex); expectedReceiverBalance = expectedReceiverBalance.add(co.rsk.core.Coin.fromBitcoin(output.getValue())); } @@ -174,7 +174,7 @@ void registerBtcTransaction_forMultipleLegacyBtcTransactionBelowMinimumWithSumAb // Arrange short numberOfOutputs = 2; Coin partOfMinimumValue = Coin.valueOf(minimumPeginValue.getValue() / numberOfOutputs); - BtcTransaction btcTransaction = createTransactionWithMultiplePegIns(federationSupport.getActiveFederation().getAddress(), btcPublicKey, partOfMinimumValue, numberOfOutputs); + BtcTransaction btcTransaction = createTransactionWithMultiplePegIns(federationSupport.getActiveFederation().getAddress(), btcPublicKey, partOfMinimumValue, numberOfOutputs, outputIndex, spendTxHashSeed); assertTrue((partOfMinimumValue.getValue() * numberOfOutputs) >= minimumPeginValue.getValue()); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); @@ -202,8 +202,6 @@ void registerBtcTransaction_forMultipleLegacyBtcTransactionBelowMinimumWithSumAb @Test void registerBtcTransaction_forARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throws Exception { // Arrange - int outputIndex = 0; - int spendTxHashSeed = 0; BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey, spendTxHashSeed, outputIndex); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); @@ -229,8 +227,6 @@ void registerBtcTransaction_forARepeatedLegacyBtcTransaction_shouldNotPerformAny @Test void registerBtcTransaction_whenLegacyBtcTransactionWithNegativeHeight_shouldNotPerformAnyChange() throws Exception { // Arrange - int spendTxHashSeed = 0; - int outputIndex = 0; BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey, spendTxHashSeed, outputIndex); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); @@ -255,8 +251,6 @@ void registerBtcTransaction_whenLegacyBtcTransactionWithNegativeHeight_shouldNot void registerBtcTransaction_whenLegacyBtcTransactionWithBalanceBelowMinimum_shouldNotRefundFunds() throws Exception { // Arrange Coin valueBelowMinimumPegin = minimumPeginValue.subtract(Coin.SATOSHI); - int spendTxHashSeed = 0; - int outputIndex = 0; BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), valueBelowMinimumPegin, btcPublicKey, spendTxHashSeed, outputIndex); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); @@ -283,7 +277,7 @@ void registerBtcTransaction_whenLegacyBtcTransactionWithBalanceBelowMinimum_shou @Test void registerBtcTransaction_whenLegacyPeginBtcTransactionFromAMultiSig_shouldRefundTheFunds() throws Exception { // Arrange - BtcTransaction btcTransaction = createMultiSigPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue); + BtcTransaction btcTransaction = createMultiSigPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, outputIndex, spendTxHashSeed); PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks(); int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(); @@ -311,8 +305,8 @@ void registerBtcTransaction_whenLegacyPeginBtcTransactionFromAMultiSig_shouldRef // Pegout value + fee == Pegin value BtcTransaction pegOut = pegOutWaitingForConfirmationEntry.getBtcTransaction(); - int outputIndex = 0; - TransactionOutput pegOutOutput = pegOut.getOutput(outputIndex); + int newOutputIndex = 0; + TransactionOutput pegOutOutput = pegOut.getOutput(newOutputIndex); Coin pegOutTotalValue = pegOutOutput.getValue().add(pegOut.getFee()); assertEquals(minimumPeginValue, pegOutTotalValue); @@ -333,15 +327,13 @@ void registerBtcTransaction_whenLegacyPeginBtcTransactionFromAMultiSig_shouldRef @Test void registerBtcTransaction_forALegacyBtcTransactionWithMultipleInputs_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception { // Arrange - int outputIndex = 0; - int nHash = 0; - BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey, outputIndex, nHash); + BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey, outputIndex, spendTxHashSeed); List fedKeys = BitcoinTestUtils.getBtcEcKeysFromSeeds(new String[]{"seed1", "seed2"}, true); for (BtcECKey fedKey : fedKeys) { - nHash++; + spendTxHashSeed++; outputIndex++; - btcTransaction.addInput(BitcoinTestUtils.createHash(nHash), outputIndex, ScriptBuilder.createInputScript(null, fedKey)); + btcTransaction.addInput(BitcoinTestUtils.createHash(spendTxHashSeed), outputIndex, ScriptBuilder.createInputScript(null, fedKey)); } PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash()), btcNetworkParams); @@ -374,12 +366,6 @@ void registerBtcTransaction_forALegacyBtcTransactionWithMultipleInputs_shouldReg assertLogPegInBtc(btcTransaction, minimumPeginValue.getValue()); } - private void assertItWasProcessed(BtcTransaction btcTransaction) throws IOException { - Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(btcTransaction.getHash()); - assertTrue(heightIfBtcTxHashIsAlreadyProcessed.isPresent()); - assertEquals(RSK_EXECUTION_BLOCK_NUMBER, heightIfBtcTxHashIsAlreadyProcessed.get()); - } - private static UTXO utxoOf(BtcTransaction btcTransaction, TransactionOutput output) { int height = 0; return new UTXO( @@ -400,23 +386,19 @@ private BtcTransaction createPegInTransaction(Address federationAddress, Coin co return btcTx; } - private BtcTransaction createTransactionWithMultiplePegIns(Address federationAddress, BtcECKey pubKey, Coin value, short numberOfOutputs) { + private BtcTransaction createTransactionWithMultiplePegIns(Address federationAddress, BtcECKey pubKey, Coin value, short numberOfOutputs, int outputIndex, int spendTxHashSeed) { BtcTransaction btcTx = new BtcTransaction(btcNetworkParams); - int outputIndex = 0; - int nHash = 0; - btcTx.addInput(BitcoinTestUtils.createHash(nHash), outputIndex, ScriptBuilder.createInputScript(null, pubKey)); + btcTx.addInput(BitcoinTestUtils.createHash(spendTxHashSeed), 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) { + private BtcTransaction createMultiSigPegInTransaction(Address federationAddress, Coin coin, int outputIndex, int spendTxHashSeed) { BtcTransaction btcTx = new BtcTransaction(btcNetworkParams); - int outputIndex = 0; - int nHash = 0; btcTx.addInput( - BitcoinTestUtils.createHash(nHash), + BitcoinTestUtils.createHash(spendTxHashSeed), outputIndex, ScriptBuilder.createP2SHMultiSigInputScript(null, federationSupport.getActiveFederation().getRedeemScript()) ); @@ -425,6 +407,12 @@ private BtcTransaction createMultiSigPegInTransaction(Address federationAddress, return btcTx; } + private void assertItWasProcessed(BtcTransaction btcTransaction) throws IOException { + Optional heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(btcTransaction.getHash()); + assertTrue(heightIfBtcTxHashIsAlreadyProcessed.isPresent()); + assertEquals(RSK_EXECUTION_BLOCK_NUMBER, heightIfBtcTxHashIsAlreadyProcessed.get()); + } + private void assertLogPegInBtc(BtcTransaction btcTransaction, long value) { CallTransaction.Function pegInBtcEvent = BridgeEvents.PEGIN_BTC.getEvent(); Sha256Hash peginTransactionHash = btcTransaction.getHash(); From d166bb8b3f73858e9bc7dccbf50b967a668a97bf Mon Sep 17 00:00:00 2001 From: Julian Len Date: Thu, 16 Jan 2025 12:18:54 -0300 Subject: [PATCH 3/3] fix: changed the test name so it reflects what the test does --- .../src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 61930683eb..15df730475 100644 --- a/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java +++ b/rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java @@ -325,7 +325,7 @@ void registerBtcTransaction_whenLegacyPeginBtcTransactionFromAMultiSig_shouldRef } @Test - void registerBtcTransaction_forALegacyBtcTransactionWithMultipleInputs_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception { + void registerBtcTransaction_forALegacyBtcTransactionWithMultipleInputs_shouldRegisterTheNewUtxoAndTransferTheRbtcBalanceToTheFirstInputAddress() throws Exception { // Arrange BtcTransaction btcTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey, outputIndex, spendTxHashSeed);