Skip to content
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 10 commits into from
Oct 24, 2020
16 changes: 16 additions & 0 deletions node/rpc/src/common_api.rs
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,
})
}
5 changes: 4 additions & 1 deletion node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

mod auth_api;
mod chain_api;
mod common_api;
mod gas_api;
mod mpool_api;
mod state_api;
mod sync_api;
mod wallet_api;

use crate::state_api::*;
use crate::{common_api::version, state_api::*};
use async_log::span;
use async_std::net::{TcpListener, TcpStream};
use async_std::sync::{Arc, RwLock, Sender};
Expand Down Expand Up @@ -254,6 +255,8 @@ where
gas_estimate_fee_cap::<DB, KS>,
false,
)
// Common
.with_method("Filecoin.Version", version, false)
.finish_unwrapped();

let try_socket = TcpListener::bind(rpc_endpoint).await;
Expand Down
2 changes: 2 additions & 0 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ base64 = { version = "0.12.1", optional = true }
forest_json_utils = { path = "../utils/json_utils", optional = true }
ipld_amt = { path = "../ipld/amt/" }
lazy_static = "1.4"
async-std = "1.6.3"


[features]
json = ["base64", "forest_json_utils"]
142 changes: 142 additions & 0 deletions types/src/build_version/mod.rs
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);
Comment on lines +49 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why rwlock? Always same?

Copy link
Contributor Author

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

Copy link
Contributor

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

}

/// 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)),
}
}
}
1 change: 1 addition & 0 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

pub mod build_version;
pub mod genesis;
mod piece;
mod randomness;
Expand Down