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

01-04-54-post-iris-call-receive-headers / 01-04-55-post-iris-call-receive-header #26

Merged
merged 10 commits into from
Jan 8, 2024
Merged
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
63 changes: 63 additions & 0 deletions lib/tests/call_receive_header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const expect = require('chai').expect;
const { getRskTransactionHelper } = require('../rsk-tx-helper-provider');
const { getBtcClient } = require('../btc-client-provider');
const { getBridge, getLatestActiveForkName } = require('../precompiled-abi-forks-util');
const { activateFork, sendTxWithCheck, getLatestForkName, waitAndUpdateBridge } = require('../rsk-utils');
const { ensure0x } = require('../utils');

let rskTxHelper;
let btcTxHelper;
let bridge;

const HEADER_RECEIVED_OK = 0;
const RECEIVE_HEADER_CALLED_TOO_SOON = -1;

const fulfillRequirementsToRunAsSingleTestFile = async () => {
const forkName = process.env.FORK_NAME || getLatestForkName().name;
await activateFork(Runners.common.forks[forkName]);
};

const execute = (description, getRskHost) => {
describe(description, function() {
before(async () => {
rskTxHelper = getRskTransactionHelper(getRskHost());
btcTxHelper = getBtcClient();

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

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

it('should return 0 and increment BTC blockchain size when calling receiveHeader method', async () => {
const cowAddress = await rskTxHelper.newAccountWithSeed('cow');

await waitAndUpdateBridge(rskTxHelper);

const blockHashes = await btcTxHelper.mine();
const blockHeader = await btcTxHelper.getBlockHeader(blockHashes[0], false);
const blockchainInitialHeigth = await bridge.methods.getBtcBlockchainBestChainHeight().call();

const receiveHeaderMethodCall = bridge.methods.receiveHeader(ensure0x(blockHeader));
const checkCallback = (result) => { expect(Number(result)).to.be.equal(HEADER_RECEIVED_OK) };

await sendTxWithCheck(rskTxHelper, receiveHeaderMethodCall, cowAddress, checkCallback);

const blockchainFinalHeight = await bridge.methods.getBtcBlockchainBestChainHeight().call();
expect(Number(blockchainFinalHeight)).to.be.equal(Number(blockchainInitialHeigth) + 1);
});

it('should return -1 when calling receiveHeader method consecutively', async () => {
const blockHashes = await btcTxHelper.mine();
const blockHeader = await btcTxHelper.getBlockHeader(blockHashes[0], false);
const result = await bridge.methods.receiveHeader(ensure0x(blockHeader)).call();
expect(Number(result)).to.be.equal(RECEIVE_HEADER_CALLED_TOO_SOON);
}
);
});
};

module.exports = {
execute,
};
52 changes: 52 additions & 0 deletions lib/tests/call_receive_headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const expect = require('chai').expect;
const { getRskTransactionHelper } = require('../rsk-tx-helper-provider');
const { getBtcClient } = require('../btc-client-provider');
const { getBridge, getLatestActiveForkName } = require('../precompiled-abi-forks-util');
const { activateFork, sendTxWithCheck, getLatestForkName, waitAndUpdateBridge } = require('../rsk-utils');
const { ensure0x } = require('../utils');

let rskTxHelper;
let btcTxHelper;

/**
* Takes the blockchain to the required state for this test file to run in isolation.
*/
const fulfillRequirementsToRunAsSingleTestFile = async () => {
const forkName = process.env.FORK_NAME || getLatestForkName().name;
await activateFork(Runners.common.forks[forkName]);
};

const execute = (description, getRskHost) => {
describe(description, function() {

before(async () => {
rskTxHelper = getRskTransactionHelper(getRskHost());
btcTxHelper = getBtcClient();

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

it('Calling receiveHeaders method with regular user should not increment BTC blockchain size', async () => {
const bridge = getBridge(rskTxHelper.getClient(), await getLatestActiveForkName());
await waitAndUpdateBridge(rskTxHelper);

const blockNumberInitial = await bridge.methods.getBtcBlockchainBestChainHeight().call();
const cowAddress = await rskTxHelper.newAccountWithSeed('cow');
const blockHashes = await btcTxHelper.mine();
const blockHeader = await btcTxHelper.getBlockHeader(blockHashes[0], false);

const receiveHeadersMethodCall = bridge.methods.receiveHeaders([ensure0x(blockHeader)]);
const checkCallback = (result) => { expect(result).to.be.empty };
await sendTxWithCheck(rskTxHelper, receiveHeadersMethodCall, cowAddress, checkCallback);

const blockNumberFinal = await bridge.methods.getBtcBlockchainBestChainHeight().call();
expect(blockNumberInitial).to.be.equal(blockNumberFinal);
});
});
};

module.exports = {
execute,
};
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"bn.js": "^4.11.8",
"bridge-transaction-parser-fingerroot500": "github:rsksmart/bridge-transaction-parser#v0.5.0-beta",
"bridge-transaction-parser-hop400": "github:rsksmart/bridge-transaction-parser#v0.4.0-beta",
"btc-transaction-helper": "git+https://[email protected]/rsksmart/btc-transaction-helper#v3.4.0",
"btc-transaction-helper": "git+https://[email protected]/rsksmart/btc-transaction-helper#v3.5.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"colors": "^1.1.2",
Expand Down
48 changes: 2 additions & 46 deletions tests/01_04_54-post_iris_call_receive_headers.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,3 @@
const expect = require('chai').expect;
const receiveHeadersTests = require('../lib/tests/call_receive_headers');

const peglib = require('peglib');
const bitcoin = peglib.bitcoin;
const rsk = peglib.rsk;
const pegUtils = peglib.pegUtils;
const libUtils = require('../lib/utils');
const rskUtilsLegacy = require('../lib/rsk-utils-legacy');
const CustomError = require('../lib/CustomError');

const NETWORK = bitcoin.networks.testnet;

describe('Calling receiveHeaders after iris300', 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);
utils = rskUtilsLegacy.with(btcClient, rskClient, pegClient);
});

it('Calling receiveHeaders method with regular user should not increment BTC blockchain size', async () => {
try {
let blockNumberInitial = await rskClient.rsk.bridge.methods.getBtcBlockchainBestChainHeight().call();
let cowAddress = await rskClient.eth.personal.newAccountWithSeed('cow');
let blockHash = await btcClient.generate(1);
let blockHeader = await btcClient.getBlockHeader(blockHash[0], false);

await utils.sendTxWithCheck(
rskClient.rsk.bridge.methods.receiveHeaders([libUtils.ensure0x(blockHeader)]),
(result) => { expect(result).to.be.empty },
cowAddress
)();

let blockNumberFinal = await rskClient.rsk.bridge.methods.getBtcBlockchainBestChainHeight().call();
expect(blockNumberInitial).to.be.equal(blockNumberFinal);
}
catch (err) {
throw new CustomError('receiverHeaders call failure', err);
}
});
});
receiveHeadersTests.execute('Calling receiveHeaders after iris300', () => Runners.hosts.federate.host);
70 changes: 2 additions & 68 deletions tests/01_04_55-post_iris_call_receive_header.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,3 @@
const expect = require('chai').expect;

