Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes the 2wp-utils-legacy.js file #31

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions lib/2wp-utils-legacy.js

This file was deleted.

9 changes: 8 additions & 1 deletion lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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 { getLogger } = require('../logger');
const { MAX_ESTIMATED_FEE_PER_PEGOUT, FEE_DIFFERENCE_PER_PEGOUT } = require('../lib/constants');

const BTC_TO_RSK_MINIMUM_CONFIRMATIONS = 3;
const TO_BRIDGE_GAS_PRICE = 2;
Expand Down Expand Up @@ -89,7 +90,7 @@ const createPegoutRequest = async (rskTxHelper, amountInRBTC, requestSize = 1) =
const AMOUNT_IN_WEIS = Number(btcEthUnitConverter.btcToWeis(amountInRBTC));
const RSK_TX_FEE_IN_WEIS = Number(btcEthUnitConverter.btcToWeis(1));
const PEGOUT_AMOUNT_PLUS_FEE = (AMOUNT_IN_WEIS + RSK_TX_FEE_IN_WEIS) * requestSize;
const rskAddress = await rskTxHelper.newAccountWithSeed('test');
const rskAddress = await rskTxHelper.newAccountWithSeed(Math.random().toString(16));
await sendFromCow(rskTxHelper, rskAddress, PEGOUT_AMOUNT_PLUS_FEE);
await rskTxHelper.unlockAccount(rskAddress);
for (let i = 0; i < requestSize; i++) {
Expand Down Expand Up @@ -227,6 +228,11 @@ const disableWhitelisting = async (rskTxHelper, btcTxHelper, blockDelay = 1) =>
}
};

const getPegoutEstimatedFees = expectedCount => {
const estimatedFees = expectedCount > 0 ? MAX_ESTIMATED_FEE_PER_PEGOUT + (expectedCount - 1) * FEE_DIFFERENCE_PER_PEGOUT : 0;
return estimatedFees;
};

