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

Remove spec_version check. #251

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Column family names and other constants.

use crate::rpc::ExpectedVersion;

/// Column family for confidence factor
pub const CONFIDENCE_FACTOR_CF: &str = "avail_light_confidence_factor_cf";

Expand All @@ -11,3 +13,9 @@ pub const APP_DATA_CF: &str = "avail_light_app_data_cf";

/// Column family for state
pub const STATE_CF: &str = "avail_light_state_cf";

/// Expected network version
pub const EXPECTED_NETWORK_VERSION: ExpectedVersion = ExpectedVersion {
version: "1.6",
spec_name: "data_avail",
};
19 changes: 8 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use tracing_subscriber::{
};

use crate::{
consts::{APP_DATA_CF, BLOCK_HEADER_CF, CONFIDENCE_FACTOR_CF},
consts::{APP_DATA_CF, BLOCK_HEADER_CF, CONFIDENCE_FACTOR_CF, EXPECTED_NETWORK_VERSION},
data::store_last_full_node_ws_in_db,
types::{Mode, RuntimeConfig},
};
Expand Down Expand Up @@ -151,12 +151,6 @@ async fn run(error_sender: Sender<anyhow::Error>) -> Result<()> {

let db = init_db(&cfg.avail_path).context("Cannot initialize database")?;

let network_version = rpc::Version {
version: "1.6".to_string(),
spec_version: 11,
spec_name: "data-avail".to_string(),
};

// Spawn tokio task which runs one http server for handling RPC
let counter = Arc::new(Mutex::new(0u32));

Expand All @@ -165,7 +159,7 @@ async fn run(error_sender: Sender<anyhow::Error>) -> Result<()> {
cfg: cfg.clone(),
counter: counter.clone(),
version: format!("v{}", clap::crate_version!().to_string()),
network_version: network_version.to_string(),
network_version: EXPECTED_NETWORK_VERSION.to_string(),
};

tokio::task::spawn(server.run());
Expand Down Expand Up @@ -262,9 +256,12 @@ async fn run(error_sender: Sender<anyhow::Error>) -> Result<()> {

let last_full_node_ws = data::get_last_full_node_ws_from_db(db.clone())?;

let (rpc_client, last_full_node_ws) =
rpc::connect_to_the_full_node(&cfg.full_node_ws, last_full_node_ws, network_version)
.await?;
let (rpc_client, last_full_node_ws) = rpc::connect_to_the_full_node(
&cfg.full_node_ws,
last_full_node_ws,
EXPECTED_NETWORK_VERSION,
)
.await?;

store_last_full_node_ws_in_db(db.clone(), last_full_node_ws)?;

Expand Down
68 changes: 44 additions & 24 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,26 @@ fn shuffle_full_nodes(full_nodes: &[String], last_full_node: Option<String>) ->
candidates
}

pub struct Version {
pub version: String,
pub spec_version: u32,
pub spec_name: String,
pub struct ExpectedVersion<'a> {
pub version: &'a str,
pub spec_name: &'a str,
}

impl Version {
pub fn matches(&self, other: &Version) -> bool {
(self.version.starts_with(&other.version) || other.version.starts_with(&self.version))
&& self.spec_name == other.spec_name
&& self.spec_version == other.spec_version
impl ExpectedVersion<'_> {
/// Checks if expected version matches network version.
/// Since the light client uses subset of the node APIs, `matches` checks only prefix of a node version.
/// This means that if expected version is `1.6`, versions `1.6.x` of the node will match.
/// Specification name is checked for exact match.
/// Since runtime `spec_version` can be changed with runtime upgrade, `spec_version` is removed.
/// NOTE: Runtime compatiblity check is currently not implemented.
pub fn matches(&self, node_version: String, spec_name: String) -> bool {
node_version.starts_with(&self.version) && self.spec_name == spec_name
}
}

impl Display for Version {
impl Display for ExpectedVersion<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"v{}/{}/{}",
self.version, self.spec_name, self.spec_version
)
write!(f, "v{}/{}", self.version, self.spec_name)
}
}

Expand All @@ -225,7 +224,7 @@ impl Display for Version {
pub async fn connect_to_the_full_node(
full_nodes: &[String],
last_full_node: Option<String>,
expected_version: Version,
expected_version: ExpectedVersion<'_>,
) -> Result<(avail::Client, String)> {
for full_node_ws in shuffle_full_nodes(full_nodes, last_full_node).iter() {
let log_warn = |error| {
Expand All @@ -237,13 +236,12 @@ pub async fn connect_to_the_full_node(
let Ok(system_version) = get_system_version(&client).await.map_err(log_warn) else { continue; };
let Ok(runtime_version) = get_runtime_version(&client).await.map_err(log_warn) else { continue; };

let version = Version {
version: system_version,
spec_name: runtime_version.spec_name,
spec_version: runtime_version.spec_version,
};
let version = format!(
"v{}/{}/{}",
system_version, runtime_version.spec_name, runtime_version.spec_version,
);

if !expected_version.matches(&version) {
if !expected_version.matches(system_version, runtime_version.spec_name) {
log_warn(anyhow!("expected {expected_version}, found {version}"));
continue;
}
Expand Down Expand Up @@ -282,15 +280,15 @@ pub fn cell_count_for_confidence(confidence: f64) -> u32 {

#[cfg(test)]
mod tests {
use crate::rpc::{shuffle_full_nodes, ExpectedVersion};
use proptest::{
prelude::any_with,
prop_assert, prop_assert_eq, proptest,
sample::size_range,
strategy::{BoxedStrategy, Strategy},
};
use rand::{seq::SliceRandom, thread_rng};

use crate::rpc::shuffle_full_nodes;
use test_case::test_case;

fn full_nodes() -> BoxedStrategy<(Vec<String>, Option<String>)> {
any_with::<Vec<String>>(size_range(10).lift())
Expand All @@ -301,6 +299,28 @@ mod tests {
.boxed()
}

#[test_case("1.6" , "data_avail" , "1.6.1" , "data_avail" , true; "1.6/data_avail matches 1.6.1/data_avail/0")]
#[test_case("1.2" , "data_avail" , "1.2.9" , "data_avail" , true; "1.2/data_avail matches 1.2.9/data_avail/0")]
#[test_case("1.6" , "data_avail" , "1.6.1" , "no_data_avail" , false; "1.6/data_avail matches 1.6.1/no_data_avail/0")]
#[test_case("1.6" , "data_avail" , "1.7.0" , "data_avail" , false; "1.6/data_avail doesn't match 1.7.0/data_avail/0")]
fn test_version_match(
expected_version: &str,
expected_spec_name: &str,
version: &str,
spec_name: &str,
matches: bool,
) {
let expected = ExpectedVersion {
version: expected_version,
spec_name: expected_spec_name,
};

assert_eq!(
expected.matches(version.to_string(), spec_name.to_string()),
matches
);
}

proptest! {
#[test]
fn shuffle_without_last((full_nodes, _) in full_nodes()) {
Expand Down
Loading