Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
simulation: Introduce CUE Schema and build script
Adrian Duke committed 25 days ago
commit 3aa436595f06d263a3ddd774532f90f7359a181e
parent f4d85a3b1df5e1a2edab2289463417bf21e1aaca
7 files changed +706 -1
modified Cargo.lock
@@ -973,6 +973,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a1c3cc8e57274ec99de65301228b537f1e4eedc1b8e0f9411c6caac8ae7308f"
dependencies = [
 "log",
+
 "regex",
]

[[package]]
@@ -984,6 +985,7 @@ dependencies = [
 "anstream 0.6.21",
 "anstyle",
 "env_filter",
+
 "jiff",
 "log",
]

@@ -1144,6 +1146,100 @@ dependencies = [
]

[[package]]
+
name = "futures"
+
version = "0.3.32"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d"
+
dependencies = [
+
 "futures-channel",
+
 "futures-core",
+
 "futures-executor",
+
 "futures-io",
+
 "futures-sink",
+
 "futures-task",
+
 "futures-util",
+
]
+

+
[[package]]
+
name = "futures-channel"
+
version = "0.3.32"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d"
+
dependencies = [
+
 "futures-core",
+
 "futures-sink",
+
]
+

+
[[package]]
+
name = "futures-core"
+
version = "0.3.32"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"
+

+
[[package]]
+
name = "futures-executor"
+
version = "0.3.32"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "baf29c38818342a3b26b5b923639e7b1f4a61fc5e76102d4b1981c6dc7a7579d"
+
dependencies = [
+
 "futures-core",
+
 "futures-task",
+
 "futures-util",
+
]
+

+
[[package]]
+
name = "futures-io"
+
version = "0.3.32"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718"
+

+
[[package]]
+
name = "futures-macro"
+
version = "0.3.32"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b"
+
dependencies = [
+
 "proc-macro2",
+
 "quote",
+
 "syn 2.0.117",
+
]
+

+
[[package]]
+
name = "futures-sink"
+
version = "0.3.32"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "c39754e157331b013978ec91992bde1ac089843443c49cbc7f46150b0fad0893"
+

+
[[package]]
+
name = "futures-task"
+
version = "0.3.32"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393"
+

+
[[package]]
+
name = "futures-timer"
+
version = "3.0.3"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24"
+

+
[[package]]
+
name = "futures-util"
+
version = "0.3.32"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
+
dependencies = [
+
 "futures-channel",
+
 "futures-core",
+
 "futures-io",
+
 "futures-macro",
+
 "futures-sink",
+
 "futures-task",
+
 "memchr",
+
 "pin-project-lite",
+
 "slab",
+
]
+

+
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1868,6 +1964,12 @@ dependencies = [
]

[[package]]
+
name = "glob"
+
version = "0.3.3"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"
+

+
[[package]]
name = "group"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2082,6 +2184,12 @@ dependencies = [
]

[[package]]
+
name = "indenter"
+
version = "0.3.4"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "964de6e86d545b246d84badc0fef527924ace5134f30641c203ef52ba83f58d5"
+

+
[[package]]
name = "indexmap"
version = "2.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3324,6 +3432,18 @@ dependencies = [
]

[[package]]
+
name = "radicle-simulation"
+
version = "0.1.0"
+
dependencies = [
+
 "env_logger",
+
 "log",
+
 "rstest",
+
 "ruast",
+
 "serde_json",
+
 "uuid",
+
]
+

+
[[package]]
name = "radicle-std-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3536,6 +3656,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"

[[package]]
+
name = "relative-path"
+
version = "1.9.3"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2"
+

+
[[package]]
name = "rfc6979"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3567,6 +3693,44 @@ dependencies = [
]

[[package]]
+
name = "rstest"
+
version = "0.19.0"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "9d5316d2a1479eeef1ea21e7f9ddc67c191d497abc8fc3ba2467857abbb68330"
+
dependencies = [
+
 "futures",
+
 "futures-timer",
+
 "rstest_macros",
+
 "rustc_version",
+
]
+

+
[[package]]
+
name = "rstest_macros"
+
version = "0.19.0"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "04a9df72cc1f67020b0d63ad9bfe4a323e459ea7eb68e03bd9824db49f9a4c25"
+
dependencies = [
+
 "cfg-if",
+
 "glob",
+
 "proc-macro2",
+
 "quote",
+
 "regex",
+
 "relative-path",
+
 "rustc_version",
+
 "syn 2.0.117",
+
 "unicode-ident",
+
]
+

