Skip to content

Commit

Permalink
feat: Added a test such that if a P2SHP2WPKH legacy peg in transactio…
Browse files Browse the repository at this point in the history
…n is sent to the federation, it should register the peg in correctly
  • Loading branch information
julianlen committed Jan 21, 2025
1 parent 9e6b4d2 commit 941e34e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.rsk.peg;

import static co.rsk.bitcoinj.script.ScriptOpCodes.OP_0;
import static co.rsk.peg.BridgeSupportTestUtil.*;
import static co.rsk.peg.pegin.RejectedPeginReason.LEGACY_PEGIN_UNDETERMINED_SENDER;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -462,6 +463,46 @@ void registerBtcTransaction_WhenLegacyPeginBtcTransactionFromP2WSHAddress_should
assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver));
}

@Test
void registerBtcTransaction_WhenLegacyPeginBtcTransactionFromP2WPKHAddress_shouldRegisterTheNewUtxoAndTransferTheRbtcBalance() throws Exception {
// Arrange
BtcTransaction btcTransaction = new BtcTransaction(btcNetworkParams);
btcTransaction.addInput(BitcoinTestUtils.createHash(spendTxHashSeed), outputIndex, new Script(new byte[]{}));
btcTransaction.addOutput(minimumPeginValue, federationSupport.getActiveFederation().getAddress());

Script redeemScript = ScriptBuilder.createInputScript(null, btcPublicKey);
byte[] redeemScriptHash = Sha256Hash.hash(redeemScript.getProgram());
Script scriptSig = new ScriptBuilder().number(OP_0).data(redeemScriptHash).build();
Script segwitScriptSig = new ScriptBuilder().data(scriptSig.getProgram()).build();
btcTransaction.getInput(0).setScriptSig(segwitScriptSig);

TransactionWitness txWitness = TransactionWitness.createWitness(null, btcPublicKey);
btcTransaction.setWitness(0, txWitness);

PartialMerkleTree pmtWithTransactions = createValidPmtForTransactions(List.of(btcTransaction.getHash(true)), btcNetworkParams);
int btcBlockWithPmtHeight = bridgeConstants.getBtcHeightWhenPegoutTxIndexActivates() + bridgeConstants.getPegoutTxIndexGracePeriodInBtcBlocks();
int chainHeight = btcBlockWithPmtHeight + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations();
recreateChainFromPmt(btcBlockStoreWithCache, chainHeight, pmtWithTransactions, btcBlockWithPmtHeight, btcNetworkParams);
bridgeSupport.save();

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

// Assert
assertItWasProcessed(btcTransaction);

int newOutputIndex = 0;
TransactionOutput output = btcTransaction.getOutput(newOutputIndex);
List<UTXO> 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));

assertLogPegInBtc(btcTransaction, minimumPeginValue.getValue());
}

private static UTXO utxoOf(BtcTransaction btcTransaction, TransactionOutput output) {
int height = 0;
return new UTXO(
Expand Down

0 comments on commit 941e34e

Please sign in to comment.