From ff1810588ac1e627afe36ded277a4c9e022b57e4 Mon Sep 17 00:00:00 2001
From: Jan Pikora <honza@Jans-MacBook-Pro.local>
Date: Wed, 27 Sep 2023 11:15:51 +0200
Subject: [PATCH 1/6] work in progress

---
 lib/2wp-utils.js                        | 24 ++++++++++++++++++++--
 tests/02_00_02-2wp_segwit_compatible.js | 27 ++++++++++++++++++++++---
 2 files changed, 46 insertions(+), 5 deletions(-)

diff --git a/lib/2wp-utils.js b/lib/2wp-utils.js
index d0bf1be1..fac6d6d5 100644
--- a/lib/2wp-utils.js
+++ b/lib/2wp-utils.js
@@ -1,5 +1,5 @@
 const expect = require('chai').expect;
-const { sendFromCow, mineAndSync } = require('./rsk-utils');
+const { sendFromCow, mineAndSync, sendTxWithCheck } = require('./rsk-utils');
 const { wait, retryWithCheck } = require('./utils');
 const { getBridge, getLatestActiveForkName } = require('./precompiled-abi-forks-util');
 const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');
@@ -239,6 +239,25 @@ const donateToBridge = async (rskTxHelper, btcTxHelper, donatingBtcAddressInform
     return peginBtcTxHash;
 };
 