+
[[package]]
+
name = "ruast"
+
version = "0.0.23"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "9d1eba4ae58b527c97246d526b90973ef18b61e98b6529fe6a33593e69009788"
+
dependencies = [
+
 "indenter",
+
]
+

+
[[package]]
name = "rustc-demangle"
version = "0.1.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3951,6 +4115,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2aa850e253778c88a04c3d7323b043aeda9d3e30d5971937c1855769763678e"

[[package]]
+
name = "slab"
+
version = "0.4.12"
+
source = "registry+https://github.com/rust-lang/crates.io-index"
+
checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"
+

+
[[package]]
name = "smallvec"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
modified Cargo.toml
@@ -1,5 +1,6 @@
[workspace]
-
members = ["crates/*"]
+
members = ["crates/*", "simulation/radicle-simulation"]
+
default-members = ["crates/*"]
resolver = "2"

[workspace.package]
added simulation/instances/schema.cue
@@ -0,0 +1,85 @@
+
@if(!debug)
+

+
package main
+

+
//
+
// Pre-calculated NIDs.
+
//
+
#BootstrapNIDs: {
+
	"bootstrap-0": "z6MkhJ3cwzpAoNjFnJXWETSPHcDyw2HuBVEhgkyTfbjQHY1B"
+
	"bootstrap-1": "z6MkjcaeSHhQVJU1UeXpnHHZ6mp67zDfQYNMDotHGxbrk7Nj"
+
	"bootstrap-2": "z6MkjNGhuJvdp2noidRMLqco4jFnNNSWzCxSZH5nJV1pGrwQ"
+
	"bootstrap-3": "z6MkpEsXUMSnmyfwdEVkAKijTxGy9WKmNoHWpoxxLM6bbz9M"
+
}
+

+
//
+
// Shared configs
+
//
+
#SeedAddress: {
+
	nid:   string
+
	name:  string
+
	role:  string | *"bootstrap"
+
	index: int | *0
+
	out:   "\(nid)@\(name)-\(index).\(role).default.svc.cluster.local:8776"
+
}
+

+
#BaseBootstrapSeedConfig: {
+
	node: {
+
		listen: ["0.0.0.0:8776"]
+
		seedingPolicy: {
+
			default: "allow"
+
			scope:   "all"
+
		}
+
		...
+
	}
+
	...
+
}
+

+
#BasePeerConfig: {
+
	node: {
+
		listen: []
+
		peers: type: "dynamic"
+
		connect: []
+
		externalAddresses: [] // Explicitly override the default to be empty
+
		log:                  "INFO"
+
		relay:                "auto"
+
		limits: {
+
			routingMaxSize:   1000
+
			routingMaxAge:    604800
+
			gossipMaxAge:     1209600
+
			fetchConcurrency: 1
+
			maxOpenFiles:     4096
+
			rate: {
+
				inbound: {fillRate: 5.0, capacity: 1024}
+
				outbound: {fillRate: 10.0, capacity: 2048}
+
			}
+
			connection: {inbound: 128, outbound: 16}
+
			fetchPackReceive: "500.0 MiB"
+
		}
+
		seedingPolicy: default: "block"
+
		...
+
	}
+
	...
+
}
+

+
//
+
// Topology Constraints
+
//
+
values: {
+
	topology: {
+
		//
+
		// Naming conventions so bindings can be generated automatically
+
		// See: [`radicle-simulations/build.rs`]
+
		//
+

+
		// Prefix enforcement `bootstrap-`, `peer-`, `seed-`
+
		[=~"^(bootstrap|peer|seed)-[a-zA-Z0-9-]+$"]: {
+
			role:     string
+
			replicas: int | *1
+
		}
+
		// Peer version enforcement
+
		[=~"^peer-v[0-9]+-[0-9]+-[0-9]+$"]: {
+
			role: "peer"
+
		}
+
	}
+
}
added simulation/radicle-simulation/Cargo.toml
@@ -0,0 +1,23 @@
+
[package]
+
name = "radicle-simulation"
+
version = "0.1.0"
+
build = "build.rs"
+
homepage.workspace = true
+
repository.workspace = true
+
license.workspace = true
+
edition.workspace = true
+
rust-version.workspace = true
+

