Skip to content

Commit

Permalink
getBlockHeader() part, test encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Pikora authored and jeremy-then committed Jan 3, 2024
1 parent 0c30e59 commit 145f475
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 5,280 deletions.
60 changes: 60 additions & 0 deletions lib/tests/post_iris_call_receive_header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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 } = require('../rsk-utils');
const { ensure0x } = require('../utils');

let rskTxHelper;
let btcTxHelper;
let bridge;

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');
const blockHashes = await btcTxHelper.mine();
const blockHeader = await btcTxHelper.getBlockHeader(blockHashes[0], false);
const blockchainInitialHeigth = await bridge.methods.getBtcBlockchainBestChainHeight().call();

await sendTxWithCheck(
rskTxHelper,
bridge.methods.receiveHeader(ensure0x(blockHeader)),
cowAddress,
(result) => { expect(Number(result)).to.be.equal(0) }
);
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(result).to.be.equal('-1');
}
);
});
};

module.exports = {
execute,
};


53 changes: 53 additions & 0 deletions lib/tests/post_iris_call_receive_headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 } = 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());
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);

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

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

module.exports = {
execute,
};
Loading

0 comments on commit 145f475

Please sign in to comment.