@@ -57,7 +57,7 @@ use bitcoin::{
5757 CompactTarget , Target ,
5858} ;
5959use mining_sv2:: { SetCustomMiningJob , SubmitSharesExtended } ;
60- use std:: { collections :: HashMap , convert:: TryInto , marker:: PhantomData } ;
60+ use std:: { convert:: TryInto , marker:: PhantomData } ;
6161use template_distribution_sv2:: { NewTemplate , SetNewPrevHash as SetNewPrevHashTdp } ;
6262use tracing:: debug;
6363
@@ -327,9 +327,10 @@ where
327327 self . target = target;
328328 }
329329
330- /// Returns the mapping of future template IDs to job IDs.
331- pub fn get_future_template_to_job_id ( & self ) -> & HashMap < u64 , u32 > {
332- self . job_store . get_future_template_to_job_id ( )
330+ /// Returns the job ID for a future job from a template ID, if any.
331+ pub fn get_future_job_id_from_template_id ( & self , template_id : u64 ) -> Option < u32 > {
332+ self . job_store
333+ . get_future_job_id_from_template_id ( template_id)
333334 }
334335
335336 /// Returns the nominal hashrate for this channel.
@@ -405,17 +406,23 @@ where
405406
406407 Ok ( ( ) )
407408 }
408- /// Returns the currently active job, if any.
409- pub fn get_active_job ( & self ) -> Option < & ExtendedJob < ' a > > {
409+
410+ /// Returns an owned copy of the currently active job, if any.
411+ pub fn get_active_job ( & self ) -> Option < ExtendedJob < ' a > > {
412+ // cloning happens inside the job store
410413 self . job_store . get_active_job ( )
411414 }
412- /// Returns all future jobs for this channel.
413- pub fn get_future_jobs ( & self ) -> & HashMap < u32 , ExtendedJob < ' a > > {
414- self . job_store . get_future_jobs ( )
415+
416+ /// Returns an owned copy of a future job from its job ID, if any.
417+ pub fn get_future_job ( & self , job_id : u32 ) -> Option < ExtendedJob < ' a > > {
418+ // cloning happens inside the job store
419+ self . job_store . get_future_job ( job_id)
415420 }
416- /// Returns all past jobs for this channel.
417- pub fn get_past_jobs ( & self ) -> & HashMap < u32 , ExtendedJob < ' a > > {
418- self . job_store . get_past_jobs ( )
421+
422+ /// Returns an owned copy of a past job from its job ID, if any.
423+ pub fn get_past_job ( & self , job_id : u32 ) -> Option < ExtendedJob < ' a > > {
424+ // cloning happens inside the job store
425+ self . job_store . get_past_job ( job_id)
419426 }
420427 /// Returns a reference to the share accounting state for this channel.
421428 pub fn get_share_accounting ( & self ) -> & ShareAccounting {
@@ -489,13 +496,13 @@ where
489496 set_new_prev_hash : SetNewPrevHashTdp < ' a > ,
490497 ) -> Result < ( ) , ExtendedChannelError > {
491498 // extended channels dedicated to custom work don't need to keep track of future jobs
492- match self . job_store . get_future_jobs ( ) . is_empty ( ) {
493- true => {
499+ match self . job_store . has_future_jobs ( ) {
500+ false => {
494501 // explicitly mark past jobs as stale, because we're not going to
495502 // do it implicitly via activate_future_job in case this extended channel is doing custom work
496503 self . job_store . mark_past_jobs_as_stale ( ) ;
497504 }
498- false => {
505+ true => {
499506 // try to activate the future job, and also mark past jobs as stale
500507 if !self . job_store . activate_future_job (
501508 set_new_prev_hash. template_id ,
@@ -570,10 +577,10 @@ where
570577 . is_some_and ( |job| job. get_job_id ( ) == job_id) ;
571578
572579 // check if job_id is past job
573- let is_past_job = self . job_store . get_past_jobs ( ) . contains_key ( & job_id ) ;
580+ let is_past_job = self . job_store . get_past_job ( job_id ) . is_some ( ) ;
574581
575582 // check if job_id is stale job
576- let is_stale_job = self . job_store . get_stale_jobs ( ) . contains_key ( & job_id ) ;
583+ let is_stale_job = self . job_store . get_stale_job ( job_id ) . is_some ( ) ;
577584
578585 if is_stale_job {
579586 return Err ( ShareValidationError :: Stale ) ;
@@ -582,21 +589,19 @@ where
582589 // if job_id is not active, past or stale, return error
583590 if !is_active_job && !is_past_job && !is_stale_job {
584591 return Err ( ShareValidationError :: InvalidJobId ) ;
585- }
592+ } ;
586593
587594 let job = if is_active_job {
588595 self . job_store
589596 . get_active_job ( )
590597 . expect ( "active job must exist" )
591598 } else if is_past_job {
592599 self . job_store
593- . get_past_jobs ( )
594- . get ( & job_id)
600+ . get_past_job ( job_id)
595601 . expect ( "past job must exist" )
596602 } else {
597603 self . job_store
598- . get_stale_jobs ( )
599- . get ( & job_id)
604+ . get_stale_job ( job_id)
600605 . expect ( "stale job must exist" )
601606 } ;
602607
@@ -733,7 +738,7 @@ mod tests {
733738 server:: {
734739 error:: ExtendedChannelError ,
735740 extended:: ExtendedChannel ,
736- jobs:: job_store:: DefaultJobStore ,
741+ jobs:: job_store:: { DefaultJobStore , JobStore } ,
737742 share_accounting:: { ShareValidationError , ShareValidationResult } ,
738743 } ,
739744 } ;
@@ -816,22 +821,17 @@ mod tests {
816821 script_pubkey: script,
817822 } ] ;
818823
819- assert ! ( channel. get_future_jobs ( ) . is_empty ( ) ) ;
824+ assert ! ( ! channel. job_store . has_future_jobs ( ) ) ;
820825 channel
821826 . on_new_template ( template. clone ( ) , coinbase_reward_outputs)
822827 . unwrap ( ) ;
823828 assert ! ( channel. get_active_job( ) . is_none( ) ) ;
824829
825830 let future_job_id = channel
826- . get_future_template_to_job_id ( )
827- . get ( & template. template_id )
831+ . get_future_job_id_from_template_id ( template. template_id )
828832 . unwrap ( ) ;
829833
830- let future_job = channel
831- . get_future_jobs ( )
832- . get ( future_job_id)
833- . unwrap ( )
834- . clone ( ) ;
834+ let future_job = channel. get_future_job ( future_job_id) . unwrap ( ) ;
835835
836836 // we know that the provided template + coinbase_reward_outputs should generate this future
837837 // job
@@ -881,7 +881,7 @@ mod tests {
881881 channel. on_set_new_prev_hash ( set_new_prev_hash) . unwrap ( ) ;
882882
883883 // we just activated the only future job
884- assert ! ( channel. get_future_jobs ( ) . is_empty ( ) ) ;
884+ assert ! ( ! channel. job_store . has_future_jobs ( ) ) ;
885885
886886 let mut previously_future_job = future_job. clone ( ) ;
887887 previously_future_job. activate ( ntime) ;
@@ -982,7 +982,7 @@ mod tests {
982982 . on_new_template ( template. clone ( ) , coinbase_reward_outputs)
983983 . unwrap ( ) ;
984984
985- assert ! ( channel. get_future_jobs ( ) . is_empty ( ) ) ;
985+ assert ! ( ! channel. job_store . has_future_jobs ( ) ) ;
986986
987987 let active_job = channel. get_active_job ( ) . unwrap ( ) . clone ( ) ;
988988
@@ -1090,7 +1090,7 @@ mod tests {
10901090 let res = channel. on_new_template ( template. clone ( ) , invalid_coinbase_reward_outputs) ;
10911091
10921092 assert ! ( res. is_err( ) ) ;
1093- assert ! ( channel. get_future_jobs ( ) . is_empty ( ) ) ;
1093+ assert ! ( ! channel. job_store . has_future_jobs ( ) ) ;
10941094 }
10951095
10961096 #[ test]
0 commit comments