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

get appropriate foundry version & kinostate for fake node version #263

Merged
merged 4 commits into from
Nov 9, 2024
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
62 changes: 62 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ alloy-sol-macro = "0.7.6"
alloy-sol-types = "0.7.6"
base64 = "0.21"
cargo_metadata = "0.18"
chrono = "0.4"
clap = { version = "4.4", features = ["cargo", "string"] }
color-eyre = { version = "0.6", features = ["capture-spantrace"] }
dirs = "5.0"
Expand Down
113 changes: 83 additions & 30 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,45 @@ use std::io::{self, Write};
use std::path::Path;

const TEMPLATES_DIR: &str = "src/new/templates";
const CHAIN_KINOSTATE_DIR: &str = "src/chain/kinostate";
const TARGET_DIR: &str = "target";
const INCLUDES: &str = "includes.rs";
const NEW_INCLUDES: &str = "new_includes.rs";
const CHAIN_INCLUDES: &str = "chain_includes.rs";

/// create target/new_includes.rs to build templates into binary
fn make_new_includes() -> anyhow::Result<()> {
let mut output_buffer = Vec::new();
writeln!(
&mut output_buffer,
"const PATH_TO_CONTENT: &[(&str, &str)] = &["
)?;
writeln!(
output_buffer,
" (\"{}\", include_str!(\"{}\")),",
"componentize.mjs", "../src/new/componentize.mjs",
)?;

visit_dirs(Path::new(TEMPLATES_DIR), &mut output_buffer)?;

writeln!(&mut output_buffer, "];")?;

let target_dir = Path::new(TARGET_DIR);
let new_output_path = target_dir.join(NEW_INCLUDES);
// create *_includes.rs if it does not exist
if !target_dir.exists() {
fs::create_dir_all(target_dir)?;
}
if !new_output_path.exists() {
fs::write(&new_output_path, &output_buffer)?;
} else {
let existing_file = fs::read(&new_output_path)?;
if output_buffer != existing_file {
fs::write(&new_output_path, &output_buffer)?;
}
}

Ok(())
}

