Skip to content

Commit

Permalink
cleanup tx-broadcaster script
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Mar 14, 2024
1 parent 18de9ac commit 97069a5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
7 changes: 4 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ x-common-vars:
- &MINE_INTERVAL ${MINE_INTERVAL:-1s}
- &MINE_INTERVAL_EPOCH25 ${MINE_INTERVAL_EPOCH25:-10s} # 10 second bitcoin block times in epoch 2.5
- &MINE_INTERVAL_EPOCH3 ${MINE_INTERVAL_EPOCH3:-600s} # 10 minute bitcoin block times in epoch 3
- &NAKAMOTO_BLOCK_INTERVAL 2 # seconds to wait between issuing stx-transfer transactions (which triggers Nakamoto block production)
- &STACKS_20_HEIGHT ${STACKS_20_HEIGHT:-101}
- &STACKS_2_05_HEIGHT ${STACKS_2_05_HEIGHT:-102}
- &STACKS_21_HEIGHT ${STACKS_21_HEIGHT:-103}
Expand Down Expand Up @@ -179,8 +180,8 @@ services:
STACKS_30_HEIGHT: *STACKS_30_HEIGHT
POX_PREPARE_LENGTH: *POX_PREPARE_LENGTH
POX_REWARD_LENGTH: *POX_REWARD_LENGTH
STACKING_INTERVAL: 2
POST_TX_WAIT: 10
STACKING_INTERVAL: 2 # interval (seconds) for checking if stacking transactions are needed
POST_TX_WAIT: 10 # seconds to wait after a stacking transaction broadcast before continuing the loop
SERVICE_NAME: stacker
depends_on:
- stacks-node
Expand Down Expand Up @@ -220,7 +221,7 @@ services:
environment:
STACKS_CORE_RPC_HOST: stacks-node
STACKS_CORE_RPC_PORT: 20443
BROADCAST_INTERVAL: 2 # seconds
NAKAMOTO_BLOCK_INTERVAL: *NAKAMOTO_BLOCK_INTERVAL
ACCOUNT_KEYS: 0d2f965b472a82efd5a96e6513c8b9f7edc725d5c96c7d35d6c722cedeb80d1b01,975b251dd7809469ef0c26ec3917971b75c51cd73a022024df4bf3b232cc2dc001,c71700b07d520a8c9731e4d0f095aa6efb91e16e25fb27ce2b72e7b698f8127a01
depends_on:
- stacks-node
Expand Down
1 change: 0 additions & 1 deletion stacking/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export function parseEnvInt<T extends boolean = false>(
return parseInt(value, 10);
}

/** hard-coded numbers from Stacks.toml file */
export function burnBlockToRewardCycle(burnBlock: number) {
const cycleLength = BigInt(POX_REWARD_LENGTH);
return Number(BigInt(burnBlock) / cycleLength) + 1;
Expand Down
27 changes: 26 additions & 1 deletion stacking/tx-broadcaster.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { StacksTestnet } from '@stacks/network';
import { StackingClient } from '@stacks/stacking';
import {
TransactionVersion,
getAddressFromPrivateKey,
Expand All @@ -7,7 +8,7 @@ import {
broadcastTransaction,
} from '@stacks/transactions';

const broadcastInterval = parseInt(process.env.BROADCAST_INTERVAL ?? '2');
const broadcastInterval = parseInt(process.env.NAKAMOTO_BLOCK_INTERVAL ?? '2');
const url = `http://${process.env.STACKS_CORE_RPC_HOST}:${process.env.STACKS_CORE_RPC_PORT}`;
const network = new StacksTestnet({ url });

Expand All @@ -16,6 +17,8 @@ const accounts = process.env.ACCOUNT_KEYS!.split(',').map(privKey => ({
stxAddress: getAddressFromPrivateKey(privKey, TransactionVersion.Testnet),
}));

const client = new StackingClient(accounts[0].stxAddress, network);

async function run() {
const accountNonces = await Promise.all(
accounts.map(async account => {
Expand Down Expand Up @@ -51,7 +54,29 @@ async function run() {
}
}

async function waitForNakamoto() {
while (true) {
try {
const poxInfo = await client.getPoxInfo();
if (!poxInfo.contract_id.endsWith('.pox-4')) {
console.log(`Pox contract is not .pox-4, waiting for pox-4 (contract=${poxInfo.contract_id}) ...`);
} else {
console.log(`Pox contract is .pox-4, ready to submit txs for Nakamoto block production`);
break;
}
} catch (error) {
if (/(ECONNREFUSED|ENOTFOUND|SyntaxError)/.test(error.cause?.message)) {
console.log(`Stacks node not ready, waiting...`);
} else {
console.error('Error getting pox info:', error);
}
}
await new Promise(resolve => setTimeout(resolve, 3000));
}
}

async function loop() {
await waitForNakamoto();
while (true) {
try {
await run();
Expand Down

0 comments on commit 97069a5

Please sign in to comment.