Skip to content

Commit

Permalink
Merge pull request #5 from rsksmart/refactor/01_03_06-2wp_no_locking_cap
Browse files Browse the repository at this point in the history
refactor of 01_03_06-2wp_no_locking_cap.js
  • Loading branch information
marcos-iov authored Sep 11, 2023
2 parents 6e2e837 + 753cc89 commit ae3be4e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 99 deletions.
2 changes: 1 addition & 1 deletion lib/precompiled-abi-forks-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const getPreForkName = (currentForkName) => {

/**
* Returns the most recent activated fork name
* @returns {string} the most recent activated fork name
* @returns {Promise<string>} the most recent activated fork name
*/
const getLatestActiveForkName = async () => {
const forks = Object.values(Runners.common.forks);
Expand Down
161 changes: 63 additions & 98 deletions tests/01_03_06-2wp_no_locking_cap.js
Original file line number Diff line number Diff line change
@@ -1,116 +1,81 @@
const expect = require('chai').expect
var { sequentialPromise, wait } = require('../lib/utils');
const peglib = require('peglib');
const bitcoin = peglib.bitcoin;
const rsk = peglib.rsk;
const pegUtils = peglib.pegUtils;
const pegAssertions = require('../lib/assertions/2wp');
const whitelistingAssertionsLegacy = require('../lib/assertions/whitelisting-legacy');
const CustomError = require('../lib/CustomError');
const rskUtils = require('../lib/rsk-utils');
const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');
const { getBtcClient } = require('../lib/btc-client-provider');
const { sendPegin, ensurePeginIsRegistered, sendTxToBridge, MINIMUM_PEGIN_VALUE_IN_BTC } = require('../lib/2wp-utils');
const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util');
const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation');
const btcEthUnitConverter = require('btc-eth-unit-converter');
const whitelistingAssertions = require('../lib/assertions/whitelisting');

describe('Transfer BTC to RBTC before papyrus200', function() {

var federationAddress;
var btcClient;
var rskClient;
var pegClient;
var test;
let whitelistTestLegacy;
let rskTxHelpers;
let btcTxHelper;
let rskTxHelper;

const NETWORK = bitcoin.networks.testnet;
const PEGIN_VALUE_IN_BTC = 3 * MINIMUM_PEGIN_VALUE_IN_BTC;
const PEGOUT_VALUE_IN_RBTC = PEGIN_VALUE_IN_BTC / 3;
const RSK_TX_FEE_IN_RBTC = 0.001;

const fulfillRequirementsToRunAsSingleTestFile = async () => {
await rskUtils.activateFork(Runners.common.forks.wasabi100);
};

before(async () => {
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);
whitelistTestLegacy = whitelistingAssertionsLegacy.with(btcClient, rskClient, pegClient);
rskTxHelpers = getRskTransactionHelpers();
btcTxHelper = getBtcClient();

// Grab the federation address
federationAddress = await rskClient.rsk.bridge.methods.getFederationAddress().call();
await btcClient.importAddress(federationAddress, 'federations');
rskTxHelpers = getRskTransactionHelpers();
rskTxHelper = rskTxHelpers[0];
btcTxHelper = getBtcClient();

if(process.env.RUNNING_SINGLE_TEST_FILE) {
await fulfillRequirementsToRunAsSingleTestFile();
};
});


it('should do a multiple pegouts from the same rsk address after making a pegin', async () => {
const btcAddressInfo = await btcTxHelper.generateBtcAddress('legacy');
await whitelistingAssertions.assertAddLimitedLockWhitelistAddress(rskTxHelper, btcAddressInfo.address, btcEthUnitConverter.btcToSatoshis(PEGIN_VALUE_IN_BTC));
await rskUtils.mineAndSync(rskTxHelpers);

const recipientRskAddressInfo = getDerivedRSKAddressInformation(btcAddressInfo.privateKey, btcTxHelper.btcConfig.network);
await rskTxHelper.importAccount(recipientRskAddressInfo.privateKey);
const unlocked = await rskTxHelper.unlockAccount(recipientRskAddressInfo.address);
expect(unlocked, 'Account was not unlocked').to.be.true;

const latestActiveForkName = await getLatestActiveForkName();
const bridge = getBridge(rskTxHelper.getClient(), latestActiveForkName);
const federationAddress = await bridge.methods.getFederationAddress().call();

const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress));

await btcTxHelper.fundAddress(btcAddressInfo.address, PEGIN_VALUE_IN_BTC + btcTxHelper.getFee());

// Mine a few rsk blocks to prevent being at the beginning of the chain,
// which could trigger border cases we're not interested in
await sequentialPromise(10, () => rskUtils.mineAndSync(rskTxHelpers));
const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, btcAddressInfo, PEGIN_VALUE_IN_BTC);
await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash);
const recipientRskAddressBalanceAfterPegin = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address));
expect(Number(recipientRskAddressBalanceAfterPegin)).to.be.equal(btcEthUnitConverter.btcToWeis(PEGIN_VALUE_IN_BTC));

await sendTxToBridge(rskTxHelper, PEGOUT_VALUE_IN_RBTC, recipientRskAddressInfo.address);
await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper);

