Skip to content

Commit

Permalink
Merge pull request #17 from rsksmart/create-disableWhitelisting-function
Browse files Browse the repository at this point in the history
Refactors fedchange file enough to make it run on its own. Added new …
  • Loading branch information
marcos-iov authored Nov 6, 2023
2 parents c9c372a + 046c06c commit d710353
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 67 deletions.
33 changes: 14 additions & 19 deletions lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,28 +242,23 @@ const donateToBridge = async (rskTxHelper, btcTxHelper, donatingBtcAddressInform
};

/**
* Disabling whitelisting
* Disable the whitelisting in the bridge and mine the required blocks specified in the blockDelay parameter.
* @param {RskTransactionHelper} rskTxHelper
* @param {BtcTransactionHelper} btcTxHelper
* @param {number} blockDelay defaults to 1. If a 0 or negative number is passed, then it will not mine. If a number greater than 0 is passed, it will mine the specified number of blocks.
* @returns {Promise<void>}
*/
const disableWhitelisting = async (rskTxHelper, btcTxHelper) => {
const btcBlocksToMine = 1;
const latestActiveForkName = await getLatestActiveForkName();
const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);

const whitelistChangeAddress = await rskTxHelper.importAccount(WHITELIST_CHANGE_PK);
expect(whitelistChangeAddress.slice(2)).to.equal(WHITELIST_CHANGE_ADDR);
await rskTxHelper.unlockAccount(whitelistChangeAddress);

await sendTxWithCheck(
rskTxHelper,
bridge.methods.setLockWhitelistDisableBlockDelay(btcBlocksToMine),
WHITELIST_CHANGE_ADDR,
(disableResult) => expect(Number(disableResult)).to.equal(1)
);

btcTxHelper.mine(btcBlocksToMine);
}
const disableWhitelisting = async (rskTxHelper, btcTxHelper, blockDelay = 1) => {
const bridge = getBridge(rskTxHelper.getClient());
const unlocked = await rskUtils.getUnlockedAddress(rskTxHelper, WHITELIST_CHANGE_PK, WHITELIST_CHANGE_ADDR);
expect(unlocked).to.be.true;
const disableLockWhitelistMethod = bridge.methods.setLockWhitelistDisableBlockDelay(blockDelay);
const disableResultCallback = (disableResult) => expect(Number(disableResult)).to.equal(1);
await rskUtils.sendTxWithCheck(rskTxHelper, disableLockWhitelistMethod, WHITELIST_CHANGE_ADDR, disableResultCallback);
if(blockDelay > 0) {
await btcTxHelper.mine(blockDelay);
}
};

