Skip to content

Commit

Permalink
Merge pull request #105 from rsksmart/remove-test-with-multiple-inputs
Browse files Browse the repository at this point in the history
Removes test with multiple inputs in a btc pegin tx because of wallet…
  • Loading branch information
marcos-iov authored Oct 2, 2024
2 parents 74ada4a + 61031ef commit 01eb237
Showing 1 changed file with 1 addition and 92 deletions.
93 changes: 1 addition & 92 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ const { sendPegin,
createSenderRecipientInfo,
createExpectedPeginBtcEvent,
get2wpBalances,
mineForPeginRegistration,
} = require('../2wp-utils');
const { getBtcAddressBalanceInSatoshis, waitForBitcoinMempoolToGetTxs } = require('../btc-utils');
const { getBtcAddressBalanceInSatoshis } = require('../btc-utils');
const { ensure0x } = require('../utils');
const bitcoinJsLib = require('bitcoinjs-lib');

let btcTxHelper;
let rskTxHelper;
Expand Down Expand Up @@ -107,74 +105,6 @@ const execute = (description, getRskHost) => {

});

it('should do legacy pegin with multiple inputs from different accounts and one output to the federation with value exactly minimum', async () => {

// Arrange

const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper);
const senderRecipientInfo1 = await createSenderRecipientInfo(rskTxHelper, btcTxHelper);
const senderRecipientInfo2 = await createSenderRecipientInfo(rskTxHelper, btcTxHelper);
const initialSender1AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo1.btcSenderAddressInfo.address);
const initialSender2AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo2.btcSenderAddressInfo.address);

const sender1PeginValueInSatoshis = minimumPeginValueInSatoshis;
const sender2PeginValueInSatoshis = minimumPeginValueInSatoshis;
const peginValueInSatoshis = sender1PeginValueInSatoshis + sender2PeginValueInSatoshis;

const sender1UtxosInfo = await btcTxHelper.selectSpendableUTXOsFromAddress(senderRecipientInfo1.btcSenderAddressInfo.address, satoshisToBtc(sender1PeginValueInSatoshis));
const sender2UtxosInfo = await btcTxHelper.selectSpendableUTXOsFromAddress(senderRecipientInfo2.btcSenderAddressInfo.address, satoshisToBtc(sender2PeginValueInSatoshis));

const sender1ChangeInSatoshis = btcToSatoshis(sender1UtxosInfo.change);
const sender2ChangeInSatoshis = btcToSatoshis(sender2UtxosInfo.change);

const tx = new bitcoinJsLib.Transaction();

// Adding inputs
addInputs(tx, sender1UtxosInfo.utxos);
addInputs(tx, sender2UtxosInfo.utxos);

// Adding output to federation
addOutput(tx, federationAddress, peginValueInSatoshis);

// Adding change outputs
addOutput(tx, senderRecipientInfo1.btcSenderAddressInfo.address, sender1ChangeInSatoshis - btcFeeInSatoshis);
addOutput(tx, senderRecipientInfo2.btcSenderAddressInfo.address, sender2ChangeInSatoshis - btcFeeInSatoshis);

const sendersPrivateKeys = [senderRecipientInfo1.btcSenderAddressInfo.privateKey, senderRecipientInfo2.btcSenderAddressInfo.privateKey]
const signedTx = await btcTxHelper.nodeClient.signTransaction(tx.toHex(), [], sendersPrivateKeys);

// Act

// Sending the pegin and ensuring the pegin is registered
const btcPeginTxHash = await btcTxHelper.nodeClient.sendTransaction(signedTx);

// Assert

// Since we are not using `sendPegin` here, we need to do some extra steps before ensuring the pegin is registered.
await ensurePeginIsPushed(btcPeginTxHash);

await assertExpectedPeginBtcEventIsEmitted(btcPeginTxHash, senderRecipientInfo1.rskRecipientRskAddressInfo.address, peginValueInSatoshis);

await assert2wpBalancesAfterSuccessfulPegin(initial2wpBalances, peginValueInSatoshis);

// The senders should have their balances reduced by the amount sent to the federation and the fee.
const finalSender1AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo1.btcSenderAddressInfo.address);
expect(finalSender1AddressBalanceInSatoshis).to.be.equal(initialSender1AddressBalanceInSatoshis - sender1PeginValueInSatoshis - btcFeeInSatoshis);

const finalSender2AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo2.btcSenderAddressInfo.address);
expect(finalSender2AddressBalanceInSatoshis).to.be.equal(initialSender2AddressBalanceInSatoshis - sender2PeginValueInSatoshis - btcFeeInSatoshis);

// Only the first sender should have the total amount in rsk since in legacy pegins the rsk address is derived from the first input.
const finalRskRecipient1BalanceInWeisBN = await rskTxHelper.getBalance(senderRecipientInfo1.rskRecipientRskAddressInfo.address);
const expectedRskRecipient1BalanceInWeisBN = rskTxHelper.getClient().utils.BN(satoshisToWeis(peginValueInSatoshis));
expect(finalRskRecipient1BalanceInWeisBN.eq(expectedRskRecipient1BalanceInWeisBN)).to.be.true;

// The second sender should have 0 balance in rsk.
const finalRskRecipient2BalanceInWeisBN = await rskTxHelper.getBalance(senderRecipientInfo2.rskRecipientRskAddressInfo.address);
expect(finalRskRecipient2BalanceInWeisBN.eq(new BN('0'))).to.be.true;

});

});

}
Expand Down Expand Up @@ -207,27 +137,6 @@ const assert2wpBalancesAfterSuccessfulPegin = async (initial2wpBalances, peginVa

};

const addInputs = (tx, utxos) => {
utxos.forEach(utxo => {
tx.addInput(Buffer.from(utxo.txid, 'hex').reverse(), utxo.vout);
});
};

const ensurePeginIsPushed = async (btcPeginTxHash, expectedUtxosCount = 1) => {
await waitForBitcoinMempoolToGetTxs(btcTxHelper, btcPeginTxHash);
await mineForPeginRegistration(rskTxHelper, btcTxHelper);
await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash, expectedUtxosCount);
};

const addOutput = (tx, address, outputValueInSatoshis) => {
if(outputValueInSatoshis > 0) {
tx.addOutput(
bitcoinJsLib.address.toOutputScript(address, btcTxHelper.btcConfig.network),
outputValueInSatoshis
);
}
};

module.exports = {
execute,
};

0 comments on commit 01eb237

Please sign in to comment.