Skip to content

Commit

Permalink
feat: testing that a tx with multiple peg ins result in one and corre…
Browse files Browse the repository at this point in the history
…ct peg in
  • Loading branch information
julianlen committed Dec 26, 2024
1 parent c9394d9 commit 2818e02
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(bitcoinTransaction.getHash());
assertTrue(heightIfBtcTxHashIsAlreadyProcessed.isPresent());
assertEquals(RSK_EXECUTION_BLOCK_NUMBER, heightIfBtcTxHashIsAlreadyProcessed.get());

List<UTXO> 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
Expand Down Expand Up @@ -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(
Expand All @@ -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<DataWord> 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);
Expand Down

0 comments on commit 2818e02

Please sign in to comment.