Skip to content

Commit

Permalink
Download Tangle binary against specific hash (#537)
Browse files Browse the repository at this point in the history
* feat: add binary download to tangle::run

* chore: fix error

* feat!(blueprint-test-utils): build tnt-core in test-utils with soldeer

* feat!(blueprint-test-utils): dynamically get mbsm bytecode from installed tnt-core version

* chore: add incredible-squaring to CI

* feat(blueprint-test-utils): utilize tnt-core bytecode crate

* chore(clippy): cleanup and fmt

* fix(blueprint-test-utils): remove tnt-core from lib

* chore: expose opts through

* chore: merge ISQ CI branch

* chore: fix

* feat: add logging of the node to stdout/stderr, make running local tangle possible

* chore: revert CI change

* chore: clippy

* chore: clippy

* chore: Add in place of bool

* chore: fix

* Update cli/src/deploy.rs

Co-authored-by: shekohex <dev+github@shadykhalifa.me>

* Update blueprint-test-utils/src/test_ext.rs

Co-authored-by: shekohex <dev+github@shadykhalifa.me>

* Update blueprint-test-utils/src/tangle/node.rs

Co-authored-by: shekohex <dev+github@shadykhalifa.me>

* chore: remove timeout

* chore: more changes

* chore: use downloaded tangle

* chore: revert

* chore: setup sending balance

* fix: pass context from job to post-processor without changing post-processor signature

* chore: fixes

* chore: pull in origin HEAD

* chore: fmt

* fix: change working dir in incredible-squaring CI

* chore: expand incredible-squaring e2e test

* fix: remove file from build.rs

* chore: clippy

---------

Co-authored-by: Tjemmmic <mdtjemmes@proton.me>
Co-authored-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
Co-authored-by: shekohex <dev+github@shadykhalifa.me>
Co-authored-by: Thomas Braun <tbraun96@gmail.com>
  • Loading branch information
5 people authored Dec 10, 2024
1 parent 47ab03e commit 12da32d
Show file tree
Hide file tree
Showing 26 changed files with 585 additions and 177 deletions.
59 changes: 52 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ jobs:
runs-on: macos-14
strategy:
matrix:
package: [
gadget-sdk,
gadget-context-derive,
gadget-blueprint-proc-macro,
gadget-blueprint-proc-macro-playground
]
package:
[
gadget-sdk,
gadget-context-derive,
gadget-blueprint-proc-macro,
gadget-blueprint-proc-macro-playground,
]
steps:
- name: checkout code
uses: actions/checkout@v2
Expand Down Expand Up @@ -126,12 +127,14 @@ jobs:
- name: doc tests
run: cargo test --package ${{ matrix.package }} --doc

eigenlayer-tests:
incredible-squaring:
timeout-minutes: 90
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v2
with:
path: gadget

- name: Install Foundry
run: |
Expand All @@ -144,6 +147,48 @@ jobs:
- name: Add Foundry to PATH
run: echo "${{ runner.temp }}/foundry/bin" >> $GITHUB_PATH

- name: Forge build
working-directory: gadget/blueprints/incredible-squaring/contracts
run: forge update && forge build

- name: install rust
uses: dtolnay/rust-toolchain@nightly
with:
toolchain: stable

- uses: swatinem/rust-cache@v2
with:
cache-on-failure: "true"

- name: install protobuf and gmp
run: |
sudo apt-get update
sudo apt-get install -y protobuf-compiler libprotobuf-dev libgmp-dev
- name: install cargo-nextest
run: cargo install cargo-nextest --locked

- name: tests
working-directory: gadget
run: cargo nextest run --nocapture --package incredible-squaring-blueprint test_blueprint --retries 0

eigenlayer-tests:
timeout-minutes: 90
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v2

- name: Install Foundry
run: |
curl -L https://foundry.paradigm.xyz | bash
${FOUNDRY_DIR:-"$HOME/.foundry"}/bin/foundryup
env:
FOUNDRY_DIR: ${{ runner.temp }}/foundry
shell: bash

- name: Add Foundry to PATH
run: echo "${{ runner.temp }}/foundry/bin" >> $GITHUB_PATH

- name: Forge build
run: forge update && cd blueprints/incredible-squaring-eigenlayer && forge build --root ./contracts && cd ../../
Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ subxt-signer = { version = "0.37.0", default-features = false }
subxt = { version = "0.37.0", default-features = false }
subxt-core = { version = "0.37.0", default-features = false }
round-based = "0.3.0"
tnt-core-bytecode = { git = "https://github.com/tangle-network/tnt-core.git", default-features = false, branch = "donovan/bytecode" }

sp-core = { version = "31.0.0", default-features = false }
sp-io = { version = "33.0.0", default-features = false }
Expand Down Expand Up @@ -97,6 +98,7 @@ cargo-generate = { version = "0.21.3", default-features = false }
cfg-if = "1.0.0"
clap = "4.5.16"
clap-cargo = "0.14"
dirs = "5.0.1"
eyre = { version = "0.6.12" }
color-eyre = { version = "0.6", features = ["tracing-error", "color-spantrace"] }
ed25519-zebra = { version = "4" }
Expand Down Expand Up @@ -126,7 +128,7 @@ proc-macro2 = "1.0"
prometheus = { version = "0.13.4", default-features = false }
quote = "1.0"
rand = { version = "0.8.5", default-features = false }
reqwest = "0.12.7"
reqwest = "0.12.9"
rustdoc-types = "0.31.0"
schnorrkel = { version = "0.11.4", default-features = false, features = ["preaudit_deprecated", "getrandom"] }
serde = { version = "1.0.208", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion blueprint-build-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn soldeer_update() {

let status = Command::new(&forge_executable)
.current_dir(&root)
.args(["soldeer", "update"])
.args(["soldeer", "update", "-d"])
.status()
.expect("Failed to execute 'forge soldeer update'");

Expand Down
7 changes: 7 additions & 0 deletions blueprint-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ blueprint-manager = { workspace = true }
futures = { workspace = true }
parking_lot = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["full"] }

# Logging and error handling
Expand All @@ -44,6 +45,7 @@ url = { workspace = true }
cargo-tangle = { workspace = true, default-features = true }
gadget-io = { workspace = true, default-features = false }
gadget-sdk = { workspace = true }
tnt-core-bytecode = { workspace = true, default-features = false }

# Alloy-related
alloy-contract = { workspace = true }
Expand All @@ -55,10 +57,15 @@ alloy-sol-types = { workspace = true }
alloy-transport = { workspace = true }

# Utility
dirs = { workspace = true }
testcontainers = { workspace = true }
uuid = { workspace = true, features = ["v4"] }
tempfile = { workspace = true }
cargo_toml = { workspace = true }
reqwest = { workspace = true }

[dev-dependencies]
cargo_metadata = { workspace = true }

[build-dependencies]
blueprint-build-utils = { workspace = true }
1 change: 1 addition & 0 deletions blueprint-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ fn inject_tangle_key<P: AsRef<Path>>(keystore_path: P, name: &str) -> color_eyre
let suri = format!("//{name}");

let sr = sp_core::sr25519::Pair::from_string(&suri, None).expect("Should be valid SR keypair");

let sr_seed = &sr.as_ref().secret.to_bytes();

let ed = sp_core::ed25519::Pair::from_string(&suri, None).expect("Should be valid ED keypair");
Expand Down
113 changes: 100 additions & 13 deletions blueprint-test-utils/src/tangle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,108 @@
use crate::tangle::node::{Error, SubstrateNode, TANGLE_NODE_ENV};
use reqwest;
use std::env;
use std::fs;
use std::io::Write;
use std::path::PathBuf;

pub mod node;
pub mod transactions;

/// Run a Tangle node with the default settings.
pub use node::NodeConfig;

const TANGLE_RELEASE_MAC: &str = "https://github.com/tangle-network/tangle/releases/download/83f587f/tangle-testnet-manual-seal-darwin-amd64";
const TANGLE_RELEASE_LINUX: &str = "https://github.com/tangle-network/tangle/releases/download/83f587f/tangle-testnet-manual-seal-linux-amd64";

/// Downloads the appropriate Tangle binary for the current platform and returns the path
pub async fn download_tangle_binary() -> Result<PathBuf, Box<dyn std::error::Error>> {
let download_url = if cfg!(target_os = "macos") {
TANGLE_RELEASE_MAC
} else if cfg!(target_os = "linux") {
TANGLE_RELEASE_LINUX
} else {
return Err("Unsupported platform".into());
};

// Create cache directory in user's home directory
let cache_dir = dirs::cache_dir()
.ok_or("Could not determine cache directory")?
.join("tangle-binary");
fs::create_dir_all(&cache_dir)?;

let binary_path = cache_dir.join("tangle");

let version_path = cache_dir.join("version.txt");
let commit_hash = download_url.split('/').nth(7).unwrap_or_default();

let should_download = if binary_path.exists() && version_path.exists() {
// Check if version matches
let stored_version = fs::read_to_string(&version_path)?;
stored_version.trim() != commit_hash
} else {
true
};

if should_download {
let response = reqwest::get(download_url).await?;
let bytes = response.bytes().await?;

let mut file = fs::File::create(&binary_path)?;
file.write_all(&bytes)?;

// Make binary executable
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = fs::metadata(&binary_path)?.permissions();
perms.set_mode(0o755);
fs::set_permissions(&binary_path, perms)?;
}

// Write version file
fs::write(&version_path, commit_hash)?;
}

Ok(binary_path)
}

/// Run a Tangle node with the given configuration.
/// The node will shut down when the returned handle is dropped.
pub fn run() -> Result<SubstrateNode, Error> {
let tangle_from_env = std::env::var(TANGLE_NODE_ENV).unwrap_or_else(|_| "tangle".to_string());
let builder = SubstrateNode::builder()
.binary_paths([
&tangle_from_env,
"../tangle/target/release/tangle",
"../../tangle/target/release/tangle",
"../../../tangle/target/release/tangle",
])
pub async fn run(config: NodeConfig) -> Result<SubstrateNode, Error> {
let mut builder = SubstrateNode::builder();

// Add binary paths
if config.use_local_tangle {
let tangle_from_env =
std::env::var(TANGLE_NODE_ENV).unwrap_or_else(|_| "tangle".to_string());
builder
.add_binary_path(tangle_from_env)
.add_binary_path("../tangle/target/release/tangle")
.add_binary_path("../../tangle/target/release/tangle")
.add_binary_path("../../../tangle/target/release/tangle");
} else {
let binary_path = download_tangle_binary().await.map_err(|e| {
Error::Io(std::io::Error::new(
std::io::ErrorKind::Other,
e.to_string(),
))
})?;
builder.add_binary_path(binary_path.to_string_lossy().to_string());
}

// Add standard arguments
builder
.arg("validator")
.arg_val("rpc-cors", "all")
.arg_val("rpc-methods", "unsafe")
.arg("rpc-external")
.arg_val("sealing", "manual")
.clone();
.arg_val("sealing", "manual");

// Add log configuration
let log_string = config.to_log_string();
if !log_string.is_empty() {
builder.arg_val("log", log_string);
}

builder.spawn()
}

Expand All @@ -29,6 +112,7 @@ macro_rules! tangle_blueprint_test_template {
(
$N:tt,
$test_logic:expr,
$node_config:expr,
) => {
use $crate::test_ext::new_test_ext_blueprint_manager;

Expand All @@ -42,6 +126,7 @@ macro_rules! tangle_blueprint_test_template {
::blueprint_test_utils::test_ext::new_test_ext_blueprint_manager::<$N, 1, String, _, _>(
tmp_dir_path,
::blueprint_test_utils::run_test_blueprint_manager,
$node_config,
)
.await
.execute_with_async($test_logic)
Expand All @@ -59,10 +144,11 @@ macro_rules! test_tangle_blueprint {
[$($inputs:expr),*],
[$($expected_output:expr),*],
$call_id:expr,
$node_config:expr,
) => {
::blueprint_test_utils::tangle_blueprint_test_template!(
$N,
|client, handles, blueprint| async move {
|client, handles, blueprint, _| async move {
let keypair = handles[0].sr25519_id().clone();
let selected_service = &blueprint.services[0];
let service_id = selected_service.id;
Expand Down Expand Up @@ -109,6 +195,7 @@ macro_rules! test_tangle_blueprint {
assert_eq!(result, expected);
}
},
$node_config,
);
};
(
Expand Down
Loading

0 comments on commit 12da32d

Please sign in to comment.