From 5f3e34145659ce41916216100a79065fb4c9f263 Mon Sep 17 00:00:00 2001 From: ak-teck007 Date: Wed, 22 May 2024 08:16:44 +0100 Subject: [PATCH] Use dynamic supported fuel-core version (#1190) Close https://github.com/FuelLabs/fuels-rs/issues/1189. ~~Waiting on a new `fuel-core` release incorporating https://github.com/FuelLabs/fuel-core/pull/1473.~~ ### Checklist - [x] I have linked to any relevant issues. - [ ] I have updated the documentation. - [ ] I have added tests that prove my fix is effective or that my feature works. - [x] I have added necessary labels. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: Ahmed Sagdati <37515857+segfault-magnet@users.noreply.github.com> --- .github/workflows/ci.yml | 6 ++ Cargo.toml | 2 + .../src/provider/supported_versions.rs | 8 +- scripts/fuel-core-version/Cargo.toml | 16 ++++ scripts/fuel-core-version/src/main.rs | 84 +++++++++++++++++++ scripts/fuel-core-version/version.rs | 1 + 6 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 scripts/fuel-core-version/Cargo.toml create mode 100644 scripts/fuel-core-version/src/main.rs create mode 100644 scripts/fuel-core-version/version.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 85280bf7..2b8240d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -163,6 +163,8 @@ jobs: args: --skip-target-dir - command: test_wasm args: + - command: check_fuel_core_version + args: - command: check_doc_anchors_valid args: - command: check_doc_unresolved_links @@ -236,6 +238,10 @@ jobs: cd wasm-tests wasm-pack test --node + - name: Check that fuel_core version.rs file is up to date + if: ${{ matrix.command == 'check_fuel_core_version' }} + run: cargo run --bin fuel-core-version -- --manifest-path ./Cargo.toml verify + - name: Check for invalid documentation anchors if: ${{ matrix.command == 'check_doc_anchors_valid' }} run: cargo run --bin check-docs diff --git a/Cargo.toml b/Cargo.toml index 1d54df83..089d3c85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ members = [ "packages/fuels-programs", "packages/fuels-test-helpers", "scripts/check-docs", + "scripts/fuel-core-version", "scripts/versions-replacer", "wasm-tests", ] @@ -100,3 +101,4 @@ fuels-core = { version = "0.62.0", path = "./packages/fuels-core", default-featu fuels-macros = { version = "0.62.0", path = "./packages/fuels-macros", default-features = false } fuels-programs = { version = "0.62.0", path = "./packages/fuels-programs", default-features = false } fuels-test-helpers = { version = "0.62.0", path = "./packages/fuels-test-helpers", default-features = false } +versions-replacer = { version = "0.62.0", path = "./scripts/versions-replacer", default-features = false } diff --git a/packages/fuels-accounts/src/provider/supported_versions.rs b/packages/fuels-accounts/src/provider/supported_versions.rs index d5d55761..da20a4b8 100644 --- a/packages/fuels-accounts/src/provider/supported_versions.rs +++ b/packages/fuels-accounts/src/provider/supported_versions.rs @@ -1,8 +1,7 @@ use semver::Version; -fn get_supported_fuel_core_version() -> Version { - "0.26.0".parse().expect("is valid version") -} +pub const SUPPORTED_FUEL_CORE_VERSION: Version = + include!("../../../../scripts/fuel-core-version/version.rs"); #[derive(Debug, PartialEq, Eq)] pub(crate) struct VersionCompatibility { @@ -13,8 +12,7 @@ pub(crate) struct VersionCompatibility { } pub(crate) fn compare_node_compatibility(network_version: Version) -> VersionCompatibility { - let supported_version = get_supported_fuel_core_version(); - check_version_compatibility(network_version, supported_version) + check_version_compatibility(network_version, SUPPORTED_FUEL_CORE_VERSION) } fn check_version_compatibility( diff --git a/scripts/fuel-core-version/Cargo.toml b/scripts/fuel-core-version/Cargo.toml new file mode 100644 index 00000000..af158ca4 --- /dev/null +++ b/scripts/fuel-core-version/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "fuel-core-version" +version = { workspace = true } +authors = { workspace = true } +edition = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } +publish = false +repository = { workspace = true } +rust-version = { workspace = true } + +[dependencies] +clap = { version = "4.5.3", features = ["derive"] } +color-eyre = "0.6.2" +semver = { workspace = true } +versions-replacer = { workspace = true } diff --git a/scripts/fuel-core-version/src/main.rs b/scripts/fuel-core-version/src/main.rs new file mode 100644 index 00000000..4725a080 --- /dev/null +++ b/scripts/fuel-core-version/src/main.rs @@ -0,0 +1,84 @@ +use std::{ + fs, + path::{Path, PathBuf}, +}; + +use clap::{Parser, Subcommand}; +use color_eyre::{ + eyre::{bail, ContextCompat}, + Result, +}; +use semver::Version; +use versions_replacer::metadata::collect_versions_from_cargo_toml; + +fn get_version_from_toml(manifest_path: impl AsRef) -> Result { + let versions = collect_versions_from_cargo_toml(manifest_path)?; + let version = versions["fuel-core-types"].parse::()?; + Ok(version) +} + +fn write_version_to_file(version: Version, version_file_path: impl AsRef) -> Result<()> { + let Version { + major, + minor, + patch, + .. + } = version; + let text = format!("Version::new({major}, {minor}, {patch})"); + fs::write(version_file_path, text.as_bytes())?; + Ok(()) +} + +fn get_version_file_path( + manifest_path: impl AsRef, +) -> Result { + Ok(manifest_path + .as_ref() + .parent() + .wrap_err("Invalid manifest path")? + .join("scripts/fuel-core-version/version.rs")) +} + +fn verify_version_from_file(version: Version) -> Result<()> { + let version_from_file: Version = include!("../version.rs"); + if version != version_from_file { + bail!( + "fuel_core version in version.rs ({}) doesn't match one in Cargo.toml ({})", + version_from_file, + version + ); + } + println!( + "fuel_core versions in versions.rs and Cargo.toml match ({})", + version + ); + Ok(()) +} + +#[derive(Debug, Parser)] +struct App { + #[clap(subcommand)] + command: Command, + #[clap(long)] + manifest_path: PathBuf, +} + +#[derive(Debug, Subcommand)] +enum Command { + Write, + Verify, +} + +fn main() -> Result<()> { + let App { + command, + manifest_path, + } = App::parse(); + let version = get_version_from_toml(&manifest_path)?; + let version_file_path = get_version_file_path(&manifest_path)?; + match command { + Command::Write => write_version_to_file(version, version_file_path)?, + Command::Verify => verify_version_from_file(version)?, + } + Ok(()) +} diff --git a/scripts/fuel-core-version/version.rs b/scripts/fuel-core-version/version.rs new file mode 100644 index 00000000..e41fa22a --- /dev/null +++ b/scripts/fuel-core-version/version.rs @@ -0,0 +1 @@ +Version::new(0, 26, 0)