+const disableWhitelisting = async (rskTxHelper) => {
+    const latestActiveForkName = await getLatestActiveForkName();
+    console.log(`latestActiveForkName - disableWhitelisting: ${latestActiveForkName}`)
+    const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);
+
+    const addr = await rskTxHelper.importAccount(WHITELIST_CHANGE_PK)
+    expect(addr.slice(2)).to.equal(WHITELIST_CHANGE_ADDR);
+    const unclock = await rskTxHelper.unlockAccount(addr);
+    console.log(`unlock: ${unclock}`)
+
+    await sendTxWithCheck(
+        rskTxHelper,
+        bridge.methods.setLockWhitelistDisableBlockDelay(1),
+        WHITELIST_CHANGE_ADDR),
+        (disableResult) => expect(Number(disableResult)).to.equal(1)();
+
+    rskTxHelper.mine(1);
+}
+
 module.exports = {
     sendTxToBridge,
     assertRefundUtxosSameAsPeginUtxos,
@@ -251,5 +270,6 @@ module.exports = {
     BRIDGE_ADDRESS,
     createPeginV1TxData,
     mineForPeginRegistration,
-    MIN_PEGOUT_VALUE_IN_RBTC
+    MIN_PEGOUT_VALUE_IN_RBTC,
+    disableWhitelisting
 };
diff --git a/tests/02_00_02-2wp_segwit_compatible.js b/tests/02_00_02-2wp_segwit_compatible.js
index 636e5a5d..f36b44b6 100644
--- a/tests/02_00_02-2wp_segwit_compatible.js
+++ b/tests/02_00_02-2wp_segwit_compatible.js
@@ -3,7 +3,7 @@ const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivatio
 const btcEthUnitConverter = require('btc-eth-unit-converter');
 const rskUtils = require('../lib/rsk-utils');
 const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');
-const { sendPegin, ensurePeginIsRegistered } = require('../lib/2wp-utils');
+const { sendPegin, ensurePeginIsRegistered, disableWhitelisting } = require('../lib/2wp-utils');
 const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util');
 const { getBtcClient } = require('../lib/btc-client-provider');
 
@@ -11,9 +11,11 @@ let rskTxHelpers;
 let rskTxHelper;
 let btcTxHelper;
 
-const fulfillRequirementsToRunAsSingleTestFile = async () => {
+const fulfillRequirementsToRunAsSingleTestFile = async (rskTxHelper) => {
     const latestForkName = rskUtils.getLatestForkName()
+    console.log(`latestForkName: ${JSON.stringify(latestForkName)}`)
     await rskUtils.activateFork(latestForkName);
+    disableWhitelisting(rskTxHelper)
 };
 
 describe('Lock using p2sh-p2wpkh address', () => {
@@ -23,12 +25,29 @@ describe('Lock using p2sh-p2wpkh address', () => {
         btcTxHelper = getBtcClient();
 
         if(process.env.RUNNING_SINGLE_TEST_FILE) {
-            await fulfillRequirementsToRunAsSingleTestFile();
+            await fulfillRequirementsToRunAsSingleTestFile(rskTxHelper);
+            // const latestActiveForkName = await getLatestActiveForkName();
+            // console.log(`latestActiveForkName: ${latestActiveForkName}`)
+            // bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);
+            
+            // const addr = await rskTxHelper.importAccount(WHITELIST_CHANGE_PK)
+            // console.log(`addr: ${addr}`)
+            // expect(addr.slice(2)).to.equal(WHITELIST_CHANGE_ADDR);
+            // await rskTxHelper.unlockAccount(addr);
+
+            // await rskUtils.sendTxWithCheck(
+            //     rskTxHelper,
+            //     bridge.methods.setLockWhitelistDisableBlockDelay(1),
+            //     WHITELIST_CHANGE_ADDR),
+            //     (disableResult) => expect(Number(disableResult)).to.equal(1)();
+
+            // rskTxHelper.mine(1);
         }
     });
 
     it('should do a legacy pegin using p2sh-p2wpkh address', async () => {
         const latestActiveForkName = await getLatestActiveForkName();
+        console.log(`latestActiveForkName: ${latestActiveForkName}`)
         const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);
 
         const minimumPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call();
@@ -36,6 +55,7 @@ describe('Lock using p2sh-p2wpkh address', () => {
 
         const federationAddress = await bridge.methods.getFederationAddress().call();
         const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress));
+        console.log(`federationAddressBalanceInitial: ${federationAddressBalanceInitial}`)
 
         const senderAddressInfo = await btcTxHelper.generateBtcAddress('p2sh-segwit');
         const recipientRskAddressInfo = getDerivedRSKAddressInformation(senderAddressInfo.privateKey, btcTxHelper.btcConfig.network);
@@ -49,6 +69,7 @@ describe('Lock using p2sh-p2wpkh address', () => {
         await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash);
 
         const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress));
+        console.log(`federationAddressBalanceAfterPegin: ${federationAddressBalanceAfterPegin}`)
         expect(federationAddressBalanceAfterPegin).to.be.equal(Number(federationAddressBalanceInitial + minimumPeginValueInBtc));
 
         const senderAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(senderAddressInfo.address));

From 875c7af6477f53f3c54ed1bff406bf731b6a5f7e Mon Sep 17 00:00:00 2001
From: Jan Pikora <honza@Jans-MacBook-Pro.local>
Date: Wed, 27 Sep 2023 11:54:34 +0200
Subject: [PATCH 2/6] parentheses mismatch fix

---
 lib/2wp-utils.js | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/2wp-utils.js b/lib/2wp-utils.js
index fac6d6d5..47411888 100644
--- a/lib/2wp-utils.js
+++ b/lib/2wp-utils.js
@@ -246,15 +246,15 @@ const disableWhitelisting = async (rskTxHelper) => {
 
     const addr = await rskTxHelper.importAccount(WHITELIST_CHANGE_PK)
     expect(addr.slice(2)).to.equal(WHITELIST_CHANGE_ADDR);
-    const unclock = await rskTxHelper.unlockAccount(addr);
-    console.log(`unlock: ${unclock}`)
+    const unlock = await rskTxHelper.unlockAccount(addr);
+    console.log(`unlock: ${unlock}`)
 
     await sendTxWithCheck(
         rskTxHelper,
         bridge.methods.setLockWhitelistDisableBlockDelay(1),
-        WHITELIST_CHANGE_ADDR),
-        (disableResult) => expect(Number(disableResult)).to.equal(1)();
-
+        WHITELIST_CHANGE_ADDR,
+        (disableResult) =>  expect(Number(disableResult)).to.equal(1)()
+    );
     rskTxHelper.mine(1);
 }
 

From 825ecadb37dfd9197014cda4811cdf9fe94ea41c Mon Sep 17 00:00:00 2001
From: Jan Pikora <honza@Jans-MacBook-Pro.local>
Date: Wed, 27 Sep 2023 16:50:29 +0200
Subject: [PATCH 3/6] adding missing await, removing comments

---
 lib/2wp-utils.js                        |  6 ++----
 tests/02_00_02-2wp_segwit_compatible.js | 22 +---------------------
 2 files changed, 3 insertions(+), 25 deletions(-)

diff --git a/lib/2wp-utils.js b/lib/2wp-utils.js
index 47411888..806bb885 100644
--- a/lib/2wp-utils.js
+++ b/lib/2wp-utils.js
@@ -241,19 +241,17 @@ const donateToBridge = async (rskTxHelper, btcTxHelper, donatingBtcAddressInform
 
 const disableWhitelisting = async (rskTxHelper) => {
     const latestActiveForkName = await getLatestActiveForkName();
-    console.log(`latestActiveForkName - disableWhitelisting: ${latestActiveForkName}`)
     const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);
 
     const addr = await rskTxHelper.importAccount(WHITELIST_CHANGE_PK)
     expect(addr.slice(2)).to.equal(WHITELIST_CHANGE_ADDR);
-    const unlock = await rskTxHelper.unlockAccount(addr);
-    console.log(`unlock: ${unlock}`)
+    await rskTxHelper.unlockAccount(addr);
 
     await sendTxWithCheck(
         rskTxHelper,
         bridge.methods.setLockWhitelistDisableBlockDelay(1),
         WHITELIST_CHANGE_ADDR,
-        (disableResult) =>  expect(Number(disableResult)).to.equal(1)()
+        (disableResult) =>  expect(Number(disableResult)).to.equal(1)
     );
     rskTxHelper.mine(1);
 }
diff --git a/tests/02_00_02-2wp_segwit_compatible.js b/tests/02_00_02-2wp_segwit_compatible.js
index f36b44b6..0be4dc37 100644
--- a/tests/02_00_02-2wp_segwit_compatible.js
+++ b/tests/02_00_02-2wp_segwit_compatible.js
@@ -13,9 +13,8 @@ let btcTxHelper;
 
 const fulfillRequirementsToRunAsSingleTestFile = async (rskTxHelper) => {
     const latestForkName = rskUtils.getLatestForkName()
-    console.log(`latestForkName: ${JSON.stringify(latestForkName)}`)
     await rskUtils.activateFork(latestForkName);
-    disableWhitelisting(rskTxHelper)
+    await disableWhitelisting(rskTxHelper)
 };
 
 describe('Lock using p2sh-p2wpkh address', () => {
@@ -26,28 +25,11 @@ describe('Lock using p2sh-p2wpkh address', () => {
 
         if(process.env.RUNNING_SINGLE_TEST_FILE) {
             await fulfillRequirementsToRunAsSingleTestFile(rskTxHelper);
-            // const latestActiveForkName = await getLatestActiveForkName();
-            // console.log(`latestActiveForkName: ${latestActiveForkName}`)
-            // bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);
-            
-            // const addr = await rskTxHelper.importAccount(WHITELIST_CHANGE_PK)
-            // console.log(`addr: ${addr}`)
-            // expect(addr.slice(2)).to.equal(WHITELIST_CHANGE_ADDR);
-            // await rskTxHelper.unlockAccount(addr);
-
-            // await rskUtils.sendTxWithCheck(
-            //     rskTxHelper,
-            //     bridge.methods.setLockWhitelistDisableBlockDelay(1),
-            //     WHITELIST_CHANGE_ADDR),
-            //     (disableResult) => expect(Number(disableResult)).to.equal(1)();
-
-            // rskTxHelper.mine(1);
         }
     });
 
     it('should do a legacy pegin using p2sh-p2wpkh address', async () => {
         const latestActiveForkName = await getLatestActiveForkName();
-        console.log(`latestActiveForkName: ${latestActiveForkName}`)
         const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);
 
         const minimumPeginValueInSatoshis = await bridge.methods.getMinimumLockTxValue().call();
@@ -55,7 +37,6 @@ describe('Lock using p2sh-p2wpkh address', () => {
 
         const federationAddress = await bridge.methods.getFederationAddress().call();
         const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress));
-        console.log(`federationAddressBalanceInitial: ${federationAddressBalanceInitial}`)
 
         const senderAddressInfo = await btcTxHelper.generateBtcAddress('p2sh-segwit');
         const recipientRskAddressInfo = getDerivedRSKAddressInformation(senderAddressInfo.privateKey, btcTxHelper.btcConfig.network);
@@ -69,7 +50,6 @@ describe('Lock using p2sh-p2wpkh address', () => {
         await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash);
 
         const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress));
-        console.log(`federationAddressBalanceAfterPegin: ${federationAddressBalanceAfterPegin}`)
         expect(federationAddressBalanceAfterPegin).to.be.equal(Number(federationAddressBalanceInitial + minimumPeginValueInBtc));
 
         const senderAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(senderAddressInfo.address));

From 58e05690e00dd88cee8fa4dec72d82dd3f0b3c0c Mon Sep 17 00:00:00 2001
From: Jan Pikora <honza@Jans-MacBook-Pro.local>
Date: Fri, 29 Sep 2023 15:58:48 +0200
Subject: [PATCH 4/6] review comments updates

---
 lib/2wp-utils.js                        | 20 ++++++++++++++------
 tests/02_00_02-2wp_segwit_compatible.js |  6 +++---
 2 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/lib/2wp-utils.js b/lib/2wp-utils.js
index 806bb885..76a88880 100644
--- a/lib/2wp-utils.js
+++ b/lib/2wp-utils.js
@@ -6,6 +6,7 @@ const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');
 const btcEthUnitConverter = require('btc-eth-unit-converter');
 const peginVerifier = require('pegin-address-verificator');
 const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');
+const { WHITELIST_CHANGE_PK, WHITELIST_CHANGE_ADDR} = require('../lib/assertions/whitelisting')
 
 const ADDRESS_TYPES_CODES = {
     p2pkh: '01',
@@ -239,21 +240,28 @@ const donateToBridge = async (rskTxHelper, btcTxHelper, donatingBtcAddressInform
     return peginBtcTxHash;
 };
 
-const disableWhitelisting = async (rskTxHelper) => {
+/**
+ * Disabling whitelisting
+ * @param {RskTransactionHelper} rskTxHelper 
+ * @param {BtcTransactionHelper} btcTxHelper 
+ */
+const disableWhitelisting = async (rskTxHelper, btcTxHelper) => {
+    btcBlocksToMine = 1;
     const latestActiveForkName = await getLatestActiveForkName();
     const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);
 
-    const addr = await rskTxHelper.importAccount(WHITELIST_CHANGE_PK)
-    expect(addr.slice(2)).to.equal(WHITELIST_CHANGE_ADDR);
-    await rskTxHelper.unlockAccount(addr);
+    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(1),
+        bridge.methods.setLockWhitelistDisableBlockDelay(btcBlocksToMine),
         WHITELIST_CHANGE_ADDR,
         (disableResult) =>  expect(Number(disableResult)).to.equal(1)
     );
-    rskTxHelper.mine(1);
+
+    btcTxHelper.mine(btcBlocksToMine);
 }
 
 module.exports = {
diff --git a/tests/02_00_02-2wp_segwit_compatible.js b/tests/02_00_02-2wp_segwit_compatible.js
index 0be4dc37..ff2d9690 100644
--- a/tests/02_00_02-2wp_segwit_compatible.js
+++ b/tests/02_00_02-2wp_segwit_compatible.js
@@ -11,10 +11,10 @@ let rskTxHelpers;
 let rskTxHelper;
 let btcTxHelper;
 
-const fulfillRequirementsToRunAsSingleTestFile = async (rskTxHelper) => {
+const fulfillRequirementsToRunAsSingleTestFile = async (rskTxHelper, btcTxHelper) => {
     const latestForkName = rskUtils.getLatestForkName()
     await rskUtils.activateFork(latestForkName);
-    await disableWhitelisting(rskTxHelper)
+    await disableWhitelisting(rskTxHelper, btcTxHelper)
 };
 
 describe('Lock using p2sh-p2wpkh address', () => {
@@ -24,7 +24,7 @@ describe('Lock using p2sh-p2wpkh address', () => {
         btcTxHelper = getBtcClient();
 
         if(process.env.RUNNING_SINGLE_TEST_FILE) {
-            await fulfillRequirementsToRunAsSingleTestFile(rskTxHelper);
+            await fulfillRequirementsToRunAsSingleTestFile(rskTxHelper, btcTxHelper);
         }
     });
 

From 2f25bd48da18c87785bbf39cc9898047cf6fab9f Mon Sep 17 00:00:00 2001
From: Jan Pikora <honza@Jans-MacBook-Pro.local>
Date: Fri, 29 Sep 2023 16:12:56 +0200
Subject: [PATCH 5/6] assertion update, adding some semicolon

---
 lib/2wp-utils.js                        | 2 +-
 tests/02_00_02-2wp_segwit_compatible.js | 4 ++--
 tests/02_00_06-2wp-new-minimum.js       | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/2wp-utils.js b/lib/2wp-utils.js
index 76a88880..6c6dba11 100644
--- a/lib/2wp-utils.js
+++ b/lib/2wp-utils.js
@@ -250,7 +250,7 @@ const disableWhitelisting = async (rskTxHelper, btcTxHelper) => {
     const latestActiveForkName = await getLatestActiveForkName();
     const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);
 
-    const whitelistChangeAddress = await rskTxHelper.importAccount(WHITELIST_CHANGE_PK)
+    const whitelistChangeAddress = await rskTxHelper.importAccount(WHITELIST_CHANGE_PK);
     expect(whitelistChangeAddress.slice(2)).to.equal(WHITELIST_CHANGE_ADDR);
     await rskTxHelper.unlockAccount(whitelistChangeAddress);
 
diff --git a/tests/02_00_02-2wp_segwit_compatible.js b/tests/02_00_02-2wp_segwit_compatible.js
index ff2d9690..12a0670c 100644
--- a/tests/02_00_02-2wp_segwit_compatible.js
+++ b/tests/02_00_02-2wp_segwit_compatible.js
@@ -12,9 +12,9 @@ let rskTxHelper;
 let btcTxHelper;
 
 const fulfillRequirementsToRunAsSingleTestFile = async (rskTxHelper, btcTxHelper) => {
-    const latestForkName = rskUtils.getLatestForkName()
+    const latestForkName = rskUtils.getLatestForkName();
     await rskUtils.activateFork(latestForkName);
-    await disableWhitelisting(rskTxHelper, btcTxHelper)
+    await disableWhitelisting(rskTxHelper, btcTxHelper);
 };
 
 describe('Lock using p2sh-p2wpkh address', () => {
diff --git a/tests/02_00_06-2wp-new-minimum.js b/tests/02_00_06-2wp-new-minimum.js
index 844d1180..894877d6 100644
--- a/tests/02_00_06-2wp-new-minimum.js
+++ b/tests/02_00_06-2wp-new-minimum.js
@@ -102,7 +102,7 @@ describe('2wp after iris300, using new minimum values', () => {
         expect(senderAddressBalanceFinal).to.be.equal(0);
 
         const federationAddressBalanceFinal = Number(await btcTxHelper.getAddressBalance(federationAddress));
-        expect(federationAddressBalanceFinal).to.be.equal(Number(federationAddressBalanceInitial + BELOW_MIN_PEGIN_VALUE_IN_BTC));
+        expect(btcEthUnitConverter.btcToSatoshis(federationAddressBalanceFinal)).to.be.equal(btcEthUnitConverter.btcToSatoshis(federationAddressBalanceInitial + BELOW_MIN_PEGIN_VALUE_IN_BTC));
 
         const bridgeAddressBalanceFinal = Number(await rskTxHelper.getBalance(BRIDGE_ADDRESS));
         expect(bridgeAddressBalanceFinal).to.be.equal(bridgeAddressBalanceInitial)

From dc14c7624ba20a687dd63c35f9464ebfa74d06b9 Mon Sep 17 00:00:00 2001
From: Jan Pikora <honza@Jans-MacBook-Pro.local>
Date: Fri, 29 Sep 2023 16:52:29 +0200
Subject: [PATCH 6/6] adding const

---
 lib/2wp-utils.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/2wp-utils.js b/lib/2wp-utils.js
index 6c6dba11..2496654c 100644
--- a/lib/2wp-utils.js
+++ b/lib/2wp-utils.js
@@ -246,7 +246,7 @@ const donateToBridge = async (rskTxHelper, btcTxHelper, donatingBtcAddressInform
  * @param {BtcTransactionHelper} btcTxHelper 
  */
 const disableWhitelisting = async (rskTxHelper, btcTxHelper) => {
-    btcBlocksToMine = 1;
+    const btcBlocksToMine = 1;
     const latestActiveForkName = await getLatestActiveForkName();
     const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);