-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Build and Api Versoining #752
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
57851aa
Implemented versioning
StaticallyTypedAnxiety ad58a52
Merge branch 'main' into api_version
StaticallyTypedAnxiety c05490e
fixed merge conflicts
StaticallyTypedAnxiety 462b7cf
cargo fmt
StaticallyTypedAnxiety a4dcb19
common changes
StaticallyTypedAnxiety 6bffb43
Update types/src/build_version/mod.rs
StaticallyTypedAnxiety 9948ba5
Merge branch 'main' into api_version
StaticallyTypedAnxiety 58e5225
changed from to_string to display
StaticallyTypedAnxiety acf0bd0
cargo fmt
StaticallyTypedAnxiety 6078bb3
Merge branch 'main' into api_version
StaticallyTypedAnxiety File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use fil_types::build_version::{ | ||
user_version, APIVersion, Version, BLOCK_DELAY_SECS, RUNNING_NODE_TYPE, | ||
}; | ||
use jsonrpc_v2::Error as JsonRpcError; | ||
use std::convert::TryInto; | ||
pub(crate) async fn version() -> Result<APIVersion, JsonRpcError> { | ||
let v: Version = (&*RUNNING_NODE_TYPE.read().await).try_into()?; | ||
Ok(APIVersion { | ||
version: user_version().await, | ||
api_version: v, | ||
block_delay: BLOCK_DELAY_SECS, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use async_std::sync::RwLock; | ||
use num_derive::FromPrimitive; | ||
use std::{ | ||
fmt::{Display, Formatter, Result as FmtResult}, | ||
process::Command, | ||
}; | ||
|
||
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(0, 17, 0); | ||
const MINER_API_VERSION: Version = new_version(0, 15, 0); | ||
const WORKER_API_VERSION: Version = new_version(0, 15, 0); | ||
|
||
/// a constant for the block delay process in seconds | ||
pub const BLOCK_DELAY_SECS: u64 = 4; | ||
|
||
lazy_static! { | ||
static ref CURRENT_COMMIT: String = { | ||
let output = Command::new("git") | ||
.args(&[ | ||
"describe", | ||
"--always", | ||
"--match=NeVeRmAtch", | ||
"--dirty", | ||
"2>/dev/null", | ||
]) | ||
.output() | ||
.map(|s| s.stdout) | ||
.unwrap_or_else(|_| { | ||
Command::new("git") | ||
.args(&["rev-parse", "--short", "HEAD", "2>/dev/null"]) | ||
.output() | ||
.map(|s| s.stdout) | ||
.unwrap_or_default() | ||
}); | ||
String::from(std::str::from_utf8(&output).unwrap_or_default()) | ||
}; | ||
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); | ||
|
||
#[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 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)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why rwlock? Always same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be injecting/populatingthese values depending on what is being run at the current time. These contribute to the API details that would be returned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only point I was making here was that there is no need to wrap it in a mutex when it is never overwritten. Doesn't really matter but just no need for the overhead right now