1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.

// OpenEthereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// OpenEthereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.

//! Block import analysis functions.

use ethcore::client::BlockQueueInfo;
use sync::SyncState;

/// Check if client is during major sync or during block import and allows defining whether 'waiting for peers' should
/// be considered a syncing state.
pub fn is_major_importing_or_waiting(
    sync_state: Option<SyncState>,
    queue_info: BlockQueueInfo,
    waiting_is_syncing_state: bool,
) -> bool {
    let is_syncing_state = sync_state.map_or(false, |s| match s {
        SyncState::Idle | SyncState::NewBlocks => false,
        SyncState::WaitingPeers if !waiting_is_syncing_state => false,
        _ => true,
    });
    let is_verifying = queue_info.unverified_queue_size + queue_info.verified_queue_size > 3;
    is_verifying || is_syncing_state
}

/// Check if client is during major sync or during block import.
pub fn is_major_importing(sync_state: Option<SyncState>, queue_info: BlockQueueInfo) -> bool {
    is_major_importing_or_waiting(sync_state, queue_info, true)
}

#[cfg(test)]
mod tests {
    use super::is_major_importing;
    use ethcore::client::BlockQueueInfo;
    use sync::SyncState;

    fn queue_info(unverified: usize, verified: usize) -> BlockQueueInfo {
        BlockQueueInfo {
            unverified_queue_size: unverified,
            verified_queue_size: verified,
            verifying_queue_size: 0,
            max_queue_size: 1000,
            max_mem_use: 1000,
            mem_used: 500,
        }
    }

    #[test]
    fn is_still_verifying() {
        assert!(!is_major_importing(None, queue_info(2, 1)));
        assert!(is_major_importing(None, queue_info(2, 2)));
    }

    #[test]
    fn is_synced_state() {
        assert!(is_major_importing(
            Some(SyncState::Blocks),
            queue_info(0, 0)
        ));
        assert!(!is_major_importing(Some(SyncState::Idle), queue_info(0, 0)));
    }
}