Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/plain-ports-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cartesi/mock-verifying-paymaster": patch
---

listen on the port given by `PORT`, 3001 by default
5 changes: 5 additions & 0 deletions .changeset/quiet-hosts-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cartesi/cli": minor
---

run the local environment of `cartesi run` as processes of the host instead of docker containers: anvil, a throwaway postgres (from `@embedded-postgres`), the rollups node, the PRT nodes and the optional services, behind a proxy served by the CLI, with everything they keep removed when the environment stops. The explorer service, which only exists as a docker image, is no longer available, and `--cpus` and `--memory` are ignored
4 changes: 2 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ The CLI build pipeline (`apps/cli`): `clean` → `codegen` (wagmi ABI generation

- **`commands/`** — Each file exports a `create*Command()` function returning a Commander command. Main commands: `build`, `run`, `deploy`, `send`, `deposit`, `create`, `doctor`, `shell`, `clean`, `hash`, `logs`, `status`, `address-book`, `foreclose`, `refund`, `withdraw`.
- **`builder/`** — Drive builder implementations (directory, docker, tar, empty, none). Each builder produces ext2 or SquashFS filesystems for Cartesi Machine drives.
- **`compose/`** — Docker Compose service definitions generated as TypeScript objects (anvil, node, bundler, database, paymaster, proxy, explorer, etc.).
- **`exec/`** — Wrappers around subprocess execution (cartesi-machine, rollups) using `execa`.
- **`host/`** — The local environment of `cartesi run`, as processes of the host (no docker): `environment.ts` starts and stops it, `process.ts` runs each service in its own process group with its output in a log file, `state.ts` keeps what other commands read (`environment.json` in a directory of the system temporary directory, removed on stop), `proxy.ts` serves every service behind one port, and `anvil.ts`, `postgres.ts` (the binaries of `@embedded-postgres`), `node.ts`, `prt-node.ts` and `services.ts` describe the services.
- **`exec/`** — Wrappers around subprocess execution (cartesi-machine, rollups) using `execa`. `node-container.ts` runs `cartesi-rollups-cli` and the other node tools with the environment of the running node.
- **`config.ts`, `config/`** — `config.ts` is the barrel the rest of the CLI imports; `config/` holds the types, defaults, errors and the parsers. An application is configured by a `cartesi.config.ts` file exporting `defineConfig({ ... })`, or by the same configuration as plain data in `cartesi.config.json`, `cartesi.config.yaml` or `cartesi.config`; `config/load.ts` finds and reads it, `config/normalize.ts` validates it, and `config/toml/` parses the deprecated `cartesi.toml`.
- **`contracts.ts`** — Generated contract addresses and ABI bindings (via `@wagmi/cli`).
- **`wallet.ts`** — Wallet utilities using `viem` for Ethereum interaction.
Expand Down
40 changes: 37 additions & 3 deletions apps/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@ cartesi --help

More documentation at [https://docs.cartesi.io](https://docs.cartesi.io).

## Local development environment

`cartesi run` runs the local environment as processes of the host, with no
docker: anvil for the devnet, a throwaway postgres for the node, the rollups
node, and the PRT nodes and optional services it is asked for, behind a single
port the CLI serves itself. `cartesi build` still uses docker to build the
drives of the machine.

It needs these tools in the `PATH`. `cartesi doctor` checks each one, and its
version; `cartesi run` names every missing one before it starts anything, and
warns of a version it is not tested with:

| Tool | Package |
| ------------------------------------------------------------------------ | ------------------------------------------------------------------------------- |
| `anvil` | [foundry](https://getfoundry.sh) 1.5.1 |
| `cartesi-rollups-node`, `cartesi-rollups-cli` | [rollups-node](https://github.com/cartesi/rollups-node/releases) 2.0.0-alpha.14 |
| `cartesi-jsonrpc-machine`, `cartesi-rollups-machine-tool` | [machine-emulator](https://github.com/cartesi/machine-emulator/releases) 0.22.0 |
| `cartesi-sling-node` (with `--prt` and a sling node) | [dave](https://github.com/cartesi/dave/releases) 3.0.0-alpha.6 |
| `alto`, `mock-verifying-paymaster`, `passkey-server` (with `--services`) | npm |
| `initdb`, `postgres` (standalone binary only) | postgres 18 |

The npm package ships postgres, through `@embedded-postgres`; the standalone
binaries use the one of the host. The devnet, a state dump of anvil with the
Cartesi contracts deployed, is downloaded on first use, checked against its
checksum, and cached; `CARTESI_DEVNET_STATE` points at another one.

Everything else the environment keeps (the database, the logs, the data of the
PRT nodes and of the withdrawals) lives in one directory of the temporary
directory of the system, removed when the environment stops: every process of
the environment is stopped along with the CLI that started it, which owns it.
A CLI that is killed (`kill -9`) can't clean up, and the next `cartesi run` of
the project stops what it left behind. The project itself is only read.

## Configuration

An application is configured by a `cartesi.config.ts` file at its root, which
Expand Down Expand Up @@ -185,16 +218,17 @@ A few things to keep in mind:
`progress: "default"` (or `"verbose"`) to get the same output as the CLI;
- functions throw on error, and never terminate the process;
- `run` returns a handle of the environment, which keeps running in the
background until `stop()` is called. Use `deploy()` to redeploy the
application after a rebuild;
background until `stop()` is called, and keeps the process up until then.
Its processes are stopped when the process exits, if `stop()` was not
called. Use `deploy()` to redeploy the application after a rebuild;
- `send` and the `deposit*` functions resolve the application address, the
sender and the RPC URL from the running project, and never prompt for them.
Amounts are given in the base unit of the asset as a `bigint`, or in its
display unit as a string (`"1.5"`). A deposit that cannot be made throws a
`DepositError` (`InsufficientBalanceError`, `InvalidAmountError` or
`TokenNotFoundError`);
- `foreclose`, `refund` and `withdraw` send their transaction with
cartesi-rollups-cli in the rollups node container, without asking for
cartesi-rollups-cli, with the environment of the rollups node, without asking for
confirmation, and return its JSON output. With `stream: true` they attach
it to the terminal instead, as the CLI does.

Expand Down
23 changes: 19 additions & 4 deletions apps/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@ const define = {
),
};

// the postgres binaries of the environment come in a package per platform,
// which can't be bundled: they are found at runtime, in node_modules, and are
// not there for the standalone binaries, which use the postgres of the host
const postgres = [
"@embedded-postgres/darwin-arm64",
"@embedded-postgres/darwin-x64",
"@embedded-postgres/linux-arm64",
"@embedded-postgres/linux-x64",
];

// build for npm package: the CLI entrypoint (executable) and the library entrypoint
await Bun.build({
banner: "#!/usr/bin/env node",
define,
entrypoints: ["./src/index.ts"],
external: postgres,
minify: true,
outdir: "dist",
sourcemap: true,
Expand All @@ -23,10 +34,13 @@ await Bun.build({
// instance of the consumer's viem classes, and viem isn't shipped twice. Keep
// in sync with the dependencies of package.json. The CLI and the binaries stay
// self-contained
const external = ["execa", "semver", "smol-toml", "viem"].flatMap((name) => [
name,
`${name}/*`,
]);
const external = [
...["execa", "semver", "smol-toml", "viem"].flatMap((name) => [
name,
`${name}/*`,
]),
...postgres,
];

await Bun.build({
define,
Expand Down Expand Up @@ -56,6 +70,7 @@ await Promise.all(
},
define,
entrypoints: ["./src/index.ts"],
external: postgres,
minify: true,
sourcemap: "linked",
target: "bun",
Expand Down
6 changes: 6 additions & 0 deletions apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
"smol-toml": "^1.9.0",
"viem": "^2.57.3"
},
"optionalDependencies": {
"@embedded-postgres/darwin-arm64": "18.7.0-beta.19",
"@embedded-postgres/darwin-x64": "18.7.0-beta.19",
"@embedded-postgres/linux-arm64": "18.7.0-beta.19",
"@embedded-postgres/linux-x64": "18.7.0-beta.19"
},
"devDependencies": {
"@cartesi/wagmi-plugin": "1.0.0-alpha.9",
"@commander-js/extra-typings": "^15.0.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/src/api/address-book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type AddressBook, getAddressBook, getProjectName } from "../base.js";

export type AddressBookOptions = {
/**
* Name of the project (used by docker compose and cartesi-rollups-node).
* Name of the project (used by cartesi-rollups-node).
* @default basename of the current working directory
*/
projectName?: string;
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/src/api/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type ConnectionOptions = {
from?: Address;

/**
* Name of the project (used by docker compose and cartesi-rollups-node).
* Name of the project (used by cartesi-rollups-node).
* @default basename of the current working directory
*/
projectName?: string;
Expand Down
99 changes: 69 additions & 30 deletions apps/cli/src/api/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { execa } from "execa";
import semver from "semver";
import { DEFAULT_SDK_IMAGE, DEFAULT_SDK_VERSION } from "../config.js";
import { cartesiMachine } from "../exec/index.js";
import { findPostgres } from "../host/postgres.js";
import { checkTool, HOST_TOOLS } from "../host/tools.js";

const MINIMUM_DOCKER_VERSION = "25.0.0"; // Replace with our minimum required Docker version
const MINIMUM_DOCKER_COMPOSE_VERSION = "2.24.0"; // Replace with our minimum required Docker Compose version
const MINIMUM_BUILDX_VERSION = "0.13.0"; // Replace with our minimum required Buildx version

const COMPOSE_NOT_INSTALLED =
"Docker Compose is required but not installed or the command execution failed. Please refer to the Docker Compose documentation for installation instructions: https://docs.docker.com/compose/install/";
const BUILDX_NOT_INSTALLED =
"Docker Buildx is required but not installed. Please refer to the Docker Desktop documentation for installation instructions: https://docs.docker.com/desktop/";

Expand All @@ -17,11 +16,11 @@ export type DoctorCheck = {
name: string;

/**
* Whether the requirement is satisfied (`ok`), is not (`failed`), or could
* not be checked because a requirement it depends on is not satisfied
* (`skipped`).
* Whether the requirement is satisfied (`ok`), is not (`failed`), is
* optional and not satisfied (`warning`), or could not be checked because
* a requirement it depends on is not satisfied (`skipped`).
*/
status: "ok" | "failed" | "skipped";
status: "ok" | "failed" | "skipped" | "warning";

/** Detail of a satisfied requirement, usually the installed version. */
detail?: string;
Expand All @@ -31,7 +30,7 @@ export type DoctorCheck = {
};

export type DoctorResult = {
/** Whether all requirements are satisfied. */
/** Whether all requirements are satisfied, the optional ones aside. */
ok: boolean;

/** Every requirement checked, satisfied or not. */
Expand Down Expand Up @@ -92,21 +91,6 @@ const checkDocker = async (): Promise<string | undefined> => {
return v?.version;
};

const checkCompose = async (): Promise<string | undefined> => {
const dockerComposeVersion = await docker(
["compose", "version", "--short"],
COMPOSE_NOT_INSTALLED,
);

const v = semver.coerce(dockerComposeVersion);
if (v !== null && !semver.gte(v, MINIMUM_DOCKER_COMPOSE_VERSION)) {
throw new Error(
`Unsupported Docker Compose version. Minimum required version is ${MINIMUM_DOCKER_COMPOSE_VERSION}. Installed version is ${v}.`,
);
}
return dockerComposeVersion;
};

const checkBuildx = async (): Promise<string | undefined> => {
const buildxOutput = await docker(
["buildx", "version"],
Expand Down Expand Up @@ -160,15 +144,43 @@ const checkCartesiMachine = async (): Promise<string | undefined> => {
return v.format();
};

/** An optional requirement that is not satisfied. */
class DoctorWarning extends Error {}

/**
* Check a tool `cartesi run` runs on the host: installed, and of a supported
* version. A tool not every run needs is only a warning.
*/
const checkHostTool =
(command: string | (() => Promise<string>)) =>
async (): Promise<string | undefined> => {
const resolved =
typeof command === "string" ? command : await command();
const check = await checkTool(resolved);
const tool = HOST_TOOLS[check.name];
const optional = tool?.neededFor !== undefined;
const fail = (message: string) => {
throw optional ? new DoctorWarning(message) : new Error(message);
};
if (!check.path) {
fail(
`${check.name} not installed, which ${tool?.neededFor ?? "cartesi run"} needs: install ${tool?.install ?? check.name}`,
);
}
if (check.warning) {
fail(check.warning);
}
return check.version;
};

/** The postgres of the environment: the embedded one, or the host's. */
const postgresBinary = async () => (await findPostgres()).postgres;
const initdbBinary = async () => (await findPostgres()).initdb;

// the Cartesi Machine check requires nothing, as a host cartesi-machine
// satisfies it without docker
const checks: Check[] = [
{ name: "Docker Engine", run: checkDocker },
{
name: "Docker Compose",
requires: ["Docker Engine"],
run: checkCompose,
},
{
name: "Docker Buildx",
requires: ["Docker Engine"],
Expand All @@ -180,6 +192,31 @@ const checks: Check[] = [
run: checkRiscv,
},
{ name: "Cartesi Machine", run: checkCartesiMachine },
// cartesi run
{ name: "anvil", run: checkHostTool("anvil") },
{
name: "cartesi-rollups-node",
run: checkHostTool("cartesi-rollups-node"),
},
{ name: "cartesi-rollups-cli", run: checkHostTool("cartesi-rollups-cli") },
{
name: "cartesi-jsonrpc-machine",
run: checkHostTool("cartesi-jsonrpc-machine"),
},
{ name: "postgres", run: checkHostTool(postgresBinary) },
{ name: "initdb", run: checkHostTool(initdbBinary) },
// what only some commands, or some runs, need
{
name: "cartesi-rollups-machine-tool",
run: checkHostTool("cartesi-rollups-machine-tool"),
},
{ name: "cartesi-sling-node", run: checkHostTool("cartesi-sling-node") },
{ name: "alto", run: checkHostTool("alto") },
{
name: "mock-verifying-paymaster",
run: checkHostTool("mock-verifying-paymaster"),
},
{ name: "passkey-server", run: checkHostTool("passkey-server") },
];

/**
Expand Down Expand Up @@ -227,7 +264,7 @@ export const doctor = async (): Promise<DoctorResult> => {
} catch (e: unknown) {
return {
name,
status: "failed",
status: e instanceof DoctorWarning ? "warning" : "failed",
message: e instanceof Error ? e.message : String(e),
};
}
Expand All @@ -236,7 +273,9 @@ export const doctor = async (): Promise<DoctorResult> => {

const resolved = skipUnmetRequirements(checks, results);
return {
ok: resolved.every((check) => check.status === "ok"),
ok: resolved.every(
(check) => check.status === "ok" || check.status === "warning",
),
checks: resolved,
};
};
Loading
Loading