Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Pikora authored and Jan Pikora committed Dec 15, 2023
1 parent 5f6fadc commit da9f531
Showing 1 changed file with 59 additions and 102 deletions.
161 changes: 59 additions & 102 deletions tests/01_03_52-disable_lock_whitelist.js
Original file line number Diff line number Diff line change
@@ -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));
});
});

0 comments on commit da9f531

Please sign in to comment.