use crate::{
chain_tip::ChainTip,
extranonce_manager::{AllocatedExtranoncePrefix, ExtranoncePrefix},
server::{
error::StandardChannelError,
jobs::{
extended::ExtendedJob,
factory::JobFactory,
job_store::{JobStore, MAX_PAST_JOBS},
standard::StandardJob,
},
share_accounting::{ShareAccounting, ShareValidationError, ShareValidationResult},
},
target::{bytes_to_hex, hash_rate_to_target, u256_to_block_hash},
MAX_EXTRANONCE_LEN, MAX_FUTURE_BLOCK_TIME, VERSION_ROLLING_MASK,
};
use bitcoin::{
absolute::LockTime,
blockdata::{
block::{Header, Version},
witness::Witness,
},
consensus::Encodable,
hashes::sha256d::Hash,
transaction::{OutPoint, Transaction, TxIn, TxOut, Version as TxVersion},
CompactTarget, Sequence, Target,
};
use mining_sv2::{
SubmitSharesStandardOwned, ERROR_CODE_OPEN_MINING_CHANNEL_INVALID_NOMINAL_HASHRATE,
ERROR_CODE_OPEN_MINING_CHANNEL_MAX_TARGET_OUT_OF_RANGE,
ERROR_CODE_SUBMIT_SHARES_DIFFICULTY_TOO_LOW, ERROR_CODE_SUBMIT_SHARES_DUPLICATE_SHARE,
ERROR_CODE_SUBMIT_SHARES_INVALID_JOB_ID,
ERROR_CODE_SUBMIT_SHARES_INVALID_NON_ROLLABLE_VERSION_BIT,
ERROR_CODE_SUBMIT_SHARES_INVALID_SHARE, ERROR_CODE_SUBMIT_SHARES_STALE_SHARE,
ERROR_CODE_UPDATE_CHANNEL_INVALID_NOMINAL_HASHRATE,
ERROR_CODE_UPDATE_CHANNEL_MAX_TARGET_OUT_OF_RANGE,
};
use std::collections::HashMap;
use template_distribution_sv2::{NewTemplateOwned, SetNewPrevHashOwned as SetNewPrevHash};
use tracing::{debug, warn};
#[derive(Debug)]
pub struct StandardChannel {
pub channel_id: u32,
user_identity: String,
extranonce_prefix: ExtranoncePrefix,
requested_max_target: Target,
target: Target,
job_id_to_target: HashMap<u32, Target>,
nominal_hashrate: f32,
stable_hashrate: bool,
share_accounting: ShareAccounting,
expected_share_per_minute: f32,
job_store: JobStore<StandardJob>,
job_factory: JobFactory,
chain_tip: Option<ChainTip>,
}
impl StandardChannel {
#[allow(clippy::too_many_arguments)]
pub fn new_for_pool(
channel_id: u32,
user_identity: String,
extranonce_prefix: AllocatedExtranoncePrefix,
requested_max_target: Target,
nominal_hashrate: f32,
share_batch_size: usize,
expected_share_per_minute: f32,
pool_tag_string: String,
max_past_jobs: Option<usize>,
) -> Result<Self, StandardChannelError> {
Self::new(
channel_id,
user_identity,
extranonce_prefix.into(),
requested_max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
Some(pool_tag_string),
None,
max_past_jobs,
)
}
#[allow(clippy::too_many_arguments)]
pub fn new_for_job_declaration_client(
channel_id: u32,
user_identity: String,
extranonce_prefix: AllocatedExtranoncePrefix,
requested_max_target: Target,
nominal_hashrate: f32,
share_batch_size: usize,
expected_share_per_minute: f32,
pool_tag_string: Option<String>,
miner_tag_string: String,
max_past_jobs: Option<usize>,
) -> Result<Self, StandardChannelError> {
Self::new(
channel_id,
user_identity,
extranonce_prefix.into(),
requested_max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
pool_tag_string,
Some(miner_tag_string),
max_past_jobs,
)
}
#[allow(clippy::too_many_arguments)]
fn new(
channel_id: u32,
user_identity: String,
extranonce_prefix: ExtranoncePrefix,
requested_max_target: Target,
nominal_hashrate: f32,
share_batch_size: usize,
expected_share_per_minute: f32,
pool_tag_string: Option<String>,
miner_tag_string: Option<String>,
max_past_jobs: Option<usize>,
) -> Result<Self, StandardChannelError> {
if requested_max_target == Target::ZERO {
return Err(StandardChannelError::OpenChannelInvalidMaxTarget(
ERROR_CODE_OPEN_MINING_CHANNEL_MAX_TARGET_OUT_OF_RANGE,
));
}
let calculated_target =
match hash_rate_to_target(nominal_hashrate.into(), expected_share_per_minute.into()) {
Ok(target_u256) => target_u256,
Err(_) => {
return Err(StandardChannelError::OpenChannelInvalidNominalHashrate(
ERROR_CODE_OPEN_MINING_CHANNEL_INVALID_NOMINAL_HASHRATE,
));
}
};
let target = calculated_target.min(requested_max_target);
if extranonce_prefix.len() > MAX_EXTRANONCE_LEN as usize {
return Err(StandardChannelError::ExtranoncePrefixTooLarge);
}
let job_factory = JobFactory::new(true, pool_tag_string, miner_tag_string);
if !job_factory.fits_script_sig_budget(extranonce_prefix.len()) {
return Err(StandardChannelError::ScriptSigSizeTooLarge);
}
let max_past_jobs = match max_past_jobs {
Some(cap) if cap > 0 => cap,
_ => MAX_PAST_JOBS,
};
Ok(Self {
channel_id,
user_identity,
extranonce_prefix,
requested_max_target,
target,
job_id_to_target: HashMap::new(),
nominal_hashrate,
stable_hashrate: false,
share_accounting: ShareAccounting::new(
share_batch_size,
crate::seen_shares_budget(expected_share_per_minute as f64),
),
expected_share_per_minute,
job_store: JobStore::new(max_past_jobs),
job_factory,
chain_tip: None,
})
}
pub fn get_channel_id(&self) -> u32 {
self.channel_id
}
pub fn get_user_identity(&self) -> &str {
&self.user_identity
}
pub fn get_extranonce_prefix(&self) -> &[u8] {
self.extranonce_prefix.as_bytes()
}
pub fn set_extranonce_prefix(
&mut self,
extranonce_prefix: AllocatedExtranoncePrefix,
) -> Result<(), StandardChannelError> {
if extranonce_prefix.len() > MAX_EXTRANONCE_LEN as usize {
return Err(StandardChannelError::ExtranoncePrefixTooLarge);
}
if !self
.job_factory
.fits_script_sig_budget(extranonce_prefix.len())
{
return Err(StandardChannelError::ScriptSigSizeTooLarge);
}
let retired_extranonce_prefix =
std::mem::replace(&mut self.extranonce_prefix, extranonce_prefix.into());
self.job_store
.retire_extranonce_prefix(retired_extranonce_prefix);
Ok(())
}
pub fn set_upstream_extranonce_prefix(
&mut self,
upstream_prefix: &[u8],
) -> Result<(), StandardChannelError> {
let updated_prefix_len = upstream_prefix.len() + self.extranonce_prefix.preserved_len();
if updated_prefix_len > MAX_EXTRANONCE_LEN as usize {
return Err(StandardChannelError::ExtranoncePrefixTooLarge);
}
if !self.job_factory.fits_script_sig_budget(updated_prefix_len) {
return Err(StandardChannelError::ScriptSigSizeTooLarge);
}
let snapshot = self
.extranonce_prefix
.snapshot_for_upstream_update(upstream_prefix);
self.extranonce_prefix
.set_upstream_prefix(upstream_prefix)
.map_err(|_| StandardChannelError::ExtranoncePrefixTooLarge)?;
if let Some(snapshot) = snapshot {
self.job_store.retire_extranonce_prefix(snapshot);
}
Ok(())
}
pub fn set_target(&mut self, target: Target) -> Result<(), StandardChannelError> {
if target == Target::ZERO {
return Err(StandardChannelError::InvalidTarget);
}
self.target = target;
Ok(())
}
pub fn set_nominal_hashrate(&mut self, nominal_hashrate: f32) {
self.nominal_hashrate = nominal_hashrate;
}
pub fn set_stable_hashrate(&mut self, stable_hashrate: bool) {
self.stable_hashrate = stable_hashrate;
}
pub fn get_stable_hashrate(&self) -> bool {
self.stable_hashrate
}
pub fn get_requested_max_target(&self) -> &Target {
&self.requested_max_target
}
pub fn get_target(&self) -> &Target {
&self.target
}
pub fn get_nominal_hashrate(&self) -> f32 {
self.nominal_hashrate
}
pub fn update_channel(
&mut self,
nominal_hashrate: f32,
requested_max_target: Option<Target>,
) -> Result<(), StandardChannelError> {
let target = match hash_rate_to_target(
nominal_hashrate.into(),
self.expected_share_per_minute.into(),
) {
Ok(target) => target,
Err(_) => {
return Err(StandardChannelError::UpdateChannelInvalidNominalHashrate(
ERROR_CODE_UPDATE_CHANNEL_INVALID_NOMINAL_HASHRATE,
));
}
};
let requested_max_target = match requested_max_target {
Some(ref requested_max_target) => *requested_max_target,
None => self.requested_max_target,
};
if requested_max_target == Target::ZERO {
return Err(StandardChannelError::UpdateChannelInvalidMaxTarget(
ERROR_CODE_UPDATE_CHANNEL_MAX_TARGET_OUT_OF_RANGE,
));
}
let target_bytes = target.to_be_bytes();
let max_target_bytes = requested_max_target.to_be_bytes();
let old_target = self.target;
let old_target_bytes = old_target.to_be_bytes();
debug!(
"updating channel target \nold target:\t{}\nnew target:\t{}\nmax_target:\t{}",
bytes_to_hex(&old_target_bytes),
bytes_to_hex(&target_bytes),
bytes_to_hex(&max_target_bytes)
);
let new_target = target.min(requested_max_target);
self.target = new_target;
self.nominal_hashrate = nominal_hashrate;
self.requested_max_target = requested_max_target;
Ok(())
}
pub fn get_active_job(&self) -> Option<&StandardJob> {
self.job_store.get_active_job()
}
pub fn get_future_job_id_from_template_id(&self, template_id: u64) -> Option<u32> {
self.job_store
.get_future_job_id_from_template_id(template_id)
}
pub fn get_future_job(&self, job_id: u32) -> Option<&StandardJob> {
self.job_store.get_future_job(job_id)
}
pub fn get_past_job(&self, job_id: u32) -> Option<&StandardJob> {
self.job_store.get_past_job(job_id)
}
pub fn get_stale_job(&self, job_id: u32) -> Option<&StandardJob> {
self.job_store.get_stale_job(job_id)
}
pub fn get_shares_per_minute(&self) -> f32 {
self.expected_share_per_minute
}
pub fn get_chain_tip(&self) -> Option<&ChainTip> {
self.chain_tip.as_ref()
}
#[cfg(test)]
fn set_chain_tip(&mut self, chain_tip: ChainTip) {
self.chain_tip = Some(chain_tip);
}
pub fn get_share_accounting(&self) -> &ShareAccounting {
&self.share_accounting
}
pub fn on_new_template(
&mut self,
template: NewTemplateOwned,
coinbase_reward_outputs: Vec<TxOut>,
) -> Result<(), StandardChannelError> {
match template.future_template {
true => {
let new_job = self
.job_factory
.new_standard_job(
self.channel_id,
None,
self.extranonce_prefix.as_bytes().to_vec(),
template.clone(),
coinbase_reward_outputs,
)
.map_err(StandardChannelError::JobFactoryError)?;
self.job_store.add_future_job(template.template_id, new_job);
}
false => {
match self.chain_tip.clone() {
None => return Err(StandardChannelError::ChainTipNotSet),
Some(chain_tip) => {
let new_job = self
.job_factory
.new_standard_job(
self.channel_id,
Some(chain_tip),
self.extranonce_prefix.as_bytes().to_vec(),
template.clone(),
coinbase_reward_outputs,
)
.map_err(StandardChannelError::JobFactoryError)?;
let job_id = new_job.get_job_id();
if let Some(evicted_job_id) = self.job_store.add_active_job(new_job) {
self.job_id_to_target.remove(&evicted_job_id);
}
self.job_id_to_target.insert(job_id, self.target);
}
}
}
}
Ok(())
}
pub fn on_group_channel_job(
&mut self,
extended_job: ExtendedJob,
) -> Result<(), StandardChannelError> {
let standard_job = extended_job
.into_standard_job(self.channel_id, self.extranonce_prefix.as_bytes().to_vec())
.map_err(|_| StandardChannelError::FailedToConvertToStandardJob)?;
match standard_job.is_future() {
true => {
self.job_store
.add_future_job(standard_job.get_template().template_id, standard_job);
}
false => {
if let Some(min_ntime) = standard_job.get_min_ntime() {
if self
.chain_tip
.as_ref()
.is_some_and(|chain_tip| min_ntime < chain_tip.min_ntime())
{
return Err(StandardChannelError::JobMinNtimeBelowChainTip);
}
}
let job_id = standard_job.get_job_id();
if let Some(evicted_job_id) = self.job_store.add_active_job(standard_job) {
self.job_id_to_target.remove(&evicted_job_id);
}
self.job_id_to_target.insert(job_id, self.target);
}
}
Ok(())
}
pub fn on_set_new_prev_hash(
&mut self,
set_new_prev_hash: SetNewPrevHash,
) -> Result<(), StandardChannelError> {
match self.job_store.has_future_jobs() {
false => {
warn!(
"SetNewPrevHash with no queued future template: non-conforming Template \
Distribution peer, recovering the chain tip"
);
self.job_store.deactivate_job();
self.job_store.mark_past_jobs_as_stale();
self.job_id_to_target.clear();
}
true => {
if !self.job_store.activate_future_job(
set_new_prev_hash.template_id,
set_new_prev_hash.header_timestamp,
) {
return Err(StandardChannelError::TemplateIdNotFound);
}
self.job_id_to_target.clear();
let job_id = self
.job_store
.get_active_job()
.expect("active job must exist")
.get_job_id();
self.job_id_to_target.insert(job_id, self.target);
}
}
if self
.chain_tip
.as_ref()
.is_some_and(|chain_tip| chain_tip.prev_hash() != set_new_prev_hash.prev_hash)
{
self.share_accounting.flush_seen_shares();
}
self.chain_tip = Some(set_new_prev_hash.into());
Ok(())
}
pub fn validate_share(
&mut self,
share: SubmitSharesStandardOwned,
) -> Result<ShareValidationResult, ShareValidationError> {
if self.share_accounting.is_seen_shares_budget_exhausted() {
return Err(ShareValidationError::SeenSharesBudgetExhausted);
}
let job_id = share.job_id;
let is_active_job = self
.job_store
.get_active_job()
.is_some_and(|job| job.get_job_id() == job_id);
let is_past_job = self.job_store.get_past_job(job_id).is_some();
let is_stale_job = self.job_store.get_stale_job(job_id).is_some();
if is_stale_job {
self.share_accounting
.increment_rejected_shares(ERROR_CODE_SUBMIT_SHARES_STALE_SHARE);
return Err(ShareValidationError::Stale(
ERROR_CODE_SUBMIT_SHARES_STALE_SHARE,
));
}
if !is_active_job && !is_past_job && !is_stale_job {
self.share_accounting
.increment_rejected_shares(ERROR_CODE_SUBMIT_SHARES_INVALID_JOB_ID);
return Err(ShareValidationError::InvalidJobId(
ERROR_CODE_SUBMIT_SHARES_INVALID_JOB_ID,
));
}
let job = if is_active_job {
self.job_store
.get_active_job()
.expect("active job must exist")
} else if is_past_job {
self.job_store
.get_past_job(job_id)
.expect("past job must exist")
} else {
self.job_store
.get_stale_job(job_id)
.expect("stale job must exist")
};
let Some(job_target) = self.job_id_to_target.get(&job_id) else {
self.share_accounting
.increment_rejected_shares(ERROR_CODE_SUBMIT_SHARES_INVALID_JOB_ID);
return Err(ShareValidationError::InvalidJobId(
ERROR_CODE_SUBMIT_SHARES_INVALID_JOB_ID,
));
};
let merkle_root = job.get_merkle_root().to_array();
let chain_tip = self
.chain_tip
.as_ref()
.ok_or(ShareValidationError::NoChainTip)?;
let prev_hash = chain_tip.prev_hash();
let nbits = CompactTarget::from_consensus(chain_tip.nbits());
let job_min_ntime = job
.get_min_ntime()
.expect("active and past jobs carry a min_ntime");
if share.ntime < job_min_ntime {
self.share_accounting
.increment_rejected_shares(ERROR_CODE_SUBMIT_SHARES_INVALID_SHARE);
return Err(ShareValidationError::Invalid(
ERROR_CODE_SUBMIT_SHARES_INVALID_SHARE,
));
}
if share.ntime > job_min_ntime.saturating_add(MAX_FUTURE_BLOCK_TIME) {
self.share_accounting
.increment_rejected_shares(ERROR_CODE_SUBMIT_SHARES_INVALID_SHARE);
return Err(ShareValidationError::Invalid(
ERROR_CODE_SUBMIT_SHARES_INVALID_SHARE,
));
}
if (share.version & !VERSION_ROLLING_MASK) != (job.get_version() & !VERSION_ROLLING_MASK) {
self.share_accounting.increment_rejected_shares(
ERROR_CODE_SUBMIT_SHARES_INVALID_NON_ROLLABLE_VERSION_BIT,
);
return Err(ShareValidationError::Invalid(
ERROR_CODE_SUBMIT_SHARES_INVALID_NON_ROLLABLE_VERSION_BIT,
));
}
let header = Header {
version: Version::from_consensus(share.version as i32),
prev_blockhash: u256_to_block_hash(prev_hash.clone()),
merkle_root: (*Hash::from_bytes_ref(&merkle_root)).into(),
time: share.ntime,
bits: nbits,
nonce: share.nonce,
};
let share_hash = header.block_hash();
let share_raw_hash: [u8; 32] = *share_hash.to_raw_hash().as_ref();
let share_hash_target = Target::from_le_bytes(share_raw_hash);
let share_hash_as_diff = share_hash_target.difficulty_float();
let network_target = Target::from_compact(nbits);
let share_hash_target_bytes = share_hash_target.to_be_bytes();
let job_target_bytes = job_target.to_be_bytes();
debug!(
"share validation \nshare:\t\t{}\njob target:\t{}\nnetwork target:\t{}",
bytes_to_hex(&share_hash_target_bytes),
bytes_to_hex(&job_target_bytes),
format!("{:x}", network_target)
);
if network_target.is_met_by(share_hash) {
if self
.share_accounting
.is_share_seen(share_hash.to_raw_hash())
{
self.share_accounting
.increment_rejected_shares(ERROR_CODE_SUBMIT_SHARES_DUPLICATE_SHARE);
return Err(ShareValidationError::DuplicateShare(
ERROR_CODE_SUBMIT_SHARES_DUPLICATE_SHARE,
));
}
self.share_accounting.update_share_accounting(
job_target.difficulty_float(),
share.sequence_number,
share_hash.to_raw_hash(),
);
self.share_accounting.increment_blocks_found();
self.share_accounting.mark_batch_acknowledged();
let op_pushbytes_pool_miner_tag = self
.job_factory
.op_pushbytes_pool_miner_tag()
.map_err(|_| ShareValidationError::InvalidCoinbase)?;
let mut script_sig = job.get_template().coinbase_prefix.to_owned_bytes();
script_sig.extend(op_pushbytes_pool_miner_tag);
script_sig.push(job.get_extranonce_prefix().len() as u8); script_sig.extend(job.get_extranonce_prefix());
let tx_in = TxIn {
previous_output: OutPoint::null(),
script_sig: script_sig.into(),
sequence: Sequence(job.get_template().coinbase_tx_input_sequence),
witness: Witness::from(vec![vec![0; 32]]),
};
let coinbase = Transaction {
version: TxVersion::non_standard(job.get_template().coinbase_tx_version as i32),
lock_time: LockTime::from_consensus(job.get_template().coinbase_tx_locktime),
input: vec![tx_in],
output: job.get_coinbase_outputs().to_vec(),
};
let mut serialized_coinbase = Vec::new();
coinbase
.consensus_encode(&mut serialized_coinbase)
.map_err(|_| ShareValidationError::InvalidCoinbase)?;
return Ok(ShareValidationResult::BlockFound(
share_hash.to_raw_hash(),
Some(job.get_template().template_id),
serialized_coinbase,
));
}
if share_hash_target <= *job_target {
if self
.share_accounting
.is_share_seen(share_hash.to_raw_hash())
{
self.share_accounting
.increment_rejected_shares(ERROR_CODE_SUBMIT_SHARES_DUPLICATE_SHARE);
return Err(ShareValidationError::DuplicateShare(
ERROR_CODE_SUBMIT_SHARES_DUPLICATE_SHARE,
));
}
self.share_accounting.update_share_accounting(
job_target.difficulty_float(),
share.sequence_number,
share_hash.to_raw_hash(),
);
self.share_accounting.update_best_diff(share_hash_as_diff);
Ok(ShareValidationResult::Valid(share_hash.to_raw_hash()))
} else {
self.share_accounting
.increment_rejected_shares(ERROR_CODE_SUBMIT_SHARES_DIFFICULTY_TOO_LOW);
Err(ShareValidationError::DoesNotMeetTarget(
ERROR_CODE_SUBMIT_SHARES_DIFFICULTY_TOO_LOW,
))
}
}
}
#[cfg(test)]
mod tests {
use crate::{
chain_tip::ChainTip,
extranonce_manager::{
AllocatedExtranoncePrefix, ExtranonceAllocator, ExtranonceAllocatorError,
ExtranoncePrefix, ExtranoncePrefixError,
},
server::{
error::StandardChannelError,
jobs::{
factory::{MAX_COINBASE_PREFIX_SIZE, MAX_SCRIPT_SIG_SIZE},
job_store::{MAX_FUTURE_JOBS, MAX_PAST_JOBS},
},
share_accounting::{ShareValidationError, ShareValidationResult},
standard::StandardChannel,
},
MAX_EXTRANONCE_LEN,
};
use binary_sv2::Sv2OptionOwned as Sv2Option;
use bitcoin::{
consensus::deserialize, transaction::TxOut, Amount, ScriptBuf, Target, Transaction,
};
use mining_sv2::{
NewMiningJobOwned as NewMiningJob, SubmitSharesStandardOwned,
ERROR_CODE_OPEN_MINING_CHANNEL_MAX_TARGET_OUT_OF_RANGE,
ERROR_CODE_SUBMIT_SHARES_DIFFICULTY_TOO_LOW,
ERROR_CODE_SUBMIT_SHARES_INVALID_NON_ROLLABLE_VERSION_BIT,
ERROR_CODE_UPDATE_CHANNEL_MAX_TARGET_OUT_OF_RANGE,
};
use std::convert::TryInto;
use template_distribution_sv2::{
NewTemplateOwned as NewTemplate, SetNewPrevHashOwned as SetNewPrevHashTdp,
};
const SATS_AVAILABLE_IN_TEMPLATE: u64 = 5000000000;
#[test]
fn test_future_job_activation_flow() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 10.0;
let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: true,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
assert!(!standard_channel.job_store.has_future_jobs());
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let expected_future_standard_job = NewMiningJob {
channel_id: standard_channel_id,
job_id: 1,
merkle_root: [
244, 175, 251, 103, 170, 111, 89, 111, 245, 253, 167, 99, 126, 231, 170, 153, 174,
69, 251, 243, 5, 119, 145, 76, 19, 107, 215, 155, 166, 36, 228, 210,
]
.into(),
version: 536870912,
min_ntime: Sv2Option::new(None),
};
let future_standard_job_from_channel = standard_channel.get_future_job(1).unwrap().clone();
assert_eq!(
future_standard_job_from_channel.get_job_message(),
&expected_future_standard_job
);
let ntime = 1747092633;
let set_new_prev_hash = SetNewPrevHashTdp {
template_id: template.template_id,
prev_hash: [
200, 53, 253, 129, 214, 31, 43, 84, 179, 58, 58, 76, 128, 213, 24, 53, 38, 144,
205, 88, 172, 20, 251, 22, 217, 141, 21, 221, 21, 0, 0, 0,
]
.into(),
header_timestamp: ntime,
n_bits: 503543726,
target: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 119, 3, 0, 0,
]
.into(),
};
standard_channel
.on_set_new_prev_hash(set_new_prev_hash)
.unwrap();
let mut previously_future_job = future_standard_job_from_channel.clone();
previously_future_job.activate(ntime);
let activated_job = standard_channel.get_active_job().unwrap();
assert_eq!(
activated_job.get_job_message(),
previously_future_job.get_job_message()
);
}
#[test]
fn test_non_future_job_creation_flow() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 10.0;
let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let ntime = 1747092633;
let prev_hash = [
200, 53, 253, 129, 214, 31, 43, 84, 179, 58, 58, 76, 128, 213, 24, 53, 38, 144, 205,
88, 172, 20, 251, 22, 217, 141, 21, 221, 21, 0, 0, 0,
]
.into();
let nbits = 503543726;
let chain_tip = ChainTip::new(prev_hash, nbits, ntime);
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
standard_channel.set_chain_tip(chain_tip);
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let expected_active_standard_job = NewMiningJob {
channel_id: standard_channel_id,
job_id: 1,
merkle_root: [
244, 175, 251, 103, 170, 111, 89, 111, 245, 253, 167, 99, 126, 231, 170, 153, 174,
69, 251, 243, 5, 119, 145, 76, 19, 107, 215, 155, 166, 36, 228, 210,
]
.into(),
version: 536870912,
min_ntime: Sv2Option::new(Some(ntime)),
};
let active_standard_job_from_channel = standard_channel.get_active_job().unwrap().clone();
assert_eq!(
active_standard_job_from_channel.get_job_message(),
&expected_active_standard_job
);
}
#[test]
fn test_share_validation_block_found() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 1.0;
let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
let ntime = 1745596910;
let prev_hash = [
251, 175, 106, 40, 35, 87, 122, 90, 58, 51, 78, 32, 202, 236, 228, 36, 154, 174, 206,
144, 147, 195, 21, 224, 195, 103, 214, 189, 51, 190, 24, 98,
]
.into();
let n_bits = 545259519;
let chain_tip = ChainTip::new(prev_hash, n_bits, ntime);
standard_channel.set_chain_tip(chain_tip);
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let active_standard_job = standard_channel.get_active_job().unwrap();
let share_valid_block = SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number: 0,
job_id: active_standard_job.get_job_id(),
nonce: 0,
ntime: 1745596932,
version: 536870912,
};
let res = standard_channel.validate_share(share_valid_block.clone());
assert!(matches!(
res,
Ok(ShareValidationResult::BlockFound(_, _, _))
));
assert_eq!(
standard_channel.get_share_accounting().get_blocks_found(),
1
);
let res = standard_channel.validate_share(share_valid_block);
assert!(matches!(
res.unwrap_err(),
ShareValidationError::DuplicateShare(_)
));
assert_eq!(
standard_channel.get_share_accounting().get_blocks_found(),
1
);
}
#[test]
fn test_share_validation_ntime_below_min_ntime() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 1.0;
let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0];
script_bytes.push(20);
script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
let prev_hash = [
251, 175, 106, 40, 35, 87, 122, 90, 58, 51, 78, 32, 202, 236, 228, 36, 154, 174, 206,
144, 147, 195, 21, 224, 195, 103, 214, 189, 51, 190, 24, 98,
]
.into();
let n_bits = 545259519;
let chain_tip = ChainTip::new(prev_hash, n_bits, 1745596933);
standard_channel.set_chain_tip(chain_tip);
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let active_standard_job = standard_channel.get_active_job().unwrap();
let share_below_min_ntime = SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number: 0,
job_id: active_standard_job.get_job_id(),
nonce: 0,
ntime: 1745596932,
version: 536870912,
};
let res = standard_channel.validate_share(share_below_min_ntime);
assert!(matches!(res.unwrap_err(), ShareValidationError::Invalid(_)));
assert_eq!(
standard_channel.get_share_accounting().get_blocks_found(),
0
);
}
#[test]
fn test_share_validation_block_found_after_extranonce_prefix_rotation() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 1.0;
let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
let ntime = 1745596910;
let prev_hash = [
251, 175, 106, 40, 35, 87, 122, 90, 58, 51, 78, 32, 202, 236, 228, 36, 154, 174, 206,
144, 147, 195, 21, 224, 195, 103, 214, 189, 51, 190, 24, 98,
]
.into();
let n_bits = 545259519;
let chain_tip = ChainTip::new(prev_hash, n_bits, ntime);
standard_channel.set_chain_tip(chain_tip);
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let active_standard_job = standard_channel.get_active_job().unwrap();
let job_id = active_standard_job.get_job_id();
let job_merkle_root = active_standard_job.get_merkle_root().to_array();
let job_extranonce_prefix = active_standard_job.get_extranonce_prefix().to_vec();
assert_eq!(job_extranonce_prefix, extranonce_prefix);
let rotated_extranonce_prefix = vec![0xab; 16];
standard_channel
.set_extranonce_prefix(
AllocatedExtranoncePrefix::for_test(rotated_extranonce_prefix).unwrap(),
)
.unwrap();
let share_valid_block = SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number: 0,
job_id,
nonce: 0,
ntime: 1745596932,
version: 536870912,
};
let Ok(ShareValidationResult::BlockFound(_, _, serialized_coinbase)) =
standard_channel.validate_share(share_valid_block)
else {
panic!("expected BlockFound");
};
let coinbase: Transaction = deserialize(&serialized_coinbase).unwrap();
let coinbase_txid: [u8; 32] = *coinbase.compute_txid().as_ref();
assert_eq!(coinbase_txid, job_merkle_root);
let script_sig = coinbase.input[0].script_sig.as_bytes();
let extranonce_start = script_sig.len() - job_extranonce_prefix.len();
assert_eq!(
script_sig[extranonce_start - 1],
job_extranonce_prefix.len() as u8
);
assert_eq!(&script_sig[extranonce_start..], &job_extranonce_prefix[..]);
}
#[test]
fn test_share_validation_does_not_meet_target() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 100.0; let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
let ntime = 1745596910;
let prev_hash = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
let n_bits = 453040064;
let chain_tip = ChainTip::new(prev_hash, n_bits, ntime);
standard_channel.set_chain_tip(chain_tip);
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let active_standard_job = standard_channel.get_active_job().unwrap();
let share_low_diff = SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number: 0,
job_id: active_standard_job.get_job_id(),
nonce: 3,
ntime: 1745596932,
version: 536870912,
};
let res = standard_channel.validate_share(share_low_diff);
assert!(matches!(
res.unwrap_err(),
ShareValidationError::DoesNotMeetTarget(_)
));
assert_eq!(
standard_channel
.get_share_accounting()
.get_rejected_shares_error_count(ERROR_CODE_SUBMIT_SHARES_DIFFICULTY_TOO_LOW),
1
);
}
#[test]
fn test_share_validation_valid_share() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 1_000.0; let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
let ntime = 1745611105 - 60;
let prev_hash = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
let n_bits = 453040064;
let chain_tip = ChainTip::new(prev_hash, n_bits, ntime);
standard_channel.set_chain_tip(chain_tip);
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let valid_share = SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number: 1,
job_id: 1,
nonce: 92092,
ntime: 1745611105,
version: 536870912,
};
let res = standard_channel.validate_share(valid_share);
assert!(matches!(res, Ok(ShareValidationResult::Valid(_))));
}
#[test]
fn test_share_validation_invalid_non_rollable_version_bit() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 1.0;
let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
standard_channel.set_target(max_target).unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
let ntime = 1745611105 - 60;
let prev_hash = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
let n_bits = 453040064;
let chain_tip = ChainTip::new(prev_hash, n_bits, ntime);
standard_channel.set_chain_tip(chain_tip);
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
for nonce in 0..1024u32 {
let share = SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number: nonce,
job_id: 1,
nonce,
ntime: 1745611105,
version: 0,
};
let res = standard_channel.validate_share(share);
let err = res.expect_err("share with non-rollable version bits must be rejected");
match err {
ShareValidationError::Invalid(code) => {
assert_eq!(
code,
ERROR_CODE_SUBMIT_SHARES_INVALID_NON_ROLLABLE_VERSION_BIT
);
}
other => panic!("expected ShareValidationError::Invalid, got {other:?}"),
}
}
}
#[test]
fn test_share_validation_rollable_version_bits() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 1.0;
let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
standard_channel.set_target(max_target).unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
let ntime = 1745611105 - 60;
let prev_hash = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
let n_bits = 453040064;
let chain_tip = ChainTip::new(prev_hash, n_bits, ntime);
standard_channel.set_chain_tip(chain_tip);
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let rolled_version = 0x20000000 | 0x1fffe0;
let share = SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number: 0,
job_id: 1,
nonce: 0,
ntime: 1745611105,
version: rolled_version,
};
assert!(standard_channel.validate_share(share).is_ok());
}
#[test]
fn test_new_clamps_target_to_max_target() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [0, 0, 0, 1].to_vec();
let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let very_small_hashrate = 0.1;
let not_so_permissive_max_target = Target::from_le_bytes([
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x00,
]);
let channel = StandardChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
not_so_permissive_max_target,
very_small_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
assert_eq!(
channel.get_requested_max_target(),
¬_so_permissive_max_target
);
assert_eq!(channel.get_target(), ¬_so_permissive_max_target);
}
const POOL_TAG_AT_SCRIPT_SIG_BUDGET: usize = 52;
const EXTRANONCE_PREFIX_LEN_AT_SCRIPT_SIG_BUDGET: usize = 32;
fn new_standard_channel_with_pool_tag(
pool_tag_len: usize,
extranonce_prefix_len: usize,
) -> Result<StandardChannel, StandardChannelError> {
StandardChannel::new(
1,
"user_identity".to_string(),
ExtranoncePrefix::from_wire(vec![0xab; extranonce_prefix_len]).unwrap(),
Target::from_le_bytes([0xff; 32]),
1.0,
100,
1.0,
Some("x".repeat(pool_tag_len)),
None,
None,
)
}
#[test]
fn test_new_rejects_oversized_script_sig() {
let channel = new_standard_channel_with_pool_tag(
POOL_TAG_AT_SCRIPT_SIG_BUDGET,
EXTRANONCE_PREFIX_LEN_AT_SCRIPT_SIG_BUDGET,
)
.unwrap();
assert_eq!(
channel.job_factory.script_sig_size(
MAX_COINBASE_PREFIX_SIZE,
channel.get_extranonce_prefix().len()
),
MAX_SCRIPT_SIG_SIZE
);
let channel = new_standard_channel_with_pool_tag(
POOL_TAG_AT_SCRIPT_SIG_BUDGET + 1,
EXTRANONCE_PREFIX_LEN_AT_SCRIPT_SIG_BUDGET,
);
assert!(matches!(
channel.unwrap_err(),
StandardChannelError::ScriptSigSizeTooLarge
));
}
const POOL_TAG_BINDING_BELOW_MAX_EXTRANONCE_LEN: usize = 60;
const EXTRANONCE_PREFIX_LEN_AT_BINDING_BUDGET: usize = 24;
#[test]
fn test_set_extranonce_prefix_rejects_oversized_script_sig() {
let original_prefix_len = 4;
let mut channel = new_standard_channel_with_pool_tag(
POOL_TAG_BINDING_BELOW_MAX_EXTRANONCE_LEN,
original_prefix_len,
)
.unwrap();
let original_prefix = channel.get_extranonce_prefix().to_vec();
channel
.set_extranonce_prefix(
AllocatedExtranoncePrefix::for_test(vec![
0xcd;
EXTRANONCE_PREFIX_LEN_AT_BINDING_BUDGET
])
.unwrap(),
)
.unwrap();
assert_eq!(
channel.get_extranonce_prefix().len(),
EXTRANONCE_PREFIX_LEN_AT_BINDING_BUDGET
);
channel
.set_extranonce_prefix(
AllocatedExtranoncePrefix::for_test(original_prefix.clone()).unwrap(),
)
.unwrap();
let oversized_prefix = vec![0xcd; EXTRANONCE_PREFIX_LEN_AT_BINDING_BUDGET + 1];
assert!(oversized_prefix.len() <= MAX_EXTRANONCE_LEN as usize);
let res = channel.set_extranonce_prefix(
AllocatedExtranoncePrefix::for_test(oversized_prefix.clone()).unwrap(),
);
assert!(matches!(
res.unwrap_err(),
StandardChannelError::ScriptSigSizeTooLarge
));
assert_eq!(channel.get_extranonce_prefix(), &original_prefix[..]);
let res = channel.set_upstream_extranonce_prefix(&oversized_prefix);
assert!(matches!(
res.unwrap_err(),
StandardChannelError::ScriptSigSizeTooLarge
));
assert_eq!(channel.get_extranonce_prefix(), &original_prefix[..]);
}
#[test]
fn test_set_upstream_extranonce_prefix_preserves_allocation_transactionally() {
let mut allocator =
ExtranonceAllocator::from_upstream_prefix(vec![0xaa], vec![0xbb], 5, 256).unwrap();
let allocated_prefix = allocator.allocate_standard().unwrap();
let old_suffix = allocated_prefix.as_bytes()[1..].to_vec();
let mut channel = StandardChannel::new(
1,
"user_identity".to_string(),
allocated_prefix.into(),
Target::from_le_bytes([0xff; 32]),
1.0,
100,
1.0,
None,
None,
None,
)
.unwrap();
allocator.set_upstream_prefix(vec![0xcc, 0xdd]).unwrap();
channel
.set_upstream_extranonce_prefix(allocator.upstream_prefix())
.unwrap();
let mut expected = vec![0xcc, 0xdd];
expected.extend_from_slice(&old_suffix);
assert_eq!(channel.get_extranonce_prefix(), &expected);
assert_eq!(allocator.allocated_count(), 1);
let prefix_before_error = channel.get_extranonce_prefix().to_vec();
let result = channel.set_upstream_extranonce_prefix(&[0xee; 29]);
assert!(matches!(
result,
Err(StandardChannelError::ExtranoncePrefixTooLarge)
));
assert_eq!(channel.get_extranonce_prefix(), &prefix_before_error);
assert_eq!(allocator.allocated_count(), 1);
}
#[test]
fn test_update_channel() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let expected_share_per_minute = 1.0;
let initial_hashrate = 10.0;
let share_batch_size = 100; let max_target = Target::from_le_bytes([0xff; 32]);
let mut channel = StandardChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
initial_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let initial_target = channel.get_target().clone();
let new_hashrate = 100.0;
channel
.update_channel(new_hashrate, Some(max_target))
.unwrap();
let new_target = channel.get_target().clone();
assert_ne!(initial_target, new_target);
assert_eq!(channel.get_nominal_hashrate(), new_hashrate);
let result = channel.update_channel(-1.0, Some(max_target));
assert!(result.is_err());
assert!(matches!(
result,
Err(StandardChannelError::UpdateChannelInvalidNominalHashrate(_))
));
let not_so_permissive_max_target = Target::from_le_bytes([
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x00,
]);
let very_small_hashrate = 0.1;
let result =
channel.update_channel(very_small_hashrate, Some(not_so_permissive_max_target));
assert!(result.is_ok());
assert_eq!(channel.get_target(), ¬_so_permissive_max_target);
let sufficiently_big_hashrate = 1000.0;
let result = channel.update_channel(
sufficiently_big_hashrate,
Some(not_so_permissive_max_target),
);
assert!(result.is_ok());
}
#[test]
fn test_update_extranonce_prefix() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let expected_share_per_minute = 1.0;
let nominal_hashrate = 1_000.0;
let share_batch_size = 100;
let mut channel = StandardChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let current_extranonce_prefix = channel.get_extranonce_prefix();
assert_eq!(current_extranonce_prefix, extranonce_prefix.as_slice());
let new_extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
]
.to_vec();
channel
.set_extranonce_prefix(
AllocatedExtranoncePrefix::for_test(new_extranonce_prefix.clone()).unwrap(),
)
.unwrap();
let current_extranonce_prefix = channel.get_extranonce_prefix();
assert_eq!(current_extranonce_prefix, new_extranonce_prefix.as_slice());
let new_extranonce_prefix_too_long = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1,
]
.to_vec();
assert!(matches!(
ExtranoncePrefix::from_wire(new_extranonce_prefix_too_long),
Err(ExtranoncePrefixError::ExceedsMaxLength)
));
}
#[test]
fn test_set_new_prev_hash_without_future_jobs_updates_chain_tip() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 100.0;
let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
share_batch_size,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0];
script_bytes.push(20);
script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
let ntime = 1745596910;
let prev_hash = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
let n_bits = 453040064;
let chain_tip = ChainTip::new(prev_hash, n_bits, ntime);
standard_channel.set_chain_tip(chain_tip);
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let active_standard_job = standard_channel.get_active_job().unwrap();
let active_job_id = active_standard_job.get_job_id();
assert!(!standard_channel.job_store.has_future_jobs());
let new_prev_hash: binary_sv2::U256Owned = [
200, 53, 253, 129, 214, 31, 43, 84, 179, 58, 58, 76, 128, 213, 24, 53, 38, 144, 205,
88, 172, 20, 251, 22, 217, 141, 21, 221, 21, 0, 0, 0,
]
.into();
let snph = SetNewPrevHashTdp {
template_id: 999,
prev_hash: new_prev_hash.clone(),
header_timestamp: ntime + 600,
n_bits,
target: [0xff; 32].into(),
};
standard_channel.on_set_new_prev_hash(snph).unwrap();
let chain_tip = standard_channel.get_chain_tip().unwrap();
assert_eq!(chain_tip.prev_hash(), new_prev_hash);
assert_eq!(chain_tip.min_ntime(), ntime + 600);
assert_eq!(chain_tip.nbits(), n_bits);
assert!(standard_channel.get_active_job().is_none());
let share_for_old_job = SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number: 0,
job_id: active_job_id,
nonce: 3,
ntime: 1745596932,
version: 536870912,
};
let res = standard_channel.validate_share(share_for_old_job);
assert!(matches!(res.unwrap_err(), ShareValidationError::Stale(_)));
let mut template = template;
template.template_id = 2;
standard_channel
.on_new_template(
template,
vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: {
let mut script_bytes = vec![0];
script_bytes.push(20);
script_bytes.extend_from_slice(&pubkey_hash);
ScriptBuf::from(script_bytes)
},
}],
)
.unwrap();
assert!(standard_channel.get_active_job().is_some());
}
fn standard_channel_with_live_job(
prefix_1: AllocatedExtranoncePrefix,
future: bool,
) -> (StandardChannel, Vec<u8>) {
let prefix_1_bytes = prefix_1.as_bytes().to_vec();
let mut standard_channel = StandardChannel::new_for_pool(
1,
"user_identity".to_string(),
prefix_1,
Target::from_le_bytes([0xff; 32]),
100.0,
100,
1.0,
String::new(),
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: future,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0];
script_bytes.push(20);
script_bytes.extend_from_slice(&pubkey_hash);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: ScriptBuf::from(script_bytes),
}];
let ntime = 1745596910;
let prev_hash = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
let n_bits = 453040064;
standard_channel.set_chain_tip(ChainTip::new(prev_hash, n_bits, ntime));
standard_channel
.on_new_template(template, coinbase_reward_outputs)
.unwrap();
(standard_channel, prefix_1_bytes)
}
#[test]
fn test_mixed_prefix_updates_reserve_slot_until_jobs_go_stale() {
for future in [false, true] {
let mut allocator =
ExtranonceAllocator::from_upstream_prefix(vec![0xaa], vec![0xbb], 32, 2).unwrap();
let prefix = allocator.allocate_standard().unwrap();
let (mut channel, old_bytes) = standard_channel_with_live_job(prefix, future);
let job_id = if future {
channel.get_future_job_id_from_template_id(1).unwrap()
} else {
channel.get_active_job().unwrap().get_job_id()
};
allocator.set_upstream_prefix(vec![0xcc]).unwrap();
channel.set_upstream_extranonce_prefix(&[0xcc]).unwrap();
assert_eq!(allocator.allocated_count(), 1);
assert_eq!(&channel.get_extranonce_prefix()[1..], &old_bytes[1..]);
let replacement = allocator.allocate_standard().unwrap();
channel.set_extranonce_prefix(replacement).unwrap();
let job = if future {
channel.get_future_job(job_id).unwrap()
} else {
channel.get_active_job().unwrap()
};
assert_eq!(job.get_extranonce_prefix(), old_bytes);
assert_eq!(allocator.allocated_count(), 2);
allocator.set_upstream_prefix(vec![0xaa]).unwrap();
assert!(matches!(
allocator.allocate_standard(),
Err(ExtranonceAllocatorError::CapacityExhausted)
));
if future {
channel
.on_set_new_prev_hash(SetNewPrevHashTdp {
template_id: 1,
prev_hash: [2; 32].into(),
header_timestamp: 1745596970,
n_bits: 453040064,
target: [0xff; 32].into(),
})
.unwrap();
assert_eq!(
channel.get_active_job().unwrap().get_extranonce_prefix(),
old_bytes
);
assert_eq!(allocator.allocated_count(), 2);
}
channel
.on_set_new_prev_hash(SetNewPrevHashTdp {
template_id: 999,
prev_hash: [1; 32].into(),
header_timestamp: 1745597510,
n_bits: 453040064,
target: [0xff; 32].into(),
})
.unwrap();
assert!(channel.get_active_job().is_none());
assert_eq!(allocator.allocated_count(), 1);
let reused = allocator.allocate_standard().unwrap();
assert_eq!(reused.as_bytes(), old_bytes);
drop(reused);
drop(channel);
assert_eq!(allocator.allocated_count(), 0);
}
}
#[test]
fn test_rotated_extranonce_prefix_slot_not_reused_while_job_live() {
let mut allocator = ExtranonceAllocator::new(vec![], 32, 2).unwrap();
let prefix_1 = allocator.allocate_standard().unwrap();
let prefix_2 = allocator.allocate_standard().unwrap();
assert_ne!(prefix_1.as_bytes(), prefix_2.as_bytes());
assert_eq!(allocator.allocated_count(), 2);
let (mut standard_channel, prefix_1_bytes) =
standard_channel_with_live_job(prefix_1, false);
standard_channel.set_extranonce_prefix(prefix_2).unwrap();
assert_eq!(allocator.allocated_count(), 2);
assert!(matches!(
allocator.allocate_standard(),
Err(ExtranonceAllocatorError::CapacityExhausted)
));
assert_eq!(
standard_channel
.get_active_job()
.unwrap()
.get_extranonce_prefix(),
prefix_1_bytes.as_slice()
);
}
#[test]
fn test_future_template_storage_is_bounded() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
Target::from_le_bytes([0xff; 32]),
10.0,
100,
1.0,
None,
None,
None,
)
.unwrap();
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
let flood_size = 10_000u64;
for template_id in 0..flood_size {
let template = NewTemplate {
template_id,
future_template: true,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113,
209, 222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153,
98, 180, 139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
standard_channel
.on_new_template(template, coinbase_reward_outputs.clone())
.unwrap();
}
for template_id in 0..flood_size - MAX_FUTURE_JOBS as u64 {
assert!(standard_channel
.get_future_job_id_from_template_id(template_id)
.is_none());
}
for template_id in flood_size - MAX_FUTURE_JOBS as u64..flood_size {
assert!(standard_channel
.get_future_job_id_from_template_id(template_id)
.is_some());
}
}
#[test]
fn test_past_job_storage_is_bounded() {
let standard_channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let mut standard_channel = StandardChannel::new(
standard_channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
Target::from_le_bytes([0xff; 32]),
10.0,
100,
1.0,
None,
None,
None,
)
.unwrap();
let ntime = 1747092633;
let prev_hash = [
200, 53, 253, 129, 214, 31, 43, 84, 179, 58, 58, 76, 128, 213, 24, 53, 38, 144, 205,
88, 172, 20, 251, 22, 217, 141, 21, 221, 21, 0, 0, 0,
]
.into();
let nbits = 503543726;
standard_channel.set_chain_tip(ChainTip::new(prev_hash, nbits, ntime));
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let script = ScriptBuf::from(script_bytes);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: script,
}];
let flood_size = 10_000u64;
for template_id in 0..flood_size {
let template = NewTemplate {
template_id,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113,
209, 222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153,
98, 180, 139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
standard_channel
.on_new_template(template, coinbase_reward_outputs.clone())
.unwrap();
}
let retained = (0..=flood_size as u32)
.filter(|job_id| standard_channel.get_past_job(*job_id).is_some())
.count();
assert_eq!(retained, MAX_PAST_JOBS);
assert!(standard_channel.get_active_job().is_some());
assert_eq!(standard_channel.job_id_to_target.len(), MAX_PAST_JOBS + 1);
}
#[test]
fn test_share_validation_ntime_above_max_future_block_time() {
let standard_channel_id = 1;
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let mut standard_channel = StandardChannel::new(
standard_channel_id,
"user_identity".to_string(),
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
Target::from_le_bytes([0xff; 32]),
1_000.0,
100,
1.0,
None,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: ScriptBuf::from(script_bytes),
}];
let share_ntime: u32 = 1745611105;
let ntime = share_ntime - crate::MAX_FUTURE_BLOCK_TIME;
let prev_hash = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
let n_bits = 453040064;
standard_channel.set_chain_tip(ChainTip::new(prev_hash, n_bits, ntime));
standard_channel
.on_new_template(template, coinbase_reward_outputs)
.unwrap();
let share = |sequence_number: u32, ntime: u32| SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number,
job_id: 1,
nonce: 92092,
ntime,
version: 536870912,
};
let res = standard_channel.validate_share(share(0, share_ntime + 1));
assert!(matches!(res.unwrap_err(), ShareValidationError::Invalid(_)));
let res = standard_channel.validate_share(share(1, u32::MAX));
assert!(matches!(res.unwrap_err(), ShareValidationError::Invalid(_)));
let res = standard_channel.validate_share(share(2, share_ntime));
assert!(matches!(res, Ok(ShareValidationResult::Valid(_))));
}
#[test]
fn test_validate_share_fails_once_seen_shares_budget_is_exhausted() {
let standard_channel_id = 1;
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let expected_share_per_minute = 1.0;
let mut standard_channel = StandardChannel::new(
standard_channel_id,
"user_identity".to_string(),
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
Target::from_le_bytes([0xff; 32]),
1_000.0,
100,
expected_share_per_minute,
None,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: ScriptBuf::from(script_bytes),
}];
let share_ntime: u32 = 1745611105;
let prev_hash = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
standard_channel.set_chain_tip(ChainTip::new(prev_hash, 453040064, share_ntime - 60));
standard_channel
.on_new_template(template, coinbase_reward_outputs)
.unwrap();
let budget = crate::seen_shares_budget(expected_share_per_minute as f64);
for i in 0..budget as u32 {
let mut bytes = [0u8; 32];
bytes[..4].copy_from_slice(&i.to_le_bytes());
standard_channel.share_accounting.update_share_accounting(
1.0,
i,
<bitcoin::hashes::sha256d::Hash as bitcoin::hashes::Hash>::from_slice(&bytes)
.unwrap(),
);
}
let valid_share = SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number: 1,
job_id: 1,
nonce: 92092,
ntime: share_ntime,
version: 536870912,
};
let res = standard_channel.validate_share(valid_share);
assert!(matches!(
res.unwrap_err(),
ShareValidationError::SeenSharesBudgetExhausted
));
}
#[test]
fn test_share_validation_ntime_below_group_job_min_ntime() {
let standard_channel_id = 1;
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let mut standard_channel = StandardChannel::new(
standard_channel_id,
"user_identity".to_string(),
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
1.0,
100,
1.0,
None,
None,
None,
)
.unwrap();
standard_channel.set_target(max_target).unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: ScriptBuf::from(script_bytes),
}];
let n_bits = 453040064;
let prev_hash: binary_sv2::U256Owned = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
let tip_ntime = 1745596910;
standard_channel.set_chain_tip(ChainTip::new(prev_hash.clone(), n_bits, tip_ntime));
let job_min_ntime = tip_ntime + 3;
let mut group_job_factory = crate::server::jobs::factory::JobFactory::new(true, None, None);
let group_job = group_job_factory
.new_extended_job(
99,
Some(ChainTip::new(prev_hash, n_bits, job_min_ntime)),
vec![],
template,
coinbase_reward_outputs,
extranonce_prefix.len(),
)
.unwrap();
assert_eq!(group_job.get_min_ntime(), Some(job_min_ntime));
standard_channel.on_group_channel_job(group_job).unwrap();
let job_id = standard_channel.get_active_job().unwrap().get_job_id();
let share = |sequence_number: u32, ntime: u32| SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number,
job_id,
nonce: 0,
ntime,
version: 536870912,
};
let res = standard_channel.validate_share(share(0, job_min_ntime - 1));
assert!(matches!(res.unwrap_err(), ShareValidationError::Invalid(_)));
assert_eq!(
standard_channel
.get_share_accounting()
.get_shares_accepted(),
0
);
let res = standard_channel
.validate_share(share(2, job_min_ntime + crate::MAX_FUTURE_BLOCK_TIME + 1));
assert!(matches!(res.unwrap_err(), ShareValidationError::Invalid(_)));
let res =
standard_channel.validate_share(share(3, job_min_ntime + crate::MAX_FUTURE_BLOCK_TIME));
assert!(matches!(res, Ok(ShareValidationResult::Valid(_))));
let res = standard_channel.validate_share(share(1, job_min_ntime));
assert!(matches!(res, Ok(ShareValidationResult::Valid(_))));
}
#[test]
fn test_repeated_prev_hash_keeps_seen_shares() {
let standard_channel_id = 1;
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let mut standard_channel = StandardChannel::new(
standard_channel_id,
"user_identity".to_string(),
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
1.0,
100,
1.0,
None,
None,
None,
)
.unwrap();
standard_channel.set_target(max_target).unwrap();
let template = |template_id: u64| NewTemplate {
template_id,
future_template: true,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: ScriptBuf::from(script_bytes),
}];
let prev_hash: binary_sv2::U256Owned = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
let set_new_prev_hash = |template_id: u64| SetNewPrevHashTdp {
template_id,
prev_hash: prev_hash.clone(),
header_timestamp: 1745596910,
n_bits: 453040064,
target: [0xff; 32].into(),
};
standard_channel
.on_new_template(template(1), coinbase_reward_outputs.clone())
.unwrap();
standard_channel
.on_set_new_prev_hash(set_new_prev_hash(1))
.unwrap();
let first_job_id = standard_channel.get_active_job().unwrap().get_job_id();
let share = |sequence_number: u32, job_id: u32| SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number,
job_id,
nonce: 0,
ntime: 1745596910,
version: 536870912,
};
assert!(matches!(
standard_channel.validate_share(share(0, first_job_id)),
Ok(ShareValidationResult::Valid(_))
));
standard_channel
.on_new_template(template(2), coinbase_reward_outputs)
.unwrap();
standard_channel
.on_set_new_prev_hash(set_new_prev_hash(2))
.unwrap();
let second_job_id = standard_channel.get_active_job().unwrap().get_job_id();
assert_ne!(first_job_id, second_job_id);
assert_eq!(
standard_channel.get_active_job().unwrap().get_merkle_root(),
standard_channel
.get_stale_job(first_job_id)
.unwrap()
.get_merkle_root()
);
assert!(matches!(
standard_channel.validate_share(share(1, second_job_id)),
Err(ShareValidationError::DuplicateShare(_))
));
assert_eq!(
standard_channel
.get_share_accounting()
.get_shares_accepted(),
1
);
}
#[test]
fn test_zero_max_target_is_rejected() {
let res = StandardChannel::new(
1,
"user_identity".to_string(),
ExtranoncePrefix::from_wire(vec![0, 0, 0, 1]).unwrap(),
Target::ZERO,
1.0,
100,
1.0,
None,
None,
None,
);
match res {
Err(StandardChannelError::OpenChannelInvalidMaxTarget(code)) => {
assert_eq!(code, ERROR_CODE_OPEN_MINING_CHANNEL_MAX_TARGET_OUT_OF_RANGE);
}
other => panic!("expected OpenChannelInvalidMaxTarget, got {other:?}"),
}
let max_target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 1.0;
let mut channel = StandardChannel::new(
1,
"user_identity".to_string(),
ExtranoncePrefix::from_wire(vec![0, 0, 0, 1]).unwrap(),
max_target,
nominal_hashrate,
100,
1.0,
None,
None,
None,
)
.unwrap();
let target_before = *channel.get_target();
let res = channel.update_channel(100.0, Some(Target::ZERO));
match res {
Err(StandardChannelError::UpdateChannelInvalidMaxTarget(code)) => {
assert_eq!(code, ERROR_CODE_UPDATE_CHANNEL_MAX_TARGET_OUT_OF_RANGE);
}
other => panic!("expected UpdateChannelInvalidMaxTarget, got {other:?}"),
}
assert!(matches!(
channel.set_target(Target::ZERO),
Err(StandardChannelError::InvalidTarget)
));
assert_eq!(channel.get_target(), &target_before);
assert_eq!(channel.get_requested_max_target(), &max_target);
assert_eq!(channel.get_nominal_hashrate(), nominal_hashrate);
}
#[test]
fn test_on_group_channel_job_rejects_min_ntime_below_chain_tip() {
let standard_channel_id = 1;
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let mut standard_channel = StandardChannel::new(
standard_channel_id,
"user_identity".to_string(),
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
Target::from_le_bytes([0xff; 32]),
1.0,
100,
1.0,
None,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: ScriptBuf::from(script_bytes),
}];
let n_bits = 453040064;
let prev_hash: binary_sv2::U256Owned = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
let tip_ntime = 1745596910;
standard_channel.set_chain_tip(ChainTip::new(prev_hash.clone(), n_bits, tip_ntime));
let mut group_job_factory = crate::server::jobs::factory::JobFactory::new(true, None, None);
let mut group_job = |min_ntime: u32| {
group_job_factory
.new_extended_job(
99,
Some(ChainTip::new(prev_hash.clone(), n_bits, min_ntime)),
vec![],
template.clone(),
coinbase_reward_outputs.clone(),
extranonce_prefix.len(),
)
.unwrap()
};
assert!(matches!(
standard_channel.on_group_channel_job(group_job(tip_ntime - 1)),
Err(StandardChannelError::JobMinNtimeBelowChainTip)
));
assert!(standard_channel.get_active_job().is_none());
standard_channel
.on_group_channel_job(group_job(tip_ntime))
.unwrap();
assert!(standard_channel.get_active_job().is_some());
}
#[test]
fn test_group_job_reusing_a_stale_job_id_replaces_the_stale_job() {
let standard_channel_id = 1;
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let mut standard_channel = StandardChannel::new(
standard_channel_id,
"user_identity".to_string(),
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
1.0,
100,
1.0,
None,
None,
None,
)
.unwrap();
standard_channel.set_target(max_target).unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: ScriptBuf::from(script_bytes),
}];
let n_bits = 453040064;
let tip_ntime = 1745596910;
let prev_hash: binary_sv2::U256Owned = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
standard_channel.set_chain_tip(ChainTip::new(prev_hash, n_bits, tip_ntime));
standard_channel
.on_new_template(template.clone(), coinbase_reward_outputs.clone())
.unwrap();
assert_eq!(standard_channel.get_active_job().unwrap().get_job_id(), 1);
let next_prev_hash: binary_sv2::U256Owned = [
200, 53, 253, 129, 214, 31, 43, 84, 179, 58, 58, 76, 128, 213, 24, 53, 38, 144, 205,
88, 172, 20, 251, 22, 217, 141, 21, 221, 21, 0, 0, 0,
]
.into();
let next_tip_ntime = tip_ntime + 600;
standard_channel
.on_set_new_prev_hash(SetNewPrevHashTdp {
template_id: 999,
prev_hash: next_prev_hash.clone(),
header_timestamp: next_tip_ntime,
n_bits,
target: [0xff; 32].into(),
})
.unwrap();
let share = |sequence_number: u32| SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number,
job_id: 1,
nonce: 0,
ntime: next_tip_ntime,
version: 536870912,
};
assert!(matches!(
standard_channel.validate_share(share(0)),
Err(ShareValidationError::Stale(_))
));
let mut group_job_factory = crate::server::jobs::factory::JobFactory::new(true, None, None);
let group_job = group_job_factory
.new_extended_job(
99,
Some(ChainTip::new(next_prev_hash, n_bits, next_tip_ntime)),
vec![],
template,
coinbase_reward_outputs,
extranonce_prefix.len(),
)
.unwrap();
assert_eq!(group_job.get_job_id(), 1);
standard_channel.on_group_channel_job(group_job).unwrap();
assert_eq!(standard_channel.get_active_job().unwrap().get_job_id(), 1);
assert!(matches!(
standard_channel.validate_share(share(1)),
Ok(ShareValidationResult::Valid(_))
));
}
#[test]
fn test_reused_job_id_evicted_from_past_jobs_keeps_the_active_job_target() {
let standard_channel_id = 1;
let extranonce_prefix = [
83, 116, 114, 97, 116, 117, 109, 32, 86, 50, 32, 83, 82, 73, 32, 80, 111, 111, 108, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let mut standard_channel = StandardChannel::new(
standard_channel_id,
"user_identity".to_string(),
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
1.0,
100,
1.0,
None,
None,
Some(2),
)
.unwrap();
standard_channel.set_target(max_target).unwrap();
let template = |template_id: u64| NewTemplate {
template_id,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![2, 159, 0, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967294,
coinbase_tx_value_remaining: SATS_AVAILABLE_IN_TEMPLATE,
coinbase_tx_outputs_count: 1,
coinbase_tx_outputs: vec![
0, 0, 0, 0, 0, 0, 0, 0, 38, 106, 36, 170, 33, 169, 237, 226, 246, 28, 63, 113, 209,
222, 253, 63, 169, 153, 223, 163, 105, 83, 117, 92, 105, 6, 137, 121, 153, 98, 180,
139, 235, 216, 54, 151, 78, 140, 249,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 158,
merkle_path: vec![].try_into().unwrap(),
};
let pubkey_hash = [
235, 225, 183, 220, 194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194,
8, 252,
];
let mut script_bytes = vec![0]; script_bytes.push(20); script_bytes.extend_from_slice(&pubkey_hash);
let coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE),
script_pubkey: ScriptBuf::from(script_bytes),
}];
let n_bits = 453040064;
let tip_ntime = 1745596910;
let prev_hash: binary_sv2::U256Owned = [
154, 124, 239, 231, 221, 122, 160, 173, 164, 175, 87, 33, 74, 214, 191, 107, 73, 34, 0,
162, 227, 16, 44, 40, 33, 73, 0, 0, 0, 0, 0, 0,
]
.into();
standard_channel.set_chain_tip(ChainTip::new(prev_hash.clone(), n_bits, tip_ntime));
let mut group_job_factory = crate::server::jobs::factory::JobFactory::new(true, None, None);
for template_id in 1..=3 {
let group_job = group_job_factory
.new_extended_job(
99,
Some(ChainTip::new(prev_hash.clone(), n_bits, tip_ntime)),
vec![],
template(template_id),
coinbase_reward_outputs.clone(),
extranonce_prefix.len(),
)
.unwrap();
standard_channel.on_group_channel_job(group_job).unwrap();
}
assert_eq!(standard_channel.get_active_job().unwrap().get_job_id(), 3);
standard_channel
.on_new_template(template(4), coinbase_reward_outputs)
.unwrap();
assert_eq!(standard_channel.get_active_job().unwrap().get_job_id(), 1);
let share = SubmitSharesStandardOwned {
channel_id: standard_channel_id,
sequence_number: 0,
job_id: 1,
nonce: 0,
ntime: tip_ntime,
version: 536870912,
};
assert!(matches!(
standard_channel.validate_share(share),
Ok(ShareValidationResult::Valid(_))
));
}
}