From e6aa82520388c4c0c77225a454ef4996bbd2a6b0 Mon Sep 17 00:00:00 2001 From: Jan Pikora Date: Fri, 15 Dec 2023 18:35:42 +0100 Subject: [PATCH 1/6] initial commit --- tests/01_03_52-disable_lock_whitelist.js | 161 +++++++++-------------- 1 file changed, 59 insertions(+), 102 deletions(-) diff --git a/tests/01_03_52-disable_lock_whitelist.js b/tests/01_03_52-disable_lock_whitelist.js index 121ee428..85eefdec 100644 --- a/tests/01_03_52-disable_lock_whitelist.js +++ b/tests/01_03_52-disable_lock_whitelist.js @@ -1,121 +1,78 @@ const expect = require('chai').expect -var { sequentialPromise, wait } = require('../lib/utils'); -const CustomError = require('../lib/CustomError'); -const peglib = require('peglib'); - -const bitcoin = peglib.bitcoin; -const rsk = peglib.rsk; -const pegUtils = peglib.pegUtils; -const pegAssertions = require('../lib/assertions/2wp'); -const rskUtilsLegacy = require('../lib/rsk-utils-legacy'); const rskUtils = require('../lib/rsk-utils'); const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); +const { getBtcClient } = require('../lib/btc-client-provider'); +const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util'); +const { satoshisToBtc } = require('@rsksmart/btc-eth-unit-converter'); +const { sendPegin, ensurePeginIsRegistered } = require('../lib/2wp-utils'); -var federationAddress; -var btcClient; -var rskClient; -var rskClients; -var pegClient; -var test; -var utils; -let rskTxHelpers; - -const NETWORK = bitcoin.networks.testnet; +const { WHITELIST_CHANGE_PK, WHITELIST_CHANGE_ADDR} = require('../lib/assertions/whitelisting') -const INITIAL_BTC_BALANCE = bitcoin.btcToSatoshis(10); +let rskTxHelpers; +let rskTxHelper; +let btcTxHelper; +let bridge; +let MINIMUM_PEGIN_VALUE_IN_BTC; +let federationAddress; -const WHITELIST_CHANGE_PK = '3890187a3071327cee08467ba1b44ed4c13adb2da0d5ffcc0563c371fa88259c'; -const WHITELIST_CHANGE_ADDR = '87d2a0f33744929da08b65fd62b627ea52b25f8e'; +const fulfillRequirementsToRunAsSingleTestFile = async () => { + await rskUtils.activateFork(Runners.common.forks.papyrus200); +}; describe('Disable whitelisting', function() { - var addresses; before(async () => { - try{ - btcClient = bitcoin.getClient( - Runners.hosts.bitcoin.rpcHost, - Runners.hosts.bitcoin.rpcUser, - Runners.hosts.bitcoin.rpcPassword, - NETWORK - ); - rskClient = rsk.getClient(Runners.hosts.federate.host); - rskClients = Runners.hosts.federates.map(federate => rsk.getClient(federate.host)); - pegClient = pegUtils.using(btcClient, rskClient); - test = pegAssertions.with(btcClient, rskClient, pegClient, rskClients); - utils = rskUtilsLegacy.with(btcClient, rskClient, pegClient); - rskTxHelpers = getRskTransactionHelpers(); - - // Grab the federation address - federationAddress = await rskClient.rsk.bridge.methods.getFederationAddress().call(); - await btcClient.importAddress(federationAddress, 'federations'); - - addresses = await pegClient.generateNewAddress('test'); - expect(addresses.inRSK).to.be.true; - - await btcClient.sendToAddress(addresses.btc, INITIAL_BTC_BALANCE); - await btcClient.generate(1); - await test.assertBitcoinBalance(addresses.btc, INITIAL_BTC_BALANCE, 'Initial BTC balance'); - - var addr = await rskClient.eth.personal.importRawKey(WHITELIST_CHANGE_PK, ''); - expect(addr.slice(2)).to.equal(WHITELIST_CHANGE_ADDR); - - await rskClient.eth.personal.unlockAccount(addr, ''); - await sequentialPromise(10, () => rskUtils.mineAndSync(rskTxHelpers)); - } - catch (err) { - throw new CustomError('Lock whitelisting failure', err); + rskTxHelpers = getRskTransactionHelpers(); + rskTxHelper = rskTxHelpers[0]; + btcTxHelper = getBtcClient(); + + const latestActiveForkName = await getLatestActiveForkName(); + bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName); + federationAddress = await bridge.methods.getFederationAddress().call(); + const minPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call(); + MINIMUM_PEGIN_VALUE_IN_BTC = Number(satoshisToBtc(minPeginValueInSatoshis)); + + if(process.env.RUNNING_SINGLE_TEST_FILE) { + await fulfillRequirementsToRunAsSingleTestFile(); } }); it('should disable lock whitelist', async () => { - const INITIAL_BTC_BALANCE = bitcoin.btcToSatoshis(40); - const INITIAL_RSK_BALANCE = bitcoin.btcToSatoshis(10); - - const addresses = await pegClient.generateNewAddress('test'); - expect(addresses.inRSK).to.be.true; + const btcAddressInfo = await btcTxHelper.generateBtcAddress('legacy'); + await btcTxHelper.fundAddress(btcAddressInfo.address, 2 * MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); + const btcAddressInfoBalanceInitial = Number(await btcTxHelper.getAddressBalance(btcAddressInfo.address)); + expect(btcAddressInfoBalanceInitial).to.be.equal(2 * MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); - await btcClient.sendToAddress(addresses.btc, INITIAL_BTC_BALANCE); - await btcClient.generate(1); - await test.assertBitcoinBalance(addresses.btc, INITIAL_BTC_BALANCE, "Wrong initial BTC balance"); - await wait(1000); + const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress)); // address is not whitelisted - await test.assertLock(addresses, [{ address: federationAddress, amount: INITIAL_RSK_BALANCE }], { fails: true }); - // wait for the btc to come back so we can use the assertLock method again - await utils.waitForBtcToReturn(addresses.btc); - - const addr = await rskClient.eth.personal.importRawKey(WHITELIST_CHANGE_PK, ''); - expect(addr.slice(2)).to.equal(WHITELIST_CHANGE_ADDR); - await rskClient.eth.personal.unlockAccount(addr, ''); - // can disable the whitelist - await utils.sendTxWithCheck( - rskClient.rsk.bridge.methods.setLockWhitelistDisableBlockDelay(200), - (disableResult) => expect(Number(disableResult)).to.equal(1), - WHITELIST_CHANGE_ADDR)(); - - // disable whitelist doesn't work the second time - await utils.sendTxWithCheck( - rskClient.rsk.bridge.methods.setLockWhitelistDisableBlockDelay(10), - (disableResult) => expect(Number(disableResult)).to.equal(-1), - WHITELIST_CHANGE_ADDR)(); - - await btcClient.generate(100); - await wait(500); - await rskClient.fed.updateBridge(); - await rskUtils.mineAndSync(rskTxHelpers); - await wait(500); - - // address is still not able to send btc to bridge after 100 blocks - await test.assertLock(addresses, [{ address: federationAddress, amount: INITIAL_RSK_BALANCE }], { fails: true }); - await utils.waitForBtcToReturn(addresses.btc); - - await btcClient.generate(100); - await wait(500); - await rskClient.fed.updateBridge(); - await rskUtils.mineAndSync(rskTxHelpers); - await wait(500); - - // after 200 blocks the whitelist period has ended and we can send money to the bridge - await test.assertLock(addresses, [{ address: federationAddress, amount: INITIAL_RSK_BALANCE }]); + await sendPegin(rskTxHelper, btcTxHelper, btcAddressInfo, MINIMUM_PEGIN_VALUE_IN_BTC); + // wait for the btc to come back so we can use the sendPegin method again + await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper); + + // disable whitelisting after 20 blocks + const unlocked = await rskUtils.getUnlockedAddress(rskTxHelper, WHITELIST_CHANGE_PK, WHITELIST_CHANGE_ADDR); + expect(unlocked).to.be.true; + const disableLockWhitelistMethod = bridge.methods.setLockWhitelistDisableBlockDelay(20); + const disableResultCallback = (disableResult) => expect(Number(disableResult)).to.equal(1); + await rskUtils.sendTxWithCheck(rskTxHelper, disableLockWhitelistMethod, WHITELIST_CHANGE_ADDR, disableResultCallback); + + await btcTxHelper.mine(10); + await rskUtils.waitAndUpdateBridge(rskTxHelper); + + // address is still not able to send btc to bridge after 10 blocks + await sendPegin(rskTxHelper, btcTxHelper, btcAddressInfo, MINIMUM_PEGIN_VALUE_IN_BTC); + await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper); + + await btcTxHelper.mine(10); + await rskUtils.waitAndUpdateBridge(rskTxHelper); + + // after 20 blocks the whitelist period has ended and we can send money to the bridge + const peginBtcTxHash = await sendPegin(rskTxHelper, btcTxHelper, btcAddressInfo, MINIMUM_PEGIN_VALUE_IN_BTC); + await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper); + await ensurePeginIsRegistered(rskTxHelper, peginBtcTxHash); + + const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress)); + expect(Number(federationAddressBalanceAfterPegin)).to.be.equal(Number(federationAddressBalanceInitial + MINIMUM_PEGIN_VALUE_IN_BTC)); }); }); From 317083264fd1d6e021b7f6e88b30876ab7268637 Mon Sep 17 00:00:00 2001 From: Jan Pikora Date: Mon, 18 Dec 2023 15:49:50 +0100 Subject: [PATCH 2/6] empty commit From 81e46aaf72ca163f4120bffe38955e191c263c1a Mon Sep 17 00:00:00 2001 From: Jan Pikora Date: Mon, 18 Dec 2023 17:52:53 +0100 Subject: [PATCH 3/6] empty commit From 872a107460bda4818f609d5ef63ab4760118b09e Mon Sep 17 00:00:00 2001 From: Jan Pikora Date: Thu, 21 Dec 2023 17:49:14 +0100 Subject: [PATCH 4/6] adding assertions --- tests/01_03_52-disable_lock_whitelist.js | 52 +++++++++++++++++------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/tests/01_03_52-disable_lock_whitelist.js b/tests/01_03_52-disable_lock_whitelist.js index 85eefdec..a019555b 100644 --- a/tests/01_03_52-disable_lock_whitelist.js +++ b/tests/01_03_52-disable_lock_whitelist.js @@ -3,7 +3,7 @@ const rskUtils = require('../lib/rsk-utils'); const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); const { getBtcClient } = require('../lib/btc-client-provider'); const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util'); -const { satoshisToBtc } = require('@rsksmart/btc-eth-unit-converter'); +const { satoshisToBtc, btcToSatoshis } = require('@rsksmart/btc-eth-unit-converter'); const { sendPegin, ensurePeginIsRegistered } = require('../lib/2wp-utils'); const { WHITELIST_CHANGE_PK, WHITELIST_CHANGE_ADDR} = require('../lib/assertions/whitelisting') @@ -12,8 +12,11 @@ let rskTxHelpers; let rskTxHelper; let btcTxHelper; let bridge; -let MINIMUM_PEGIN_VALUE_IN_BTC; let federationAddress; +let MINIMUM_PEGIN_VALUE_IN_BTC; +let MINIMUM_PEGIN_VALUE_IN_SATS; +let WHITELIST_DISABLE_BLOCK_DELAY; +let FEE_IN_SATOSHI; const fulfillRequirementsToRunAsSingleTestFile = async () => { await rskUtils.activateFork(Runners.common.forks.papyrus200); @@ -22,6 +25,10 @@ const fulfillRequirementsToRunAsSingleTestFile = async () => { describe('Disable whitelisting', function() { before(async () => { + if(process.env.RUNNING_SINGLE_TEST_FILE) { + await fulfillRequirementsToRunAsSingleTestFile(); + } + rskTxHelpers = getRskTransactionHelpers(); rskTxHelper = rskTxHelpers[0]; btcTxHelper = getBtcClient(); @@ -29,42 +36,59 @@ describe('Disable whitelisting', function() { const latestActiveForkName = await getLatestActiveForkName(); bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName); federationAddress = await bridge.methods.getFederationAddress().call(); - const minPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call(); - MINIMUM_PEGIN_VALUE_IN_BTC = Number(satoshisToBtc(minPeginValueInSatoshis)); - - if(process.env.RUNNING_SINGLE_TEST_FILE) { - await fulfillRequirementsToRunAsSingleTestFile(); - } + MINIMUM_PEGIN_VALUE_IN_SATS = await bridge.methods.getMinimumLockTxValue().call(); + MINIMUM_PEGIN_VALUE_IN_BTC = Number(satoshisToBtc(MINIMUM_PEGIN_VALUE_IN_SATS)); + WHITELIST_DISABLE_BLOCK_DELAY = 20; + FEE_IN_SATOSHI = btcToSatoshis(btcTxHelper.getFee()) }); it('should disable lock whitelist', async () => { const btcAddressInfo = await btcTxHelper.generateBtcAddress('legacy'); - await btcTxHelper.fundAddress(btcAddressInfo.address, 2 * MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); - const btcAddressInfoBalanceInitial = Number(await btcTxHelper.getAddressBalance(btcAddressInfo.address)); - expect(btcAddressInfoBalanceInitial).to.be.equal(2 * MINIMUM_PEGIN_VALUE_IN_BTC + btcTxHelper.getFee()); + const btcAmountToFund = 2 * MINIMUM_PEGIN_VALUE_IN_BTC + 2 * btcTxHelper.getFee(); + await btcTxHelper.fundAddress(btcAddressInfo.address, btcAmountToFund); + const btcAddressBalanceInitial = Number(btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddressInfo.address))); + expect(btcAddressBalanceInitial).to.be.equal(Number(btcToSatoshis(btcAmountToFund))); const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress)); // address is not whitelisted await sendPegin(rskTxHelper, btcTxHelper, btcAddressInfo, MINIMUM_PEGIN_VALUE_IN_BTC); + const btcAddressBalanceAfterFirstPegin = Number(btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddressInfo.address))); + expect(btcAddressBalanceAfterFirstPegin).to.be.equal(btcAddressBalanceInitial - MINIMUM_PEGIN_VALUE_IN_SATS - FEE_IN_SATOSHI); + const federationAddressBalanceAfterFirstPegin = Number(await btcTxHelper.getAddressBalance(federationAddress)); + expect(federationAddressBalanceAfterFirstPegin).to.be.equal(federationAddressBalanceInitial + MINIMUM_PEGIN_VALUE_IN_BTC) + // wait for the btc to come back so we can use the sendPegin method again await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper); + const btcAddressBalanceAfterFirstTriggerRelease = Number(btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddressInfo.address))); + expect(btcAddressBalanceInitial - btcAddressBalanceAfterFirstTriggerRelease).to.be.at.most(FEE_IN_SATOSHI * 2) + const federationAddressBalanceAfterFirstTriggerRelease = Number(await btcTxHelper.getAddressBalance(federationAddress)); + expect(federationAddressBalanceAfterFirstTriggerRelease).to.be.equal(federationAddressBalanceInitial) // disable whitelisting after 20 blocks const unlocked = await rskUtils.getUnlockedAddress(rskTxHelper, WHITELIST_CHANGE_PK, WHITELIST_CHANGE_ADDR); expect(unlocked).to.be.true; - const disableLockWhitelistMethod = bridge.methods.setLockWhitelistDisableBlockDelay(20); + const disableLockWhitelistMethod = bridge.methods.setLockWhitelistDisableBlockDelay(WHITELIST_DISABLE_BLOCK_DELAY); const disableResultCallback = (disableResult) => expect(Number(disableResult)).to.equal(1); await rskUtils.sendTxWithCheck(rskTxHelper, disableLockWhitelistMethod, WHITELIST_CHANGE_ADDR, disableResultCallback); - await btcTxHelper.mine(10); + await btcTxHelper.mine(WHITELIST_DISABLE_BLOCK_DELAY / 2); await rskUtils.waitAndUpdateBridge(rskTxHelper); // address is still not able to send btc to bridge after 10 blocks await sendPegin(rskTxHelper, btcTxHelper, btcAddressInfo, MINIMUM_PEGIN_VALUE_IN_BTC); + const btcAddressBalanceAfterSecondPegin = Number(btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddressInfo.address))); + expect(btcAddressBalanceAfterFirstTriggerRelease - btcAddressBalanceAfterSecondPegin).to.be.at.most(Number(MINIMUM_PEGIN_VALUE_IN_SATS + FEE_IN_SATOSHI * 2)) + const federationAddressBalanceAfterSecondPegin = Number(await btcTxHelper.getAddressBalance(federationAddress)); + expect(federationAddressBalanceAfterSecondPegin).to.be.equal(federationAddressBalanceInitial + MINIMUM_PEGIN_VALUE_IN_BTC) + await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper); + const btcAddressBalanceAfterSecondTriggerRelease = Number(btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddressInfo.address))); + expect(btcAddressBalanceAfterFirstTriggerRelease - btcAddressBalanceAfterSecondTriggerRelease).to.be.at.most(FEE_IN_SATOSHI * 2) + const federationAddressBalanceAfterSecondTriggerRelease = Number(await btcTxHelper.getAddressBalance(federationAddress)); + expect(federationAddressBalanceAfterSecondTriggerRelease).to.be.equal(federationAddressBalanceInitial) - await btcTxHelper.mine(10); + await btcTxHelper.mine(WHITELIST_DISABLE_BLOCK_DELAY / 2); await rskUtils.waitAndUpdateBridge(rskTxHelper); // after 20 blocks the whitelist period has ended and we can send money to the bridge From 7c0185c54bca4e327acc070f231560cf2070835a Mon Sep 17 00:00:00 2001 From: Jan Pikora Date: Fri, 22 Dec 2023 09:40:31 +0100 Subject: [PATCH 5/6] asserting user balance in RSK --- tests/01_03_52-disable_lock_whitelist.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/01_03_52-disable_lock_whitelist.js b/tests/01_03_52-disable_lock_whitelist.js index a019555b..db90464c 100644 --- a/tests/01_03_52-disable_lock_whitelist.js +++ b/tests/01_03_52-disable_lock_whitelist.js @@ -3,8 +3,9 @@ const rskUtils = require('../lib/rsk-utils'); const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); const { getBtcClient } = require('../lib/btc-client-provider'); const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util'); -const { satoshisToBtc, btcToSatoshis } = require('@rsksmart/btc-eth-unit-converter'); +const { satoshisToBtc, btcToSatoshis, satoshisToWeis } = require('@rsksmart/btc-eth-unit-converter'); const { sendPegin, ensurePeginIsRegistered } = require('../lib/2wp-utils'); +const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); const { WHITELIST_CHANGE_PK, WHITELIST_CHANGE_ADDR} = require('../lib/assertions/whitelisting') @@ -98,5 +99,9 @@ describe('Disable whitelisting', function() { const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress)); expect(Number(federationAddressBalanceAfterPegin)).to.be.equal(Number(federationAddressBalanceInitial + MINIMUM_PEGIN_VALUE_IN_BTC)); + + const recipientRskAddressInfo = getDerivedRSKAddressInformation(btcAddressInfo.privateKey, btcTxHelper.btcConfig.network); + const recipientRskAddressBalance = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); + expect(recipientRskAddressBalance).to.be.equal(Number(satoshisToWeis(MINIMUM_PEGIN_VALUE_IN_SATS))); }); }); From 4bd22df37faa42c29422e0b0211ad29dc65e4c01 Mon Sep 17 00:00:00 2001 From: Jan Pikora Date: Thu, 11 Jan 2024 14:00:02 +0100 Subject: [PATCH 6/6] just added a constant --- tests/01_03_52-disable_lock_whitelist.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/01_03_52-disable_lock_whitelist.js b/tests/01_03_52-disable_lock_whitelist.js index db90464c..63f42994 100644 --- a/tests/01_03_52-disable_lock_whitelist.js +++ b/tests/01_03_52-disable_lock_whitelist.js @@ -18,6 +18,7 @@ let MINIMUM_PEGIN_VALUE_IN_BTC; let MINIMUM_PEGIN_VALUE_IN_SATS; let WHITELIST_DISABLE_BLOCK_DELAY; let FEE_IN_SATOSHI; +const DELAY_SET_SUCCSSFULY = 1; const fulfillRequirementsToRunAsSingleTestFile = async () => { await rskUtils.activateFork(Runners.common.forks.papyrus200); @@ -70,7 +71,7 @@ describe('Disable whitelisting', function() { const unlocked = await rskUtils.getUnlockedAddress(rskTxHelper, WHITELIST_CHANGE_PK, WHITELIST_CHANGE_ADDR); expect(unlocked).to.be.true; const disableLockWhitelistMethod = bridge.methods.setLockWhitelistDisableBlockDelay(WHITELIST_DISABLE_BLOCK_DELAY); - const disableResultCallback = (disableResult) => expect(Number(disableResult)).to.equal(1); + const disableResultCallback = (disableResult) => expect(Number(disableResult)).to.equal(DELAY_SET_SUCCSSFULY); await rskUtils.sendTxWithCheck(rskTxHelper, disableLockWhitelistMethod, WHITELIST_CHANGE_ADDR, disableResultCallback); await btcTxHelper.mine(WHITELIST_DISABLE_BLOCK_DELAY / 2);