use crate::{
chain_tip::ChainTip,
extranonce_manager::{AllocatedExtranoncePrefix, ExtranoncePrefix},
merkle_root::merkle_root_from_path,
server::{
error::ExtendedChannelError,
jobs::{extended::ExtendedJob, factory::JobFactory, job_store::JobStore, JobOrigin},
share_accounting::{ShareAccounting, ShareValidationError, ShareValidationResult},
},
target::{bytes_to_hex, hash_rate_to_target, u256_to_block_hash},
MAX_EXTRANONCE_LEN,
};
use bitcoin::{
blockdata::block::{Header, Version},
hashes::sha256d::Hash,
transaction::TxOut,
CompactTarget, Target,
};
use mining_sv2::{
SetCustomMiningJob, SubmitSharesExtended,
ERROR_CODE_OPEN_MINING_CHANNEL_INVALID_NOMINAL_HASHRATE,
ERROR_CODE_SUBMIT_SHARES_BAD_EXTRANONCE_SIZE, 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_SHARE, ERROR_CODE_SUBMIT_SHARES_STALE_SHARE,
ERROR_CODE_UPDATE_CHANNEL_INVALID_NOMINAL_HASHRATE, ERROR_CODE_VERSION_ROLLING_NOT_ALLOWED,
};
use std::{collections::HashMap, convert::TryInto, marker::PhantomData};
use template_distribution_sv2::{NewTemplate, SetNewPrevHash as SetNewPrevHashTdp};
use tracing::debug;
#[derive(Debug)]
pub struct ExtendedChannel<'a, J>
where
J: JobStore<ExtendedJob<'a>>,
{
channel_id: u32,
user_identity: String,
extranonce_prefix: ExtranoncePrefix,
rollable_extranonce_size: u16,
requested_max_target: Target,
target: Target,
job_id_to_target: HashMap<u32, Target>,
nominal_hashrate: f32,
stable_hashrate: bool,
job_store: J,
job_factory: JobFactory,
share_accounting: ShareAccounting,
expected_share_per_minute: f32,
chain_tip: Option<ChainTip>,
phantom: PhantomData<&'a ()>,
}
impl<'a, J> ExtendedChannel<'a, J>
where
J: JobStore<ExtendedJob<'a>>,
{
#[allow(clippy::too_many_arguments)]
pub fn new_for_pool(
channel_id: u32,
user_identity: String,
extranonce_prefix: AllocatedExtranoncePrefix,
max_target: Target,
nominal_hashrate: f32,
version_rolling_allowed: bool,
rollable_extranonce_size: u16,
share_batch_size: usize,
expected_share_per_minute: f32,
job_store: J,
pool_tag_string: String,
) -> Result<Self, ExtendedChannelError> {
Self::new(
channel_id,
user_identity,
extranonce_prefix.into(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
Some(pool_tag_string),
None,
)
}
#[allow(clippy::too_many_arguments)]
pub fn new_for_job_declaration_client(
channel_id: u32,
user_identity: String,
extranonce_prefix: AllocatedExtranoncePrefix,
max_target: Target,
nominal_hashrate: f32,
version_rolling_allowed: bool,
rollable_extranonce_size: u16,
share_batch_size: usize,
expected_share_per_minute: f32,
job_store: J,
pool_tag_string: Option<String>,
miner_tag_string: String,
) -> Result<Self, ExtendedChannelError> {
Self::new(
channel_id,
user_identity,
extranonce_prefix.into(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
pool_tag_string,
Some(miner_tag_string),
)
}
#[allow(clippy::too_many_arguments)]
fn new(
channel_id: u32,
user_identity: String,
extranonce_prefix: ExtranoncePrefix,
max_target: Target,
nominal_hashrate: f32,
version_rolling_allowed: bool,
rollable_extranonce_size: u16,
share_batch_size: usize,
expected_share_per_minute: f32,
job_store: J,
pool_tag: Option<String>,
miner_tag: Option<String>,
) -> Result<Self, ExtendedChannelError> {
let target =
match hash_rate_to_target(nominal_hashrate.into(), expected_share_per_minute.into()) {
Ok(target) => target,
Err(_) => {
return Err(ExtendedChannelError::OpenChannelInvalidNominalHashrate(
ERROR_CODE_OPEN_MINING_CHANNEL_INVALID_NOMINAL_HASHRATE,
));
}
};
let target = target.min(max_target);
if extranonce_prefix.len() > MAX_EXTRANONCE_LEN as usize {
return Err(ExtendedChannelError::ExtranoncePrefixTooLarge);
}
let script_sig_size = 5 + 1 + 3 + pool_tag.as_ref().map_or(0, |s| s.len()) +
miner_tag.as_ref().map_or(0, |s| s.len()) +
1 + extranonce_prefix.len() +
rollable_extranonce_size as usize;
if script_sig_size > 100 {
return Err(ExtendedChannelError::ScriptSigSizeTooLarge);
}
Ok(Self {
channel_id,
user_identity,
extranonce_prefix,
rollable_extranonce_size,
requested_max_target: max_target,
target,
job_id_to_target: HashMap::new(),
nominal_hashrate,
stable_hashrate: false,
job_store,
job_factory: JobFactory::new(version_rolling_allowed, pool_tag, miner_tag),
share_accounting: ShareAccounting::new(share_batch_size),
expected_share_per_minute,
chain_tip: None,
phantom: PhantomData,
})
}
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 get_chain_tip(&self) -> Option<&ChainTip> {
self.chain_tip.as_ref()
}
pub fn get_shares_per_minute(&self) -> f32 {
self.expected_share_per_minute
}
#[cfg(test)]
fn set_chain_tip(&mut self, chain_tip: ChainTip) {
self.chain_tip = Some(chain_tip);
}
pub fn set_extranonce_prefix(
&mut self,
extranonce_prefix: AllocatedExtranoncePrefix,
) -> Result<(), ExtendedChannelError> {
if extranonce_prefix.len() > MAX_EXTRANONCE_LEN as usize {
return Err(ExtendedChannelError::ExtranoncePrefixTooLarge);
}
self.extranonce_prefix = extranonce_prefix.into();
Ok(())
}
pub fn get_rollable_extranonce_size(&self) -> u16 {
self.rollable_extranonce_size
}
pub fn get_full_extranonce_size(&self) -> usize {
self.extranonce_prefix.len() + self.rollable_extranonce_size as usize
}
pub fn get_requested_max_target(&self) -> &Target {
&self.requested_max_target
}
pub fn get_target(&self) -> &Target {
&self.target
}
pub fn set_target(&mut self, target: Target) {
self.target = target;
}
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_nominal_hashrate(&self) -> f32 {
self.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 set_nominal_hashrate(&mut self, hashrate: f32) {
self.nominal_hashrate = hashrate;
}
pub fn update_channel(
&mut self,
new_nominal_hashrate: f32,
requested_max_target: Option<Target>,
) -> Result<(), ExtendedChannelError> {
let target = match hash_rate_to_target(
new_nominal_hashrate.into(),
self.expected_share_per_minute.into(),
) {
Ok(target) => target,
Err(_) => {
return Err(ExtendedChannelError::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,
};
let target_bytes = target.to_be_bytes();
let max_target = requested_max_target;
let max_target_bytes = 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.nominal_hashrate = new_nominal_hashrate;
self.target = new_target;
self.requested_max_target = *requested_max_target;
Ok(())
}
pub fn get_active_job(&self) -> Option<ExtendedJob<'a>> {
self.job_store.get_active_job()
}
pub fn get_future_job(&self, job_id: u32) -> Option<ExtendedJob<'a>> {
self.job_store.get_future_job(job_id)
}
pub fn get_past_job(&self, job_id: u32) -> Option<ExtendedJob<'a>> {
self.job_store.get_past_job(job_id)
}
pub fn get_share_accounting(&self) -> &ShareAccounting {
&self.share_accounting
}
pub fn on_new_template(
&mut self,
template: NewTemplate<'a>,
coinbase_reward_outputs: Vec<TxOut>,
) -> Result<(), ExtendedChannelError> {
match template.future_template {
true => {
let new_job = self
.job_factory
.new_extended_job(
self.channel_id,
None,
self.extranonce_prefix.as_bytes().to_vec(),
template.clone(),
coinbase_reward_outputs,
self.get_full_extranonce_size(),
)
.map_err(ExtendedChannelError::JobFactoryError)?;
self.job_store.add_future_job(template.template_id, new_job);
}
false => {
match self.chain_tip.clone() {
None => return Err(ExtendedChannelError::ChainTipNotSet),
Some(chain_tip) => {
let new_job = self
.job_factory
.new_extended_job(
self.channel_id,
Some(chain_tip),
self.extranonce_prefix.as_bytes().to_vec(),
template.clone(),
coinbase_reward_outputs,
self.get_full_extranonce_size(),
)
.map_err(ExtendedChannelError::JobFactoryError)?;
self.job_id_to_target
.insert(new_job.get_job_id(), self.target);
self.job_store.add_active_job(new_job);
}
}
}
}
Ok(())
}
pub fn on_group_channel_job(
&mut self,
mut extended_job: ExtendedJob<'a>,
) -> Result<(), ExtendedChannelError> {
extended_job.set_extranonce_prefix(self.extranonce_prefix.as_bytes().to_vec());
let template_id = match extended_job.get_origin() {
JobOrigin::NewTemplate(template) => template.template_id,
JobOrigin::SetCustomMiningJob(_) => {
return Err(ExtendedChannelError::InvalidJobOrigin);
}
};
match extended_job.is_future() {
true => {
self.job_store.add_future_job(template_id, extended_job);
}
false => {
self.job_id_to_target
.insert(extended_job.get_job_id(), self.target);
self.job_store.add_active_job(extended_job);
}
}
Ok(())
}
pub fn on_set_new_prev_hash(
&mut self,
set_new_prev_hash: SetNewPrevHashTdp<'a>,
) -> Result<(), ExtendedChannelError> {
match self.job_store.has_future_jobs() {
false => {
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(ExtendedChannelError::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);
}
}
self.share_accounting.flush_seen_shares();
self.chain_tip = Some(set_new_prev_hash.into());
Ok(())
}
pub fn on_set_custom_mining_job(
&mut self,
set_custom_mining_job: SetCustomMiningJob<'a>,
) -> Result<u32, ExtendedChannelError> {
let new_job = self
.job_factory
.new_extended_job_from_custom_job(
set_custom_mining_job.clone(),
self.extranonce_prefix.as_bytes().to_vec(),
self.get_full_extranonce_size(),
)
.map_err(ExtendedChannelError::JobFactoryError)?;
let set_custom_mining_job_static = set_custom_mining_job.into_static();
let prev_hash = set_custom_mining_job_static.prev_hash;
let nbits = set_custom_mining_job_static.nbits;
let min_ntime = set_custom_mining_job_static.min_ntime;
let new_chain_tip = ChainTip::new(prev_hash, nbits, min_ntime);
let is_new_chain_tip = self.chain_tip.as_ref().is_some_and(|chain_tip| {
chain_tip.prev_hash() != new_chain_tip.prev_hash()
|| chain_tip.nbits() != new_chain_tip.nbits()
|| chain_tip.min_ntime() != new_chain_tip.min_ntime()
});
let job_id = new_job.get_job_id();
self.job_store.add_active_job(new_job);
if is_new_chain_tip {
self.job_store.mark_past_jobs_as_stale();
self.share_accounting.flush_seen_shares();
self.job_id_to_target.clear();
}
self.chain_tip = Some(new_chain_tip);
self.job_id_to_target.insert(job_id, self.target);
Ok(job_id)
}
pub fn validate_share(
&mut self,
share: SubmitSharesExtended,
) -> Result<ShareValidationResult, ShareValidationError> {
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 extranonce_size = share.extranonce.inner_as_ref().len();
if extranonce_size != self.rollable_extranonce_size as usize {
self.share_accounting
.increment_rejected_shares(ERROR_CODE_SUBMIT_SHARES_BAD_EXTRANONCE_SIZE);
return Err(ShareValidationError::BadExtranonceSize(
ERROR_CODE_SUBMIT_SHARES_BAD_EXTRANONCE_SIZE,
));
}
let extranonce_prefix = job.get_extranonce_prefix();
let mut full_extranonce = vec![];
full_extranonce.extend_from_slice(extranonce_prefix);
full_extranonce.extend(share.extranonce.inner_as_ref());
let merkle_root: [u8; 32] = merkle_root_from_path(
&job.get_coinbase_tx_prefix_without_bip141(),
&job.get_coinbase_tx_suffix_without_bip141(),
full_extranonce.as_ref(),
&job.get_merkle_path().inner_as_ref(),
)
.ok_or(ShareValidationError::Invalid(
ERROR_CODE_SUBMIT_SHARES_INVALID_SHARE,
))?
.try_into()
.expect("merkle root must be 32 bytes");
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());
if !job.version_rolling_allowed() {
if (share.version & 0x1fffe000) != 0 {
self.share_accounting
.increment_rejected_shares(ERROR_CODE_VERSION_ROLLING_NOT_ALLOWED);
return Err(ShareValidationError::VersionRollingNotAllowed(
ERROR_CODE_VERSION_ROLLING_NOT_ALLOWED,
));
}
}
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 raw_share_hash: [u8; 32] = *share_hash.to_raw_hash().as_ref();
let share_hash_target = Target::from_le_bytes(raw_share_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 mut coinbase = vec![];
coinbase.extend(job.get_coinbase_tx_prefix_with_bip141());
coinbase.extend(full_extranonce.clone());
coinbase.extend(job.get_coinbase_tx_suffix_with_bip141());
match job.get_origin() {
JobOrigin::NewTemplate(template) => {
let template_id = template.template_id;
return Ok(ShareValidationResult::BlockFound(
share_hash.to_raw_hash(),
Some(template_id),
coinbase,
));
}
JobOrigin::SetCustomMiningJob(_set_custom_mining_job) => {
return Ok(ShareValidationResult::BlockFound(
share_hash.to_raw_hash(),
None,
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, ExtranoncePrefix, ExtranoncePrefixError},
server::{
error::ExtendedChannelError,
extended::ExtendedChannel,
jobs::{
extended::ExtendedJob,
job_store::{DefaultJobStore, JobStore},
},
share_accounting::{ShareValidationError, ShareValidationResult},
},
};
use binary_sv2::{Sv2Option, U256};
use bitcoin::{transaction::TxOut, Amount, ScriptBuf, Target};
use mining_sv2::{
NewExtendedMiningJob, SetCustomMiningJob, SubmitSharesExtended,
ERROR_CODE_SUBMIT_SHARES_DIFFICULTY_TOO_LOW,
};
use std::convert::TryInto;
use template_distribution_sv2::{NewTemplate, SetNewPrevHash};
const SATS_AVAILABLE_IN_TEMPLATE: u64 = 5000000000;
#[test]
fn test_future_job_activation_flow() {
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, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let expected_share_per_minute = 1.0;
let nominal_hashrate = 1.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 4u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: true,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![82, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967295,
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: 0,
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!(!channel.job_store.has_future_jobs());
channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
assert!(channel.get_active_job().is_none());
let future_job_id = channel
.get_future_job_id_from_template_id(template.template_id)
.unwrap();
let future_job = channel.get_future_job(future_job_id).unwrap();
let expected_job = NewExtendedMiningJob {
channel_id: 1,
job_id: 1,
min_ntime: Sv2Option::new(None),
version: 536870912,
version_rolling_allowed: true,
coinbase_tx_prefix: vec![
2, 0, 0, 0, 1, 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, 0, 0, 0, 0, 0, 255, 255, 255, 255, 38, 82, 0, 3, 47, 47, 47, 31,
]
.try_into()
.unwrap(),
coinbase_tx_suffix: vec![
255, 255, 255, 255, 2, 0, 242, 5, 42, 1, 0, 0, 0, 22, 0, 20, 235, 225, 183, 220,
194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194, 8, 252, 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, 0, 0, 0, 0,
]
.try_into()
.unwrap(),
merkle_path: vec![].try_into().unwrap(),
};
assert_eq!(future_job.get_job_message(), &expected_job);
let ntime = 1746839905;
let set_new_prev_hash = SetNewPrevHash {
template_id: 1,
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(),
};
channel.on_set_new_prev_hash(set_new_prev_hash).unwrap();
assert!(!channel.job_store.has_future_jobs());
let mut previously_future_job = future_job.clone();
previously_future_job.activate(ntime);
let activated_job = 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 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, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let expected_share_per_minute = 1.0;
let nominal_hashrate = 1.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 4u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let ntime = 1746839905;
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 n_bits = 503543726;
let chain_tip = ChainTip::new(prev_hash, n_bits, ntime);
channel.set_chain_tip(chain_tip);
let template = NewTemplate {
template_id: 1,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![82, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967295,
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: 0,
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,
}];
channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
assert!(!channel.job_store.has_future_jobs());
let active_job = channel.get_active_job().unwrap().clone();
let expected_job = NewExtendedMiningJob {
channel_id: 1,
job_id: 1,
min_ntime: Sv2Option::new(Some(ntime)),
version: 536870912,
version_rolling_allowed: true,
coinbase_tx_prefix: vec![
2, 0, 0, 0, 1, 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, 0, 0, 0, 0, 0, 255, 255, 255, 255, 38, 82, 0, 3, 47, 47, 47, 31,
]
.try_into()
.unwrap(),
coinbase_tx_suffix: vec![
255, 255, 255, 255, 2, 0, 242, 5, 42, 1, 0, 0, 0, 22, 0, 20, 235, 225, 183, 220,
194, 147, 204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194, 8, 252, 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, 0, 0, 0, 0,
]
.try_into()
.unwrap(),
merkle_path: vec![].try_into().unwrap(),
};
assert_eq!(active_job.get_job_message(), &expected_job);
}
#[test]
fn test_coinbase_reward_outputs_sum_above_template_value() {
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, 1,
]
.to_vec();
let max_target = Target::from_le_bytes([0xff; 32]);
let expected_share_per_minute = 1.0;
let nominal_hashrate = 1.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 4u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: true,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![82, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967295,
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: 0,
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 invalid_coinbase_reward_outputs = vec![TxOut {
value: Amount::from_sat(SATS_AVAILABLE_IN_TEMPLATE + 1),
script_pubkey: script,
}];
let res = channel.on_new_template(template.clone(), invalid_coinbase_reward_outputs);
assert!(res.is_err());
assert!(!channel.job_store.has_future_jobs());
}
#[test]
fn test_share_validation_block_found() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 8u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let template_id = 1;
let template = NewTemplate {
template_id,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![82, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967295,
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: 0,
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);
channel.set_chain_tip(chain_tip);
channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let share_valid_block = SubmitSharesExtended {
channel_id,
sequence_number: 0,
job_id: 1,
nonce: 8,
ntime: 1745596971,
version: 536870912,
extranonce: vec![1, 0, 0, 0, 0, 0, 0, 0].try_into().unwrap(),
};
let res = channel.validate_share(share_valid_block.clone());
assert!(matches!(
res,
Ok(ShareValidationResult::BlockFound(_, _, _))
));
assert_eq!(channel.get_share_accounting().get_blocks_found(), 1);
let res = channel.validate_share(share_valid_block);
assert!(matches!(
res.unwrap_err(),
ShareValidationError::DuplicateShare(_)
));
assert_eq!(channel.get_share_accounting().get_blocks_found(), 1);
}
#[test]
fn test_share_validation_does_not_meet_target() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 = 100.0; let version_rolling_allowed = true;
let rollable_extranonce_size = 8u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let template_id = 1;
let template = NewTemplate {
template_id,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![82, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967295,
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: 0,
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);
channel.set_chain_tip(chain_tip);
channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let share_low_diff = SubmitSharesExtended {
channel_id,
sequence_number: 0,
job_id: 1,
nonce: 0,
ntime: 1745596971,
version: 536870912,
extranonce: vec![1, 0, 0, 0, 0, 0, 0, 0].try_into().unwrap(),
};
let res = channel.validate_share(share_low_diff);
assert!(matches!(
res.unwrap_err(),
ShareValidationError::DoesNotMeetTarget(_)
));
assert_eq!(
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 channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 version_rolling_allowed = true;
let rollable_extranonce_size = 8u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let template_id = 1;
let template = NewTemplate {
template_id,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![82, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967295,
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: 0,
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 n_bits = 453040064;
let ntime = 1745611105;
let prev_hash = [
23, 205, 72, 134, 153, 86, 220, 153, 224, 28, 216, 146, 228, 120, 227, 157, 213, 99,
160, 163, 128, 59, 139, 190, 158, 62, 0, 0, 0, 0, 0, 0,
]
.into();
let chain_tip = ChainTip::new(prev_hash, n_bits, ntime);
channel.set_chain_tip(chain_tip);
channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let valid_share = SubmitSharesExtended {
channel_id,
sequence_number: 1,
job_id: 1,
nonce: 51208,
ntime: 1745611105,
version: 536870912,
extranonce: vec![1, 0, 0, 0, 0, 0, 0, 0].try_into().unwrap(),
};
let res = channel.validate_share(valid_share);
assert!(matches!(res, Ok(ShareValidationResult::Valid(_))));
let repeated_share = SubmitSharesExtended {
channel_id,
sequence_number: 2,
job_id: 1,
nonce: 51208,
ntime: 1745611105,
version: 536870912,
extranonce: vec![1, 0, 0, 0, 0, 0, 0, 0].try_into().unwrap(),
};
let res = channel.validate_share(repeated_share);
assert!(matches!(res, Err(ShareValidationError::DuplicateShare(_))));
}
#[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 version_rolling_allowed = true;
let rollable_extranonce_size = 4u16;
let share_batch_size = 100;
let expected_share_per_minute = 1.0;
let very_small_hashrate = 0.1;
let job_store = DefaultJobStore::new();
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 = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
not_so_permissive_max_target,
very_small_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
assert_eq!(
channel.get_requested_max_target(),
¬_so_permissive_max_target
);
assert_eq!(channel.get_target(), ¬_so_permissive_max_target);
}
#[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, 1,
]
.to_vec();
let expected_share_per_minute = 1.0;
let initial_hashrate = 10.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 4u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let max_target = Target::from_le_bytes([0xff; 32]);
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
initial_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
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(ExtendedChannelError::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 = [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 version_rolling_allowed = true;
let rollable_extranonce_size = 4u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let current_extranonce_prefix = channel.get_extranonce_prefix();
assert_eq!(current_extranonce_prefix, extranonce_prefix.as_slice());
let new_extranonce_prefix = [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_large = [
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, 2, 0, 0, 0, 0, 0, 0,
]
.to_vec();
assert!(matches!(
ExtranoncePrefix::from_wire(new_extranonce_prefix_too_large.clone()),
Err(ExtranoncePrefixError::ExceedsMaxLength)
));
}
#[test]
fn test_on_group_channel_job_assigns_extranonce_prefix_to_future_job() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let channel_extranonce_prefix = vec![1, 2, 3, 4, 5, 6, 7];
let max_target = Target::from_le_bytes([0xff; 32]);
let expected_share_per_minute = 1.0;
let nominal_hashrate = 1.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 4u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(channel_extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: true,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![82, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967295,
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: 0,
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 group_job = ExtendedJob::from_template(
template.clone(),
vec![], coinbase_reward_outputs,
vec![],
vec![],
NewExtendedMiningJob {
channel_id,
job_id: 1,
min_ntime: Sv2Option::new(None),
version: template.version,
version_rolling_allowed,
coinbase_tx_prefix: vec![].try_into().unwrap(),
coinbase_tx_suffix: vec![].try_into().unwrap(),
merkle_path: vec![].try_into().unwrap(),
},
)
.unwrap();
assert_eq!(group_job.get_extranonce_prefix(), &vec![]);
assert!(!channel.job_store.has_future_jobs());
channel.on_group_channel_job(group_job).unwrap();
assert!(channel.job_store.has_future_jobs());
let future_job_id = channel
.get_future_job_id_from_template_id(template.template_id)
.unwrap();
let stored_job = channel.get_future_job(future_job_id).unwrap();
assert_eq!(
stored_job.get_extranonce_prefix(),
&channel_extranonce_prefix
);
}
#[test]
fn test_on_group_channel_job_assigns_extranonce_prefix_to_active_job() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let channel_extranonce_prefix = vec![10, 20, 30, 40, 50];
let max_target = Target::from_le_bytes([0xff; 32]);
let expected_share_per_minute = 1.0;
let nominal_hashrate = 1.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 4u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(channel_extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let template = NewTemplate {
template_id: 1,
future_template: false, version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![82, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967295,
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: 0,
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 = 1746839905;
let group_job = ExtendedJob::from_template(
template.clone(),
vec![], coinbase_reward_outputs,
vec![],
vec![],
NewExtendedMiningJob {
channel_id,
job_id: 1,
min_ntime: Sv2Option::new(Some(ntime)),
version: template.version,
version_rolling_allowed,
coinbase_tx_prefix: vec![].try_into().unwrap(),
coinbase_tx_suffix: vec![].try_into().unwrap(),
merkle_path: vec![].try_into().unwrap(),
},
)
.unwrap();
channel.set_chain_tip(ChainTip::new(U256::from([0; 32]), 0, ntime));
assert_eq!(group_job.get_extranonce_prefix(), &vec![]);
assert!(channel.get_active_job().is_none());
channel.on_group_channel_job(group_job).unwrap();
let active_job = channel.get_active_job().unwrap();
assert_eq!(
active_job.get_extranonce_prefix(),
&channel_extranonce_prefix
);
}
#[test]
fn test_on_group_channel_job_rejects_custom_mining_job() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = vec![1, 2, 3, 4];
let max_target = Target::from_le_bytes([0xff; 32]);
let expected_share_per_minute = 1.0;
let nominal_hashrate = 1.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 4u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix.clone()).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let custom_job = SetCustomMiningJob {
channel_id,
request_id: 0,
token: vec![].try_into().unwrap(),
version: 536870912,
prev_hash: [0; 32].into(),
min_ntime: 1746839905,
nbits: 503543726,
coinbase_tx_version: 2,
coinbase_prefix: vec![].try_into().unwrap(),
coinbase_tx_input_n_sequence: 4294967295,
coinbase_tx_outputs: vec![].try_into().unwrap(),
coinbase_tx_locktime: 0,
merkle_path: vec![].try_into().unwrap(),
};
let extended_job = ExtendedJob::from_custom_job(
custom_job,
vec![], vec![],
vec![],
vec![],
NewExtendedMiningJob {
channel_id,
job_id: 1,
min_ntime: Sv2Option::new(None),
version: 536870912,
version_rolling_allowed,
coinbase_tx_prefix: vec![].try_into().unwrap(),
coinbase_tx_suffix: vec![].try_into().unwrap(),
merkle_path: vec![].try_into().unwrap(),
},
);
let result = channel.on_group_channel_job(extended_job);
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
ExtendedChannelError::InvalidJobOrigin
));
}
#[test]
fn test_set_new_prev_hash_without_future_jobs_marks_active_as_stale() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 = 100.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 8u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let template_id = 1;
let template = NewTemplate {
template_id,
future_template: false,
version: 536870912,
coinbase_tx_version: 2,
coinbase_prefix: vec![82, 0].try_into().unwrap(),
coinbase_tx_input_sequence: 4294967295,
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: 0,
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);
channel.set_chain_tip(chain_tip);
channel
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();
let active_job_id = channel.get_active_job().unwrap().get_job_id();
assert!(!channel.job_store.has_future_jobs());
let new_prev_hash = SetNewPrevHash {
template_id: 999,
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 + 600,
n_bits,
target: [0xff; 32].into(),
};
channel.on_set_new_prev_hash(new_prev_hash).unwrap();
let late_share = SubmitSharesExtended {
channel_id,
sequence_number: 0,
job_id: active_job_id,
nonce: 0,
ntime: 1745596971,
version: 536870912,
extranonce: vec![1, 0, 0, 0, 0, 0, 0, 0].try_into().unwrap(),
};
let res = channel.validate_share(late_share);
assert!(matches!(res, Err(ShareValidationError::Stale(_))));
}
#[test]
fn test_set_custom_mining_job_chain_tip_change_marks_past_job_stale() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = vec![1, 2, 3, 4];
let max_target = Target::from_le_bytes([0xff; 32]);
let expected_share_per_minute = 1.0;
let nominal_hashrate = 100.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 8u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
let first_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,
];
let second_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,
];
let first_job_id = channel
.on_set_custom_mining_job(custom_mining_job(
channel_id,
1,
first_prev_hash,
1745596910,
))
.unwrap();
let second_job_id = channel
.on_set_custom_mining_job(custom_mining_job(
channel_id,
2,
second_prev_hash,
1745596970,
))
.unwrap();
assert_ne!(first_job_id, second_job_id);
assert!(channel.job_store.get_stale_job(first_job_id).is_some());
let late_share = SubmitSharesExtended {
channel_id,
sequence_number: 0,
job_id: first_job_id,
nonce: 0,
ntime: 1745596930,
version: 536870912,
extranonce: vec![1, 0, 0, 0, 0, 0, 0, 0].try_into().unwrap(),
};
let res = channel.validate_share(late_share);
assert!(matches!(res, Err(ShareValidationError::Stale(_))));
}
#[test]
fn test_set_custom_mining_job_same_chain_tip_keeps_past_job() {
let channel_id = 1;
let user_identity = "user_identity".to_string();
let extranonce_prefix = vec![1, 2, 3, 4];
let max_target = Target::from_le_bytes([0xff; 32]);
let expected_share_per_minute = 1.0;
let nominal_hashrate = 100.0;
let version_rolling_allowed = true;
let rollable_extranonce_size = 8u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let mut channel = ExtendedChannel::new(
channel_id,
user_identity,
ExtranoncePrefix::from_wire(extranonce_prefix).unwrap(),
max_target,
nominal_hashrate,
version_rolling_allowed,
rollable_extranonce_size,
share_batch_size,
expected_share_per_minute,
job_store,
None,
None,
)
.unwrap();
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,
];
let min_ntime = 1745596910;
let first_job_id = channel
.on_set_custom_mining_job(custom_mining_job(channel_id, 1, prev_hash, min_ntime))
.unwrap();
let second_job_id = channel
.on_set_custom_mining_job(custom_mining_job(channel_id, 2, prev_hash, min_ntime))
.unwrap();
assert_ne!(first_job_id, second_job_id);
assert!(channel.job_store.get_past_job(first_job_id).is_some());
assert!(channel.job_store.get_stale_job(first_job_id).is_none());
}
fn custom_mining_job(
channel_id: u32,
request_id: u32,
prev_hash: [u8; 32],
min_ntime: u32,
) -> SetCustomMiningJob<'static> {
SetCustomMiningJob {
channel_id,
request_id,
token: vec![request_id as u8].try_into().unwrap(),
version: 536870912,
prev_hash: prev_hash.into(),
min_ntime,
nbits: 453040064,
coinbase_tx_version: 2,
coinbase_prefix: vec![82, 0].try_into().unwrap(),
coinbase_tx_input_n_sequence: 4294967295,
coinbase_tx_outputs: vec![
1u8, 0, 0xf2, 0x05, 0x2a, 0x01, 0, 0, 0, 22, 0, 20, 235, 225, 183, 220, 194, 147,
204, 170, 14, 231, 67, 168, 111, 137, 223, 130, 88, 194, 8, 252,
]
.try_into()
.unwrap(),
coinbase_tx_locktime: 0,
merkle_path: vec![].try_into().unwrap(),
}
}
}