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

Redeem script tests refactor #18

Closed
wants to merge 11 commits into from
6 changes: 3 additions & 3 deletions lib/2wp-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const expect = require('chai').expect;
const { sendFromCow, mineAndSync, sendTxWithCheck } = require('./rsk-utils');
const { sendFromCow, mineAndSync, sendTxWithCheck, getUnlockedAddress } = require('./rsk-utils');
const { wait, retryWithCheck } = require('./utils');
const { getBridge, getLatestActiveForkName } = require('./precompiled-abi-forks-util');
const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');
Expand Down Expand Up @@ -250,11 +250,11 @@ const donateToBridge = async (rskTxHelper, btcTxHelper, donatingBtcAddressInform
*/
const disableWhitelisting = async (rskTxHelper, btcTxHelper, blockDelay = 1) => {
const bridge = getBridge(rskTxHelper.getClient());
const unlocked = await rskUtils.getUnlockedAddress(rskTxHelper, WHITELIST_CHANGE_PK, WHITELIST_CHANGE_ADDR);
const unlocked = await 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);
await sendTxWithCheck(rskTxHelper, disableLockWhitelistMethod, WHITELIST_CHANGE_ADDR, disableResultCallback);
if(blockDelay > 0) {
await btcTxHelper.mine(blockDelay);
}
Expand Down
58 changes: 36 additions & 22 deletions tests/01_05_57-post_hop_active_powpeg_redeem_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,44 @@ const chai = require('chai');
chai.use(require('chai-as-promised'));
const expect = chai.expect;

const rsk = require('peglib').rsk;
const redeemScriptParser = require('@rsksmart/powpeg-redeemscript-parser');
const CustomError = require('../lib/CustomError');
const removePrefix0x = require("../lib/utils").removePrefix0x;
const { GENESIS_FEDERATION_ADDRESS, GENESIS_FEDERATION_REDEEM_SCRIPT } = require('../lib/constants');

const removePrefix0x = require('../lib/utils').removePrefix0x;
const {getRskTransactionHelpers} = require('../lib/rsk-tx-helper-provider');
const {getBridge, getLatestActiveForkName} = require('../lib/precompiled-abi-forks-util');
const {GENESIS_FEDERATION_ADDRESS, GENESIS_FEDERATION_REDEEM_SCRIPT} = require('../lib/constants');
const {activateFork} = require('../lib/rsk-utils');
/**
* Takes the blockchain to the required state for this test file to run in isolation.
*/
const fulfillRequirementsToRunAsSingleTestFile = async () => {
await activateFork(Runners.common.forks.hop400);
};
describe('Calling getActivePowpegRedeemScript method after hop', function() {
let rskTxHelpers;
let rskTxHelper;
let bridge;
before(async () => {
if (process.env.RUNNING_SINGLE_TEST_FILE) {
await fulfillRequirementsToRunAsSingleTestFile();
}
rskTxHelpers = getRskTransactionHelpers();
rskTxHelper = rskTxHelpers[0];
bridge = getBridge(rskTxHelper.getClient(), await getLatestActiveForkName());
});

it('should return the active powpeg redeem script', async () => {
try {
const activePowpegRedeemScript = await bridge.methods.getActivePowpegRedeemScript().call();
const activeFederationAddressFromBridge = await bridge.methods.getFederationAddress().call();
const addressFromRedeemScript = redeemScriptParser.getAddressFromRedeemScript(
'REGTEST', Buffer.from(removePrefix0x(activePowpegRedeemScript), 'hex'),
);

before(() => {
rskClient = rsk.getClient(Runners.hosts.federate.host);
});

it('should return the active powpeg redeem script', async () => {
try{
const activePowpegRedeemScript = await rskClient.rsk.bridge.methods.getActivePowpegRedeemScript().call();
const activeFederationAddressFromBridge = await rskClient.rsk.bridge.methods.getFederationAddress().call();
const addressFromRedeemScript = redeemScriptParser.getAddressFromRedeemScript(
'REGTEST', Buffer.from(removePrefix0x(activePowpegRedeemScript), 'hex')
);

expect(activePowpegRedeemScript).to.eq(GENESIS_FEDERATION_REDEEM_SCRIPT);
expect(addressFromRedeemScript).to.eq(GENESIS_FEDERATION_ADDRESS).to.eq(activeFederationAddressFromBridge);
} catch (err) {
throw new CustomError('getActivePowpegRedeemScript method validation failure', err);
}
})
expect(activePowpegRedeemScript).to.eq(GENESIS_FEDERATION_REDEEM_SCRIPT);
expect(addressFromRedeemScript).to.eq(GENESIS_FEDERATION_ADDRESS).to.eq(activeFederationAddressFromBridge);
} catch (err) {
throw new CustomError('getActivePowpegRedeemScript method validation failure', err);
}
});
});
60 changes: 60 additions & 0 deletions tests/03_02_01-last_fork_active_powpeg_redeem_script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const chai = require('chai');
chai.use(require('chai-as-promised'));
const expect = chai.expect;
const redeemScriptParser = require('@rsksmart/powpeg-redeemscript-parser');
const {getRskTransactionHelpers} = require('../lib/rsk-tx-helper-provider');
const {getBridge, getLatestActiveForkName} = require('../lib/precompiled-abi-forks-util');
const CustomError = require('../lib/CustomError');
const {disableWhitelisting} = require('../lib/2wp-utils');
const removePrefix0x = require('../lib/utils').removePrefix0x;
const {getBtcClient} = require('../lib/btc-client-provider');
const rskUtils = require('../lib/rsk-utils');
const {
GENESIS_FEDERATION_ADDRESS,
GENESIS_FEDERATION_REDEEM_SCRIPT,
} = require('../lib/constants');

const fulfillRequirementsToRunAsSingleTestFile = async (rskTxHelper, btcTxHelper) => {
const latestForkName = rskUtils.getLatestForkName();
await rskUtils.activateFork(latestForkName);
await disableWhitelisting(rskTxHelper, btcTxHelper);
ed-iov marked this conversation as resolved.
Show resolved Hide resolved
};

describe('Calling getActivePowpegRedeemScript method after last fork before fedchange', function() {
let rskTxHelpers;
let rskTxHelper;
let bridge;

before(async () => {
const btcTxHelper = getBtcClient();

rskTxHelpers = getRskTransactionHelpers();
rskTxHelper = rskTxHelpers[0];
if (process.env.RUNNING_SINGLE_TEST_FILE) {
await fulfillRequirementsToRunAsSingleTestFile(rskTxHelper, btcTxHelper);
}
bridge = getBridge(rskTxHelper.getClient(), await getLatestActiveForkName());
});

it('should return the active powpeg redeem script', async () => {
try {
const activePowpegRedeemScript = await bridge.methods.getActivePowpegRedeemScript().call();
const activeFederationAddressFromBridge = await bridge.methods.getFederationAddress().call();
const addressFromRedeemScript =
redeemScriptParser.getAddressFromRedeemScript(
'REGTEST',
Buffer.from(removePrefix0x(activePowpegRedeemScript), 'hex'),
);

expect(activePowpegRedeemScript).to.eq(GENESIS_FEDERATION_REDEEM_SCRIPT);
expect(addressFromRedeemScript)
.to.eq(GENESIS_FEDERATION_ADDRESS)
.to.eq(activeFederationAddressFromBridge);
} catch (err) {
throw new CustomError(
'getActivePowpegRedeemScript method validation failure',
err,
);
}
});
});
1 change: 1 addition & 0 deletions tests/04_00_02-fedchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ describe('RSK Federation change', function() {
throw new CustomError('Transfer BTC to RBTC with outputs both federations failure', err);
}
});

});

