diff --git a/Cargo.lock b/Cargo.lock index bd66c6c..1a3e460 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -473,7 +473,7 @@ checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" [[package]] name = "logos-devnet" -version = "0.9.1" +version = "0.9.2" dependencies = [ "anyhow", "bs58", diff --git a/Cargo.toml b/Cargo.toml index 0b9527b..a1c5d59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "logos-devnet" -version = "0.9.1" +version = "0.9.2" edition = "2024" rust-version = "1.89" description = "A local LEZ chain for Logos development: one command to start it, fund accounts and deploy programs" diff --git a/README.md b/README.md index 101e5c7..3fc83b3 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ cargo install --locked logos-devnet Or take a binary from the [latest release](https://github.com/drptbl/logos-devnet/releases/latest), built for macOS (`aarch64-apple-darwin`, `x86_64-apple-darwin`) and Linux (`x86_64-unknown-linux-musl`, `aarch64-unknown-linux-musl`, static, so any distribution), and check it against its `.sha256`: ```bash -v=v0.9.1 t=aarch64-apple-darwin +v=v0.9.2 t=aarch64-apple-darwin curl -fsSL --remote-name-all https://github.com/drptbl/logos-devnet/releases/download/$v/logos-devnet-$v-$t.tar.gz{,.sha256} shasum -a 256 -c logos-devnet-$v-$t.tar.gz.sha256 \ && tar -xzf logos-devnet-$v-$t.tar.gz \ @@ -41,7 +41,7 @@ source ~/.logos-devnet/env # LOGOS_DEVNET_SEQUENCER, LOG | `down` | Stops it. The chain is kept. | | `reset` | Deletes the chain and starts a new one, every account group funded at genesis. Takes the same options as `up`. | | `status` | What runs, the chain head, the faucet, programs, account groups, sessions (and which stopped while the chain runs), the messaging hub and local storage. | -| `doctor` | Checks the project file, Docker and the image (or git and cargo with `--native`), whether the chain takes the project's settings, the `[programs]` files, the port, the hub's ports, the faucet, sessions that stopped while the chain runs, `curl` for programs from a release and, when sessions or the hub need it, `logosctl`, and says how to fix what fails. | +| `doctor` | Checks the project file, Docker and the image (or git and cargo with `--native`), whether the chain takes the project's settings, the `[programs]` files, the port, the hub's ports, the faucet, the hub's nodes and sessions that stopped while the chain runs, `curl` for programs from a release and, when sessions or the hub need it, `logosctl`, and says how to fix what fails. | | `fund [amount]` | Sends lepta from the faucet to a public account, 10B by default. | | `deploy [--mutable]` | Deploys a program, from its `.bin` (what `cargo risczero build` makes, not the bare ELF), and adds `LOGOS_PROGRAM_` and `LOGOS_PROGRAM__HEX` to the env file. Names are lower-case letters, digits and `-`. With `--mutable`, a new build updates it in place. | | `wallet-config ` | Writes a `wallet_config.json` pointing at the devnet into a new wallet directory. | @@ -279,7 +279,7 @@ With `--json`, a command prints one JSON document and nothing else on stdout. No The repo is a GitHub Action. On a Linux runner it installs `logos-devnet`, starts the devnet from your project file and exports the env into the job: ```yaml -- uses: drptbl/logos-devnet@v0.9.1 +- uses: drptbl/logos-devnet@v0.9.2 with: args: --max-txs-per-block 100 # optional, more options for up - run: cargo test # LOGOS_DEVNET_SEQUENCER, LOGOS_PROGRAM_*, LOGOS_DEVNET_ACCOUNTS_*, RISC0_DEV_MODE are set diff --git a/examples/github-actions/devnet.yml b/examples/github-actions/devnet.yml index cfd8cc2..ad36df4 100644 --- a/examples/github-actions/devnet.yml +++ b/examples/github-actions/devnet.yml @@ -21,7 +21,7 @@ jobs: # Installs logos-devnet, starts the devnet from logos-devnet.toml, and exports its env # (LOGOS_DEVNET_SEQUENCER, LOGOS_PROGRAM_*, LOGOS_DEVNET_ACCOUNTS_*, RISC0_DEV_MODE). - id: devnet - uses: drptbl/logos-devnet@v0.9.1 + uses: drptbl/logos-devnet@v0.9.2 - run: cargo test - name: The devnet's logs if: failure() diff --git a/skills/logos-devnet/SKILL.md b/skills/logos-devnet/SKILL.md index 1a03c5e..aba9c11 100644 --- a/skills/logos-devnet/SKILL.md +++ b/skills/logos-devnet/SKILL.md @@ -79,7 +79,7 @@ Each refusal changes nothing. `up` prefixes a program's refusal with `program on 127.0.0.1 is taken` | `up --port `, remembered unless the file sets a fixed `port`. A new project home with `[devnet] port = 0` takes a free one on its first `up`. | | `sessions and the messaging hub need logosctl` | Install the Logos Core CLI, or remove the sessions and `[messaging]`. | | `port on 127.0.0.1, which the messaging hub keeps, is taken` | Stop what holds it, or `messaging rm` and `up` for new ports and peer ids. | -| `the session has , not ` | A session keeps the versions it was made with: `session rm `, then `up` makes it again. | +| `the session has , not ` or `its daemon has no package` | A session keeps the versions it was made with: `session rm `, then `up` makes it again. | | `not a RISC Zero program binary` | Deploy the guest's `.bin` from `cargo risczero build`, not its ELF. | | `no Docker daemon answers` | Start Docker (OrbStack, Docker Desktop or Colima). | | `no block within` / `the sequencer stopped while starting` | Read `logos-devnet logs`. | diff --git a/src/devnet.rs b/src/devnet.rs index cafa35b..f99bd42 100644 --- a/src/devnet.rs +++ b/src/devnet.rs @@ -36,6 +36,9 @@ pub const DEFAULT_FUND: u64 = 10_000_000_000; const READY_TIMEOUT: Duration = Duration::from_secs(180); /// How long `status` waits for the runtime to say whether the sequencer runs. const RUNTIME_CHECK_TIMEOUT: Duration = Duration::from_secs(10); +/// How long `status`, `doctor` and `session ls` wait for a session's daemon to answer: a wedged +/// one would sit out logosctl's 20 s. +const SESSION_CHECK_TIMEOUT: Duration = Duration::from_secs(5); /// How often wallets poll the devnet for a transaction: a block or two. const WALLET_POLL_TIMEOUT: &str = "2s"; @@ -749,17 +752,20 @@ impl Devnet { /// Restarts each of `names`' daemons, in order, and brings it back as `up` does: a fresh /// start for modules that keep state while loaded, e.g. `libp2p_module`, which advertises one - /// service per load. Their packages, keys and funded wallets stay. Every name is checked - /// first, so a typo restarts none. + /// service per load. Their packages, keys and funded wallets stay. Every session is checked + /// first, so a typo or a broken one restarts none. pub fn session_restart(&self, names: &[String]) -> Result { + // A session that can't come back stays as it is, and so do the others. let sessions = names .iter() - .map(|name| self.existing_session(name)) + .map(|name| { + let session = self.existing_session(name)?; + session.info()?; + Ok(session) + }) .collect::>>()?; self.require_running()?; for session in &sessions { - // A session that can't come back stays as it is. - session.info()?; session.logosctl().stop()?; self.resume_session(session, false) .with_context(|| format!("restarting session {}", session.name))?; @@ -788,7 +794,7 @@ impl Devnet { fn sessions_not_running(&self) -> Vec { self.sessions() .into_iter() - .filter(|s| !s.logosctl().running()) + .filter(|s| !s.logosctl().answers_within(SESSION_CHECK_TIMEOUT)) .map(|s| s.name) .collect() } @@ -997,6 +1003,15 @@ impl Devnet { }) } + /// The parts of the hub that should run and don't, or `None` when the devnet has no hub or + /// no logosctl to ask it with. + pub fn hub_down(&self) -> Option> { + let hub = self.hub(); + let (messaging, storage) = (hub.runs_messaging(), hub.runs_storage()); + ((messaging || storage) && session::logosctl_on_path()) + .then(|| hub.health().down(messaging, storage)) + } + fn storage_status(&self) -> Option { let hub = self.hub(); hub.runs_storage().then(|| report::Storage { @@ -1106,23 +1121,31 @@ impl Devnet { let mut info = session.info()?; let ctl = session.logosctl(); let had = info.modules.clone(); - let missing = - session::missing_modules(&mut info.modules, extra, |m| ctl.installed_version(m))?; + // Started only when needed: to ask it what a pin names, or to install. `up` may get + // here before it resumes the session. + let wake = || -> Result<()> { + if session.start_daemon(&ctl, self.settings.dev_mode, &self.session_storage())? { + // A daemon that started now has nothing loaded: bring the session back first. + ctl.resume( + &had, + &session.wallet_config(), + &session.wallet_storage(), + &self.settings.sequencer_url(), + )?; + } + Ok(()) + }; + let missing = session::missing_modules(&mut info.modules, extra, |m| { + wake()?; + ctl.installed_version(m) + })?; if info.modules != had { session.save_info(&info)?; } if missing.is_empty() { return Ok(missing); } - if session.start_daemon(&ctl, self.settings.dev_mode, &self.session_storage())? { - // A daemon that started now has nothing loaded: bring the session back first. - ctl.resume( - &info.modules, - &session.wallet_config(), - &session.wallet_storage(), - &self.settings.sequencer_url(), - )?; - } + wake()?; // Recorded one at a time, so a module that fails keeps the ones before it. for module in &missing { let one = std::slice::from_ref(module); @@ -1132,6 +1155,14 @@ impl Devnet { session.save_info(&info)?; ctl.load(one)?; } + // An install that upgraded what other modules depend on unloads them, and an upgraded + // lez_core comes back with its wallet closed: bring the whole session back. + ctl.resume( + &info.modules, + &session.wallet_config(), + &session.wallet_storage(), + &self.settings.sequencer_url(), + )?; self.join_hub(&ctl)?; Ok(missing) } @@ -1176,7 +1207,7 @@ impl Devnet { let rows = session::list(&self.home.sessions_dir())? .into_iter() .map(|session| { - let running = session.logosctl().running(); + let running = session.logosctl().answers_within(SESSION_CHECK_TIMEOUT); match session.info() { Ok(info) => SessionRow { balance: chain_up @@ -1886,9 +1917,21 @@ mod tests { let devnet = devnet_in(&dir.path().join("home")); let err = devnet.session_restart(&["nobody".into()]).unwrap_err(); assert!(err.to_string().contains("no session nobody"), "{err}"); - std::fs::create_dir_all(devnet.home.sessions_dir().join("alice")).unwrap(); + let alice = devnet.home.sessions_dir().join("alice"); + std::fs::create_dir_all(&alice).unwrap(); + let info = format!(r#"{{"modules":["lez_core"],"account":"{ID}","funded":1}}"#); + std::fs::write(alice.join("session.json"), info).unwrap(); let err = devnet.session_restart(&["alice".into()]).unwrap_err(); assert!(err.to_string().contains("not running"), "{err}"); + // A session that couldn't come back is found before any restarts. + std::fs::create_dir_all(devnet.home.sessions_dir().join("bob")).unwrap(); + let err = devnet + .session_restart(&["alice".into(), "bob".into()]) + .unwrap_err(); + assert!( + format!("{err:#}").contains("session bob is incomplete"), + "{err:#}" + ); // Every name is checked before any session restarts. let err = devnet .session_restart(&["alice".into(), "nobody".into()]) diff --git a/src/doctor.rs b/src/doctor.rs index 12fc207..ff23099 100644 --- a/src/doctor.rs +++ b/src/doctor.rs @@ -133,6 +133,23 @@ pub fn run(devnet: &Devnet, project_error: Option<&anyhow::Error>) -> Doctor { ) }); + // The hub stops with the chain, so like sessions it's only news while the chain runs. + if running && let Some(down) = devnet.hub_down() { + checks.push(if down.is_empty() { + pass("hub", "running") + } else { + fail( + "hub", + format!( + "its {} {} running", + down.join(" and "), + if down.len() == 1 { "isn't" } else { "aren't" } + ), + "`logos-devnet up` starts it", + ) + }); + } + let stopped = devnet.stopped_sessions(running); if !stopped.is_empty() { checks.push(fail( diff --git a/src/messaging.rs b/src/messaging.rs index de59419..5470821 100644 --- a/src/messaging.rs +++ b/src/messaging.rs @@ -130,6 +130,23 @@ pub struct Health { pub storage: bool, } +impl Health { + /// The parts that should run and don't, for a hub that runs messaging, storage or both. + pub fn down(&self, messaging: bool, storage: bool) -> Vec<&'static str> { + if !self.daemon { + return vec!["daemon"]; + } + [ + (messaging && !self.delivery, "delivery node"), + (messaging && !self.discovery, "discovery node"), + (storage && !self.storage, "storage node"), + ] + .into_iter() + .filter_map(|(down, part)| down.then_some(part)) + .collect() + } +} + impl Hub { pub fn new(dir: PathBuf, packages: PathBuf) -> Self { let ctl = Logosctl::new(dir.join("logosctl")); @@ -706,6 +723,35 @@ esac"# hub } + #[test] + fn names_the_parts_of_the_hub_that_are_down() { + let all = Health { + daemon: true, + delivery: true, + discovery: true, + storage: true, + }; + assert!(all.down(true, true).is_empty()); + let no_storage = Health { + storage: false, + ..all + }; + assert_eq!(no_storage.down(true, true), ["storage node"]); + assert!( + no_storage.down(true, false).is_empty(), + "storage wasn't asked for" + ); + let no_discovery = Health { + discovery: false, + ..all + }; + assert_eq!(no_discovery.down(true, false), ["discovery node"]); + let no_daemon = Health { + daemon: false, + ..all + }; + assert_eq!(no_daemon.down(true, true), ["daemon"]); + } #[test] fn a_storage_hub_records_its_bootstrap_and_runs_no_messaging_nodes() { let dir = tempfile::tempdir().unwrap(); diff --git a/src/session.rs b/src/session.rs index 06a0e29..d32eee1 100644 --- a/src/session.rs +++ b/src/session.rs @@ -180,8 +180,8 @@ pub fn list(dir: &Path) -> Result> { /// The modules `extra` adds (as `modules_with` takes them) that a session with `have` lacks, in /// order. A module left out of `extra` is not removed. A session keeps the version it /// installed: asking for another one is refused, since only a new session takes it. A pin on a -/// module the session installed unpinned is checked against `installed`, the version its daemon -/// reports, and recorded in `have` when it matches. +/// module the session installed unpinned, or got through another module, is checked against +/// `installed`, the version its daemon reports, and recorded in `have` when it matches. pub fn missing_modules( have: &mut [String], extra: &[String], @@ -197,21 +197,27 @@ pub fn missing_modules( } continue; }; - let Some(at) = have.iter().position(|h| split_module(h).0 == name) else { - missing.push(module); - continue; - }; - let had = match pinned_version(&have[at]) { - Some(had) => had.to_owned(), - None => installed(name)?.unwrap_or_default(), + let at = have.iter().position(|h| split_module(h).0 == name); + // Listed unpinned, or brought along by another module: the daemon knows its version. + let had = match at.and_then(|at| pinned_version(&have[at])) { + Some(had) => Some(had.to_owned()), + None => installed(name)?, }; - if had != version { - bail!( + match (at, had) { + (None, None) => missing.push(module), + (_, Some(had)) if had != version => bail!( "the session has {name} {had}, not {version}: `logos-devnet session rm` it, and \ `up` makes it again with {module}" - ); + ), + // There at that version through another module: recorded and loaded, and + // `install` finds nothing to change. + (None, Some(_)) => missing.push(module), + (Some(at), Some(_)) => pinned.push((at, module.clone())), + (Some(_), None) => bail!( + "the session lists {name}, but its daemon has no {name} package: \ + `logos-devnet session rm` it, and `up` makes it again with {module}" + ), } - pinned.push((at, module.clone())); } // Recorded only once every pin checks out, so a refusal changes nothing. for (at, module) in pinned { @@ -432,7 +438,8 @@ impl Logosctl { Ok(()) } - /// Installs what `package install ` would, package by package, from `packages`. + /// Installs what `package install ` would, package by package, from `packages`, + /// leaving alone what the daemon already has at that version. fn install_one(&self, module: &str, packages: &Path) -> Result<()> { let mut plan = vec!["package", "install", split_module(module).0]; if let Some(version) = pinned_version(module) { @@ -449,6 +456,11 @@ impl Logosctl { else { continue; }; + // Already there at that version. Installed again from a file, it would count as a + // reinstall, which unloads the modules that depend on it and leaves them so. + if change["action"] == "installed" { + continue; + } check_module_name(name)?; check_version(version)?; self.install_package(name, version, packages)?; @@ -867,6 +879,20 @@ esac"#, assert!(format!("{err:#}").contains("session rm"), "{err:#}"); } + #[test] + fn a_pin_on_what_a_session_has_through_another_module_asks_the_daemon() { + // chat_module brought delivery_module 0.3.3 along. + let have = ["lez_core", "chat_module"]; + let (missing, _) = reconcile(&have, &["delivery_module@0.3.2"], Some("0.3.3")); + let err = missing.unwrap_err(); + assert!( + format!("{err:#}").contains("has delivery_module 0.3.3"), + "{err:#}" + ); + let (missing, _) = reconcile(&have, &["delivery_module@0.3.3"], Some("0.3.3")); + assert_eq!(missing.unwrap(), ["delivery_module@0.3.3"]); + } + #[test] fn a_pin_on_what_a_session_installed_unpinned_asks_the_daemon() { let have = ["lez_core", "chat_module"]; @@ -1349,6 +1375,29 @@ esac"#, assert_eq!(parts, 1, "nothing of this download is kept"); } + #[test] + fn leaves_alone_what_a_daemon_already_has() { + let dir = tempfile::tempdir().unwrap(); + let sha = crate::binary::sha256_hex(b"chat_module-0.3.0"); + let ctl = fake_logosctl( + dir.path(), + &format!( + r#"case "$2 $3 $4" in + "package install chat_module") echo '{{"changes":[{{"name":"delivery_module","action":"installed","toVersion":"0.3.3"}},{{"name":"chat_module","action":"install","toVersion":"0.3.0"}}]}}' ;; + "package show chat_module") echo '{{"versions":[{{"manifest":{{"version":"0.3.0"}},"sha256":"{sha}"}}]}}' ;; + "package download "*) printf '%s' "$4-$6" > "$8/$4-$6.lgx"; echo '{{"status":"ok"}}' ;; + *) echo '{{"status":"ok"}}' ;; +esac"# + ), + ); + ctl.install(&["chat_module".into()], &dir.path().join("packages")) + .unwrap(); + let log = calls(dir.path()); + // Installing it again from a file would unload what depends on it, e.g. chat_module. + assert!(!log.contains("delivery_module"), "{log}"); + assert!(log.contains("chat_module-0.3.0.lgx --yes"), "{log}"); + } + #[test] fn takes_only_plain_versions() { assert!(check_version("0.5.1").is_ok());