polkadot_node_core_dispute_coordinator/status.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16
17use polkadot_node_primitives::{dispute_is_inactive, DisputeStatus, Timestamp};
18use polkadot_primitives::{CandidateHash, SessionIndex};
19use std::time::{SystemTime, UNIX_EPOCH};
20
21use crate::LOG_TARGET;
22
23/// Get active disputes as iterator, preserving its `DisputeStatus`.
24pub fn get_active_with_status(
25 recent_disputes: impl Iterator<Item = ((SessionIndex, CandidateHash), DisputeStatus)>,
26 now: Timestamp,
27) -> impl Iterator<Item = ((SessionIndex, CandidateHash), DisputeStatus)> {
28 recent_disputes.filter(move |(_, status)| !dispute_is_inactive(status, &now))
29}
30
31pub trait Clock: Send + Sync {
32 fn now(&self) -> Timestamp;
33}
34
35pub struct SystemClock;
36
37impl Clock for SystemClock {
38 fn now(&self) -> Timestamp {
39 // `SystemTime` is notoriously non-monotonic, so our timers might not work
40 // exactly as expected.
41 //
42 // Regardless, disputes are considered active based on an order of minutes,
43 // so a few seconds of slippage in either direction shouldn't affect the
44 // amount of work the node is doing significantly.
45 match SystemTime::now().duration_since(UNIX_EPOCH) {
46 Ok(d) => d.as_secs(),
47 Err(e) => {
48 gum::warn!(
49 target: LOG_TARGET,
50 err = ?e,
51 "Current time is before unix epoch. Validation will not work correctly."
52 );
53
54 0
55 },
56 }
57 }
58}