const getActiveFederationAddress = async() => {
Expand Down
87 changes: 87 additions & 0 deletions tests/05_02_01-last_fork_active_powpeg_redeem_script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const chai = require('chai');
chai.use(require('chai-as-promised'));
const expect = chai.expect;
const peglib = require('peglib');
ed-iov marked this conversation as resolved.
Show resolved Hide resolved
const bitcoin = peglib.bitcoin;
ed-iov marked this conversation as resolved.
Show resolved Hide resolved
const {compareFederateKeys} = require('../lib/federation-utils');
const {getRskTransactionHelpers} = require('../lib/rsk-tx-helper-provider');
const redeemScriptParser = require('@rsksmart/powpeg-redeemscript-parser');
const CustomError = require('../lib/CustomError');
const removePrefix0x = require('../lib/utils').removePrefix0x;
const {getBridge, getLatestActiveForkName} = require('../lib/precompiled-abi-forks-util');

// in order to run this as a single test file, it requires a federation change so follow the following command
// npm run run-single-test-file 04_00_02-fedchange.js,05_02_01-last_fork_active_powpeg_redeem_script.js
ed-iov marked this conversation as resolved.
Show resolved Hide resolved

const {
ERP_PUBKEYS,
ERP_CSV_VALUE,
KEY_TYPE_BTC,
KEY_TYPE_RSK,
KEY_TYPE_MST,
} = require('../lib/constants');
const INITIAL_FEDERATION_SIZE = 3;

let rskTxHelpers;
let rskTxHelper;
let bridge;

describe('Calling getActivePowpegRedeemScript method after last fork after fed change', function() {
before(async () => {
rskTxHelpers = getRskTransactionHelpers();
rskTxHelper = rskTxHelpers[0];
bridge = getBridge(rskTxHelper.getClient(), await getLatestActiveForkName());
});

it('should return the active powpeg redeem script', async () => {
try {
const activePowpegRedeemScript = await bridge.methods
.getActivePowpegRedeemScript()
.call();
const activeFederationAddressFromBridge = await bridge.methods.getFederationAddress().call();
const addressFromRedeemScript = redeemScriptParser.getAddressFromRedeemScript(
'REGTEST',
Buffer.from(removePrefix0x(activePowpegRedeemScript), 'hex'),
);
const newFederationPublicKeys = Runners.hosts.federates
.filter((federate, index) => index >= INITIAL_FEDERATION_SIZE)
.map((federate) => ({
[KEY_TYPE_BTC]: bitcoin.keys.publicKeyToCompressed(
ed-iov marked this conversation as resolved.
Show resolved Hide resolved
federate.publicKeys[KEY_TYPE_BTC],
),
[KEY_TYPE_RSK]: bitcoin.keys.publicKeyToCompressed(
federate.publicKeys[KEY_TYPE_RSK],
),
[KEY_TYPE_MST]: bitcoin.keys.publicKeyToCompressed(
federate.publicKeys[KEY_TYPE_MST],
),
}))
.sort(compareFederateKeys);
const newFederationBtcPublicKeys = newFederationPublicKeys.map(
(publicKeys) => publicKeys[KEY_TYPE_BTC],
);
const p2shErpFedRedeemScript = redeemScriptParser.getP2shErpRedeemScript(
newFederationBtcPublicKeys,
ERP_PUBKEYS,
ERP_CSV_VALUE,
);
const expectedNewFederationAddress =
redeemScriptParser.getAddressFromRedeemScript(
'REGTEST',
p2shErpFedRedeemScript,
);

expect(activePowpegRedeemScript)
.to.eq('0x' + p2shErpFedRedeemScript.toString('hex'),
);
expect(addressFromRedeemScript)
.to.eq(expectedNewFederationAddress)
.to.eq(activeFederationAddressFromBridge);
} catch (err) {
throw new CustomError(
'getActivePowpegRedeemScript method validation failure',
err,
);
}
});
});
Loading