const peglib = require('peglib');
const bitcoin = peglib.bitcoin;
const rsk = peglib.rsk;
const pegUtils = peglib.pegUtils;
const libUtils = require('../lib/utils');
const rskUtilsLegacy = require('../lib/rsk-utils-legacy');
const CustomError = require('../lib/CustomError');
const rskUtils = require('../lib/rsk-utils');
const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider');

const NETWORK = bitcoin.networks.testnet;
let rskTxHelpers;

describe('Calling method receiveHeader after iris300', 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);
utils = rskUtilsLegacy.with(btcClient, rskClient, pegClient);
rskTxHelpers = getRskTransactionHelpers();
});

it('should return 0 and increment BTC blockchain size when calling receiveHeader method', async () => {
try {
let cowAddress = await rskClient.eth.personal.newAccountWithSeed('cow');

await rskClient.fed.updateBridge();
await rskUtils.mineAndSync(rskTxHelpers);
let blockHash = await btcClient.generate(1);
let blockHeader = await btcClient.getBlockHeader(blockHash[0], false);

let blockchainInitialHeigth = await rskClient.rsk.bridge.methods.getBtcBlockchainBestChainHeight().call();

await utils.sendTxWithCheck(
rskClient.rsk.bridge.methods.receiveHeader(libUtils.ensure0x(blockHeader)),
(result) => { expect(Number(result)).to.be.equal(0) },
cowAddress
)();

let blockchainFinalHeight = await rskClient.rsk.bridge.methods.getBtcBlockchainBestChainHeight().call();
expect(Number(blockchainFinalHeight)).to.be.equal(Number(blockchainInitialHeigth) + 1);
}
catch (err) {
throw new CustomError('receiveHeader call failure', err);
}
});

it('should return -1 when calling receiveHeader method consecutively', async () => {
try {
let blockHash = await btcClient.generate(1);
let blockHeader = await btcClient.getBlockHeader(blockHash[0], false);
let result = await rskClient.rsk.bridge.methods.receiveHeader(libUtils.ensure0x(blockHeader)).call();
expect(result).to.be.equal('-1');
}
catch (err) {
throw new CustomError('receiveHeader call failure', err);
}
});
});
const receiveHeaderTests = require('../lib/tests/call_receive_header');

receiveHeaderTests.execute('Calling receiveHeader after iris300', () => Runners.hosts.federate.host);
3 changes: 3 additions & 0 deletions tests/03_00_02-call_receive_headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const receiveHeadersTests = require('../lib/tests/call_receive_headers');

receiveHeadersTests.execute('Calling receiveHeaders last fork is active', () => Runners.hosts.federate.host);
3 changes: 3 additions & 0 deletions tests/03_00_03-call_receive_header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const receiveHeaderTests = require('../lib/tests/call_receive_header');

receiveHeaderTests.execute('Calling receiveHeader last fork is active', () => Runners.hosts.federate.host);
3 changes: 3 additions & 0 deletions tests/05_00_02-call_receive_headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const receiveHeadersTests = require('../lib/tests/call_receive_headers');

receiveHeadersTests.execute('Calling receiveHeaders after federation change', () => Runners.hosts.federate.host);
3 changes: 3 additions & 0 deletions tests/05_00_03-call_receive_header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const receiveHeaderTests = require('../lib/tests/call_receive_header');

receiveHeaderTests.execute('Calling receiveHeader after federation change', () => Runners.hosts.federates[Runners.hosts.federates.length - 1].host);
Loading