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
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use super::super::types::{StorageRef, BlockHeight};

// AtomicU32 is unstable => using AtomicUsize here

/// Shared synchronization client state.
/// It can be slightly innacurate, but the accuracy is not required for it
#[derive(Debug)]
pub struct SynchronizationState {
	/// Is synchronization in progress?
	is_synchronizing: AtomicBool,
	/// Height of best block in the storage
	best_storage_block_height: AtomicUsize,
}

impl SynchronizationState {
	pub fn with_storage(storage: StorageRef) -> Self {
		let best_storage_block_height = storage.best_block().number;
		SynchronizationState {
			is_synchronizing: AtomicBool::new(false),
			best_storage_block_height: AtomicUsize::new(best_storage_block_height as usize),
		}
	}

	pub fn synchronizing(&self) -> bool {
		self.is_synchronizing.load(Ordering::SeqCst)
	}

	pub fn update_synchronizing(&self, synchronizing: bool) {
		self.is_synchronizing.store(synchronizing, Ordering::SeqCst);
	}

	pub fn best_storage_block_height(&self) -> BlockHeight {
		self.best_storage_block_height.load(Ordering::SeqCst) as BlockHeight
	}

	pub fn update_best_storage_block_height(&self, height: BlockHeight) {
		self.best_storage_block_height.store(height as usize, Ordering::SeqCst);
	}
}