Skip to content

Commit

Permalink
Restore the `latest_binary_is_backward_compatible_and_follows_blocks_…
Browse files Browse the repository at this point in the history
…created_by_genesis_binary()` test
  • Loading branch information
rafal-ch committed Oct 1, 2024
1 parent df4504d commit 13f2653
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 7 deletions.
9 changes: 9 additions & 0 deletions version-compatibility/forkless-upgrade/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ license = "BUSL-1.1"
name = "forkless-upgrade"
publish = false
version = "0.0.0"
build = "build.rs"

[dev-dependencies]
anyhow = "1.0"
Expand Down Expand Up @@ -32,6 +33,14 @@ latest-fuel-core-upgradable-executor = { path = "../../crates/services/upgradabl
"wasm-executor",
] }

# Genesis fuel-core
genesis-fuel-core-bin = { version = "0.26.0", package = "fuel-core-bin", features = [
"parquet",
"p2p",
] }
genesis-fuel-core-client = { version = "0.26.0", package = "fuel-core-client" }
genesis-fuel-core-services = { version = "0.26.0", package = "fuel-core-services" }

# Fuel core version 0.36.0
version-36-fuel-core-bin = { version = "0.36.0", package = "fuel-core-bin", features = [
"parquet",
Expand Down
58 changes: 58 additions & 0 deletions version-compatibility/forkless-upgrade/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::{
env,
path::{
Path,
PathBuf,
},
process::Command,
};

fn main() {
// It only forces a rerun of the build when `build.rs` is changed.
println!("cargo:rerun-if-changed=build.rs");
// Because `fuel-core-wasm-executor 0.26.0` is using `--offline` flag,
// we need to download all dependencies before building the WASM executor.
// This is a workaround specifically for `fuel-core 0.26.0`.
// Future version of the `fuel-core` will not use `--offline` flag.
build_fuel_core_26()
}

fn build_fuel_core_26() {
let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let args = vec![
"install".to_owned(),
"fuel-core-wasm-executor".to_owned(),
"--target=wasm32-unknown-unknown".to_owned(),
"--no-default-features".to_owned(),
"--locked".to_owned(),
"--version".to_owned(),
"0.26.0".to_owned(),
];

let mut cargo = Command::new(cargo);
cargo.current_dir(project_root()).args(args);

let output = cargo.output();

match output {
Ok(output) => {
if !output.status.success() {
panic!(
"Got an error status during compiling WASM executor: \n{:#?}",
output
);
}
}
Err(err) => {
panic!("\n{:#?}", err);
}
}
}

fn project_root() -> PathBuf {
Path::new(&env!("CARGO_MANIFEST_DIR"))
.ancestors()
.nth(1)
.unwrap()
.to_path_buf()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::tests_helper::{
default_multiaddr,
GenesisFuelCoreDriver,
LatestFuelCoreDriver,
Version36FuelCoreDriver,
IGNITION_TESTNET_SNAPSHOT,
Expand Down Expand Up @@ -46,6 +47,76 @@ async fn latest_binary_is_backward_compatible_and_can_load_testnet_config() {
assert!(latest_node.node.state().started())
}

#[tokio::test]
async fn latest_binary_is_backward_compatible_and_follows_blocks_created_by_genesis_binary(
) {
// Given
let genesis_keypair = SecpKeypair::generate();
let hexed_secret = hex::encode(genesis_keypair.secret().to_bytes());
let genesis_port = "30333";
let genesis_node = GenesisFuelCoreDriver::spawn(&[
"--service-name",
"GenesisProducer",
"--debug",
"--poa-instant",
"true",
"--consensus-key",
POA_SECRET_KEY,
"--snapshot",
IGNITION_TESTNET_SNAPSHOT,
"--enable-p2p",
"--keypair",
hexed_secret.as_str(),
"--peering-port",
genesis_port,
])
.await
.unwrap();
let public_key = Keypair::from(genesis_keypair).public();
let genesis_peer_id = PeerId::from_public_key(&public_key);
let genesis_multiaddr = default_multiaddr(genesis_port, genesis_peer_id);

// Starting node that uses latest fuel core.
// It will connect to the genesis node and sync blocks.
let latest_keypair = SecpKeypair::generate();
let hexed_secret = hex::encode(latest_keypair.secret().to_bytes());
let latest_node = LatestFuelCoreDriver::spawn(&[
"--service-name",
"LatestValidator",
"--debug",
"--poa-instant",
"false",
"--snapshot",
IGNITION_TESTNET_SNAPSHOT,
"--enable-p2p",
"--keypair",
hexed_secret.as_str(),
"--reserved-nodes",
genesis_multiaddr.as_str(),
"--peering-port",
"0",
])
.await
.unwrap();
let mut imported_blocks = latest_node.node.shared.block_importer.events();

// When
const BLOCKS_TO_PRODUCE: u32 = 10;
genesis_node
.client
.produce_blocks(BLOCKS_TO_PRODUCE, None)
.await
.unwrap();

// Then
for i in 0..BLOCKS_TO_PRODUCE {
let _ = tokio::time::timeout(Duration::from_secs(120), imported_blocks.next())
.await
.expect(format!("Timed out waiting for block import {i}").as_str())
.expect(format!("Failed to import block {i}").as_str());
}
}

#[tokio::test]
async fn latest_binary_is_backward_compatible_and_follows_blocks_created_by_v36_binary() {
// Given
Expand Down
40 changes: 33 additions & 7 deletions version-compatibility/forkless-upgrade/src/tests_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use fuel_tx::{
UploadSubsection,
Witness,
};
use genesis_fuel_core_bin::FuelService as GenesisFuelService;
use genesis_fuel_core_client::client::FuelClient as GenesisClient;
use genesis_fuel_core_services::Service as _;
use latest_fuel_core_bin::FuelService as LatestFuelService;
use latest_fuel_core_client::client::FuelClient as LatestClient;
use libp2p::PeerId;
Expand All @@ -27,8 +30,19 @@ use version_36_fuel_core_bin::FuelService as Version36FuelService;
use version_36_fuel_core_client::client::FuelClient as Version36Client;
use version_36_fuel_core_services as _;

// Awful version compatibility hack.
// `$bin_crate::cli::run::get_service` is async in the later versions of fuel-core-bin.
macro_rules! maybe_await {
(true, $expr:expr) => {
$expr.await
};
(false, $expr:expr) => {
$expr
};
}

macro_rules! define_core_driver {
($bin_crate:ident, $service:ident, $client:ident, $name:ident) => {
($bin_crate:ident, $service:ident, $client:ident, $name:ident, $bin_crate_get_service_is_async:tt) => {
pub struct $name {
/// This must be before the _db_dir as the drop order matters here.
pub node: $service,
Expand All @@ -53,10 +67,12 @@ macro_rules! define_core_driver {
];
args.extend(extra_args);

let node = $bin_crate::cli::run::get_service(
$bin_crate::cli::run::Command::parse_from(args),
)
.await?;
let node = maybe_await!(
$bin_crate_get_service_is_async,
$bin_crate::cli::run::get_service(
$bin_crate::cli::run::Command::parse_from(args),
)
)?;

node.start_and_await().await?;

Expand All @@ -71,18 +87,28 @@ macro_rules! define_core_driver {
};
}

define_core_driver!(
genesis_fuel_core_bin,
GenesisFuelService,
GenesisClient,
GenesisFuelCoreDriver,
false
);

define_core_driver!(
version_36_fuel_core_bin,
Version36FuelService,
Version36Client,
Version36FuelCoreDriver
Version36FuelCoreDriver,
true
);

define_core_driver!(
latest_fuel_core_bin,
LatestFuelService,
LatestClient,
LatestFuelCoreDriver
LatestFuelCoreDriver,
true
);

pub const IGNITION_TESTNET_SNAPSHOT: &str = "./chain-configurations/ignition";
Expand Down

0 comments on commit 13f2653

Please sign in to comment.