+
[dependencies]
+
uuid = { version = "1.8.0", features = ["v4"] }
+
log = "0.4"
+
env_logger = "0.11"
+

+
[dev-dependencies]
+
rstest = "0.19.0"
+
serde_json = "1.0"
+
ruast = "0.0.23"
+

+
[build-dependencies]
+
serde_json = "1.0"
+
ruast = "0.0.23"
added simulation/radicle-simulation/build.rs
@@ -0,0 +1,384 @@
+
use std::env;
+
use std::fs;
+
use std::path::Path as StdPath;
+
use std::process::Command;
+

+
// Include the shared constants
+
include!("src/constants.rs");
+

+
// Helper to parse "peer-v1-5-0" into a tuple (1, 5, 0) for sorting
+
fn parse_version(name: &str) -> (u32, u32, u32) {
+
    let parts: Vec<&str> = name.split('-').collect();
+
    if parts.len() >= 4 && parts[0] == "peer" && parts[1].starts_with('v') {
+
        let major = parts[1][1..].parse().unwrap_or(0);
+
        let minor = parts[2].parse().unwrap_or(0);
+
        let patch = parts[3].parse().unwrap_or(0);
+
        (major, minor, patch)
+
    } else {
+
        (0, 0, 0)
+
    }
+
}
+

+
// Helper to generate the `radicle_simulation::node::Node` path
+
fn node_type_path() -> ruast::Path {
+
    ruast::Path::new(vec![
+
        ruast::PathSegment::simple("radicle_simulation"),
+
        ruast::PathSegment::simple("node"),
+
        ruast::PathSegment::simple("Node"),
+
    ])
+
}
+

+
// Helper to generate the `radicle_simulation::node::Node::new` path
+
fn node_new_path() -> ruast::Path {
+
    ruast::Path::new(vec![
+
        ruast::PathSegment::simple("radicle_simulation"),
+
        ruast::PathSegment::simple("node"),
+
        ruast::PathSegment::simple("Node"),
+
        ruast::PathSegment::simple("new"),
+
    ])
+
}
+

+
// Helper to build the AST for `format!("{}-{}", node_name, index)`
+
fn build_format_macro(node_name: &str) -> ruast::Expr {
+
    let mut ts = ruast::TokenStream::new();
+
    ts.extend(ruast::TokenStream::from(ruast::Expr::new(ruast::Lit::str(
+
        format!("{}-{{}}", node_name),
+
    ))));
+
    ts.push(ruast::Token::Comma);
+
    ts.extend(ruast::TokenStream::from(ruast::Expr::new(
+
        ruast::Path::single("index"),
+
    )));
+

+
    ruast::Expr::new(ruast::MacCall::new(
+
        ruast::Path::single("format"),
+
        ruast::DelimArgs::parenthesis(ts),
+
    ))
+
}
+

+
// Generates the AST for an individual node function
+
//   `pub fn <node_name>([index: usize]) -> radicle_simulation::node::Node`
+
fn generate_node_function(node_name: &str, replicas: u64) -> ruast::Item {
+
    let fn_name = node_name.replace("-", "_");
+
    let mut base_fn = ruast::Fn::empty(fn_name);
+

+
    base_fn
+
        .fn_decl
+
        .set_output(ruast::Type::Path(node_type_path()));
+

+
    if replicas == 1 {
+
        base_fn.body = Some(ruast::Block::from(ruast::Expr::new(node_new_path()).call(
+
            vec![ruast::Expr::new(ruast::Lit::str(format!(
+
                "{}-0",
+
                node_name
+
            )))],
+
        )));
+
    } else {
+
        base_fn.fn_decl.add_input(ruast::Param::ident(
+
            "index",
+
            ruast::Type::Path(ruast::Path::single("usize")),
+
        ));
+

+
        let format_mac = build_format_macro(node_name);
+

+
        base_fn.body = Some(ruast::Block::from(
+
            ruast::Expr::new(node_new_path()).call(vec![format_mac]),
+
        ));
+
    }
+

+
    ruast::Item::public(base_fn)
+
}
+

