Skip to content

Commit

Permalink
test: add multi miner tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Oct 4, 2024
1 parent 4dccb3e commit 65b72ac
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ export async function resumeProxy(name: ProxyName) {

// BITCOIND RPC ================================================================
export const bitcoindClient = new RpcClient('http://btc:btc@localhost:18443').Typed;
// =============================================================================

export function getPubKeyHashFromTx(tx: string) {
const transaction = btc.Transaction.fromRaw(hexToBytes(tx), {
Expand All @@ -519,4 +520,3 @@ export function getPubKeyHashFromTx(tx: string) {
const decodedScript = btc.Script.decode(input.finalScriptSig);
return bytesToHex(decodedScript[1] as Uint8Array);
}
// =============================================================================
33 changes: 1 addition & 32 deletions src/tests/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
waitForBurnBlockHeight,
getStacksBlock,
getStacksBlockRaw,
getStacksBlockHeight,
bitcoindClient,
getPubKeyHashFromTx,
} from '../helpers';
import { waitForBurnBlockHeight, getStacksBlock, getStacksBlockRaw } from '../helpers';
import { regtestComposeDown, regtestComposeUp } from '../utils';

test('wip test', async () => {
Expand All @@ -29,27 +22,3 @@ test('signer rollover', async () => {
// power up new stackers (in cycle 6)
// new stackers take on cycle 7
});

test('multiple miners are active', async () => {
// PREP
await waitForBurnBlockHeight(109);

const height = await getStacksBlockHeight();
const range = Array.from({ length: height - 1 }, (_, i) => i + 1);
console.log('height', height, 'range', range.length);

const pubKeyHashes = await Promise.all(
range.map(async height => {
const block = await getStacksBlock(height);
const tx = await bitcoindClient.getrawtransaction({
txid: block.miner_txid.replace('0x', ''),
});
return getPubKeyHashFromTx(tx as string);
})
);

expect(range.length).toBeGreaterThan(0);
expect(pubKeyHashes.length).toBeGreaterThan(0);

expect(new Set(pubKeyHashes).size).toBe(2);
});
91 changes: 91 additions & 0 deletions src/tests/regtest-multiminer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {
bitcoindClient,
getPubKeyHashFromTx,
getStacksBlock,
getStacksBlockHeight,
stacksNetwork,
waitForBurnBlockHeight,
waitForNetwork,
} from '../helpers';
import { networkEnvDown, networkEnvUp, regtestComposeDown, regtestComposeUp } from '../utils';

const network = stacksNetwork();

beforeEach(async () => {
await networkEnvUp();
await waitForNetwork();
});

afterEach(async () => {
await networkEnvDown();
});

test('multiple miners are active', async () => {
// TEST CASE
// wait for some stacks blocks to be mined
// get the pubkey hashes from the stacks blocks
// ensure there are EXACTLY TWO unique pubkeys mining

await waitForBurnBlockHeight(130);

const height = await getStacksBlockHeight();
const range = Array.from({ length: height - 1 }, (_, i) => i + 1);

const pubKeyHashes = await Promise.all(
range.map(async height => {
const block = await getStacksBlock(height);
const tx = await bitcoindClient.getrawtransaction({
txid: block.miner_txid.replace('0x', ''),
});
return getPubKeyHashFromTx(tx as string);
})
);

console.log('pubkey hashes length', pubKeyHashes.length);

expect(range.length).toBeGreaterThan(0);
expect(pubKeyHashes.length).toBeGreaterThan(0);

const uniques = new Set(pubKeyHashes);
console.log('unique pubkeys', uniques.size);

const counts = pubKeyHashes.reduce(
(acc, hash) => {
acc[hash] = (acc[hash] || 0) + 1;
return acc;
},
{} as Record<string, number>
);
console.log('miner counts', counts);

expect(uniques.size).toBe(2);
});

test('miner recovers after restart in live network', async () => {
// TEST CASE
// two miner setup
// wait for some stacks blocks to be mined
// compose DOWN a miner (without signers or other event observers)
// wait for a few blocks
// compose UP the miner
// wait for a few blocks
// ensure the miner has caught up to the network

await waitForBurnBlockHeight(120);

await regtestComposeDown('stacks-miner-2');

await waitForBurnBlockHeight(130);

await regtestComposeUp('stacks-miner-2');

await waitForBurnBlockHeight(140);

const info1: any = await fetch('http://localhost:20443/v2/info').then(r => r.json());
const info2: any = await fetch('http://localhost:40443/v2/info').then(r => r.json());

expect(info1.pox_consensus).toBe(info2.pox_consensus);
expect(info1.burn_block_height).toBe(info2.burn_block_height);
expect(info1.stacks_tip_height).toBe(info2.stacks_tip_height);
expect(info1.stacks_tip_consensus_hash).toBe(info2.stacks_tip_consensus_hash);
});

0 comments on commit 65b72ac

Please sign in to comment.