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

Adds 'should create multiple pegouts with mixed values same and above… #163

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions lib/btc-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,16 @@ const waitForBitcoinMempoolToGetTxs = async (btcTxHelper, maxAttempts = 3, check
return Number(btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddress)));
};

const base58AddressToHexString = (base58Address) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toHexString doesn't say much. Maybe we should call this toHash160? If that is the case, not sure what the hex string is representing here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

return bitcoinJs.address.fromBase58Check(base58Address).hash.toString('hex');
};

module.exports = {
publicKeyToCompressed,
fundAddressAndGetData,
getBitcoinTransactionsInMempool,
waitForBitcoinTxToBeInMempool,
waitForBitcoinMempoolToGetTxs,
getBtcAddressBalanceInSatoshis,
base58AddressToHexString,
};
35 changes: 33 additions & 2 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const { sendPegin,
fundRskAccountThroughAPegin,
createExpectedReleaseRequestRejectedEvent,
} = require('../2wp-utils');
const { getBtcAddressBalanceInSatoshis } = require('../btc-utils');
const { ensure0x, removePrefix0x } = require('../utils');
const { getBtcAddressBalanceInSatoshis, base58AddressToHexString } = require('../btc-utils');
const { ensure0x, removePrefix0x, wait } = require('../utils');
const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');
const bitcoinJsLib = require('bitcoinjs-lib');
const { deployCallReleaseBtcContract } = require('../contractDeployer');
Expand Down Expand Up @@ -962,6 +962,21 @@ const execute = (description, getRskHost) => {

// Assert
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check the Bridge state at this point? To assert there are 3 pegout requests created

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.


const bridgeStateAfterPegoutRequestsReceived = await getBridgeState(rskTxHelper.getClient());
const pegoutRequests = bridgeStateAfterPegoutRequestsReceived.pegoutRequests;

expect(pegoutRequests.length).to.be.equal(3);

// They could be in any order, so we need to find them.
const pegoutRequest1 = findPegoutRequest(pegoutRequests, pegoutTransaction1.transactionHash);
const pegoutRequest2 = findPegoutRequest(pegoutRequests, pegoutTransaction2.transactionHash);
const pegoutRequest3 = findPegoutRequest(pegoutRequests, pegoutTransaction3.transactionHash);

// Assert that the pegout requests are in the Bridge
assertBridgePegoutRequest(pegoutRequest1, base58AddressToHexString(senderRecipientInfo1.btcSenderAddressInfo.address), pegout1ValueInSatoshis, pegoutTransaction1.transactionHash);
assertBridgePegoutRequest(pegoutRequest2, base58AddressToHexString(senderRecipientInfo2.btcSenderAddressInfo.address), pegout2ValueInSatoshis, pegoutTransaction2.transactionHash);
assertBridgePegoutRequest(pegoutRequest3, base58AddressToHexString(senderRecipientInfo3.btcSenderAddressInfo.address), pegout3ValueInSatoshis, pegoutTransaction3.transactionHash);

let bridgeStateAfterPegoutCreation;

// Callback to get the bridge state after the pegout is created
Expand Down Expand Up @@ -1278,6 +1293,22 @@ const getAddressUtxo = (outputs, address) => {
});
};

const assertBridgePegoutRequest = (pegoutRequest, btcDestinationAddressHash160, amountInSatoshis, rskTxHash) => {
expect(pegoutRequest.destinationAddressHash160).to.be.equal(btcDestinationAddressHash160);
expect(Number(pegoutRequest.amountInSatoshis)).to.be.equal(amountInSatoshis);
expect(ensure0x(pegoutRequest.rskTxHash)).to.be.equal(rskTxHash);
};

/**
*
* @param {Array<PegoutRequest>} bridgePegoutRequests the pegout requests array from the bridge state
* @param {string} rskTxHash the tx hash of the pegout request
* @returns {PegoutRequest | undefined} the pegout request object from the bridge state, or undefined if not found
*/
const findPegoutRequest = (bridgePegoutRequests, rskTxHash) => {
return bridgePegoutRequests.find(pegoutRequest => ensure0x(pegoutRequest.rskTxHash) === rskTxHash);
};

module.exports = {
execute,
};
Loading