+
// Generates the AST for the `peer_relative` function
+
//   `pub fn peer_relative(offset: isize, index: usize) -> radicle_simulation::node::Node`
+
fn generate_peer_relative_function(versioned_peers: &[(String, (u32, u32, u32))]) -> ruast::Item {
+
    let len = versioned_peers.len() as isize;
+
    let mut peer_relative_fn = ruast::Fn::empty("peer_relative");
+

+
    peer_relative_fn.fn_decl.add_input(ruast::Param::ident(
+
        "offset",
+
        ruast::Type::Path(ruast::Path::single("isize")),
+
    ));
+
    peer_relative_fn.fn_decl.add_input(ruast::Param::ident(
+
        "index",
+
        ruast::Type::Path(ruast::Path::single("usize")),
+
    ));
+
    peer_relative_fn
+
        .fn_decl
+
        .set_output(ruast::Type::Path(node_type_path()));
+

+
    if len > 0 {
+
        let match_arms = versioned_peers
+
            .iter()
+
            .enumerate()
+
            .map(|(i, (node_name, _))| {
+
                let offset = (i as isize) - len + 1;
+
                let format_mac = build_format_macro(node_name);
+

+
                ruast::Arm::new(
+
                    ruast::Pat::Lit(ruast::Expr::new(ruast::Lit::int(offset.to_string()))),
+
                    None,
+
                    ruast::Expr::new(node_new_path()).call(vec![format_mac]),
+
                )
+
            })
+
            .chain(std::iter::once({
+
                // Build `panic!("Invalid relative offset. Available offsets: ...")`
+
                let mut ts = ruast::TokenStream::new();
+
                ts.extend(ruast::TokenStream::from(ruast::Expr::new(ruast::Lit::str(
+
                    format!(
+
                        "Invalid relative offset. Available offsets: {}..=0",
+
                        1 - len
+
                    ),
+
                ))));
+

+
                let panic_mac = ruast::Expr::new(ruast::MacCall::new(
+
                    ruast::Path::single("panic"),
+
                    ruast::DelimArgs::parenthesis(ts),
+
                ));
+

+
                ruast::Arm::new(ruast::Pat::Wild, None, panic_mac)
+
            }))
+
            .collect::<Vec<_>>();
+

+
        peer_relative_fn.body = Some(ruast::Block::from(ruast::Expr::new(ruast::Match::new(
+
            ruast::Expr::new(ruast::Path::single("offset")),
+
            match_arms,
+
        ))));
+
    } else {
+
        // Build `panic!("No versioned peers available")`
+
        let mut ts = ruast::TokenStream::new();
+
        ts.extend(ruast::TokenStream::from(ruast::Expr::new(ruast::Lit::str(
+
            "No versioned peers available",
+
        ))));
+

+
        let panic_mac = ruast::Expr::new(ruast::MacCall::new(
+
            ruast::Path::single("panic"),
+
            ruast::DelimArgs::parenthesis(ts),
+
        ));
+

+
        peer_relative_fn.body = Some(ruast::Block::from(panic_mac));
+
    };
+

+
    ruast::Item::public(peer_relative_fn)
+
}
+

+
/// Pure function to generate Rust bindings from a JSON string using `ruast`.
+
fn generate_bindings(json_str: &str) -> Result<String, String> {
+
    let topology: serde_json::Value =
+
        serde_json::from_str(json_str).map_err(|e| format!("Invalid JSON: {}", e))?;
+

+
    let obj = topology.as_object().cloned().unwrap_or_default();
+

+
    let mut keys: Vec<String> = obj.keys().cloned().collect();
+
    keys.sort();
+

+
    // Generate the individual node functions
+
    let node_items = keys.iter().map(|node_name| {
+
        let config = &obj[node_name];
+
        let replicas = config.get("replicas").and_then(|r| r.as_u64()).unwrap_or(1);
+
        generate_node_function(node_name, replicas)
+
    });
+

+
    // Extract and sort versioned peers
+
    let mut versioned_peers: Vec<(String, (u32, u32, u32))> = keys
+
        .iter()
+
        .filter(|name| name.starts_with("peer-v"))
+
        .map(|name| {
+
            let version = parse_version(name);
+
            (name.clone(), version)
+
        })
+
        .collect();
+

+
    versioned_peers.sort_by_key(|k| k.1);
+

+
    // Generate the `peer_relative` function
+
    let peer_relative_fn = generate_peer_relative_function(&versioned_peers);
+

+
    // Assemble the final crate AST
+
    let mut krate = ruast::Crate::new();
+

+
    for item in node_items {
+
        krate.add_item(item);
+
    }
+
    krate.add_item(peer_relative_fn);
+

+
    Ok(krate.to_string())
+
}
+

