dpp/version/
mod.rs

1use crate::ProtocolError;
2use lazy_static::lazy_static;
3pub use platform_version::error::PlatformVersionError;
4pub use platform_version::version::*;
5pub use platform_version::*;
6use std::sync::RwLock;
7
8lazy_static! {
9    static ref CURRENT_PLATFORM_VERSION: RwLock<Option<&'static PlatformVersion>> =
10        RwLock::new(None);
11}
12
13/// Number of votes for a protocol version upgrade.
14pub type ProtocolVersionVoteCount = u64;
15
16pub trait PlatformVersionCurrentVersion {
17    fn set_current(platform_version: &'static PlatformVersion);
18    fn get_current<'a>() -> Result<&'a Self, ProtocolError>;
19    fn get_maybe_current<'a>() -> Option<&'a Self>;
20    fn get_version_or_current_or_latest<'a>(
21        version: Option<u32>,
22    ) -> Result<&'a Self, ProtocolError>;
23}
24
25impl PlatformVersionCurrentVersion for PlatformVersion {
26    fn set_current(platform_version: &'static PlatformVersion) {
27        let mut context = CURRENT_PLATFORM_VERSION.write().unwrap();
28        *context = Some(platform_version);
29    }
30
31    fn get_current<'a>() -> Result<&'a Self, ProtocolError> {
32        CURRENT_PLATFORM_VERSION
33            .read()
34            .unwrap()
35            .ok_or(ProtocolError::CurrentProtocolVersionNotInitialized)
36    }
37
38    fn get_maybe_current<'a>() -> Option<&'a Self> {
39        let lock_guard = CURRENT_PLATFORM_VERSION.read().unwrap();
40
41        if lock_guard.is_some() {
42            Some(lock_guard.unwrap())
43        } else {
44            None
45        }
46    }
47
48    fn get_version_or_current_or_latest<'a>(
49        version: Option<u32>,
50    ) -> Result<&'a Self, ProtocolError> {
51        if let Some(version) = version {
52            Ok(PlatformVersion::get(version)?)
53        } else if let Some(current_version) = Self::get_maybe_current() {
54            Ok(current_version)
55        } else {
56            Ok(Self::latest())
57        }
58    }
59}