cfx_rpc_eth_types/
sync.rs

1// Copyright 2019-2021 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4
5// Copyright 2015-2020 Parity Technologies (UK) Ltd.
6// This file is part of OpenEthereum.
7
8// OpenEthereum is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// OpenEthereum is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.
20
21use cfx_types::U256;
22use serde::{Serialize, Serializer};
23
24/// Sync info
25#[derive(Default, Debug, Serialize, PartialEq, Clone)]
26#[serde(rename_all = "camelCase")]
27pub struct SyncInfo {
28    /// Starting block
29    pub starting_block: U256,
30    /// Current block
31    pub current_block: U256,
32    /// Highest block seen so far
33    pub highest_block: U256,
34    /// Warp sync snapshot chunks total.
35    pub warp_chunks_amount: Option<U256>,
36    /// Warp sync snpashot chunks processed.
37    pub warp_chunks_processed: Option<U256>,
38}
39
40/// Sync status
41#[derive(Debug, PartialEq, Clone)]
42pub enum SyncStatus {
43    /// Info when syncing
44    Info(SyncInfo),
45    /// Not syncing
46    None,
47}
48
49impl Serialize for SyncStatus {
50    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
51    where S: Serializer {
52        match *self {
53            SyncStatus::Info(ref info) => info.serialize(serializer),
54            SyncStatus::None => false.serialize(serializer),
55        }
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::{SyncInfo, SyncStatus};
62    use serde_json;
63
64    #[test]
65    fn test_serialize_sync_info() {
66        let t = SyncInfo::default();
67        let serialized = serde_json::to_string(&t).unwrap();
68        assert_eq!(
69            serialized,
70            r#"{"startingBlock":"0x0","currentBlock":"0x0","highestBlock":"0x0","warpChunksAmount":null,"warpChunksProcessed":null}"#
71        );
72    }
73
74    #[test]
75    fn test_serialize_sync_status() {
76        let t = SyncStatus::None;
77        let serialized = serde_json::to_string(&t).unwrap();
78        assert_eq!(serialized, "false");
79
80        let t = SyncStatus::Info(SyncInfo::default());
81        let serialized = serde_json::to_string(&t).unwrap();
82        assert_eq!(
83            serialized,
84            r#"{"startingBlock":"0x0","currentBlock":"0x0","highestBlock":"0x0","warpChunksAmount":null,"warpChunksProcessed":null}"#
85        );
86    }
87}