module.exports = {
sendTxToBridge,
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
"mocha-junit-reporter": "^1.17.0",
"pegin-address-verificator": "git+https://[email protected]/rsksmart/pegin-address-verifier#v0.2.1",
"peglib": "git+https://github.com/rsksmart/rsk-peglib#v1.4.15",
"precompiled-fingerroot500": "npm:@rsksmart/rsk-precompiled-abis@^5.0.0-FINGERROOT",
"precompiled-hop400": "npm:@rsksmart/rsk-precompiled-abis@^5.0.0-HOP",
"precompiled-iris300": "npm:@rsksmart/rsk-precompiled-abis@^4.0.0-IRIS",
"precompiled-orchid": "npm:@rsksmart/rsk-precompiled-abis@^1.0.0-ORCHID",
"precompiled-wasabi100": "npm:@rsksmart/rsk-precompiled-abis@^2.0.0-WASABI",
"precompiled-papyrus200": "npm:@rsksmart/rsk-precompiled-abis@^3.0.0-PAPYRUS",
"precompiled-iris300": "npm:@rsksmart/rsk-precompiled-abis@^4.0.0-IRIS",
"precompiled-hop400": "npm:@rsksmart/rsk-precompiled-abis@^5.0.0-HOP",
"precompiled-fingerroot500": "npm:@rsksmart/rsk-precompiled-abis@^5.0.0-FINGERROOT",
"precompiled-wasabi100": "npm:@rsksmart/rsk-precompiled-abis@^2.0.0-WASABI",
"rsk-transaction-helper": "git+https://github.com/rsksmart/rootstock-transaction-helper#v3.1.0",
"solc": "^0.7.5",
"stream-line-wrapper": "^0.1.1",
Expand Down
105 changes: 61 additions & 44 deletions tests/04_00_02-fedchange.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require('chai').expect
const BN = require('bn.js');
var { sequentialPromise, wait, randomElement, randomNElements, additionalFederationAddresses } = require('../lib/utils');
const { sequentialPromise, wait, randomElement, randomNElements, additionalFederationAddresses } = require('../lib/utils');
const CustomError = require('../lib/CustomError');
const peglib = require('peglib');
const redeemScriptParser = require('@rsksmart/powpeg-redeemscript-parser');
Expand All @@ -17,6 +17,8 @@ const libUtils = require('../lib/utils');
const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');
const { getBtcClient } = require('../lib/btc-client-provider');
const removePrefix0x = require("../lib/utils").removePrefix0x;
const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter');
const { sendPegin, ensurePeginIsRegistered, disableWhitelisting } = require('../lib/2wp-utils');
const {
KEY_TYPE_BTC,
KEY_TYPE_RSK,
Expand Down Expand Up @@ -54,33 +56,44 @@ const PENDING_FEDERATION_RANDOM_HASH = '0x27da3e662acf8862ba5c42fbdcc023c4f81355
const INITIAL_BTC_BALANCE = bitcoin.btcToSatoshis(200);
const EXPECTED_UNSUCCESSFUL_RESULT = -10;

var btcClient;
var rskClientOldFed;
var rskClientNewFed;
var rskClients;
var pegClient;
var test;
var testNewFed;
var utils;
var expectedNewFederationCreationTime, expectedNewFederationCreationBlockNumber;
var oldFederation;
var newFederationBtcPublicKeys;
var newFederationPublicKeys;
var newFederatorRskAddressesRsk;
var expectedNewFederationAddress;
let btcClient;
let rskClientOldFed;
let rskClientNewFed;
let rskClients;
let pegClient;
let test;
let testNewFed;
let utils;
let expectedNewFederationCreationTime, expectedNewFederationCreationBlockNumber;
let oldFederation;
let newFederationBtcPublicKeys;
let newFederationPublicKeys;
let newFederatorRskAddressesRsk;
let expectedNewFederationAddress;
let p2shErpFedRedeemScript;
var expectedNewFederationThreshold;
let expectedNewFederationThreshold;
let amountOfUtxosToMigrate;
let federationBalanceBeforeMigration;
let whitelistingAssertionsTestLegacy;
let rskTxHelpers;
let btcTxHelper;
let rskTxHelper;

/**
* Takes the blockchain to the required state for this test file to run in isolation.
*/
const fulfillRequirementsToRunAsSingleTestFile = async (rskTxHelper, btcTxHelper) => {
await rskUtils.activateFork(rskUtils.getLatestForkName());
await disableWhitelisting(rskTxHelper, btcTxHelper);
};

describe('RSK Federation change', function() {
var addresses;
let addresses;

before(async () => {

try {

rskClientOldFed = rsk.getClient(Runners.hosts.federate.host);
await Runners.startAdditionalFederateNodes(await rskClientOldFed.eth.getBlock('latest'));
btcClient = bitcoin.getClient(
Expand All @@ -90,6 +103,13 @@ describe('RSK Federation change', function() {
NETWORK
);
rskClients = Runners.hosts.federates.map(federate => rsk.getClient(federate.host));
rskTxHelpers = getRskTransactionHelpers();
rskTxHelper = rskTxHelpers[0];
btcTxHelper = getBtcClient();

if(process.env.RUNNING_SINGLE_TEST_FILE) {
await fulfillRequirementsToRunAsSingleTestFile(rskTxHelper, btcTxHelper);
}

// Assume the last of the running federators belongs to the new federation
rskClientNewFed = rskClients[rskClients.length-1];
Expand All @@ -101,9 +121,7 @@ describe('RSK Federation change', function() {
whitelistingAssertionstestNewFed = whitelistingAssertionsLegacy.with(btcClient, rskClientNewFed, pegClient);
utils = rskUtilsLegacy.with(btcClient, rskClientOldFed, pegClient);
utilsNewFed = rskUtilsLegacy.with(btcClient, rskClientNewFed, pegClient);
rskTxHelpers = getRskTransactionHelpers();
btcTxHelper = getBtcClient();


await rskUtilsLegacy.waitForSync(rskClients);

// Grab the new federation public keys and calculate the federators addresses and expected federation
Expand Down Expand Up @@ -305,36 +323,35 @@ describe('RSK Federation change', function() {

// this is not an actual test, this just modifies the blockchain state ensuring that the active federation contains several UTXOs
it('generates several UTXOs in the active federation', async () => {
try{
try {

const EXPECTED_UTXOS = 15;
let bridgeStatus = await getBridgeState(rskClientNewFed);
let existingUtxos = bridgeStatus.activeFederationUtxos.length;
amountOfUtxosToMigrate = existingUtxos < EXPECTED_UTXOS ? EXPECTED_UTXOS : existingUtxos;
const bridgeStatus = await getBridgeState(rskClientNewFed);
const existingUtxosCount = bridgeStatus.activeFederationUtxos.length;
amountOfUtxosToMigrate = existingUtxosCount < EXPECTED_UTXOS ? EXPECTED_UTXOS : existingUtxosCount;

// Ensure there are enough UTXOs so that the migration need to be done in more than one transaction
if (existingUtxos < EXPECTED_UTXOS) {
if (existingUtxosCount < EXPECTED_UTXOS) {

const UTXOS_TO_PAY_FEES = 1
const UTXOS_TO_TRANSFER = EXPECTED_UTXOS - existingUtxos;
var utxosToGenerate = UTXOS_TO_TRANSFER + UTXOS_TO_PAY_FEES;
var utxoValue = bitcoin.btcToSatoshis(1);

let activeFederationAddress = await getActiveFederationAddress();
var addresses = await pegClient.generateNewAddress('for_migration');
expect(addresses.inRSK).to.be.true;

await btcClient.sendToAddress(addresses.btc, utxoValue * utxosToGenerate);
await btcClient.generate(1);
await test.assertBitcoinBalance(addresses.btc, utxoValue * utxosToGenerate, "Wrong balance in account to migrate");
await wait(1000);
await sequentialPromise(
UTXOS_TO_TRANSFER,
() => test.assertLock(addresses, [{ address: activeFederationAddress, amount: utxoValue }])
);
const UTXOS_TO_TRANSFER = EXPECTED_UTXOS - existingUtxosCount;
const utxosToGenerate = UTXOS_TO_TRANSFER + UTXOS_TO_PAY_FEES;
const utxoValueInSatoshis = bitcoin.btcToSatoshis(1);
const peginSenderAddressInfo = await btcTxHelper.generateBtcAddress('legacy');
const totalFeesInSatoshis = (utxosToGenerate * btcEthUnitConverter.btcToSatoshis(btcTxHelper.getFee()));
const totalAmountToSendInSatoshis = (utxoValueInSatoshis * utxosToGenerate) + totalFeesInSatoshis;

await btcTxHelper.fundAddress(peginSenderAddressInfo.address, btcEthUnitConverter.satoshisToBtc(totalAmountToSendInSatoshis));

for(let i = 0; i < utxosToGenerate; i++) {
const peginBtcTxHash = await sendPegin(rskTxHelper, btcTxHelper, peginSenderAddressInfo, btcEthUnitConverter.satoshisToBtc(utxoValueInSatoshis));
await ensurePeginIsRegistered(rskTxHelper, peginBtcTxHash);
}

}
await rskUtilsLegacy.waitForSync(rskClients);

bridgeStatus = await getBridgeState(rskClientNewFed);
federationBalanceBeforeMigration = bridgeStatus.activeFederationUtxos.reduce(
const finalBridgeStatus = await getBridgeState(rskClientNewFed);
federationBalanceBeforeMigration = finalBridgeStatus.activeFederationUtxos.reduce(
(previousValue, currentUtxo) => previousValue + currentUtxo.valueInSatoshis, 0
);
} catch (err) {
Expand Down

0 comments on commit d710353

Please sign in to comment.