Skip to content

Commit 32d11e9

Browse files
committed
feat(mining): check eligibility for solving a data request in wit/2
1 parent d9641e0 commit 32d11e9

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

node/src/actors/chain_manager/handlers.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,19 @@ impl Handler<EpochNotification<EveryEpochPayload>> for ChainManager {
233233
// Data request mining MUST finish BEFORE the block has been mined!!!!
234234
// The transactions must be included into this block, both the transactions from
235235
// our node and the transactions from other nodes
236-
self.try_mine_data_request(ctx);
236+
if let Err(e) = self.try_mine_data_request(ctx) {
237+
match e {
238+
// Lack of eligibility is logged as debug
239+
e @ ChainManagerError::NotEligible => {
240+
log::debug!("{}", e);
241+
}
242+
// Any other errors are logged as warning (considering that this is a best-effort
243+
// method)
244+
e => {
245+
log::warn!("{}", e);
246+
}
247+
}
248+
}
237249
}
238250

239251
// Clear candidates

node/src/actors/chain_manager/mining.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]

validations/src/eligibility/current.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ where
6969
&self,
7070
validator: ISK,
7171
epoch: Epoch,
72-
witnesses: u8,
73-
round: u8,
72+
witnesses: u16,
73+
round: u16,
7474
) -> StakesResult<Eligible, Address, Coins, Epoch>
7575
where
7676
ISK: Into<Address>;
@@ -81,8 +81,8 @@ where
8181
&self,
8282
validator: ISK,
8383
epoch: Epoch,
84-
witnesses: u8,
85-
round: u8,
84+
witnesses: u16,
85+
round: u16,
8686
) -> bool
8787
where
8888
ISK: Into<Address>,
@@ -178,8 +178,8 @@ where
178178
&self,
179179
key: ISK,
180180
epoch: Epoch,
181-
witnesses: u8,
182-
round: u8,
181+
witnesses: u16,
182+
round: u16,
183183
) -> StakesResult<Eligible, Address, Coins, Epoch>
184184
where
185185
ISK: Into<Address>,

0 commit comments

Comments
 (0)