diff --git a/lib/tests/2wp-legacy.js b/lib/tests/2wp-legacy.js index 3b2204da..c5a3ecb0 100644 --- a/lib/tests/2wp-legacy.js +++ b/lib/tests/2wp-legacy.js @@ -1,5 +1,5 @@ const expect = require('chai').expect -const { ensure0x, removePrefix0x} = require('../utils'); +const { ensure0x} = require('../utils'); const whitelistingAssertions = require('../assertions/whitelisting'); const rskUtils = require('../rsk-utils'); const CustomError = require('../CustomError'); @@ -10,8 +10,6 @@ const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivatio const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter'); const { sendTxToBridge, donateToBridge } = require('../2wp-utils'); const { waitAndUpdateBridge } = require('../rsk-utils'); -const { decodeOutpointValues, encodeOutpointValuesAsMap } = require("../varint"); -const {getBridgeState} = require("@rsksmart/bridge-state-data-parser"); const {PEGOUT_EVENTS} = require("../constants"); const DONATION_AMOUNT = 250; @@ -23,18 +21,6 @@ let rskTxHelpers; let federationAddress; let minimumPeginValueInBtc; -const assertPegoutTransactionCreatedEventIsEmitted = async (localRskTxHelper, activeFederationUtxosBeforePegout) => { - const pegoutTransactionCreatedEvent = await rskUtils.findEventInBlock(localRskTxHelper, PEGOUT_EVENTS.PEGOUT_TRANSACTION_CREATED.name); - expect(pegoutTransactionCreatedEvent).to.not.be.null; - const encodedUtxoOutpointValues = Buffer.from(removePrefix0x(pegoutTransactionCreatedEvent.arguments.utxoOutpointValues), 'hex'); - - const federationUtxoValues = encodeOutpointValuesAsMap(activeFederationUtxosBeforePegout); - - const outpointValues = decodeOutpointValues(encodedUtxoOutpointValues); - - expect(outpointValues.every(value => value in federationUtxoValues)).to.be.true; -} - const execute = (description, getRskHost) => { describe(description, () => { @@ -69,71 +55,6 @@ const execute = (description, getRskHost) => { return federationAddress; }); - it('should transfer RBTC to BTC - Above minimum pegout value', async () => { - - const INITIAL_RSK_BALANCE = 1; - const PEGOUT_VALUE_IN_RBTC = 0.5; - const MAX_EXPECTED_FEE = 0.001; - const pegoutValueInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(PEGOUT_VALUE_IN_RBTC)); - const maxExpectedFeeInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(MAX_EXPECTED_FEE)); - - const initialFederationBalanceInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(federationAddress))); - - const btcAddressInformation = await btcTxHelper.generateBtcAddress('legacy'); - - const recipientRskAddressInfo = getDerivedRSKAddressInformation(btcAddressInformation.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; - - await rskUtils.sendFromCow(rskTxHelper, recipientRskAddressInfo.address, Number(btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE))); - - const bridgeStateBeforePegout = await getBridgeState(rskTxHelper.getClient()); - const activeFederationUtxosBeforePegout = bridgeStateBeforePegout.activeFederationUtxos; - const pegoutTransaction = await sendTxToBridge(rskTxHelper, PEGOUT_VALUE_IN_RBTC, recipientRskAddressInfo.address); - - const pegoutRequestReceivedEvent = await rskUtils.findEventInBlock(rskTxHelper, PEGOUT_EVENTS.RELEASE_REQUEST_RECEIVED.name); - expect(pegoutRequestReceivedEvent).to.not.be.null; - const btcDestinationAddress = pegoutRequestReceivedEvent.arguments.btcDestinationAddress; - expect(pegoutRequestReceivedEvent.arguments.sender.toLowerCase()).to.equal(ensure0x(recipientRskAddressInfo.address)); - expect(Number(pegoutRequestReceivedEvent.arguments.amount)).to.equal(pegoutValueInSatoshis); - expect(btcAddressInformation.address).to.equal(btcDestinationAddress); - - const pegoutCreatedValidations = async (localRskTxHelper) => { - - const pegoutRequestedEvent = await rskUtils.findEventInBlock(localRskTxHelper, PEGOUT_EVENTS.RELEASE_REQUESTED.name); - expect(pegoutRequestedEvent).to.not.be.null; - expect(Number(pegoutRequestedEvent.arguments.amount)).to.equal(pegoutValueInSatoshis); - - const batchPegoutCreatedEvent = await rskUtils.findEventInBlock(localRskTxHelper, PEGOUT_EVENTS.BATCH_PEGOUT_CREATED.name); - expect(batchPegoutCreatedEvent).to.not.be.null; - expect(batchPegoutCreatedEvent.arguments.releaseRskTxHashes.includes(pegoutTransaction.transactionHash)).to.be.true; - - // TODO: Uncomment this line when lovell700 is active. - // await assertPegoutTransactionCreatedEventIsEmitted(localRskTxHelper, activeFederationUtxosBeforePegout); - - }; - - const pegoutConfirmedValidations = async (localRskTxHelper) => { - const pegoutConfirmedEvent = await rskUtils.findEventInBlock(localRskTxHelper, PEGOUT_EVENTS.PEGOUT_CONFIRMED.name); - expect(pegoutConfirmedEvent).to.not.be.null; - }; - - const callbacks = { - pegoutCreatedCallback: pegoutCreatedValidations, - pegoutConfirmedCallback: pegoutConfirmedValidations, - }; - - await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper, callbacks); - const finalFederationBalanceInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(federationAddress))); - const finalDestinationAddressBalanceInSatoshis = Number(btcEthUnitConverter.btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddressInformation.address))); - const difference = pegoutValueInSatoshis - finalDestinationAddressBalanceInSatoshis; - expect(difference).to.be.at.most(maxExpectedFeeInSatoshis); - expect(finalFederationBalanceInSatoshis).to.equal(initialFederationBalanceInSatoshis - pegoutValueInSatoshis); - - }); - it('should transfer RBTC to BTC - Below minimum pegout value', async() => { try { const INITIAL_RSK_BALANCE = 2; diff --git a/tests/01_03_06-2wp_no_locking_cap.js b/tests/01_03_06-2wp_no_locking_cap.js deleted file mode 100644 index 0ea0672c..00000000 --- a/tests/01_03_06-2wp_no_locking_cap.js +++ /dev/null @@ -1,72 +0,0 @@ -const expect = require('chai').expect -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 } = require('../lib/2wp-utils'); -const { getBridge } = require('../lib/precompiled-abi-forks-util'); -const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); -const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter'); -const whitelistingAssertions = require('../lib/assertions/whitelisting'); - -describe('Transfer BTC to RBTC before papyrus200', function() { - - let rskTxHelpers; - let btcTxHelper; - let rskTxHelper; - - const PEGIN_VALUE_IN_BTC = 3; - const PEGOUT_VALUE_IN_RBTC = PEGIN_VALUE_IN_BTC / 3; - const RSK_TX_FEE_IN_RBTC = 0.001; - - before(async () => { - rskTxHelpers = getRskTransactionHelpers(); - rskTxHelper = rskTxHelpers[0]; - btcTxHelper = getBtcClient(); - }); - - - 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, Number(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 bridge = getBridge(rskTxHelper.getClient()); - 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()); - - 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(Number(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(Number(btcEthUnitConverter.btcToWeis(PEGIN_VALUE_IN_BTC - PEGOUT_VALUE_IN_RBTC - RSK_TX_FEE_IN_RBTC))).and.below(Number(btcEthUnitConverter.btcToWeis(PEGIN_VALUE_IN_BTC - PEGOUT_VALUE_IN_RBTC))); - - await sendTxToBridge(rskTxHelper, PEGOUT_VALUE_IN_RBTC, recipientRskAddressInfo.address); - await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper); - - 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); - - const recipientRskAddressBalanceAfterSecondPegout = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); - expect(Number(recipientRskAddressBalanceAfterSecondPegout)).to.be.above(recipientRskAddressBalanceAfterFirstPegout - Number(btcEthUnitConverter.btcToWeis(PEGOUT_VALUE_IN_RBTC + RSK_TX_FEE_IN_RBTC))).and.below(recipientRskAddressBalanceAfterFirstPegout - Number(btcEthUnitConverter.btcToWeis(PEGOUT_VALUE_IN_RBTC))); - }); -}); - diff --git a/tests/01_06_02-pre-fingerroot500-release_request_received_bytes.js b/tests/01_06_02-pre-fingerroot500-release_request_received_bytes.js deleted file mode 100644 index d9e2eef9..00000000 --- a/tests/01_06_02-pre-fingerroot500-release_request_received_bytes.js +++ /dev/null @@ -1,62 +0,0 @@ -const expect = require('chai').expect -const CustomError = require('../lib/CustomError'); -const rskUtils = require('../lib/rsk-utils'); -const { getBtcClient } = require('../lib/btc-client-provider'); -const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); -const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); -const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter'); -const { sendTxToBridge } = require('../lib/2wp-utils'); -const { ensure0x } = require('../lib/utils'); - -let btcTxHelper; -let rskTxHelpers; - -describe('pegout events improvements - pre fingerroot', () => { - - before(async () => { - btcTxHelper = getBtcClient(); - rskTxHelpers = getRskTransactionHelpers(); - }); - - it('release_request_received event generates address as hash160', async () => { - try { - - const rskTxHelper = rskTxHelpers[rskTxHelpers.length - 1]; - - const INITIAL_RSK_BALANCE = 0.01; - const PEGOUT_AMOUNT_IN_RBTC = 0.005; - const PEGOUT_AMOUNT_IN_SATOSHIS = Number(btcEthUnitConverter.btcToSatoshis(PEGOUT_AMOUNT_IN_RBTC)); - - const btcAddressInformation = await btcTxHelper.generateBtcAddress('legacy'); - - const recipientRskAddressInfo = getDerivedRSKAddressInformation(btcAddressInformation.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; - - await rskUtils.sendFromCow(rskTxHelper, recipientRskAddressInfo.address, Number(btcEthUnitConverter.btcToWeis(INITIAL_RSK_BALANCE))); - - await sendTxToBridge(rskTxHelper, PEGOUT_AMOUNT_IN_RBTC, recipientRskAddressInfo.address); - - const pegoutRequestReceivedEvent = await rskUtils.findEventInBlock(rskTxHelper, 'release_request_received'); - - expect(pegoutRequestReceivedEvent).to.not.be.null; - - const btcDestinationAddress = pegoutRequestReceivedEvent.arguments.btcDestinationAddress; - - expect(pegoutRequestReceivedEvent.arguments.sender.toLowerCase()).to.equal(ensure0x(recipientRskAddressInfo.address)); - expect(Number(pegoutRequestReceivedEvent.arguments.amount)).to.equal(PEGOUT_AMOUNT_IN_SATOSHIS); - - expect(ensure0x(btcTxHelper.decodeBase58Address(btcAddressInformation.address, false))).to.equal(btcDestinationAddress); - - // This is to ensure that the pegout is not left behind in the bridge by pushing it all the way through - await rskUtils.triggerRelease(rskTxHelpers, btcTxHelper); - - } - catch (err) { - throw new CustomError('Transfer RBTC to BTC failure', err); - } - }); - -}); diff --git a/tests/02_00_06-2wp-new-minimum.js b/tests/02_00_06-2wp-new-minimum.js deleted file mode 100644 index 144ebf49..00000000 --- a/tests/02_00_06-2wp-new-minimum.js +++ /dev/null @@ -1,80 +0,0 @@ -const { expect } = require('chai'); -const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); -const { getBtcClient } = require('../lib/btc-client-provider'); -const { triggerRelease } = require('../lib/rsk-utils'); -const { sendPegin, ensurePeginIsRegistered, sendTxToBridge, BRIDGE_ADDRESS, MIN_PEGOUT_VALUE_IN_RBTC } = require('../lib/2wp-utils'); -const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); -const { getBridge } = require('../lib/precompiled-abi-forks-util'); -const { btcToSatoshis, btcToWeis, satoshisToBtc } = require('@rsksmart/btc-eth-unit-converter'); - -describe('2wp after iris300, using new minimum values', () => { - let rskTxHelpers; - let btcTxHelper; - let rskTxHelper; - let minimumPeginValueInBTC; - let bridge; - let federationAddress; - - before(async () => { - rskTxHelpers = getRskTransactionHelpers(); - rskTxHelper = rskTxHelpers[0]; - btcTxHelper = getBtcClient(); - - // Get the current peg-in minimum - bridge = getBridge(rskTxHelper.getClient()); - const minimumPeginValueInSatoshi = await bridge.methods.getMinimumLockTxValue().call(); - minimumPeginValueInBTC = Number(satoshisToBtc(minimumPeginValueInSatoshi)); - - //get federation address - federationAddress = await bridge.methods.getFederationAddress().call(); - }); - - it('should peg-out successfully when sending exactly the minimum pegout value', async () => { - // Do a peg-in first to ensure the federation has funds to do the peg-out - const TX_FEE_IN_RBTC = 0.001; - - // Create legacy type address to use as sender - const senderAddressInfo = await btcTxHelper.generateBtcAddress('legacy'); - - await btcTxHelper.fundAddress(senderAddressInfo.address, minimumPeginValueInBTC + btcTxHelper.getFee()); - - // Get the RSK address where the funds should be locked to - const recipientRskAddressInfo = getDerivedRSKAddressInformation(senderAddressInfo.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 recipientRskAddressBalanceInitial = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); - expect(recipientRskAddressBalanceInitial).to.be.equal(0); - - const federationAddressBalanceInitial = Number(await btcTxHelper.getAddressBalance(federationAddress)); - - // Execute peg-in - const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderAddressInfo, minimumPeginValueInBTC); - await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash); - - const recipientRskAddressBalanceAfterPegin = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); - expect(recipientRskAddressBalanceAfterPegin).to.be.equal(Number(btcToWeis(minimumPeginValueInBTC))); - - const federationAddressBalanceAfterPegin = Number(await btcTxHelper.getAddressBalance(federationAddress)); - const expectedFederationAddressBalanceAfterPegin = federationAddressBalanceInitial + minimumPeginValueInBTC; - expect(Number(btcToSatoshis(federationAddressBalanceAfterPegin))).to.be.equal(Number(btcToSatoshis(expectedFederationAddressBalanceAfterPegin))); - - // Execute peg-out - await sendTxToBridge(rskTxHelper, MIN_PEGOUT_VALUE_IN_RBTC, recipientRskAddressInfo.address); - await triggerRelease(rskTxHelpers, btcTxHelper); - - const senderAddressBalanceFinal = Number(await btcTxHelper.getAddressBalance(senderAddressInfo.address)); - const maxExpectedSenderAddressBalanceFinal = Number(btcToSatoshis(MIN_PEGOUT_VALUE_IN_RBTC)); - const minExpectedSenderAddressBalanceFinal = Number(btcToSatoshis(MIN_PEGOUT_VALUE_IN_RBTC - TX_FEE_IN_RBTC)); - expect(Number(btcToSatoshis(senderAddressBalanceFinal))).to.be.above(minExpectedSenderAddressBalanceFinal).and.below(maxExpectedSenderAddressBalanceFinal); - - const federationAddressBalanceFinal = Number(await btcTxHelper.getAddressBalance(federationAddress)); - const expectedFederationAddressBalanceFinal = federationAddressBalanceAfterPegin - MIN_PEGOUT_VALUE_IN_RBTC; - expect(Number(btcToSatoshis(federationAddressBalanceFinal))).to.be.equal(Number(btcToSatoshis(expectedFederationAddressBalanceFinal))); - - const recipientRskAddressBalanceFinal = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); - const maxExpectedRecipientRskAddressBalanceFinal = Number(btcToWeis(minimumPeginValueInBTC - MIN_PEGOUT_VALUE_IN_RBTC)); - const minExpectedRecipientRskAddressBalanceFinal = Number(btcToWeis(minimumPeginValueInBTC - MIN_PEGOUT_VALUE_IN_RBTC - TX_FEE_IN_RBTC)); - expect(recipientRskAddressBalanceFinal).to.be.above(minExpectedRecipientRskAddressBalanceFinal).and.below(maxExpectedRecipientRskAddressBalanceFinal); - }); -});