Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 86 additions & 35 deletions sv2/channels-sv2/src/server/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ use crate::{
merkle_root::merkle_root_from_path,
server::{
error::ExtendedChannelError,
jobs::{extended::ExtendedJob, factory::JobFactory, job_store::JobStore, JobOrigin},
jobs::{
extended::ExtendedJob, factory::JobFactoryExtended, job_store::JobStore, JobOrigin,
},
share_accounting::{ShareAccounting, ShareValidationError, ShareValidationResult},
},
target::{bytes_to_hex, hash_rate_to_target, u256_to_block_hash},
Expand Down Expand Up @@ -83,9 +85,11 @@ use tracing::debug;
/// - the channel's job factory
/// - the channel's chain tip
#[derive(Debug)]
pub struct ExtendedChannel<'a, J>
pub struct ExtendedChannel<'a, J, E, F>
where
J: JobStore<ExtendedJob<'a>>,
E: ExtendedJob<'a>,
F: JobFactoryExtended<'a, E>,
J: JobStore<E>,
{
channel_id: u32,
user_identity: String,
Expand All @@ -95,16 +99,18 @@ where
target: Target, // todo: try to use Target from rust-bitcoin
nominal_hashrate: f32,
job_store: J,
job_factory: JobFactory,
job_factory: F,
share_accounting: ShareAccounting,
expected_share_per_minute: f32,
chain_tip: Option<ChainTip>,
phantom: PhantomData<&'a ()>,
phantom: PhantomData<(E, &'a ())>,
}

impl<'a, J> ExtendedChannel<'a, J>
impl<'a, J, E, F> ExtendedChannel<'a, J, E, F>
where
J: JobStore<ExtendedJob<'a>>,
E: ExtendedJob<'a>,
F: JobFactoryExtended<'a, E>,
J: JobStore<E>,
{
/// Constructor of `ExtendedChannel` for a Sv2 Pool Server.
/// Not meant for usage on a Sv2 Job Declaration Client.
Expand Down Expand Up @@ -243,7 +249,7 @@ where
target,
nominal_hashrate,
job_store,
job_factory: JobFactory::new(version_rolling_allowed, pool_tag, miner_tag),
job_factory: F::new_factory_extended(version_rolling_allowed, pool_tag, miner_tag),
share_accounting: ShareAccounting::new(share_batch_size),
expected_share_per_minute,
chain_tip: None,
Expand Down Expand Up @@ -408,19 +414,19 @@ where
}

/// Returns an owned copy of the currently active job, if any.
pub fn get_active_job(&self) -> Option<ExtendedJob<'a>> {
pub fn get_active_job(&self) -> Option<E> {
// cloning happens inside the job store
self.job_store.get_active_job()
}

/// Returns an owned copy of a future job from its job ID, if any.
pub fn get_future_job(&self, job_id: u32) -> Option<ExtendedJob<'a>> {
pub fn get_future_job(&self, job_id: u32) -> Option<E> {
// cloning happens inside the job store
self.job_store.get_future_job(job_id)
}

/// Returns an owned copy of a past job from its job ID, if any.
pub fn get_past_job(&self, job_id: u32) -> Option<ExtendedJob<'a>> {
pub fn get_past_job(&self, job_id: u32) -> Option<E> {
// cloning happens inside the job store
self.job_store.get_past_job(job_id)
}
Expand Down Expand Up @@ -738,7 +744,12 @@ mod tests {
server::{
error::ExtendedChannelError,
extended::ExtendedChannel,
jobs::job_store::{DefaultJobStore, JobStore},
jobs::{
extended::{DefaultExtendedJob, ExtendedJob},
factory::DefaultJobFactory,
job_store::{DefaultJobStore, JobStore},
Job,
},
share_accounting::{ShareValidationError, ShareValidationResult},
},
};
Expand Down Expand Up @@ -768,9 +779,14 @@ mod tests {
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(
let job_store: DefaultJobStore<DefaultExtendedJob> = DefaultJobStore::new();

let mut channel: ExtendedChannel<
'_,
DefaultJobStore<DefaultExtendedJob>,
DefaultExtendedJob,
DefaultJobFactory,
> = ExtendedChannel::new(
channel_id,
user_identity,
extranonce_prefix,
Expand Down Expand Up @@ -914,9 +930,14 @@ mod tests {
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(
let job_store: DefaultJobStore<DefaultExtendedJob> = DefaultJobStore::new();

let mut channel: ExtendedChannel<
'_,
DefaultJobStore<DefaultExtendedJob>,
DefaultExtendedJob,
DefaultJobFactory,
> = ExtendedChannel::new(
channel_id,
user_identity,
extranonce_prefix,
Expand Down Expand Up @@ -1034,9 +1055,14 @@ mod tests {
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(
let job_store: DefaultJobStore<DefaultExtendedJob> = DefaultJobStore::new();

let mut channel: ExtendedChannel<
'_,
DefaultJobStore<DefaultExtendedJob>,
DefaultExtendedJob,
DefaultJobFactory,
> = ExtendedChannel::new(
channel_id,
user_identity,
extranonce_prefix,
Expand Down Expand Up @@ -1112,9 +1138,14 @@ mod tests {
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(
let job_store: DefaultJobStore<DefaultExtendedJob> = DefaultJobStore::new();

let mut channel: ExtendedChannel<
'_,
DefaultJobStore<DefaultExtendedJob>,
DefaultExtendedJob,
DefaultJobFactory,
> = ExtendedChannel::new(
channel_id,
user_identity,
extranonce_prefix,
Expand Down Expand Up @@ -1224,9 +1255,14 @@ mod tests {
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(
let job_store: DefaultJobStore<DefaultExtendedJob> = DefaultJobStore::new();

let mut channel: ExtendedChannel<
'_,
DefaultJobStore<DefaultExtendedJob>,
DefaultExtendedJob,
DefaultJobFactory,
> = ExtendedChannel::new(
channel_id,
user_identity,
extranonce_prefix,
Expand Down Expand Up @@ -1336,9 +1372,14 @@ mod tests {
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(
let job_store: DefaultJobStore<DefaultExtendedJob> = DefaultJobStore::new();

let mut channel: ExtendedChannel<
'_,
DefaultJobStore<DefaultExtendedJob>,
DefaultExtendedJob,
DefaultJobFactory,
> = ExtendedChannel::new(
channel_id,
user_identity,
extranonce_prefix,
Expand Down Expand Up @@ -1459,13 +1500,18 @@ mod tests {
let version_rolling_allowed = true;
let rollable_extranonce_size = 4u16;
let share_batch_size = 100;
let job_store = DefaultJobStore::new();
let job_store: DefaultJobStore<DefaultExtendedJob> = DefaultJobStore::new();

// this is the most permissive possible max_target
let max_target = Target::from_le_bytes([0xff; 32]);

// Create a channel with initial hashrate
let mut channel = ExtendedChannel::new(
let mut channel: ExtendedChannel<
'_,
DefaultJobStore<DefaultExtendedJob>,
DefaultExtendedJob,
DefaultJobFactory,
> = ExtendedChannel::new(
channel_id,
user_identity,
extranonce_prefix,
Expand Down Expand Up @@ -1550,9 +1596,14 @@ mod tests {
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(
let job_store: DefaultJobStore<DefaultExtendedJob> = DefaultJobStore::new();

let mut channel: ExtendedChannel<
'_,
DefaultJobStore<DefaultExtendedJob>,
DefaultExtendedJob,
DefaultJobFactory,
> = ExtendedChannel::new(
channel_id,
user_identity,
extranonce_prefix.clone(),
Expand Down
55 changes: 38 additions & 17 deletions sv2/channels-sv2/src/server/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::{
chain_tip::ChainTip,
server::{
error::GroupChannelError,
jobs::{extended::ExtendedJob, factory::JobFactory, job_store::JobStore},
jobs::{extended::ExtendedJob, factory::JobFactoryExtended, job_store::JobStore},
},
};
use bitcoin::transaction::TxOut;
Expand All @@ -58,22 +58,26 @@ use template_distribution_sv2::{NewTemplate, SetNewPrevHash as SetNewPrevHashTdp
/// - the group channel's stale jobs
/// - the group channel's share validation state
#[derive(Debug)]
pub struct GroupChannel<'a, J>
pub struct GroupChannel<'a, J, E, F>
where
J: JobStore<ExtendedJob<'a>>,
J: JobStore<E>,
E: ExtendedJob<'a>,
F: JobFactoryExtended<'a, E>,
{
group_channel_id: u32,
standard_channel_ids: HashSet<u32>,
job_factory: JobFactory,
job_factory: F,
job_store: J,
chain_tip: Option<ChainTip>,
full_extranonce_size: usize,
phantom: PhantomData<&'a ()>,
phantom: PhantomData<(E, &'a ())>,
}

impl<'a, J> GroupChannel<'a, J>
impl<'a, J, E, F> GroupChannel<'a, J, E, F>
where
J: JobStore<ExtendedJob<'a>>,
J: JobStore<E>,
E: ExtendedJob<'a>,
F: JobFactoryExtended<'a, E>,
{
/// Constructor of `GroupChannel` for a Sv2 Pool Server.
/// Not meant for usage on a Sv2 Job Declaration Client.
Expand Down Expand Up @@ -149,7 +153,7 @@ where
Ok(Self {
group_channel_id,
standard_channel_ids: HashSet::new(),
job_factory: JobFactory::new(true, pool_tag, miner_tag),
job_factory: F::new_factory_extended(true, pool_tag, miner_tag),
job_store,
chain_tip: None,
full_extranonce_size,
Expand Down Expand Up @@ -193,7 +197,7 @@ where
}

/// Returns an owned copy of the currently active job, if any.
pub fn get_active_job(&self) -> Option<ExtendedJob<'a>> {
pub fn get_active_job(&self) -> Option<E> {
// cloning happens inside the job store
self.job_store.get_active_job()
}
Expand All @@ -205,7 +209,7 @@ where
}

/// Returns an owned copy of a future job from its job ID, if any.
pub fn get_future_job(&self, job_id: u32) -> Option<ExtendedJob<'a>> {
pub fn get_future_job(&self, job_id: u32) -> Option<E> {
// cloning happens inside the job store
self.job_store.get_future_job(job_id)
}
Expand Down Expand Up @@ -300,7 +304,12 @@ mod tests {
chain_tip::ChainTip,
server::{
group::GroupChannel,
jobs::job_store::{DefaultJobStore, JobStore},
jobs::{
extended::{DefaultExtendedJob, ExtendedJob},
factory::DefaultJobFactory,
job_store::{DefaultJobStore, JobStore},
Job,
},
},
};
use binary_sv2::Sv2Option;
Expand All @@ -317,9 +326,13 @@ mod tests {
// the messages on this test were collected from a sane message flow
// we use them as test vectors to assert correct behavior of job creation
let group_channel_id = 1;
let job_store = DefaultJobStore::new();
let job_store: DefaultJobStore<DefaultExtendedJob<'_>> = DefaultJobStore::new();
let full_extranonce_size = 32;
let mut group_channel = GroupChannel::new(
let mut group_channel = GroupChannel::<
DefaultJobStore<DefaultExtendedJob<'_>>,
DefaultExtendedJob<'_>,
DefaultJobFactory,
>::new(
group_channel_id,
job_store,
full_extranonce_size,
Expand Down Expand Up @@ -449,7 +462,11 @@ mod tests {

let job_store = DefaultJobStore::new();
let full_extranonce_size = 32;
let mut group_channel = GroupChannel::new(
let mut group_channel = GroupChannel::<
DefaultJobStore<DefaultExtendedJob<'_>>,
DefaultExtendedJob<'_>,
DefaultJobFactory,
>::new(
group_channel_id,
job_store,
full_extranonce_size,
Expand Down Expand Up @@ -507,7 +524,7 @@ mod tests {
.on_new_template(template.clone(), coinbase_reward_outputs)
.unwrap();

let active_job = group_channel.get_active_job().unwrap();
let active_job: DefaultExtendedJob<'_> = group_channel.get_active_job().unwrap();

// we know that the provided template + coinbase_reward_outputs should generate this
// non-future job
Expand Down Expand Up @@ -545,9 +562,13 @@ mod tests {
// we use them as test vectors to assert correct behavior of job creation
let group_channel_id = 1;

let job_store = DefaultJobStore::new();
let job_store: DefaultJobStore<DefaultExtendedJob<'_>> = DefaultJobStore::new();
let full_extranonce_size = 32;
let mut group_channel = GroupChannel::new(
let mut group_channel = GroupChannel::<
DefaultJobStore<DefaultExtendedJob<'_>>,
DefaultExtendedJob<'_>,
DefaultJobFactory,
>::new(
group_channel_id,
job_store,
full_extranonce_size,
Expand Down
Loading
Loading