Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Pikora authored and Jan Pikora committed Sep 27, 2023
1 parent 968f120 commit 2bf7649
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
24 changes: 23 additions & 1 deletion 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 } = 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');
Expand All @@ -15,6 +15,8 @@ const ADDRESS_TYPES_CODES = {
const BTC_TO_RSK_MINIMUM_CONFIRMATIONS = 3;
const TO_BRIDGE_GAS_PRICE = 2;
const BRIDGE_ADDRESS = '0x0000000000000000000000000000000001000006';
const WHITELIST_CHANGE_PK = '3890187a3071327cee08467ba1b44ed4c13adb2da0d5ffcc0563c371fa88259c';
const WHITELIST_CHANGE_ADDR = '87d2a0f33744929da08b65fd62b627ea52b25f8e';

/**
*
Expand Down Expand Up @@ -238,6 +240,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,
Expand All @@ -250,4 +271,5 @@ module.exports = {
BRIDGE_ADDRESS,
createPeginV1TxData,
mineForPeginRegistration,
disableWhitelisting
};
27 changes: 24 additions & 3 deletions tests/02_00_02-2wp_segwit_compatible.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ 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');

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', () => {
Expand All @@ -23,19 +25,37 @@ 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();
const minimumPeginValueInBtc = btcEthUnitConverter.satoshisToBtc(minimumPeginValueInSatoshis);

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);
Expand All @@ -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));
Expand Down

0 comments on commit 2bf7649

Please sign in to comment.