+
fn is_valid_cue_instance(path: &StdPath) -> bool {
+
    let is_cue = path.extension().and_then(|s| s.to_str()) == Some("cue");
+
    let is_not_schema = path.file_name().and_then(|s| s.to_str()) != Some(SCHEMA_FILE);
+
    is_cue && is_not_schema
+
}
+

+
#[allow(dead_code)]
+
fn main() {
+
    // Tell Cargo to recompile if anything in the instances directory changes.
+
    //
+
    // This may not trigger because of the top-level `Cargo.toml` containing
+
    // `default-members` that doesn't include `radicle-simulation` (so that we
+
    // don't run the simulation tests with a regular `cargo test`).
+
    //
+
    // You instead can use `$ cargo check -p radicle-simulation`
+
    println!("cargo:rerun-if-changed={}", INSTANCES_DIR);
+

+
    let out_dir = env::var_os("OUT_DIR").unwrap();
+
    let out_path = StdPath::new(&out_dir);
+

+
    let entries = fs::read_dir(INSTANCES_DIR).expect("Failed to read instances directory");
+

+
    for entry in entries {
+
        let entry = entry.expect("Failed to read directory entry");
+
        let path = entry.path();
+

+
        if path.is_file() && is_valid_cue_instance(&path) {
+
            let file_stem = path.file_stem().unwrap().to_str().unwrap();
+
            let cue_file = path.to_str().unwrap();
+

+
            // Export the topology block from CUE to JSON
+
            let output = Command::new("cue")
+
                .args([
+
                    "export",
+
                    cue_file,
+
                    SCHEMA_CUE_PATH,
+
                    "-e",
+
                    CUE_TOPOLOGY_PATH,
+
                    "--out",
+
                    "json",
+
                ])
+
                .output()
+
                .unwrap_or_else(|_| {
+
                    panic!(
+
                        "Failed to run `cue export` on {}. Ensure the CUE CLI is installed.",
+
                        cue_file
+
                    )
+
                });
+

+
            if !output.status.success() {
+
                panic!(
+
                    "Failed to export {}:\n{}",
+
                    cue_file,
+
                    String::from_utf8_lossy(&output.stderr)
+
                );
+
            }
+

+
            let json_str = String::from_utf8_lossy(&output.stdout);
+

+
            match generate_bindings(&json_str) {
+
                Ok(generated_code) => {
+
                    let dest_path = out_path.join(format!("{}_bindings.rs", file_stem));
+
                    fs::write(&dest_path, generated_code).unwrap();
+
                }
+
                Err(e) => {
+
                    println!(
+
                        "cargo:warning=Failed to generate bindings for {}: {}",
+
                        cue_file, e
+
                    );
+
                }
+
            }
+
        }
+
    }
+
}
+

+
#[cfg(test)]
+
mod tests {
+
    use super::*;
+

+
    #[test]
+
    fn test_parse_version() {
+
        assert_eq!(parse_version("peer-v1-5-0"), (1, 5, 0));
+
        assert_eq!(parse_version("peer-v1-6-1"), (1, 6, 1));
+
        assert_eq!(parse_version("peer-v2-0-0"), (2, 0, 0));
+

+
        assert_eq!(parse_version("bootstrap-v1-6-1"), (0, 0, 0));
+
        assert_eq!(parse_version("peer-latest"), (0, 0, 0));
+
        assert_eq!(parse_version("random-string"), (0, 0, 0));
+
    }
+

+
    #[test]
+
    fn test_generate_bindings_empty() {
+
        let json = "{}";
+
        let code = generate_bindings(json).unwrap();
+
        assert!(code.contains("No versioned peers available"));
+
    }
+

+
    #[test]
+
    fn test_generate_bindings_single_replica() {
+
        let json = r#"{
+
            "peer-v1-5-0": {
+
                "replicas": 1
+
            }
+
        }"#;
+
        let code = generate_bindings(json).unwrap();
+
        let code_no_space = code.replace(" ", "");
+

+
        assert!(code_no_space.contains("pubfnpeer_v1_5_0()->radicle_simulation::node::Node"));
+
        assert!(code_no_space.contains("radicle_simulation::node::Node::new(\"peer-v1-5-0-0\")"));
+
        assert!(
+
            code_no_space.contains(
+
                "0=>radicle_simulation::node::Node::new(format!(\"peer-v1-5-0-{}\",index))"
+
            )
+
        );
+
    }
