-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from rsksmart/redeem-script-tests-refactor
Redeem script tests refactor
- Loading branch information
Showing
6 changed files
with
189 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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 removePrefix0x = require('../lib/utils').removePrefix0x; | ||
const rskUtils = require('../lib/rsk-utils'); | ||
const { | ||
GENESIS_FEDERATION_ADDRESS, | ||
GENESIS_FEDERATION_REDEEM_SCRIPT, | ||
} = require('../lib/constants'); | ||
|
||
const fulfillRequirementsToRunAsSingleTestFile = async () => { | ||
const latestForkName = rskUtils.getLatestForkName(); | ||
await rskUtils.activateFork(latestForkName); | ||
}; | ||
|
||
describe('Calling getActivePowpegRedeemScript method after last fork before fedchange', function() { | ||
let rskTxHelpers; | ||
let rskTxHelper; | ||
let bridge; | ||
|
||
before(async () => { | ||
rskTxHelpers = getRskTransactionHelpers(); | ||
rskTxHelper = rskTxHelpers[0]; | ||
if (process.env.RUNNING_SINGLE_TEST_FILE) { | ||
await fulfillRequirementsToRunAsSingleTestFile(); | ||
} | ||
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, | ||
); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {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 publicKeyToCompressed = require('../lib/utils').publicKeyToCompressed; | ||
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 | ||
|
||
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]: publicKeyToCompressed( | ||
federate.publicKeys[KEY_TYPE_BTC], | ||
), | ||
[KEY_TYPE_RSK]: publicKeyToCompressed( | ||
federate.publicKeys[KEY_TYPE_RSK], | ||
), | ||
[KEY_TYPE_MST]: 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(removePrefix0x(activePowpegRedeemScript)).to.eq(p2shErpFedRedeemScript.toString('hex')); | ||
|
||
|
||
expect(addressFromRedeemScript) | ||
.to.eq(expectedNewFederationAddress); | ||
expect(addressFromRedeemScript) | ||
.to.eq(activeFederationAddressFromBridge); | ||
} catch (err) { | ||
throw new CustomError( | ||
'getActivePowpegRedeemScript method validation failure', | ||
err, | ||
); | ||
} | ||
}); | ||
}); |