From 65b72aca9086576e3e0db60f473ddffd956a59c5 Mon Sep 17 00:00:00 2001 From: janniks Date: Fri, 4 Oct 2024 16:28:15 +0200 Subject: [PATCH] test: add multi miner tests --- src/helpers.ts | 2 +- src/tests/misc.test.ts | 33 +--------- src/tests/regtest-multiminer.test.ts | 91 ++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 src/tests/regtest-multiminer.test.ts diff --git a/src/helpers.ts b/src/helpers.ts index 504cf20..0da9b75 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -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), { @@ -519,4 +520,3 @@ export function getPubKeyHashFromTx(tx: string) { const decodedScript = btc.Script.decode(input.finalScriptSig); return bytesToHex(decodedScript[1] as Uint8Array); } -// ============================================================================= diff --git a/src/tests/misc.test.ts b/src/tests/misc.test.ts index cb93035..3498385 100644 --- a/src/tests/misc.test.ts +++ b/src/tests/misc.test.ts @@ -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 () => { @@ -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); -}); diff --git a/src/tests/regtest-multiminer.test.ts b/src/tests/regtest-multiminer.test.ts new file mode 100644 index 0000000..1b13129 --- /dev/null +++ b/src/tests/regtest-multiminer.test.ts @@ -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 + ); + 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); +});