module.exports = {
sendTxToBridge,
assertRefundUtxosSameAsPeginUtxos,
Expand All @@ -240,4 +246,5 @@ module.exports = {
mineForPeginRegistration,
MIN_PEGOUT_VALUE_IN_RBTC,
disableWhitelisting,
getPegoutEstimatedFees,
};
5 changes: 2 additions & 3 deletions lib/bitcoin-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ BitcoinRunner.prototype.start = function() {
}, {
host: '127.0.0.1',
port: this.ports.rpc
}],
{
numRetries: 100,
}], {
numRetries: 120,
retryInterval: 1000
}).then((r) => {
this.running = true;
Expand Down
2 changes: 1 addition & 1 deletion lib/federate-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ FederateRunner.prototype.start = function() {
host: '127.0.0.1',
port: this.ports.rpc
}], {
numRetries: 100,
numRetries: 120,
retryInterval: 1000
}).then((r) => {
this.running = true;
Expand Down
6 changes: 3 additions & 3 deletions lib/rsk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ const waitForSync = async (rskTransactionHelpers) => {
* If the blockchain is at least advancing, we know that some time in the future the `blockNumber` will be reached, so no need to stop trying to find it.
* @param {Web3} rskClient web3 client to make calls to the rsk network.
* @param {Number} blockNumber min block height to wait for.
* @param {Number} waitTime defaults to 200 milliseconds. Time to wait before checking for the block on every iteration.
* @param {Number} maxAttempts defaults to 80 attempts by block.
* @param {Number} waitTime defaults to 500 milliseconds. Time to wait before checking for the block on every iteration.
* @param {Number} maxAttempts defaults to 200 attempts by block.
* @returns {Promise<Number>} the latest block number the same or greater than `blockNumber`.
*/
const waitForBlock = (rskClient, blockNumber, waitTime = 200, maxAttempts = 80) => {
const waitForBlock = (rskClient, blockNumber, waitTime = 500, maxAttempts = 200) => {
return new Promise((resolve, reject) => {
let attempts = 1;
let latestBlockNumber = -1;
Expand Down
92 changes: 63 additions & 29 deletions tests/01_05_52-post_hop_pegout_batching_first_requests.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,96 @@
const { bitcoin, rsk, pegUtils } = require('peglib');
const NETWORK = bitcoin.networks.testnet;
const CustomError = require('../lib/CustomError');
const _2wpUtilsLegacy = require('../lib/2wp-utils-legacy');
const pegAssertions = require('../lib/assertions/2wp');
const _2wpUtils = require('../lib/2wp-utils');
const { getRskTransactionHelper } = require('../lib/rsk-tx-helper-provider');
const { activateFork } = require('../lib/rsk-utils');
const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util');

let pegoutCount = 0;
/**
* Takes the blockchain to the required state for this test file to run in isolation.
*/
const fulfillRequirementsToRunAsSingleTestFile = async () => {
await activateFork(Runners.common.forks.hop401);
};

describe('Pegout Batching - New Pegout Requests Then Call new bridge methods', function () {

before(() => {
rskClient = rsk.getClient(Runners.hosts.federate.host);
btcClient = bitcoin.getClient(
Runners.hosts.bitcoin.rpcHost,
Runners.hosts.bitcoin.rpcUser,
Runners.hosts.bitcoin.rpcPassword,
NETWORK
);
pegClient = pegUtils.using(btcClient, rskClient);
assertCallToBridgeMethodsRunner = pegAssertions.assertCallToPegoutBatchingBridgeMethods(rskClient);
let rskTxHelper;
let pegoutCount = 0;
let bridge;

before(async () => {

rskTxHelper = getRskTransactionHelper(Runners.hosts.federate.host);

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

bridge = getBridge(rskTxHelper.getClient(), await getLatestActiveForkName());

});

it('should create single pegout and call new bridge methods', async () => {
try {
await _2wpUtilsLegacy.createPegoutRequest(rskClient, pegClient, 0.1);
await _2wpUtils.createPegoutRequest(rskTxHelper, 0.1);

pegoutCount++;

assertCallToBridgeMethodsRunner(pegoutCount, 0);
const pendingPegoutCount = await bridge.methods.getQueuedPegoutsCount().call();
expect(Number(pendingPegoutCount)).to.equal(pegoutCount);

const expectedEstimatedFee = _2wpUtils.getPegoutEstimatedFees(pegoutCount);
const estimatedFees = await bridge.methods.getEstimatedFeesForNextPegOutEvent().call();
expect(Number(estimatedFees)).to.equal(expectedEstimatedFee);

const nextPegoutCreationBlockNumber = await bridge.methods.getNextPegoutCreationBlockNumber().call();
expect(Number(nextPegoutCreationBlockNumber)).to.equal(0);

} catch (error) {
throw new CustomError('pegout request creation failure', error);
}
})

it('should create 1 pegout in a block, 1 pegout in the following block and call bridge methods', async () => {
try {
await _2wpUtilsLegacy.createPegoutRequest(rskClient, pegClient, 0.5);
pegoutCount++;

await _2wpUtilsLegacy.createPegoutRequest(rskClient, pegClient, 0.4);
pegoutCount++;
await _2wpUtils.createPegoutRequest(rskTxHelper, 0.5);
await _2wpUtils.createPegoutRequest(rskTxHelper, 0.4);

pegoutCount += 2;

const pendingPegoutCount = await bridge.methods.getQueuedPegoutsCount().call();
expect(Number(pendingPegoutCount)).to.equal(pegoutCount);

const expectedEstimatedFee = _2wpUtils.getPegoutEstimatedFees(pegoutCount);
const estimatedFees = await bridge.methods.getEstimatedFeesForNextPegOutEvent().call();
expect(Number(estimatedFees)).to.equal(expectedEstimatedFee);

const nextPegoutCreationBlockNumber = await bridge.methods.getNextPegoutCreationBlockNumber().call();
expect(Number(nextPegoutCreationBlockNumber)).to.equal(0);

assertCallToBridgeMethodsRunner(pegoutCount, 0);
} catch (error) {
throw new CustomError('pegout request creation failure', error);
}
})

it('should create 1 pegout in a block, 1 pegout in the following block, 2 in the following block and call bridge methods', async () => {
try {
await _2wpUtilsLegacy.createPegoutRequest(rskClient, pegClient, 0.5);
pegoutCount++;
await _2wpUtils.createPegoutRequest(rskTxHelper, 0.5);
await _2wpUtils.createPegoutRequest(rskTxHelper, 0.8);
await _2wpUtils.createPegoutRequest(rskTxHelper, 0.6, 2);

await _2wpUtilsLegacy.createPegoutRequest(rskClient, pegClient, 0.8);
pegoutCount++;
pegoutCount += 4;

await _2wpUtilsLegacy.createPegoutRequest(rskClient, pegClient, 0.6, 2);
pegoutCount += 2;
const pendingPegoutCount = await bridge.methods.getQueuedPegoutsCount().call();
expect(Number(pendingPegoutCount)).to.equal(pegoutCount);

const expectedEstimatedFee = _2wpUtils.getPegoutEstimatedFees(pegoutCount);
const estimatedFees = await bridge.methods.getEstimatedFeesForNextPegOutEvent().call();
expect(Number(estimatedFees)).to.equal(expectedEstimatedFee);

const nextPegoutCreationBlockNumber = await bridge.methods.getNextPegoutCreationBlockNumber().call();
expect(Number(nextPegoutCreationBlockNumber)).to.equal(0);

assertCallToBridgeMethodsRunner(pegoutCount, 0);
} catch (error) {
throw new CustomError('pegout request creation failure', error);
}
Expand Down
115 changes: 78 additions & 37 deletions tests/01_05_53-post_hop_pegout_batching_second_requests.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,114 @@
const chai = require('chai');
chai.use(require('chai-as-promised'));
const expect = chai.expect;
const { bitcoin, rsk, pegUtils } = require('peglib');
const NETWORK = bitcoin.networks.testnet;
const CustomError = require('../lib/CustomError');
const rskUtilsLegacy = require('../lib/rsk-utils-legacy');
const _2wpUtilsLegacy = require('../lib/2wp-utils-legacy');
const pegAssertions = require('../lib/assertions/2wp');
const { NUMBER_OF_BLOCKS_BTW_PEGOUTS } = require('../lib/constants');
const rskUtils = require('../lib/rsk-utils');
const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');
const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util');
const _2wpUtils = require('../lib/2wp-utils');
const { activateFork, waitAndUpdateBridge } = require('../lib/rsk-utils');
const { getBtcClient } = require('../lib/btc-client-provider');

/**
* Takes the blockchain to the required state for this test file to run in isolation.
*/
const fulfillRequirementsToRunAsSingleTestFile = async () => {
await activateFork(Runners.common.forks.hop401);
};

let pegoutCount = 0;
let currentBlockNumber;
let assertCallToBridgeMethodsRunner;
let rskTxHelpers;
let rskTxHelper;
let bridge;
let btcTxHelper;

describe('Pegout Batching - Execute Pegout Transaction And Call New Bridge Methods', function () {

before(() => {
rskClients = Runners.hosts.federates.map(federate => rsk.getClient(federate.host));
rskClient = rsk.getClient(Runners.hosts.federate.host);
btcClient = bitcoin.getClient(
Runners.hosts.bitcoin.rpcHost,
Runners.hosts.bitcoin.rpcUser,
Runners.hosts.bitcoin.rpcPassword,
NETWORK
);
assertCallToBridgeMethodsRunner = pegAssertions.assertCallToPegoutBatchingBridgeMethods(rskClient);
pegClient = pegUtils.using(btcClient, rskClient);
before(async () => {

rskTxHelpers = getRskTransactionHelpers();
rskTxHelper = rskTxHelpers[0];
btcTxHelper = getBtcClient();

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

bridge = getBridge(rskTxHelper.getClient(), await getLatestActiveForkName());

});

it('Execute Pegout Transaction and Call new bridge methods after successful pegout transaction', async () => {
try {
await rskUtilsLegacy.triggerPegoutEvent(rskClients, async () => currentBlockNumber = await rskClient.eth.getBlockNumber());

await assertCallToBridgeMethodsRunner(0, currentBlockNumber + NUMBER_OF_BLOCKS_BTW_PEGOUTS);
let blockNumberWhenPegoutsWhereReleased;

const pegoutCreatedValidations = async (localRskTxHelper) => {
blockNumberWhenPegoutsWhereReleased = await localRskTxHelper.getBlockNumber();
};

const callbacks = {
pegoutCreatedCallback: pegoutCreatedValidations,
};

await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper, callbacks);

const pendingPegoutCount = await bridge.methods.getQueuedPegoutsCount().call();
expect(0).to.equal(pendingPegoutCount);

const expectedEstimatedFee = _2wpUtils.getPegoutEstimatedFees(pegoutCount);
const estimatedFees = await bridge.methods.getEstimatedFeesForNextPegOutEvent().call();
expect(Number(estimatedFees)).to.equal(expectedEstimatedFee);

const newExpectedNextPegoutsCreationBlockNumber = blockNumberWhenPegoutsWhereReleased + NUMBER_OF_BLOCKS_BTW_PEGOUTS;
const nextPegoutCreationBlockNumber = await bridge.methods.getNextPegoutCreationBlockNumber().call();
expect(Number(nextPegoutCreationBlockNumber)).to.equal(newExpectedNextPegoutsCreationBlockNumber);

} catch (error) {
throw new CustomError('new bridge methods call failure', error);
}
})

it('should create pegout requests, execute pegout transaction when height is not reached and when height is reached', async () => {
try {
await _2wpUtilsLegacy.createPegoutRequest(rskClient, pegClient, 0.5);
pegoutCount++;
await _2wpUtils.createPegoutRequest(rskTxHelper, 0.5);
await _2wpUtils.createPegoutRequest(rskTxHelper, 0.8);
await _2wpUtils.createPegoutRequest(rskTxHelper, 0.6, 2);

await _2wpUtilsLegacy.createPegoutRequest(rskClient, pegClient, 0.8);
pegoutCount++;

await _2wpUtilsLegacy.createPegoutRequest(rskClient, pegClient, 0.6, 2);
pegoutCount += 2;
pegoutCount += 4;

// Execute pegout transaction when height is not reached
await rskClient.fed.updateBridge();
await rskUtils.mineAndSync(rskTxHelpers);
await waitAndUpdateBridge(rskTxHelpers, 500);

const initialPegoutCount = await bridge.methods.getQueuedPegoutsCount().call();
expect(Number(initialPegoutCount)).to.equal(pegoutCount);

let blockNumberWhenPegoutsWhereReleased = await rskTxHelper.getBlockNumber();
const nextPegoutCreationBlockNumber = await bridge.methods.getNextPegoutCreationBlockNumber().call();
expect(Number(nextPegoutCreationBlockNumber)).to.be.greaterThan(blockNumberWhenPegoutsWhereReleased);

// Call new bridge methods after failed pegout transaction because height is not reached
const count = await rskClient.rsk.bridge.methods.getQueuedPegoutsCount().call();
expect(Number(count)).to.equal(pegoutCount);
const pegoutCreatedCallback = async (localRskTxHelper) => {
blockNumberWhenPegoutsWhereReleased = await localRskTxHelper.getBlockNumber();
};

const callbacks = {
pegoutCreatedCallback,
};

currentBlockNumber = await rskClient.eth.getBlockNumber();
const nextPegoutCreationBlockNumber = await rskClient.rsk.bridge.methods.getNextPegoutCreationBlockNumber().call();
expect(Number(nextPegoutCreationBlockNumber)).to.be.greaterThan(currentBlockNumber);
await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper, callbacks);

const pendingPegoutCount = await bridge.methods.getQueuedPegoutsCount().call();
expect(pendingPegoutCount).to.equal(0);

await rskUtilsLegacy.triggerPegoutEvent(rskClients, async () => currentBlockNumber = await rskClient.eth.getBlockNumber());
const expectedEstimatedFee = _2wpUtils.getPegoutEstimatedFees(pegoutCount);
const estimatedFees = await bridge.methods.getEstimatedFeesForNextPegOutEvent().call();
expect(Number(estimatedFees)).to.equal(expectedEstimatedFee);

const newExpectedNextPegoutsCreationBlockNumber = blockNumberWhenPegoutsWhereReleased + NUMBER_OF_BLOCKS_BTW_PEGOUTS;
const finalNextPegoutCreationBlockNumber = await bridge.methods.getNextPegoutCreationBlockNumber().call();
expect(Number(finalNextPegoutCreationBlockNumber)).to.equal(newExpectedNextPegoutsCreationBlockNumber);

await assertCallToBridgeMethodsRunner(0, currentBlockNumber + NUMBER_OF_BLOCKS_BTW_PEGOUTS);
} catch (error) {
throw new CustomError('pegout request creation failure', error);
}
Expand Down
Loading
Loading