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

Test for multiple outputs #2906

Draft
wants to merge 1 commit into
base: pegin-it-belowMinimum
Choose a base branch
from
Draft
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 @@ -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 @@ -274,6 +307,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 @@ -286,14 +330,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
Loading