diff --git a/Cargo.lock b/Cargo.lock index 1a3e460..ee095b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -473,7 +473,7 @@ checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" [[package]] name = "logos-devnet" -version = "0.9.2" +version = "0.9.3" dependencies = [ "anyhow", "bs58", diff --git a/Cargo.toml b/Cargo.toml index a1c5d59..4487160 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "logos-devnet" -version = "0.9.2" +version = "0.9.3" 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 3fc83b3..748f9c4 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.2 t=aarch64-apple-darwin +v=v0.9.3 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 \ @@ -101,7 +101,7 @@ modules = ["chat_module"] # or "chat_module@0.3.0" for that version A program can come from a release instead of a path: `up` downloads its `url` once, with `curl`, into `~/.logos-devnet/downloads`, which every home shares, and deploys it only if its SHA-256 is the `sha256` given (as `shasum -a 256` prints it). -`up` deploys missing programs, makes and funds missing accounts and sessions, and changes nothing the second time (0.6 s). A `mutable` program whose binary changed is updated in place, with the same id. An immutable one keeps its old build, and `up` and `deploy` fail saying so, so tests don't run against it: `reset` deploys the new build. A module added to a session's `modules` is installed into it. Modules come from the catalog at its latest version, `lez_core` at `0.5.1`, unless the file names one: `chat_module@0.3.0`. A session keeps the versions it was made with, so `up` refuses another version for an existing one: `session rm` it, and `up` makes it again. A pin on what it installed unpinned is checked against the version its daemon reports, and recorded when it matches. `up` checks the whole file before it touches the devnet. +`up` deploys missing programs, makes and funds missing accounts and sessions, and changes nothing the second time (0.6 s). A `mutable` program whose binary changed is updated in place, with the same id. An immutable one keeps its old build, and `up` and `deploy` fail saying so, so tests don't run against it: `reset` deploys the new build. A module added to a session's `modules` is installed into it. Modules come from the catalog at its latest version, `lez_core` at `0.5.1`, unless the file names one: `chat_module@0.3.0`. A session keeps the versions it was made with, so `up` refuses another version for an existing one: `session rm` it, and `up` makes it again. A pin on what it installed unpinned is checked against the version its daemon reports, and recorded when it matches. `up` checks the whole file before it touches the devnet. A stopped sequencer replays its chain's history before it serves, which takes a while on a long chain (166k blocks took about 15 minutes): `up` waits as long as its log grows, up to an hour, and says so. Projects share one devnet, `~/.logos-devnet` on port 3040, unless the file sets `home`, e.g. `home = ".logos-devnet"`, for a devnet of the project's own. Downloads stay shared. Give it `[devnet] port = 0` too: it takes a free port on the first `up` and keeps it, so it runs next to other devnets, and `logos-devnet env` shows it. With a fixed `port` in the file, `--port` applies for that run, and the next `up` goes back to the file's port. With `port = 0`, `--port` is remembered. On a shared devnet, a `[devnet]` section that disagrees with the running chain is refused rather than restarting the chain under the other projects: `down` and `up` apply it, or `reset` for `lez` and `dev_mode`, which only a new chain takes. A `reset` there funds every project's account groups again, and each project's next `up` deploys its programs on the new chain. @@ -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.2 +- uses: drptbl/logos-devnet@v0.9.3 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 ad36df4..35617d3 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.2 + uses: drptbl/logos-devnet@v0.9.3 - 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 aba9c11..7cb8579 100644 --- a/skills/logos-devnet/SKILL.md +++ b/skills/logos-devnet/SKILL.md @@ -82,7 +82,7 @@ Each refusal changes nothing. `up` prefixes a program's refusal with `program , 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`. | +| `no block, and nothing in the sequencer's log` / `the sequencer stopped while starting` | Read `logos-devnet logs`. A long chain loading is no error: `up` waits as long as the log grows. | ## Debugging diff --git a/src/devnet.rs b/src/devnet.rs index f99bd42..d81b28f 100644 --- a/src/devnet.rs +++ b/src/devnet.rs @@ -32,8 +32,17 @@ const FAUCET_BALANCE: u64 = 1_000_000_000_000_000_000; const FAUCET_LABEL: &str = "faucet"; /// What `fund`, sessions and account groups send when no amount is given. pub const DEFAULT_FUND: u64 = 10_000_000_000; -/// How long `up` waits for the first block. -const READY_TIMEOUT: Duration = Duration::from_secs(180); +/// How `up` waits for a starting sequencer's first block. A long chain loads for minutes first +/// (166k blocks took 15), logging as it goes, so `up` waits as long as the log grows. +const READY_WAIT: ReadyWait = ReadyWait { + quiet: Duration::from_secs(180), + limit: Duration::from_secs(3600), + poll: Duration::from_millis(500), + log_every: Duration::from_secs(10), +}; +/// When `up` says it's still waiting, and then how often. +const LOADING_NOTICE: Duration = Duration::from_secs(30); +const LOADING_NOTICE_EVERY: Duration = Duration::from_secs(120); /// 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 @@ -858,22 +867,14 @@ impl Devnet { fn wait_ready(&self) -> Result<()> { let rpc = self.rpc(); - let deadline = Instant::now() + READY_TIMEOUT; - loop { - if rpc.last_block_id().is_ok_and(|head| head >= 1) { - return Ok(()); - } - if !self.runtime.running() { - bail!("the sequencer stopped while starting. See `logos-devnet logs`"); - } - if Instant::now() > deadline { - bail!( - "no block within {}s. See `logos-devnet logs`", - READY_TIMEOUT.as_secs() - ); - } - std::thread::sleep(Duration::from_millis(500)); - } + wait_for_blocks( + &Startup { + serves: &|| rpc.last_block_id().is_ok_and(|head| head >= 1), + running: &|| self.runtime.running(), + log_mark: &|| self.runtime.log_mark(), + }, + &READY_WAIT, + ) } /// Writes the env file: `env`'s sequencer and programs, and every account group. @@ -1834,6 +1835,87 @@ fn labelled(list: &str, label: &str) -> Option { .find_map(first_public_id) } +/// How long `wait_for_blocks` waits. +struct ReadyWait { + /// The longest the sequencer's log may go without a new line. + quiet: Duration, + /// The longest it waits in all, however busy the log. + limit: Duration, + /// How often it asks for a block. + poll: Duration, + /// How often it looks at the log. + log_every: Duration, +} + +/// What `wait_for_blocks` watches while the sequencer starts. +struct Startup<'a> { + /// Whether it serves a block. + serves: &'a dyn Fn() -> bool, + /// Whether it still runs. + running: &'a dyn Fn() -> bool, + /// Something that changes whenever it writes to its log, e.g. its last line. + log_mark: &'a dyn Fn() -> Option, +} + +/// Waits until the sequencer serves a block. It may take as long as it keeps writing to its +/// log, up to `wait.limit`: it fails after `wait.quiet` without a new line, or when the +/// sequencer stops. A log that can't be read counts as busy, since not knowing isn't quiet. +fn wait_for_blocks(startup: &Startup, wait: &ReadyWait) -> Result<()> { + let start = Instant::now(); + let mut mark = (startup.log_mark)(); + let mut last_change = start; + let mut last_check = start; + let mut next_notice = LOADING_NOTICE; + loop { + if (startup.serves)() { + return Ok(()); + } + if !(startup.running)() { + bail!("the sequencer stopped while starting. See `logos-devnet logs`"); + } + if last_check.elapsed() >= wait.log_every { + last_check = Instant::now(); + let now = (startup.log_mark)(); + if now.is_none() || now != mark { + mark = now; + last_change = last_check; + } + } + if last_change.elapsed() > wait.quiet { + bail!( + "no block, and nothing in the sequencer's log for {} s. See `logos-devnet logs`", + wait.quiet.as_secs() + ); + } + let waited = start.elapsed(); + if waited > wait.limit { + bail!( + "no block within {} min, though the sequencer keeps logging. See `logos-devnet \ + logs`", + wait.limit.as_secs() / 60 + ); + } + if waited >= next_notice { + eprintln!( + "No block yet after {}: a long chain takes a while to load. Waiting as long as \ + the sequencer keeps logging, up to {} min; `logos-devnet logs -f` shows it.", + duration_text(waited), + wait.limit.as_secs() / 60 + ); + next_notice += LOADING_NOTICE_EVERY; + } + std::thread::sleep(wait.poll); + } +} + +/// `30 s`, or `4 min` from two minutes on. +fn duration_text(d: Duration) -> String { + match d.as_secs() { + s if s < 120 => format!("{s} s"), + s => format!("{} min", s / 60), + } +} + /// A logosctl daemon's log, in its config directory. fn daemon_log(logosctl_dir: &Path) -> PathBuf { logosctl_dir.join("logs").join("daemon.log") @@ -2038,6 +2120,73 @@ mod tests { ); } + /// How a sequencer behaves while it starts. + enum Log { + Busy, + Quiet, + Unreadable, + } + + /// A sequencer that serves its first block after `ready`, and stops after `stops` if given. + fn starting_for(ready: Duration, log: Log, stops: Option) -> Result<()> { + let start = Instant::now(); + let serves = move || start.elapsed() >= ready; + let running = move || stops.is_none_or(|at| start.elapsed() < at); + let log_mark = move || match log { + Log::Busy => Some(format!("{:?}", start.elapsed())), + Log::Quiet => Some("the same line".to_owned()), + Log::Unreadable => None, + }; + wait_for_blocks( + &Startup { + serves: &serves, + running: &running, + log_mark: &log_mark, + }, + &ReadyWait { + quiet: Duration::from_millis(200), + limit: Duration::from_millis(1500), + poll: Duration::from_millis(10), + log_every: Duration::from_millis(10), + }, + ) + } + + #[test] + fn waits_while_a_long_chain_loads() { + // Replaying 166k blocks took 15 min, far past the quiet limit, the log busy throughout. + starting_for(Duration::from_millis(600), Log::Busy, None).unwrap(); + } + + #[test] + fn gives_up_on_a_sequencer_that_went_quiet() { + let err = starting_for(Duration::from_secs(60), Log::Quiet, None).unwrap_err(); + assert!( + err.to_string().contains("nothing in the sequencer's log"), + "{err}" + ); + } + + #[test] + fn waits_on_a_log_it_cant_read_up_to_the_limit() { + starting_for(Duration::from_millis(600), Log::Unreadable, None).unwrap(); + let err = starting_for(Duration::from_secs(60), Log::Busy, None).unwrap_err(); + assert!(err.to_string().contains("keeps logging"), "{err}"); + } + + #[test] + fn gives_up_on_a_sequencer_that_stopped() { + let stops = Some(Duration::from_millis(50)); + let err = starting_for(Duration::from_secs(60), Log::Busy, stops).unwrap_err(); + assert!(err.to_string().contains("stopped while starting"), "{err}"); + } + + #[test] + fn says_how_long_it_waited() { + assert_eq!(duration_text(Duration::from_secs(30)), "30 s"); + assert_eq!(duration_text(Duration::from_secs(250)), "4 min"); + } + #[test] fn logs_name_what_they_cant_show() { let dir = tempfile::tempdir().unwrap(); diff --git a/src/runtime/docker.rs b/src/runtime/docker.rs index 8ef8ea3..c47ebc1 100644 --- a/src/runtime/docker.rs +++ b/src/runtime/docker.rs @@ -274,6 +274,20 @@ impl Runtime for Docker { } run_attached(cmd.arg("sequencer")) } + + fn log_mark(&self) -> Option { + // Its last line, with Docker's timestamp to the nanosecond, so a repeated line counts. + let mut cmd = self.compose(); + cmd.args([ + "logs", + "--tail", + "1", + "--timestamps", + "--no-log-prefix", + "sequencer", + ]); + stdout_within(&mut cmd, Duration::from_secs(10)).ok() + } } /// `logos-devnet` for the default home, and a name derived from the path for any other, so diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index b4a1215..3bbcb28 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -89,6 +89,9 @@ pub trait Runtime { fn wallet_batch(&self, home: &Path, runs: &[Vec]) -> Result>; /// Prints the sequencer's log. fn logs(&self, follow: bool) -> Result<()>; + /// Something that changes whenever the sequencer writes to its log, to tell one still + /// working from one that went quiet. `None` when it can't be read. + fn log_mark(&self) -> Option; } /// Runs `cmd` with stdin closed and returns its stdout. On failure the error carries both diff --git a/src/runtime/native.rs b/src/runtime/native.rs index 03bce58..716f41f 100644 --- a/src/runtime/native.rs +++ b/src/runtime/native.rs @@ -228,6 +228,11 @@ impl Runtime for Native { } run_attached(cmd.arg(self.log_file())) } + + fn log_mark(&self) -> Option { + let size = std::fs::metadata(self.log_file()).ok()?.len(); + Some(size.to_string()) + } } /// Whether a process's command line (as `ps -o command=` prints it) runs `bin`: it starts