@@ -281,22 +281,23 @@ impl ChainManager {
281281 // TODO: double check if this is correct or not, because the need for
282282 // using the `unused_assignments` directive smells bad.
283283 #[ allow( unused_assignments) ]
284- pub fn try_mine_data_request ( & mut self , ctx : & mut Context < Self > ) {
284+ pub fn try_mine_data_request (
285+ & mut self ,
286+ ctx : & mut Context < Self > ,
287+ ) -> Result < ( ) , ChainManagerError > {
288+ if !self . mining_enabled {
289+ return Err ( ChainManagerError :: MiningIsDisabled ) ;
290+ }
291+
285292 let vrf_input = self
286293 . chain_state
287294 . chain_info
288295 . as_ref ( )
289296 . map ( |x| x. highest_vrf_output ) ;
290297
291- if self . current_epoch . is_none ( ) || self . own_pkh . is_none ( ) || vrf_input. is_none ( ) {
292- log:: warn!( "Cannot mine a data request because current epoch or own pkh is unknown" ) ;
293-
294- return ;
295- }
296-
297- let vrf_input = vrf_input. unwrap ( ) ;
298- let own_pkh = self . own_pkh . unwrap ( ) ;
299- let current_epoch = self . current_epoch . unwrap ( ) ;
298+ let vrf_input = vrf_input. ok_or ( ChainManagerError :: ChainNotReady ) ?;
299+ let own_pkh = self . own_pkh . ok_or ( ChainManagerError :: ChainNotReady ) ?;
300+ let current_epoch = self . current_epoch . ok_or ( ChainManagerError :: ChainNotReady ) ?;
300301 let data_request_timeout = self . data_request_timeout ;
301302 let timestamp = u64:: try_from ( get_timestamp ( ) ) . unwrap ( ) ;
302303 let consensus_constants = self . consensus_constants ( ) ;
@@ -359,11 +360,12 @@ impl ChainManager {
359360 ) ,
360361 None => {
361362 log:: error!( "ChainInfo is None" ) ;
362- return ;
363+ return Err ( ChainManagerError :: ChainNotReady ) ;
363364 }
364365 } ;
365366
366367 let num_witnesses = dr_state. data_request . witnesses ;
368+ let round = dr_state. info . current_commit_round ;
367369 let num_backup_witnesses = dr_state. backup_witnesses ( ) ;
368370 // The vrf_input used to create and verify data requests must be set to the current epoch
369371 let dr_vrf_input = CheckpointVRF {
@@ -380,6 +382,21 @@ impl ChainManager {
380382 collateral_age
381383 } ;
382384 let ( target_hash, probability) = if protocol_version >= V2_0 {
385+ let eligibility = self
386+ . chain_state
387+ . stakes
388+ . witnessing_eligibility ( own_pkh, current_epoch, num_witnesses, round)
389+ . map_err ( ChainManagerError :: Staking ) ?;
390+
391+ match eligibility {
392+ Eligible :: Yes => {
393+ log:: info!( "Hurray! Found eligibility for solving a data request!" ) ;
394+ }
395+ Eligible :: No ( _) => {
396+ log:: info!( "No eligibility for solving a data request." ) ;
397+ return Ok ( ( ) ) ;
398+ }
399+ }
383400 // Using a target hash of zero for V2_X essentially disables preliminary VRF checks
384401 ( Hash :: min ( ) , 0f64 )
385402 } else {
@@ -459,7 +476,6 @@ impl ChainManager {
459476 . map ( move |( vrf_proof, vrf_proof_hash) | {
460477 // This is where eligibility is verified for several protocol versions
461478 if protocol_version >= V2_0 {
462- // FIXME run v2.x eligibility here
463479 Ok ( vrf_proof)
464480 } else {
465481 // invalid: vrf_hash > target_hash
@@ -727,6 +743,8 @@ impl ChainManager {
727743 . map ( |_res : Result < ( ) , ( ) > , _act, _ctx| ( ) )
728744 . spawn ( ctx) ;
729745 }
746+
747+ Ok ( ( ) )
730748 }
731749
732750 #[ allow( clippy:: needless_collect) ]
0 commit comments