+

+
    #[test]
+
    fn test_generate_bindings_multi_replica() {
+
        let json = r#"{
+
            "peer-v1-7-0": {
+
                "replicas": 3
+
            }
+
        }"#;
+
        let code = generate_bindings(json).unwrap();
+
        let code_no_space = code.replace(" ", "");
+

+
        assert!(
+
            code_no_space.contains("pubfnpeer_v1_7_0(index:usize)->radicle_simulation::node::Node")
+
        );
+
        assert!(
+
            code_no_space
+
                .contains("radicle_simulation::node::Node::new(format!(\"peer-v1-7-0-{}\",index))")
+
        );
+
    }
+

+
    #[test]
+
    fn test_generate_bindings_relative_offsets() {
+
        let json = r#"{
+
            "peer-v1-7-0": { "replicas": 1 },
+
            "peer-v1-5-0": { "replicas": 1 },
+
            "peer-v1-6-1": { "replicas": 1 }
+
        }"#;
+
        let code = generate_bindings(json).unwrap();
+
        let code_no_space = code.replace(" ", "");
+

+
        assert!(code_no_space.contains(
+
            "-2=>radicle_simulation::node::Node::new(format!(\"peer-v1-5-0-{}\",index))"
+
        ));
+
        assert!(code_no_space.contains(
+
            "-1=>radicle_simulation::node::Node::new(format!(\"peer-v1-6-1-{}\",index))"
+
        ));
+
        assert!(
+
            code_no_space.contains(
+
                "0=>radicle_simulation::node::Node::new(format!(\"peer-v1-7-0-{}\",index))"
+
            )
+
        );
+

+
        assert!(code.contains("Available offsets: -2..=0"));
+
    }
+

+
    #[test]
+
    fn test_is_valid_cue_instance() {
+
        assert!(is_valid_cue_instance(StdPath::new("network.cue")));
+
        assert!(is_valid_cue_instance(StdPath::new("phase1_network.cue")));
+
        assert!(is_valid_cue_instance(StdPath::new(
+
            "/some/path/network.cue"
+
        )));
+

+
        assert!(!is_valid_cue_instance(StdPath::new("schema.cue")));
+
        assert!(!is_valid_cue_instance(StdPath::new(
+
            "../instances/schema.cue"
+
        )));
+

+
        assert!(!is_valid_cue_instance(StdPath::new("network.json")));
+
        assert!(!is_valid_cue_instance(StdPath::new("schema.json")));
+
        assert!(!is_valid_cue_instance(StdPath::new("README.md")));
+
    }
+
}
added simulation/radicle-simulation/src/constants.rs
@@ -0,0 +1,37 @@
+
// Shared constants for the Radicle simulation testing framework.
+
//
+
// These constants are used by both the main test framework (`src/`) and the
+
// build script (`build.rs`). Because `build.rs` is compiled separately, it
+
// includes this file directly via the `include!` macro.
+

+
macro_rules! instances_dir {
+
    () => {
+
        "../instances"
+
    };
+
}
+

+
macro_rules! schema_file {
+
    () => {
+
        "schema.cue"
+
    };
+
}
+

+
/// The directory containing the CUE network topology instances.
+
pub const INSTANCES_DIR: &str = instances_dir!();
+

+
/// Schema filename
+
pub const SCHEMA_FILE: &str = schema_file!();
+

+
/// The path to the shared CUE schema file.
+
pub const SCHEMA_CUE_PATH: &str = concat!(instances_dir!(), "/", schema_file!());
+

+
/// The CUE path used to extract the topology map during export.
+
pub const CUE_TOPOLOGY_PATH: &str = "values.topology";
+

+
/// The path to the Timoni module used to deploy the network.
+
#[allow(unused)]
+
pub const TIMONI_MODULE_PATH: &str = "../modules/radicle-node";
+

+
/// The name of the Timoni instance deployed to the Kubernetes cluster.
+
#[allow(unused)]
+
pub const TIMONI_INSTANCE_NAME: &str = "radicle-network";
added simulation/radicle-simulation/tests/build_tests.rs
@@ -0,0 +1,5 @@
+
//
+
// Load build.rs as a module so the tests can be ran
+
//
+
#[path = "../build.rs"]
+
mod build;