fn visit_dirs(dir: &Path, output_buffer: &mut Vec<u8>) -> io::Result<()> {
if !dir.is_dir() {
Expand Down Expand Up @@ -47,6 +84,48 @@ fn visit_dirs(dir: &Path, output_buffer: &mut Vec<u8>) -> io::Result<()> {
Ok(())
}

fn make_chain_includes() -> anyhow::Result<()> {
let mut output_buffer = Vec::new();
writeln!(
&mut output_buffer,
"const FOUNDRY_COMMIT_TO_CONTENT: &[(&str, &str)] = &["
)?;

for entry in fs::read_dir(CHAIN_KINOSTATE_DIR)? {
let entry = entry?;
let path = entry.path();
let commit = path
.file_stem()
.and_then(|c| c.to_str())
.ok_or_else(|| anyhow::anyhow!("couldn't get commit from {path:?}"))?;
writeln!(
output_buffer,
" (\"{}\", include_str!(\"{}\")),",
commit,
Path::new("..").join(&path).display(),
)?;
}

writeln!(&mut output_buffer, "];")?;

let target_dir = Path::new(TARGET_DIR);
let chain_output_path = target_dir.join(CHAIN_INCLUDES);
// create *_includes.rs if it does not exist
if !target_dir.exists() {
fs::create_dir_all(target_dir)?;
}
if !chain_output_path.exists() {
fs::write(&chain_output_path, &output_buffer)?;
} else {
let existing_file = fs::read(&chain_output_path)?;
if output_buffer != existing_file {
fs::write(&chain_output_path, &output_buffer)?;
}
}

Ok(())
}

fn add_commit_hash(repo: &git2::Repository) -> anyhow::Result<()> {
let sha = repo
.head()?
Expand All @@ -70,36 +149,10 @@ fn add_branch_name(repo: &git2::Repository) -> anyhow::Result<()> {
}

fn main() -> anyhow::Result<()> {
let mut output_buffer = Vec::new();
writeln!(
&mut output_buffer,
"const PATH_TO_CONTENT: &[(&str, &str)] = &["
)?;
writeln!(
output_buffer,
" (\"{}\", include_str!(\"{}\")),",
"componentize.mjs", "../src/new/componentize.mjs",
)?;

visit_dirs(Path::new(TEMPLATES_DIR), &mut output_buffer)?;

writeln!(&mut output_buffer, "];")?;

let target_dir = Path::new(TARGET_DIR);
let output_path = target_dir.join(INCLUDES);
// create includes.rs if it does not exist
if !target_dir.exists() {
fs::create_dir_all(target_dir)?;
}
if !output_path.exists() {
fs::write(&output_path, &output_buffer)?;
} else {
let existing_file = fs::read(&output_path)?;
if output_buffer != existing_file {
fs::write(&output_path, &output_buffer)?;
}
}
make_new_includes()?;
make_chain_includes()?;

// write version info into binary
let repo = git2::Repository::open(".")?;

add_commit_hash(&repo)?;
Expand Down
40 changes: 33 additions & 7 deletions src/boot_fake_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ pub fn get_platform_runtime_name(is_simulation_mode: bool) -> Result<String> {
}

#[instrument(level = "trace", skip_all)]
pub async fn get_runtime_binary(version: &str, is_simulation_mode: bool) -> Result<PathBuf> {
pub async fn get_runtime_binary(
version: &str,
is_simulation_mode: bool,
) -> Result<(PathBuf, String)> {
let zip_name = get_platform_runtime_name(is_simulation_mode)?;

let version = if version != "latest" {
Expand Down Expand Up @@ -193,7 +196,7 @@ pub async fn get_runtime_binary(version: &str, is_simulation_mode: bool) -> Resu
get_runtime_binary_inner(&version, &zip_name, &runtime_dir).await?;
}

Ok(runtime_path)
Ok((runtime_path, version))
}

#[instrument(level = "trace", skip_all)]
Expand Down Expand Up @@ -403,13 +406,13 @@ pub async fn execute(
) -> Result<()> {
let detached = false; // TODO: to argument?
// TODO: factor out with run_tests?
let runtime_path = match runtime_path {
let (runtime_path, version) = match runtime_path {
None => get_runtime_binary(&version, true).await?,
Some(runtime_path) => {
if !runtime_path.exists() {
return Err(eyre!("--runtime-path {:?} does not exist.", runtime_path));
}
if runtime_path.is_dir() {
let runtime_path = if runtime_path.is_dir() {
// Compile the runtime binary
compile_runtime(&runtime_path, release, true)?;
runtime_path
Expand All @@ -418,9 +421,26 @@ pub async fn execute(
.join("kinode")
} else {
runtime_path
}
};
let Some((output, _)) = build::run_command(
Command::new("bash").args(["-c", &format!("{} --version", runtime_path.display())]),
false,
)?
else {
return Err(eyre!("couldn't get Kinode version"));
};
let version = output
.split('\n')
.rev()
.nth(1)
.unwrap()
.split(' ')
.last()
.unwrap();
(runtime_path, version.to_string())
}
};
let version = version.strip_prefix("v").unwrap_or_else(|| &version);

let mut task_handles = Vec::new();

Expand Down Expand Up @@ -455,8 +475,14 @@ pub async fn execute(
}

// boot fakechain
let anvil_process =
chain::start_chain(fakechain_port, true, recv_kill_in_start_chain, false).await?;
let version = version.parse()?;
let anvil_process = chain::start_chain(
fakechain_port,
recv_kill_in_start_chain,
Some(version),
false,
)
.await?;

if let Some(rpc) = rpc {
args.extend_from_slice(&["--rpc".into(), rpc.into()]);
Expand Down
5 changes: 4 additions & 1 deletion src/boot_real_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ pub async fn execute(
let detached = false; // TODO: to argument?
// TODO: factor out with run_tests?
let runtime_path = match runtime_path {
None => get_runtime_binary(&version, false).await?,
None => {
let (runtime_path, _) = get_runtime_binary(&version, false).await?;
runtime_path
}
Some(runtime_path) => {
if !runtime_path.exists() {
return Err(eyre!("--runtime-path {:?} does not exist.", runtime_path));
Expand Down
Loading
Loading