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

Adds register svp spend tx test #205

Open
wants to merge 1 commit into
base: adds-svp-spend-tx-created-test
Choose a base branch
from
Open
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
66 changes: 65 additions & 1 deletion tests/00_00_04-change-federation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
splitStringIntoChunks,
getBridgeStorageValueDecodedHexString,
decodeRlp,
wait,
} = require('../lib/utils');
const {
KEY_TYPE_BTC,
Expand All @@ -32,7 +33,8 @@ const {
const {
createSenderRecipientInfo,
sendPegin,
ensurePeginIsRegistered
ensurePeginIsRegistered,
BTC_TO_RSK_MINIMUM_CONFIRMATIONS
} = require('../lib/2wp-utils');
const { BRIDGE_ADDRESS } = require('../lib/bridge-constants');
const { getBtcClient } = require('../lib/btc-client-provider');
Expand All @@ -43,6 +45,7 @@ const {
getProposedFederationPublicKeys
} = require('../lib/federation-utils');
const { decodeOutpointValues } = require('../lib/varint');
const { waitForBitcoinTxToBeInMempool } = require('../lib/btc-utils');

// Generated with seed newFed1
const newFederator1PublicKey = '0x02f80abfd3dac069887f974ac033cb62991a0ed55b9880faf8b8cbd713b75d649e';
Expand Down Expand Up @@ -386,6 +389,29 @@ describe('Change federation', async function() {

});

it('should register the SVP Spend transaction and finish the SVP process', async () => {

const blockNumberBeforeUpdateCollections = await rskTxHelper.getBlockNumber();
const expectedCountOfSignatures = Math.floor(newFederationPublicKeys.length / 2) + 1;

await waitForExpectedCountOfAddSignatureEventsToBeEmitted(rskTxHelper, blockNumberBeforeUpdateCollections, expectedCountOfSignatures);

// Finding the SVP Spend transaction release_btc event
const blockNumberAfterRelease = await rskTxHelper.getBlockNumber();
const releaseBtcEvent = await rskUtils.findEventInBlock(rskTxHelper, PEGOUT_EVENTS.RELEASE_BTC.name, blockNumberBeforeUpdateCollections, blockNumberAfterRelease);
const releaseBtcTransaction = bitcoinJsLib.Transaction.fromHex(removePrefix0x(releaseBtcEvent.arguments.btcRawTransaction));

// Mining the SVP Spend transaction in bitcoin to register it in the Bridge
await waitForBitcoinTxToBeInMempool(btcTxHelper, releaseBtcTransaction.getId());
await btcTxHelper.mine(BTC_TO_RSK_MINIMUM_CONFIRMATIONS);
await rskUtils.waitAndUpdateBridge(rskTxHelper);

await assertProposedFederationIsNotInStorage(bridge);

await assertSvpValuesNotPresentInStorage(rskTxHelper);

});

it('should activate federation', async () => {

const federationActivationBlockNumber = commitFederationCreationBlockNumber + FEDERATION_ACTIVATION_AGE;
Expand Down Expand Up @@ -521,3 +547,41 @@ const assertProposedFederationIsStillInStorage = async (bridge, expectedProposed
expect(proposedFederationMembers).to.be.deep.equal(expectedProposedFederationMembers, 'The proposed federation members should still be in storage.');

};

const assertProposedFederationIsNotInStorage = async (bridge) => {

const proposedFederationAddress = await bridge.methods.getProposedFederationAddress().call();
expect(proposedFederationAddress).to.be.equal('');

const proposedFederationSize = Number(await bridge.methods.getProposedFederationSize().call());
expect(proposedFederationSize).to.be.equal(-1);

const proposedFederationMembers = await getProposedFederationPublicKeys(bridge);
expect(proposedFederationMembers).to.be.empty;

};

const waitForExpectedCountOfAddSignatureEventsToBeEmitted = async (rskTxHelper, fromBlockNumber, expectedCountOfSignatures, checkEveryMilliseconds = 1000, maxAttempts = 16) => {

const addSignatureEvents = [];

while(addSignatureEvents.length < expectedCountOfSignatures) {
await wait(checkEveryMilliseconds);
await rskUtils.findEventInBlock(rskTxHelper, PEGOUT_EVENTS.ADD_SIGNATURE.name, fromBlockNumber, fromBlockNumber, event => {
if(event.name === PEGOUT_EVENTS.ADD_SIGNATURE.name) {
addSignatureEvents.push(event);
}
});

await rskTxHelper.mine();
fromBlockNumber++;

if(maxAttempts === 0) {
throw new Error(`The expected count of add signature events was not reached after ${maxAttempts} attempts checking every ${checkEveryMilliseconds} milliseconds.`);
}

maxAttempts--;

}

};
Loading