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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use async_std::sync::RwLock;
use git_version::git_version;
use num_derive::FromPrimitive;
use std::fmt::{Display, Formatter, Result as FmtResult};

use serde::Serialize;
const BUILD_VERSION: &str = "0.10.2";

// masks
const MINOR_MASK: u32 = 0xffff00;
const MAJOR_ONLY_MASK: u32 = 0xff0000;
const MINOR_ONLY_MASK: u32 = 0x00ff00;
const PATCH_ONLY_MASK: u32 = 0x0000ff;

// api versions
const FULL_API_VERSION: Version = new_version(1, 0, 0);
const MINER_API_VERSION: Version = new_version(0, 15, 0);
const WORKER_API_VERSION: Version = new_version(0, 15, 0);

lazy_static! {
    pub static ref CURRENT_COMMIT: String = git_version!(fallback = "unknown").to_string();
    pub static ref BUILD_TYPE: RwLock<BuildType> = RwLock::new(BuildType::BuildDefault);
    pub static ref RUNNING_NODE_TYPE: RwLock<NodeType> = RwLock::new(NodeType::Full);
}

/// Represents the current version of the API.
#[derive(Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct APIVersion {
    pub version: String,
    pub api_version: Version,
    pub block_delay: u64,
}

/// Integer based value on version information. Highest order bits for Major, Mid order for Minor
/// and lowest for Patch.
#[derive(Serialize)]
pub struct Version(u32);

/// Build type for the node. This shares which build type the node is from the RPC API.
#[derive(FromPrimitive)]
#[repr(u64)]
pub enum BuildType {
    BuildDefault = 0x0,
    Build2k = 0x1,
    BuildDebug = 0x2,
}

/// The type of node that is running.
#[derive(FromPrimitive)]
#[repr(u64)]
pub enum NodeType {
    Unknown = 0,
    Full = 1,
    Miner = 2,
    Worker = 3,
}

impl Display for NodeType {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}", self)
    }
}

impl BuildType {
    fn to_str(&self) -> &str {
        match self {
            BuildType::BuildDefault => "",
            BuildType::Build2k => "+debug",
            BuildType::BuildDebug => "+2k",
        }
    }
}

const fn new_version(major: u32, minor: u32, patch: u32) -> Version {
    Version(major << 16 | minor << 8 | patch)
}

/// Gets the formatted current user version.
pub async fn user_version() -> String {
    BUILD_VERSION.to_owned() + &*BUILD_TYPE.read().await.to_str() + &CURRENT_COMMIT
}

impl Version {
    fn ints(&self) -> (u32, u32, u32) {
        let v = self.0;
        (
            (v & MAJOR_ONLY_MASK) >> 16,
            (v & MINOR_ONLY_MASK) >> 8,
            v & PATCH_ONLY_MASK,
        )
    }
}

impl PartialEq for Version {
    fn eq(&self, other: &Self) -> bool {
        self.0 & MINOR_MASK == other.0 & MINOR_MASK
    }
}

impl Display for Version {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        let (major, minor, patch) = self.ints();
        write!(f, "{}.{}.{}", major, minor, patch)
    }
}

impl std::convert::TryFrom<&NodeType> for Version {
    type Error = String;
    fn try_from(node_type: &NodeType) -> Result<Self, Self::Error> {
        match node_type {
            NodeType::Full => Ok(FULL_API_VERSION),
            NodeType::Miner => Ok(MINER_API_VERSION),
            NodeType::Worker => Ok(WORKER_API_VERSION),
            _ => Err(format!("unknown node type {}", node_type)),
        }
    }
}