const federationAddressBalanceAfterFirstPegout = Number(await btcTxHelper.getAddressBalance(federationAddress));
expect(Number(federationAddressBalanceAfterFirstPegout)).to.be.equal(Number(federationAddressBalanceInitial + PEGIN_VALUE_IN_BTC - PEGOUT_VALUE_IN_RBTC));

const senderAddressBalanceAfterFirstPegout = Number(await btcTxHelper.getAddressBalance(btcAddressInfo.address));
expect(Number(senderAddressBalanceAfterFirstPegout)).to.be.above(PEGOUT_VALUE_IN_RBTC - btcTxHelper.getFee()).and.below(PEGOUT_VALUE_IN_RBTC);

const recipientRskAddressBalanceAfterFirstPegout = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address));
expect(Number(recipientRskAddressBalanceAfterFirstPegout)).to.be.above(btcEthUnitConverter.btcToWeis(PEGIN_VALUE_IN_BTC - PEGOUT_VALUE_IN_RBTC - RSK_TX_FEE_IN_RBTC)).and.below(btcEthUnitConverter.btcToWeis(PEGIN_VALUE_IN_BTC - PEGOUT_VALUE_IN_RBTC));

// // Update the bridge to sync btc blockchains
await rskClient.fed.updateBridge();
await rskUtils.mineAndSync(rskTxHelpers);
await sendTxToBridge(rskTxHelper, PEGOUT_VALUE_IN_RBTC, recipientRskAddressInfo.address);
await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper);

return federationAddress;
});
const senderAddressBalanceAfterSecondPegout = Number(await btcTxHelper.getAddressBalance(btcAddressInfo.address));
expect(Number(senderAddressBalanceAfterSecondPegout)).to.be.above(senderAddressBalanceAfterFirstPegout + PEGOUT_VALUE_IN_RBTC - btcTxHelper.getFee()).and.below(senderAddressBalanceAfterFirstPegout + PEGOUT_VALUE_IN_RBTC);

it('should transfer BTC to RBTC', async () => {
try {
var initialFedBalance = (await btcClient.getAddressBalance(federationAddress))[federationAddress] || 0;
expect(initialFedBalance).to.be.lessThan(bitcoin.btcToSatoshis(1000));

const INITIAL_RSK_BALANCE = bitcoin.btcToSatoshis(1000) + bitcoin.btcToSatoshis(1);
const INITIAL_BTC_BALANCE = INITIAL_RSK_BALANCE + bitcoin.btcToSatoshis(1);
const TO_BRIDGE_GAS_PRICE = 1;
const WEIS_TO_RELEASE = '500000000000000000000';

var addresses = await pegClient.generateNewAddress('test');
expect(addresses.inRSK).to.be.true;

await whitelistTestLegacy.assertAddLimitedLockWhitelistAddress(addresses.btc, INITIAL_BTC_BALANCE)();
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);
await test.assertLock(addresses, [{ address: federationAddress, amount: INITIAL_RSK_BALANCE }]);

var lockedFedBalance = Number((await btcClient.getAddressBalance(federationAddress))[federationAddress]);
expect(lockedFedBalance).to.be.greaterThan(bitcoin.btcToSatoshis(1000) + initialFedBalance);

await rskClient.eth.personal.unlockAccount(addresses.rsk, '');

//Need to call sendTx in order to make it always with value < 1000, otherwise it fails
await rskClient.rsk.sendTx({
from: addresses.rsk,
to: rsk.getBridgeAddress(),
value: WEIS_TO_RELEASE,
gasPrice: TO_BRIDGE_GAS_PRICE
}, rskClient.evm.mine);

//Call release after sendTx so requests are released. Goes one by one
await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper);

await wait(500);

var afterFirstReleaseFedBalance = Number((await btcClient.getAddressBalance(federationAddress))[federationAddress]);
expect(afterFirstReleaseFedBalance).to.be.at.most(INITIAL_BTC_BALANCE / 2 + initialFedBalance);

//Need to call sendTx in order to make it always with value < 1000, otherwise it fails
await rskClient.rsk.sendTx({
from: addresses.rsk,
to: rsk.getBridgeAddress(),
value: WEIS_TO_RELEASE,
gasPrice: TO_BRIDGE_GAS_PRICE
}, rskClient.evm.mine);

//Call release after sendTx so requests are released. Goes one by one
await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper);

var finalFedBalance = Number((await btcClient.getAddressBalance(federationAddress))[federationAddress]);
expect(finalFedBalance).to.be.at.most(initialFedBalance + bitcoin.btcToSatoshis(1));
}
catch (err) {
throw new CustomError('Transfer BTC to RBTC failure', err);
}
const recipientRskAddressBalanceAfterSecondPegout = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address));
expect(Number(recipientRskAddressBalanceAfterSecondPegout)).to.be.above(recipientRskAddressBalanceAfterFirstPegout - btcEthUnitConverter.btcToWeis(PEGOUT_VALUE_IN_RBTC + RSK_TX_FEE_IN_RBTC)).and.below(recipientRskAddressBalanceAfterFirstPegout - btcEthUnitConverter.btcToWeis(PEGOUT_VALUE_IN_RBTC));
});
});

0 comments on commit ae3be4e

Please sign in to comment.