use {
bitcoin::{Address, address::NetworkUnchecked},
command_builder::CommandBuilder,
para::{
ckpool::{self, HashRate, HashRateStatus, PoolStatus, ShareStatus, User, Worker},
subcommand::server::{
NodeStatus,
notifications::{NotificationHandler, NotificationPriority, NotificationType},
},
},
pretty_assertions::assert_eq as pretty_assert_eq,
reqwest::{StatusCode, Url},
serde::{Deserialize, Serialize, de::DeserializeOwned},
serial_test::serial,
std::{
collections::{BTreeMap, HashSet},
ffi::{OsStr, OsString},
fs,
io::Write,
path::PathBuf,
process::{Child, Command, Stdio},
str::FromStr,
sync::{Arc, Barrier},
thread,
time::Duration,
},
tempfile::TempDir,
test_server::TestServer,
to_args::ToArgs,
tokio::time::timeout,
};
#[cfg(target_os = "linux")]
use {
crate::{
sync::BATCH_COUNTER,
test_psql::{
create_test_block, create_test_shares, insert_test_account,
insert_test_account_with_diff, insert_test_block, insert_test_payout,
insert_test_remote_shares, insert_test_shares, refresh_round_participation_view,
setup_test_schema,
},
},
anyhow::Error,
api::UserDetail,
base64::{Engine, engine::general_purpose},
bip322::sign_simple_encoded,
bitcoin::{
CompressedPublicKey, Network, PrivateKey, block::Header, hashes::Hash,
key::UntweakedPublicKey, secp256k1::Secp256k1, sign_message::MessageSignature,
},
harness::bitcoind::Bitcoind,
ntest::timeout,
para::{
ENONCE1_EXTENSION_SIZE, ENONCE1_SIZE, MAX_ENONCE_SIZE, USER_AGENT, api,
hash::{HashDays, HashPrice},
router::order::{OrderStatus, Review},
subcommand::{
miner::Share,
server::{
account::{Account, AccountMetadataUpdate, AccountUpdate},
database::{Database, HighestDiff, Payout, PendingPayout},
},
sync::{FoundBlockRecord, ShareBatch, Sync, SyncResponse},
},
},
pgtemp::{PgTempDB, PgTempDBBuilder},
reqwest::Response,
serde_json::json,
std::{
io::{BufRead, BufReader, stderr},
net::{TcpListener, TcpStream},
process::ChildStdout,
sync::atomic::{AtomicUsize, Ordering},
time::Instant,
},
stratum::{
self, Difficulty, Extranonce, JobId, Nonce, Notify, Ntime, StratumError, Username, Version,
client::ClientError,
},
tempfile::tempdir,
test_ckpool::TestCkpool,
test_pool::TestPool,
test_proxy::TestProxy,
test_router::TestRouter,
tokio::{sync::mpsc, time::sleep},
};
mod command_builder;
#[cfg(target_os = "linux")]
mod test_ckpool;
#[cfg(target_os = "linux")]
mod test_pool;
#[cfg(target_os = "linux")]
mod test_proxy;
#[cfg(target_os = "linux")]
mod test_psql;
#[cfg(target_os = "linux")]
mod test_router;
mod test_server;
mod to_args;
#[cfg(target_os = "linux")]
mod account;
mod alerts;
#[cfg(target_os = "linux")]
mod event_sink;
#[cfg(target_os = "linux")]
mod generator;
#[cfg(target_os = "linux")]
mod payouts;
#[cfg(target_os = "linux")]
mod ping;
#[cfg(target_os = "linux")]
mod pool;
#[cfg(target_os = "linux")]
mod proxy;
#[cfg(target_os = "linux")]
mod router;
mod server;
#[cfg(target_os = "linux")]
mod server_with_db;
#[cfg(target_os = "linux")]
mod sync;
#[cfg(target_os = "linux")]
mod template;
#[cfg(target_os = "linux")]
mod wallet;
#[cfg(target_os = "linux")]
fn allocate_port() -> u16 {
TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port()
}
#[cfg(target_os = "linux")]
fn spawn_regtest() -> Bitcoind {
let tempdir = Arc::new(TempDir::new().unwrap());
let rpc_port = allocate_port();
let zmq_port = allocate_port();
Bitcoind::spawn_no_listen(tempdir, rpc_port, zmq_port, false, Network::Regtest).unwrap()
}
#[cfg(target_os = "linux")]
async fn generate_to_address(bitcoind: &Bitcoind, n: u64, address: &str) {
bitcoind
.client()
.unwrap()
.call_raw::<serde_json::Value>("generatetoaddress", &[json!(n), json!(address)])
.await
.unwrap();
}
#[cfg(target_os = "linux")]
fn bitcoind() -> Bitcoind {
for attempt in 0..3 {
let tempdir = Arc::new(TempDir::new().unwrap());
let rpc_port = allocate_port();
let zmq_port = allocate_port();
match Bitcoind::spawn_no_listen(tempdir, rpc_port, zmq_port, false, Network::Signet) {
Ok(bitcoind) => return bitcoind,
Err(e) if attempt < 2 => {
eprintln!("bitcoind spawn attempt {attempt} failed: {e}, retrying with new ports");
continue;
}
Err(e) => panic!("bitcoind failed to spawn after 3 attempts: {e}"),
}
}
unreachable!()
}
#[cfg(target_os = "linux")]
fn next_json<T: DeserializeOwned>(r: &mut BufReader<ChildStdout>) -> T {
let de = serde_json::Deserializer::from_reader(&mut *r);
let mut stream = de.into_iter::<T>();
stream.next().expect("stream ended").expect("bad json")
}
#[cfg(target_os = "linux")]
fn signet_username() -> Username {
"tb1qft5p2uhsdcdc3l2ua4ap5qqfg4pjaqlp250x7us7a8qqhrxrxfsqaqh7jw.tick.abcdef@lnurl.com"
.parse()
.unwrap()
}
#[cfg(target_os = "linux")]
fn solve_share(
notify: &stratum::Notify,
enonce1: &Extranonce,
enonce2: &Extranonce,
difficulty: stratum::Difficulty,
) -> (Ntime, Nonce) {
solve_share_with_version_bits(notify, enonce1, enonce2, difficulty, None, None)
}
#[cfg(target_os = "linux")]
fn solve_share_with_version_bits(
notify: &stratum::Notify,
enonce1: &Extranonce,
enonce2: &Extranonce,
difficulty: stratum::Difficulty,
version_bits: Option<Version>,
version_mask: Option<Version>,
) -> (Ntime, Nonce) {
let merkle_root = stratum::merkle_root(
¬ify.coinb1,
¬ify.coinb2,
enonce1,
enonce2,
¬ify.merkle_branches,
)
.unwrap();
let version = match (version_bits, version_mask) {
(Some(version_bits), Some(mask)) => (notify.version & !mask) | (version_bits & mask),
_ => notify.version,
};
let mut header = Header {
version: version.into(),
prev_blockhash: notify.prevhash.clone().into(),
merkle_root: merkle_root.into(),
time: notify.ntime.into(),
bits: notify.nbits.into(),
nonce: 0,
};
let target = difficulty.to_target();
loop {
let hash = header.block_hash();
if target.is_met_by(hash) {
return (Ntime::from(header.time), Nonce::from(header.nonce));
}
header.nonce += 1;
if header.nonce == 0 {
panic!(
"Nonce wrapped around without finding share at diff {}",
difficulty
);
}
}
}
#[cfg(target_os = "linux")]
async fn wait_for_notify(
events: &mut stratum::client::EventReceiver,
) -> (stratum::Notify, Difficulty) {
let mut difficulty = Difficulty::from(1);
timeout(Duration::from_secs(30), async {
loop {
match events.recv().await.unwrap() {
stratum::client::Event::SetDifficulty(diff) => difficulty = diff,
stratum::client::Event::Notify(notify) => return (notify, difficulty),
_ => {}
}
}
})
.await
.expect("Timeout waiting for notify")
}
#[cfg(target_os = "linux")]
async fn submit_share(
client: &stratum::client::Client,
notify: &stratum::Notify,
enonce1: &Extranonce,
enonce2_size: usize,
difficulty: Difficulty,
) -> Result<Duration, ClientError> {
let enonce2 = Extranonce::random(enonce2_size);
let (ntime, nonce) = solve_share(notify, enonce1, &enonce2, difficulty);
client
.submit(notify.job_id, enonce2, ntime, nonce, None)
.await
}
#[cfg(target_os = "linux")]
async fn mine_until_difficulty_increases(
client: &stratum::client::Client,
events: &mut stratum::client::EventReceiver,
notify: &mut stratum::Notify,
enonce1: &Extranonce,
enonce2_size: usize,
initial_difficulty: Difficulty,
) -> Difficulty {
let deadline = tokio::time::Instant::now() + Duration::from_secs(60);
loop {
while let Some(Ok(event)) = events.try_recv() {
match event {
stratum::client::Event::Notify(n) => *notify = n,
stratum::client::Event::SetDifficulty(d) if d > initial_difficulty => return d,
_ => {}
}
}
assert!(
tokio::time::Instant::now() < deadline,
"Timeout waiting for difficulty increase above {}",
initial_difficulty
);
if submit_share(client, notify, enonce1, enonce2_size, initial_difficulty)
.await
.is_ok()
{
tokio::time::sleep(Duration::from_millis(20)).await;
}
}
}
#[cfg(target_os = "linux")]
async fn wait_for_job_update(
events: &mut stratum::client::EventReceiver,
old_job_id: JobId,
) -> stratum::Notify {
timeout(Duration::from_secs(30), async {
loop {
match events.recv().await.unwrap() {
stratum::client::Event::Notify(n) if n.job_id != old_job_id && !n.clean_jobs => {
return n;
}
_ => {}
}
}
})
.await
.expect("Timeout waiting for template update")
}
#[cfg(target_os = "linux")]
async fn wait_for_new_block(
events: &mut stratum::client::EventReceiver,
old_job_id: JobId,
) -> stratum::Notify {
timeout(Duration::from_secs(90), async {
loop {
match events.recv().await.unwrap() {
stratum::client::Event::Notify(n) if n.job_id != old_job_id && n.clean_jobs => {
return n;
}
_ => {}
}
}
})
.await
.expect("Timeout waiting for new block")
}
#[cfg(target_os = "linux")]
fn assert_stratum_error<T: std::fmt::Debug>(
result: Result<T, ClientError>,
expected: StratumError,
) {
assert!(
matches!(
&result,
Err(ClientError::Stratum { response }) if response.error_code == expected as i32
),
"Expected {:?}, got {:?}",
expected,
result
);
}
pub(crate) fn address(n: u32) -> Address {
match n {
0 => "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4",
1 => "bc1qhl452zcq3ng5kzajzkx9jnzncml9tnsk3w96s6",
2 => "bc1qqqcjq9jydx79rywltc38g5qfrjq485a8xfmkf7",
3 => "bc1qcq2uv5nk6hec6kvag3wyevp6574qmsm9scjxc2",
4 => "bc1qukgekwq8e68ay0mewdrvg0d3cfuc094aj2rvx9",
5 => "bc1qtdjs8tgkaja5ddxs0j7rn52uqfdtqa53mum8xc",
6 => "bc1qd3ex6kwlc5ett55hgsnk94y8q2zhdyxyqyujkl",
7 => "bc1q8dcv8r903evljd87mcg0hq8lphclch7pd776wt",
8 => "bc1q9j6xvm3td447ygnhfra5tfkpkcupwe9937nhjq",
9 => "bc1qlyrhjzvxdzmvxe2mnr37p68vkl5fysyhfph8z0",
_ => panic!(),
}
.parse::<Address<NetworkUnchecked>>()
.unwrap()
.assume_checked()
}
fn zero_status() -> ckpool::Status {
ckpool::Status {
pool: PoolStatus {
runtime: 0,
lastupdate: 0,
users: 0,
workers: 0,
idle: 0,
disconnected: 0,
},
hash_rates: HashRateStatus {
hashrate1m: HashRate(0.0),
hashrate5m: HashRate(0.0),
hashrate15m: HashRate(0.0),
hashrate1hr: HashRate(0.0),
hashrate6hr: HashRate(0.0),
hashrate1d: HashRate(0.0),
hashrate7d: HashRate(0.0),
},
shares: ShareStatus {
diff: 0.0,
accepted: 0,
rejected: 0,
bestshare: 0,
sps1m: 0.0,
sps5m: 0.0,
sps15m: 0.0,
sps1h: 0.0,
},
}
}
fn typical_status() -> ckpool::Status {
ckpool::Status {
pool: PoolStatus {
runtime: 86400,
lastupdate: 0,
users: 5000,
workers: 20000,
idle: 1000,
disconnected: 500,
},
hash_rates: HashRateStatus {
hashrate1m: HashRate::from_str("100E").unwrap(),
hashrate5m: HashRate::from_str("95E").unwrap(),
hashrate15m: HashRate::from_str("98E").unwrap(),
hashrate1hr: HashRate::from_str("102E").unwrap(),
hashrate6hr: HashRate::from_str("99E").unwrap(),
hashrate1d: HashRate::from_str("100E").unwrap(),
hashrate7d: HashRate::from_str("105E").unwrap(),
},
shares: ShareStatus {
diff: 120.5,
accepted: 1_000_000_000_000,
rejected: 10_000_000_000,
bestshare: 500_000_000_000,
sps1m: 5000.0,
sps5m: 4900.0,
sps15m: 4950.0,
sps1h: 5050.0,
},
}
}
fn zero_user() -> User {
User {
hashrate1m: HashRate(0.0),
hashrate5m: HashRate(0.0),
hashrate1hr: HashRate(0.0),
hashrate1d: HashRate(0.0),
hashrate7d: HashRate(0.0),
lastshare: 0,
workers: 0,
shares: 0,
bestshare: 0.0,
bestever: 0,
authorised: 0,
worker: vec![],
}
}
fn typical_user() -> User {
let now = 4_000_000_000;
User {
hashrate1m: HashRate::from_str("10P").unwrap(),
hashrate5m: HashRate::from_str("9.5P").unwrap(),
hashrate1hr: HashRate::from_str("10.2P").unwrap(),
hashrate1d: HashRate::from_str("10P").unwrap(),
hashrate7d: HashRate::from_str("10.5P").unwrap(),
lastshare: now - 60,
workers: 3,
shares: 1_000_000_000,
bestshare: 1e12,
bestever: 5_000_000_000,
authorised: 1,
worker: vec![
Worker {
workername: "rig1".to_string(),
hashrate1m: HashRate::from_str("4P").unwrap(),
hashrate5m: HashRate::from_str("3.8P").unwrap(),
hashrate1hr: HashRate::from_str("4.1P").unwrap(),
hashrate1d: HashRate::from_str("4P").unwrap(),
hashrate7d: HashRate::from_str("4.2P").unwrap(),
lastshare: now - 120,
shares: 400_000_000,
bestshare: 5e11,
bestever: 2_000_000_000,
},
Worker {
workername: "rig2".to_string(),
hashrate1m: HashRate::from_str("3P").unwrap(),
hashrate5m: HashRate::from_str("2.9P").unwrap(),
hashrate1hr: HashRate::from_str("3.0P").unwrap(),
hashrate1d: HashRate::from_str("3P").unwrap(),
hashrate7d: HashRate::from_str("3.1P").unwrap(),
lastshare: now - 180,
shares: 300_000_000,
bestshare: 3e11,
bestever: 1_500_000_000,
},
Worker {
workername: "rig3".to_string(),
hashrate1m: HashRate::from_str("3P").unwrap(),
hashrate5m: HashRate::from_str("2.8P").unwrap(),
hashrate1hr: HashRate::from_str("3.1P").unwrap(),
hashrate1d: HashRate::from_str("3P").unwrap(),
hashrate7d: HashRate::from_str("3.2P").unwrap(),
lastshare: now - 60,
shares: 300_000_000,
bestshare: 4e11,
bestever: 1_500_000_000,
},
],
}
}