diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed0de65d..194646f3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -135,7 +135,37 @@ jobs: env: RISC0_DEV_MODE: "1" RUST_LOG: "info" - run: cargo nextest run --workspace --exclude integration_tests --all-features + run: cargo nextest run --workspace --exclude integration_tests --exclude test_fixtures --all-features + + test-fixtures-tests: + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v5 + with: + ref: ${{ github.event.pull_request.head.sha || github.head_ref }} + + - uses: ./.github/actions/install-system-deps + + - uses: ./.github/actions/install-risc0 + + - name: Install active toolchain + run: rustup install + + - name: Restore Rust cache + uses: Swatinem/rust-cache@v2 + with: + shared-key: ci-rust-cache + save-if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev' }} + + - name: Install nextest + run: cargo install --locked cargo-nextest + + - name: Run test_fixtures tests + env: + RISC0_DEV_MODE: "1" + RUST_LOG: "info" + run: cargo nextest run -p test_fixtures integration-tests-prebuild: runs-on: ubuntu-latest @@ -193,7 +223,7 @@ jobs: echo "Discovered integration targets: $targets_json" integration-tests: - needs: integration-tests-prebuild + needs: [test-fixtures-tests, integration-tests-prebuild] runs-on: ubuntu-latest timeout-minutes: 90 strategy: diff --git a/Cargo.lock b/Cargo.lock index fbe52a3c..d6afde36 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1154,6 +1154,25 @@ dependencies = [ "serde", ] +[[package]] +name = "bridge_lock_core" +version = "0.1.0" +dependencies = [ + "lee_core", + "serde", +] + +[[package]] +name = "bridge_lock_program" +version = "0.1.0" +dependencies = [ + "bridge_lock_core", + "cross_zone_outbox_core", + "lee_core", + "risc0-zkvm", + "wrapped_token_core", +] + [[package]] name = "bridge_program" version = "0.1.0" @@ -1833,6 +1852,76 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" +[[package]] +name = "cross_zone" +version = "0.1.0" +dependencies = [ + "bridge_lock_core", + "cross_zone_inbox_core", + "lee", + "lee_core", + "ping_core", + "programs", + "risc0-zkvm", +] + +[[package]] +name = "cross_zone_chat" +version = "0.1.0" +dependencies = [ + "anyhow", + "axum 0.8.9", + "common", + "cross_zone_inbox_core", + "cross_zone_outbox_core", + "env_logger", + "lee", + "log", + "ping_core", + "programs", + "risc0-zkvm", + "sequencer_service_rpc", + "serde", + "test_fixtures", + "tokio", +] + +[[package]] +name = "cross_zone_inbox_core" +version = "0.1.0" +dependencies = [ + "borsh", + "lee_core", + "risc0-zkvm", + "serde", +] + +[[package]] +name = "cross_zone_inbox_program" +version = "0.1.0" +dependencies = [ + "cross_zone_inbox_core", + "lee_core", +] + +[[package]] +name = "cross_zone_outbox_core" +version = "0.1.0" +dependencies = [ + "borsh", + "lee_core", + "risc0-zkvm", + "serde", +] + +[[package]] +name = "cross_zone_outbox_program" +version = "0.1.0" +dependencies = [ + "cross_zone_outbox_core", + "lee_core", +] + [[package]] name = "crossbeam-channel" version = "0.5.15" @@ -1854,9 +1943,9 @@ dependencies = [ [[package]] name = "crossbeam-epoch" -version = "0.9.18" +version = "0.9.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +checksum = "2d6914041f254d6e9176c01941b21115dcfb7089e55135a35411081bd106ef3f" dependencies = [ "crossbeam-utils", ] @@ -2076,7 +2165,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccc2776f0c61eca1ca32528f85548abd1a4be8fb53d1b21c013e4f18da1e7090" dependencies = [ "data-encoding", - "syn 1.0.109", + "syn 2.0.117", ] [[package]] @@ -3851,18 +3940,25 @@ dependencies = [ "async-stream", "borsh", "common", + "cross_zone", + "cross_zone_inbox_core", "futures", + "hex", "humantime-serde", "lee", "lee_core", "log", "logos-blockchain-core", "logos-blockchain-zone-sdk", + "ping_core", + "programs", + "risc0-zkvm", "serde", "serde_json", "storage", "tempfile", "testnet_initial_state", + "thiserror 2.0.18", "tokio", "url", ] @@ -3910,6 +4006,7 @@ dependencies = [ "base64", "common", "hex", + "indexer_core", "lee", "lee_core", "schemars 1.2.1", @@ -4010,8 +4107,11 @@ dependencies = [ "authenticated_transfer_core", "borsh", "bridge_core", + "bridge_lock_core", "bytesize", "common", + "cross_zone_inbox_core", + "cross_zone_outbox_core", "faucet_core", "futures", "hex", @@ -4027,8 +4127,10 @@ dependencies = [ "logos-blockchain-key-management-system-service", "logos-blockchain-zone-sdk", "num-bigint 0.4.6", + "ping_core", "programs", "reqwest", + "risc0-zkvm", "sequencer_core", "sequencer_service_rpc", "serde_json", @@ -4041,6 +4143,7 @@ dependencies = [ "vault_core", "wallet", "wallet-ffi", + "wrapped_token_core", ] [[package]] @@ -7173,6 +7276,31 @@ dependencies = [ "token_core", ] +[[package]] +name = "ping_core" +version = "0.1.0" +dependencies = [ + "lee_core", + "serde", +] + +[[package]] +name = "ping_receiver_program" +version = "0.1.0" +dependencies = [ + "lee_core", + "ping_core", +] + +[[package]] +name = "ping_sender_program" +version = "0.1.0" +dependencies = [ + "cross_zone_outbox_core", + "lee_core", + "ping_core", +] + [[package]] name = "pkcs1" version = "0.7.5" @@ -7445,15 +7573,20 @@ dependencies = [ "associated_token_account_program", "authenticated_transfer_core", "bridge_core", + "bridge_lock_core", "build_utils", "clock_core", + "cross_zone_inbox_core", + "cross_zone_outbox_core", "faucet_core", "lee", "lee_core", + "ping_core", "risc0-zkvm", "token_core", "token_program", "vault_core", + "wrapped_token_core", ] [[package]] @@ -8892,10 +9025,13 @@ dependencies = [ "bytesize", "chrono", "common", + "cross_zone", + "cross_zone_inbox_core", "faucet_core", "futures", "hex", "humantime-serde", + "itertools 0.14.0", "key_protocol", "lee", "lee_core", @@ -9457,11 +9593,13 @@ dependencies = [ "borsh", "common", "lee", + "log", "programs", "rocksdb", "system_accounts", "tempfile", "thiserror 2.0.18", + "zstd", ] [[package]] @@ -9735,6 +9873,7 @@ name = "test_fixtures" version = "0.1.0" dependencies = [ "anyhow", + "bip39", "bytesize", "common", "env_logger", @@ -9754,6 +9893,7 @@ dependencies = [ "tempfile", "testcontainers", "tokio", + "tokio-util", "url", "wallet", ] @@ -9836,6 +9976,7 @@ dependencies = [ "programs", "serde", "system_accounts", + "wrapped_token_core", ] [[package]] @@ -11545,6 +11686,23 @@ dependencies = [ "wasmparser", ] +[[package]] +name = "wrapped_token_core" +version = "0.1.0" +dependencies = [ + "lee_core", + "risc0-zkvm", + "serde", +] + +[[package]] +name = "wrapped_token_program" +version = "0.1.0" +dependencies = [ + "lee_core", + "wrapped_token_core", +] + [[package]] name = "writeable" version = "0.6.3" @@ -11797,3 +11955,31 @@ dependencies = [ "log", "simd-adler32", ] + +[[package]] +name = "zstd" +version = "0.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a" +dependencies = [ + "zstd-safe", +] + +[[package]] +name = "zstd-safe" +version = "7.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d" +dependencies = [ + "zstd-sys", +] + +[[package]] +name = "zstd-sys" +version = "2.0.16+zstd.1.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e19ebc2adc8f83e43039e79776e3fda8ca919132d68a1fed6a5faca2683748" +dependencies = [ + "cc", + "pkg-config", +] diff --git a/Cargo.toml b/Cargo.toml index 34333b5a..164b64cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,13 @@ members = [ "lez/programs/pinata_token", "lez/programs/token", "lez/programs/vault", + "lez/programs/cross_zone_inbox", + "lez/programs/cross_zone_outbox", + "lez/programs/bridge_lock", + "lez/programs/wrapped_token", + "lez/programs/ping_sender", + "lez/programs/ping_receiver", + "lez/cross_zone", "test_programs", "test_programs/guest", @@ -58,6 +65,7 @@ members = [ "tools/cycle_bench", "tools/crypto_primitives_bench", "tools/integration_bench", + "tools/cross_zone_chat", ] [workspace.dependencies] @@ -94,6 +102,12 @@ authenticated_transfer_core = { path = "lez/programs/authenticated_transfer/core faucet_core = { path = "lez/programs/faucet/core" } bridge_core = { path = "lez/programs/bridge/core" } vault_core = { path = "lez/programs/vault/core" } +cross_zone_inbox_core = { path = "lez/programs/cross_zone_inbox/core" } +cross_zone_outbox_core = { path = "lez/programs/cross_zone_outbox/core" } +bridge_lock_core = { path = "lez/programs/bridge_lock/core" } +wrapped_token_core = { path = "lez/programs/wrapped_token/core" } +ping_core = { path = "lez/programs/ping_core" } +cross_zone = { path = "lez/cross_zone" } build_utils = { path = "build_utils" } test_programs = { path = "test_programs" } testnet_initial_state = { path = "lez/testnet_initial_state" } @@ -147,6 +161,7 @@ bip39 = "2.2.0" hmac-sha512 = "1.1.7" chrono = "0.4.41" borsh = "1.5.7" +zstd = "0.13" base58 = "0.2.0" itertools = "0.14.0" num-bigint = "0.4.6" @@ -181,6 +196,7 @@ elliptic-curve = { version = "0.13.8", features = ["arithmetic"] } actix-web = { version = "4.13.0", default-features = false, features = [ "macros", ] } +axum = "0.8.4" clap = { version = "4.5.42", features = ["derive", "env"] } reqwest = { version = "0.12", features = ["json", "rustls-tls", "stream"] } pyo3 = { version = "0.29", features = ["auto-initialize"] } @@ -343,4 +359,4 @@ clippy.cargo = { level = "deny", priority = -1 } clippy.cargo-common-metadata = "allow" # Reason: hard to address right now and mostly comes from dependencies # so the fix would be just a long list of exceptions. -clippy.multiple-crate-versions = "allow" \ No newline at end of file +clippy.multiple-crate-versions = "allow" diff --git a/Justfile b/Justfile index afd3d9b7..cf7c833e 100644 --- a/Justfile +++ b/Justfile @@ -6,6 +6,11 @@ default: # ---- Configuration ---- ARTIFACTS := "artifacts" +# On macOS the integration-test binary links pyo3 against the CommandLineTools +# Python framework with no embedded rpath, so it needs this to launch. Empty on +# Linux/CI, which is unaffected. +DEMO_ENV := if os() == "macos" { "DYLD_FALLBACK_FRAMEWORK_PATH=/Library/Developer/CommandLineTools/Library/Frameworks" } else { "" } + # Build risc0 program artifacts. build-artifacts: @echo "πŸ”¨ Building artifacts" @@ -35,6 +40,11 @@ test: @echo "πŸ§ͺ Running tests" RISC0_DEV_MODE=1 cargo nextest run --no-fail-fast +# Regenerate the prebuilt sequencer db dump for fast TestContext::new() (needs Docker; commit the dump). +regenerate-test-fixture: + @echo "πŸ§ͺ Regenerating test fixtures" + RISC0_DEV_MODE=1 cargo run -p test_fixtures --bin regenerate_test_fixture + # Run criterion benches: fast crypto primitives, then the slow PPE verify (real proving setup). bench: @echo "πŸ“Š Running criterion benches" @@ -94,6 +104,26 @@ wallet-import-test-accounts: just run-wallet account list +# Demo: cross-zone ping. Boots two zones on one Bedrock and sends a message from +# zone A to zone B, where the indexer re-derives and verifies it (Option B) +# before ping_receiver records it. Dev mode, no proving. +demo-cross-zone-ping: + @echo "πŸ“‘ Cross-zone ping demo (message A β†’ B, indexer-verified)" + {{DEMO_ENV}} RISC0_DEV_MODE=1 cargo test -p integration_tests --release --test cross_zone_verified -- --nocapture + +# Demo: cross-zone wrapped-token bridge. Locks a balance on zone A and mints the +# wrapped token to a recipient on zone B over the same verified spine. +demo-cross-zone-bridge: + @echo "πŸŒ‰ Cross-zone bridge demo (lock on A, mint on B)" + {{DEMO_ENV}} RISC0_DEV_MODE=1 cargo test -p integration_tests --release --test cross_zone_bridge -- --nocapture + +# Demo: interactive cross-zone chat. Boots two zones on one Bedrock and serves a +# local two-column web UI; type in one zone and watch the message cross into the +# other. Two people can chat across the zones. Dev mode, no proving. +cross-zone-chat: + @echo "πŸ’¬ Cross-zone chat demo β€” open the printed localhost URL" + {{DEMO_ENV}} RISC0_DEV_MODE=1 cargo run -p cross_zone_chat --release + # Clean runtime data clean: @echo "🧹 Cleaning run artifacts" diff --git a/artifacts/lez/programs/amm.bin b/artifacts/lez/programs/amm.bin index 2bf13353..13d9aad6 100644 Binary files a/artifacts/lez/programs/amm.bin and b/artifacts/lez/programs/amm.bin differ diff --git a/artifacts/lez/programs/associated_token_account.bin b/artifacts/lez/programs/associated_token_account.bin index 31604d5a..81c9e602 100644 Binary files a/artifacts/lez/programs/associated_token_account.bin and b/artifacts/lez/programs/associated_token_account.bin differ diff --git a/artifacts/lez/programs/authenticated_transfer.bin b/artifacts/lez/programs/authenticated_transfer.bin index 25ff2172..85419195 100644 Binary files a/artifacts/lez/programs/authenticated_transfer.bin and b/artifacts/lez/programs/authenticated_transfer.bin differ diff --git a/artifacts/lez/programs/bridge.bin b/artifacts/lez/programs/bridge.bin index 0063fff9..1458cb82 100644 Binary files a/artifacts/lez/programs/bridge.bin and b/artifacts/lez/programs/bridge.bin differ diff --git a/artifacts/lez/programs/bridge_lock.bin b/artifacts/lez/programs/bridge_lock.bin new file mode 100644 index 00000000..989b95ec Binary files /dev/null and b/artifacts/lez/programs/bridge_lock.bin differ diff --git a/artifacts/lez/programs/clock.bin b/artifacts/lez/programs/clock.bin index 10cc941c..c8794191 100644 Binary files a/artifacts/lez/programs/clock.bin and b/artifacts/lez/programs/clock.bin differ diff --git a/artifacts/lez/programs/cross_zone_inbox.bin b/artifacts/lez/programs/cross_zone_inbox.bin new file mode 100644 index 00000000..c35173c1 Binary files /dev/null and b/artifacts/lez/programs/cross_zone_inbox.bin differ diff --git a/artifacts/lez/programs/cross_zone_outbox.bin b/artifacts/lez/programs/cross_zone_outbox.bin new file mode 100644 index 00000000..bc7f52a1 Binary files /dev/null and b/artifacts/lez/programs/cross_zone_outbox.bin differ diff --git a/artifacts/lez/programs/faucet.bin b/artifacts/lez/programs/faucet.bin index c389298b..de80915b 100644 Binary files a/artifacts/lez/programs/faucet.bin and b/artifacts/lez/programs/faucet.bin differ diff --git a/artifacts/lez/programs/pinata.bin b/artifacts/lez/programs/pinata.bin index 82e7fb78..02bc72af 100644 Binary files a/artifacts/lez/programs/pinata.bin and b/artifacts/lez/programs/pinata.bin differ diff --git a/artifacts/lez/programs/pinata_token.bin b/artifacts/lez/programs/pinata_token.bin index 08900cda..ccd41eb3 100644 Binary files a/artifacts/lez/programs/pinata_token.bin and b/artifacts/lez/programs/pinata_token.bin differ diff --git a/artifacts/lez/programs/ping_receiver.bin b/artifacts/lez/programs/ping_receiver.bin new file mode 100644 index 00000000..24c8b572 Binary files /dev/null and b/artifacts/lez/programs/ping_receiver.bin differ diff --git a/artifacts/lez/programs/ping_sender.bin b/artifacts/lez/programs/ping_sender.bin new file mode 100644 index 00000000..e9570781 Binary files /dev/null and b/artifacts/lez/programs/ping_sender.bin differ diff --git a/artifacts/lez/programs/token.bin b/artifacts/lez/programs/token.bin index 568c6f40..43c43c96 100644 Binary files a/artifacts/lez/programs/token.bin and b/artifacts/lez/programs/token.bin differ diff --git a/artifacts/lez/programs/vault.bin b/artifacts/lez/programs/vault.bin index 91482c04..1f962a88 100644 Binary files a/artifacts/lez/programs/vault.bin and b/artifacts/lez/programs/vault.bin differ diff --git a/artifacts/lez/programs/wrapped_token.bin b/artifacts/lez/programs/wrapped_token.bin new file mode 100644 index 00000000..d93b4c32 Binary files /dev/null and b/artifacts/lez/programs/wrapped_token.bin differ diff --git a/integration_tests/Cargo.toml b/integration_tests/Cargo.toml index bba06ec0..a225d657 100644 --- a/integration_tests/Cargo.toml +++ b/integration_tests/Cargo.toml @@ -22,6 +22,12 @@ associated_token_account_core.workspace = true vault_core.workspace = true faucet_core.workspace = true bridge_core.workspace = true +ping_core.workspace = true +cross_zone_outbox_core.workspace = true +cross_zone_inbox_core.workspace = true +bridge_lock_core.workspace = true +wrapped_token_core.workspace = true +risc0-zkvm.workspace = true indexer_service_rpc = { workspace = true, features = ["client"] } sequencer_service_rpc = { workspace = true, features = ["client"] } wallet-ffi.workspace = true diff --git a/integration_tests/tests/auth_transfer/private.rs b/integration_tests/tests/auth_transfer/private.rs index 205cbfed..af016dc6 100644 --- a/integration_tests/tests/auth_transfer/private.rs +++ b/integration_tests/tests/auth_transfer/private.rs @@ -69,8 +69,8 @@ async fn private_transfer_to_foreign_account() -> Result<()> { }); let result = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - let SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash } = result else { - anyhow::bail!("Expected PrivacyPreservingTransfer return value"); + let SubcommandReturnValue::TransactionExecuted { tx_hash } = result else { + anyhow::bail!("Expected TransactionExecuted return value"); }; info!("Waiting for next block creation"); @@ -158,8 +158,8 @@ async fn private_transfer_to_owned_account_using_claiming_path() -> Result<()> { }); let sub_ret = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - let SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash } = sub_ret else { - anyhow::bail!("Expected PrivacyPreservingTransfer return value"); + let SubcommandReturnValue::TransactionExecuted { tx_hash } = sub_ret else { + anyhow::bail!("Expected TransactionExecuted return value"); }; let tx = fetch_privacy_preserving_tx(ctx.sequencer_client(), tx_hash).await; @@ -237,8 +237,8 @@ async fn shielded_transfer_to_foreign_account() -> Result<()> { }); let result = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - let SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash } = result else { - anyhow::bail!("Expected PrivacyPreservingTransfer return value"); + let SubcommandReturnValue::TransactionExecuted { tx_hash } = result else { + anyhow::bail!("Expected TransactionExecuted return value"); }; info!("Waiting for next block creation"); @@ -297,7 +297,7 @@ async fn private_transfer_to_owned_account_continuous_run_path() -> Result<()> { }); let sub_ret = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - let SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash } = sub_ret else { + let SubcommandReturnValue::TransactionExecuted { tx_hash } = sub_ret else { anyhow::bail!("Failed to send transaction"); }; diff --git a/integration_tests/tests/cross_zone_bridge.rs b/integration_tests/tests/cross_zone_bridge.rs new file mode 100644 index 00000000..63b14c95 --- /dev/null +++ b/integration_tests/tests/cross_zone_bridge.rs @@ -0,0 +1,197 @@ +#![expect( + clippy::tests_outside_test_module, + reason = "top-level test functions are conventional for integration tests" +)] + +//! Demo 2: a wrapped-token bridge over the cross-zone spine. A holder locks part +//! of their bridgeable balance on zone A; the watcher carries the emitted mint to +//! zone B, where the indexer re-derives and verifies it (Option B) before the +//! wrapped token is minted to the recipient. Reuses the M3/M4 spine unchanged; +//! only the source caller (`bridge_lock`) and target (`wrapped_token`) are new. + +use std::{net::SocketAddr, time::Duration}; + +use anyhow::{Context as _, Result}; +use common::transaction::LeeTransaction; +use cross_zone_outbox_core::outbox_pda; +use integration_tests::{ + config::{self, SequencerPartialConfig}, + indexer_client::IndexerClient, + setup::{setup_bedrock_node, setup_indexer, setup_sequencer}, +}; +use lee::{ + AccountId, PrivateKey, PublicKey, PublicTransaction, + public_transaction::{Message, WitnessSet}, +}; +use sequencer_core::config::{CrossZoneConfig, CrossZonePeer, GenesisAction}; +use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder}; +use tokio::test; + +const DELIVERY_TIMEOUT: Duration = Duration::from_secs(600); +const INITIAL_BALANCE: u128 = 100; +const LOCK_AMOUNT: u128 = 30; +const RECIPIENT: [u8; 32] = [9; 32]; + +#[test] +async fn lock_on_zone_a_mints_wrapped_token_on_zone_b() -> Result<()> { + // Declared first so it outlives both zones (drops run in reverse order). + let (_bedrock, bedrock_addr) = setup_bedrock_node() + .await + .context("Failed to set up shared Bedrock node")?; + + let partial = SequencerPartialConfig::default(); + let channel_a = config::bedrock_channel_id(); + let channel_b = config::bedrock_channel_id_b(); + let zone_b: [u8; 32] = *channel_b.as_ref(); + + let holder_key = PrivateKey::try_new([7; 32]).expect("valid key"); + let holder_id = AccountId::from(&PublicKey::new_from_private_key(&holder_key)); + + let wrapped_token_id = programs::wrapped_token().id(); + let cross_zone = CrossZoneConfig { + peers: vec![CrossZonePeer { + channel_id: *channel_a.as_ref(), + allowed_targets: vec![wrapped_token_id], + expected_block_signing_pubkey: None, + }], + }; + + // Zone A seeds the holder's bridgeable balance. Zone B runs the watcher on its + // sequencer and the verifier on its indexer. + let genesis_a = vec![GenesisAction::SupplyBridgeLockHolding { + holder: holder_id, + amount: INITIAL_BALANCE, + }]; + let (seq_a, _seq_a_home) = setup_sequencer(partial, bedrock_addr, genesis_a, channel_a, None) + .await + .context("Failed to set up zone A sequencer")?; + let (_seq_b, _seq_b_home) = setup_sequencer( + partial, + bedrock_addr, + vec![], + channel_b, + Some(cross_zone.clone()), + ) + .await + .context("Failed to set up zone B sequencer")?; + let (idx_b, _idx_b_home) = setup_indexer(bedrock_addr, channel_b, Some(cross_zone)) + .await + .context("Failed to set up zone B indexer")?; + + // Lock LOCK_AMOUNT on zone A, addressed to the recipient on zone B. + let lock = build_lock_tx(&holder_key, holder_id, zone_b); + sequencer_client(seq_a.addr())? + .send_transaction(lock) + .await + .context("Failed to submit lock on zone A")?; + + // Wait until zone B's indexer reflects the verified mint. + let holding_id = wrapped_token_core::holding_account_id(wrapped_token_id, &RECIPIENT); + let indexer_url = config::addr_to_url(config::UrlProtocol::Ws, idx_b.addr()) + .context("Failed to build indexer URL")?; + let indexer = IndexerClient::new(&indexer_url) + .await + .context("Failed to build indexer client")?; + + let minted = wait_for_mint(&indexer, holding_id).await?; + assert_eq!( + minted, LOCK_AMOUNT, + "zone B must mint exactly the locked amount" + ); + + // Conservation: the mint on B must be backed by an equal lock on A. The lock + // has already landed (it preceded delivery), so zone A reflects the debit and + // escrow now. + let seq_a_client = sequencer_client(seq_a.addr())?; + let escrow_id = bridge_lock_core::escrow_account_id(programs::bridge_lock().id()); + let escrowed = bridge_lock_core::read_balance( + &seq_a_client.get_account(escrow_id).await?.data.into_inner(), + ); + assert_eq!( + escrowed, LOCK_AMOUNT, + "zone A escrow must hold the locked amount" + ); + let remaining = bridge_lock_core::read_balance( + &seq_a_client.get_account(holder_id).await?.data.into_inner(), + ); + assert_eq!( + remaining, + INITIAL_BALANCE - LOCK_AMOUNT, + "zone A holder must be debited by the locked amount" + ); + Ok(()) +} + +/// Builds a signed `bridge_lock` Lock that forwards a wrapped-token Mint of the +/// locked amount to the recipient on the target zone. +fn build_lock_tx( + holder_key: &PrivateKey, + holder_id: AccountId, + target_zone: [u8; 32], +) -> LeeTransaction { + let bridge_lock_id = programs::bridge_lock().id(); + let wrapped_token_id = programs::wrapped_token().id(); + let outbox_id = programs::cross_zone_outbox().id(); + let ordinal = 0; + + let mint = wrapped_token_core::Instruction::Mint { + recipient: RECIPIENT, + amount: LOCK_AMOUNT, + }; + let words = risc0_zkvm::serde::to_vec(&mint).expect("serialize mint"); + let payload: Vec = words.iter().flat_map(|word| word.to_le_bytes()).collect(); + + let target_accounts = vec![ + wrapped_token_core::config_account_id(wrapped_token_id).into_value(), + wrapped_token_core::holding_account_id(wrapped_token_id, &RECIPIENT).into_value(), + ]; + let lock = bridge_lock_core::Instruction::Lock { + amount: LOCK_AMOUNT, + target_zone, + target_program_id: wrapped_token_id, + target_accounts, + payload, + outbox_program_id: outbox_id, + ordinal, + }; + + let accounts = vec![ + holder_id, + bridge_lock_core::escrow_account_id(bridge_lock_id), + outbox_pda(outbox_id, &target_zone, ordinal), + ]; + // One nonce per signature: the holder signs, at its genesis nonce 0. + let message = Message::try_new(bridge_lock_id, accounts, vec![0_u128.into()], lock) + .expect("build lock message"); + let witness = WitnessSet::for_message(&message, &[holder_key]); + LeeTransaction::Public(PublicTransaction::new(message, witness)) +} + +fn sequencer_client(addr: SocketAddr) -> Result { + let url = config::addr_to_url(config::UrlProtocol::Http, addr) + .context("Failed to build sequencer URL")?; + SequencerClientBuilder::default() + .build(url) + .context("Failed to build sequencer client") +} + +/// Polls zone B's indexer until the recipient's wrapped holding is non-zero. +async fn wait_for_mint(indexer: &IndexerClient, holding_id: AccountId) -> Result { + let account_id = indexer_service_protocol::AccountId { + value: holding_id.into_value(), + }; + let wait = async { + loop { + let account = + indexer_service_rpc::RpcClient::get_account(&**indexer, account_id).await?; + let balance = wrapped_token_core::read_balance(&account.data.0); + if balance != 0 { + return Ok::(balance); + } + tokio::time::sleep(Duration::from_secs(3)).await; + } + }; + tokio::time::timeout(DELIVERY_TIMEOUT, wait) + .await + .context("Zone B's indexer did not mint the wrapped token in time")? +} diff --git a/integration_tests/tests/cross_zone_ingress_guard.rs b/integration_tests/tests/cross_zone_ingress_guard.rs new file mode 100644 index 00000000..5f401869 --- /dev/null +++ b/integration_tests/tests/cross_zone_ingress_guard.rs @@ -0,0 +1,80 @@ +#![expect( + clippy::tests_outside_test_module, + reason = "We don't care about these in tests" +)] + +//! M6 ingress guard: the cross-zone inbox is sequencer-only. Only the watcher +//! injects inbox dispatches; a user must not be able to invoke the inbox through +//! the public RPC, or anyone could forge an inbound cross-zone delivery. The +//! inbox guest's caller-is-none assertion passes for a top-level user tx, so the +//! sequencer ingress guard is the only thing that stops this. + +use std::net::SocketAddr; + +use anyhow::{Context as _, Result}; +use common::transaction::LeeTransaction; +use cross_zone_inbox_core::{ + CrossZoneMessage, Instruction, inbox_config_account_id, inbox_seen_shard_account_id, +}; +use integration_tests::{ + config::{self, SequencerPartialConfig}, + setup::{setup_bedrock_node, setup_sequencer}, +}; +use lee::{ + PublicTransaction, + public_transaction::{Message, WitnessSet}, +}; +use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder}; +use tokio::test; + +#[test] +async fn user_origin_inbox_call_rejected() -> Result<()> { + let (_bedrock, bedrock_addr) = setup_bedrock_node() + .await + .context("Failed to set up Bedrock node")?; + let partial = SequencerPartialConfig::default(); + let channel = config::bedrock_channel_id(); + let (seq, _seq_home) = setup_sequencer(partial, bedrock_addr, vec![], channel, None) + .await + .context("Failed to set up sequencer")?; + + // A user hand-builds a top-level inbox Dispatch and submits it via RPC. + let inbox_id = programs::cross_zone_inbox().id(); + let msg = CrossZoneMessage { + src_zone: [2; 32], + src_block_id: 1, + src_tx_index: 0, + src_program_id: [9; 8], + target_program_id: programs::ping_receiver().id(), + payload: vec![], + l1_inclusion_witness: None, + }; + let seen_id = inbox_seen_shard_account_id(inbox_id, &msg.src_zone, msg.src_block_id); + let message = Message::try_new( + inbox_id, + vec![inbox_config_account_id(inbox_id), seen_id], + vec![], + Instruction::Dispatch(msg), + ) + .expect("build dispatch message"); + let tx = LeeTransaction::Public(PublicTransaction::new( + message, + WitnessSet::from_raw_parts(vec![]), + )); + + let result = sequencer_client(seq.addr())?.send_transaction(tx).await; + let err = result.expect_err("the sequencer must reject a user-origin inbox call"); + assert!( + err.to_string().contains("sequencer-only"), + "rejection should cite the sequencer-only guard, got: {err}" + ); + Ok(()) +} + +fn sequencer_client(addr: SocketAddr) -> Result { + let url = config::addr_to_url(config::UrlProtocol::Http, addr) + .context("Failed to build sequencer URL")?; + SequencerClientBuilder::default() + .build(url) + .context("Failed to build sequencer client") +} diff --git a/integration_tests/tests/cross_zone_ping.rs b/integration_tests/tests/cross_zone_ping.rs new file mode 100644 index 00000000..19223b5f --- /dev/null +++ b/integration_tests/tests/cross_zone_ping.rs @@ -0,0 +1,143 @@ +#![expect( + clippy::tests_outside_test_module, + reason = "top-level test functions are conventional for integration tests" +)] + +//! End-to-end cross-zone round trip: a ping submitted on zone A is delivered by +//! zone B's watcher to `ping_receiver` on zone B, which records the payload. +//! +//! Two sequencers share one Bedrock node (no indexers): zone A publishes the +//! ping to Bedrock, zone B's watcher reads zone A's finalized blocks, injects the +//! inbox dispatch, and zone B's sequencer delivers it. This is the M3 milestone, +//! sequencer-trusted, with no indexer re-derivation (that is M4). + +use std::{net::SocketAddr, time::Duration}; + +use anyhow::{Context as _, Result}; +use common::transaction::LeeTransaction; +use cross_zone_outbox_core::outbox_pda; +use integration_tests::{ + config::{self, SequencerPartialConfig}, + setup::{setup_bedrock_node, setup_sequencer}, +}; +use lee::{AccountId, PublicTransaction, public_transaction::Message}; +use lee_core::program::ProgramId; +use ping_core::{ReceiverInstruction, SenderInstruction, ping_record_pda}; +use sequencer_core::config::{CrossZoneConfig, CrossZonePeer}; +use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder}; +use tokio::test; + +const DELIVERY_TIMEOUT: Duration = Duration::from_secs(480); +const PING_PAYLOAD: &[u8] = b"hello-cross-zone"; + +#[test] +async fn ping_crosses_from_zone_a_to_zone_b() -> Result<()> { + // Declared first so it outlives both zones (drops run in reverse order). + let (_bedrock, bedrock_addr) = setup_bedrock_node() + .await + .context("Failed to set up shared Bedrock node")?; + + let partial = SequencerPartialConfig::default(); + let channel_a = config::bedrock_channel_id(); + let channel_b = config::bedrock_channel_id_b(); + let zone_a: [u8; 32] = *channel_a.as_ref(); + let zone_b: [u8; 32] = *channel_b.as_ref(); + + let receiver_id = programs::ping_receiver().id(); + + // Zone B watches zone A and allows delivery only to ping_receiver. + let cross_zone = CrossZoneConfig { + peers: vec![CrossZonePeer { + channel_id: zone_a, + allowed_targets: vec![receiver_id], + expected_block_signing_pubkey: None, + }], + }; + + let (seq_a, _seq_a_home) = setup_sequencer(partial, bedrock_addr, vec![], channel_a, None) + .await + .context("Failed to set up zone A sequencer")?; + let (seq_b, _seq_b_home) = + setup_sequencer(partial, bedrock_addr, vec![], channel_b, Some(cross_zone)) + .await + .context("Failed to set up zone B sequencer")?; + + // Submit the ping on zone A, addressed to ping_receiver on zone B. + let ping = build_ping_tx(zone_b, receiver_id); + sequencer_client(seq_a.addr())? + .send_transaction(ping) + .await + .context("Failed to submit ping on zone A")?; + + // Wait until zone B's sequencer records the delivered payload. + let record_id = ping_record_pda(receiver_id); + let delivered = wait_for_delivery(sequencer_client(seq_b.addr())?, record_id).await?; + + assert_eq!( + delivered, PING_PAYLOAD, + "Zone B must record the payload delivered from zone A" + ); + Ok(()) +} + +/// Builds a top-level `ping_sender` transaction that chains into the outbox to emit +/// a message carrying a `ping_receiver::Record` instruction for the target zone. +fn build_ping_tx(target_zone: [u8; 32], receiver_id: ProgramId) -> LeeTransaction { + let outbox_id = programs::cross_zone_outbox().id(); + let ordinal = 0; + + // The payload is the ping_receiver instruction, serialized as risc0 words in + // little-endian bytes (the contract the inbox reverses when forwarding). + let words = risc0_zkvm::serde::to_vec(&ReceiverInstruction::Record { + payload: PING_PAYLOAD.to_vec(), + }) + .expect("serialize ping instruction"); + let payload: Vec = words.iter().flat_map(|word| word.to_le_bytes()).collect(); + + let send = SenderInstruction::Send { + outbox_program_id: outbox_id, + target_zone, + target_program_id: receiver_id, + target_accounts: vec![ping_record_pda(receiver_id).into_value()], + payload, + ordinal, + }; + + let outbox_account = outbox_pda(outbox_id, &target_zone, ordinal); + let message = Message::try_new( + programs::ping_sender().id(), + vec![outbox_account], + vec![], + send, + ) + .expect("build ping message"); + LeeTransaction::Public(PublicTransaction::new( + message, + lee::public_transaction::WitnessSet::from_raw_parts(vec![]), + )) +} + +fn sequencer_client(addr: SocketAddr) -> Result { + let url = config::addr_to_url(config::UrlProtocol::Http, addr) + .context("Failed to build sequencer URL")?; + SequencerClientBuilder::default() + .build(url) + .context("Failed to build sequencer client") +} + +/// Polls zone B's sequencer until the ping record PDA holds a payload. +async fn wait_for_delivery(client: SequencerClient, record_id: AccountId) -> Result> { + let wait = async { + loop { + let account = client.get_account(record_id).await?; + let data = account.data.into_inner(); + if !data.is_empty() { + return Ok::, anyhow::Error>(data); + } + tokio::time::sleep(Duration::from_secs(3)).await; + } + }; + tokio::time::timeout(DELIVERY_TIMEOUT, wait) + .await + .context("Zone B did not record the cross-zone payload in time")? +} diff --git a/integration_tests/tests/cross_zone_state_machine.rs b/integration_tests/tests/cross_zone_state_machine.rs new file mode 100644 index 00000000..1b67914a --- /dev/null +++ b/integration_tests/tests/cross_zone_state_machine.rs @@ -0,0 +1,373 @@ +#![expect( + clippy::tests_outside_test_module, + reason = "top-level test functions are conventional for integration tests" +)] + +//! Single-zone state-machine tests for cross-zone delivery (ping demo) and the +//! wrapped-token bridge (Demo 2). They drive the guests in isolation, no watcher +//! or Bedrock: a hand-built `cross_zone_inbox::Dispatch` (as the watcher would +//! inject) and the source `bridge_lock::Lock` (which escrows and chains +//! `outbox::Emit`). Fast, so they pin guest logic before the e2e exercises the +//! plumbing. Run with `RISC0_DEV_MODE=1`. + +use std::collections::BTreeMap; + +use cross_zone_inbox_core::{ + CrossZoneMessage, InboxConfig, Instruction as InboxInstruction, SeenShard, + inbox_config_account_id, inbox_seen_shard_account_id, message_key, +}; +use cross_zone_outbox_core::{OutboxRecord, outbox_pda}; +use lee::{ + AccountId, PrivateKey, PublicKey, PublicTransaction, V03State, ValidatedStateDiff, + public_transaction::{Message, WitnessSet}, +}; +use lee_core::account::Account; +use ping_core::{ReceiverInstruction, ping_record_pda}; + +const INITIAL_BALANCE: u128 = 100; +const LOCK_AMOUNT: u128 = 30; +const RECIPIENT: [u8; 32] = [9; 32]; + +/// State registering the cross-zone builtins these tests exercise. +fn base_state() -> V03State { + V03State::new().with_programs([ + programs::cross_zone_inbox(), + programs::cross_zone_outbox(), + programs::ping_receiver(), + programs::bridge_lock(), + programs::wrapped_token(), + ]) +} + +/// Seeds an inbox config (inbox-owned) allowing `src_zone -> target`. +fn seed_inbox_config( + state: &mut V03State, + self_zone: [u8; 32], + src_zone: [u8; 32], + target: lee_core::program::ProgramId, +) { + let inbox_id = programs::cross_zone_inbox().id(); + let mut allowed_targets = BTreeMap::new(); + allowed_targets.insert(src_zone, vec![target]); + let config = InboxConfig { + self_zone, + allowed_peers: BTreeMap::new(), + allowed_targets, + }; + state.insert_genesis_account( + inbox_config_account_id(inbox_id), + Account { + program_owner: inbox_id, + balance: 0, + data: config + .to_bytes() + .try_into() + .expect("config fits in account data"), + nonce: 0_u128.into(), + }, + ); +} + +/// Seeds the wrapped-token config account pinning the inbox as authorized minter, +/// matching what genesis seeds for a real zone. +fn seed_wrapped_config(state: &mut V03State) { + let wrapped_token_id = programs::wrapped_token().id(); + state.insert_genesis_account( + wrapped_token_core::config_account_id(wrapped_token_id), + Account { + program_owner: wrapped_token_id, + data: wrapped_token_core::minter_bytes(programs::cross_zone_inbox().id()) + .to_vec() + .try_into() + .expect("minter id fits in account data"), + ..Default::default() + }, + ); +} + +/// The wrapped-token `Mint` the bridge forwards, serialized as the cross-zone +/// payload (risc0 words, little-endian bytes). +fn mint_payload() -> Vec { + let mint = wrapped_token_core::Instruction::Mint { + recipient: RECIPIENT, + amount: LOCK_AMOUNT, + }; + let words = risc0_zkvm::serde::to_vec(&mint).expect("serialize mint"); + words.iter().flat_map(|word| word.to_le_bytes()).collect() +} + +/// Drives `cross_zone_inbox::Dispatch` directly through the state machine +/// (no watcher) and asserts the message is delivered to `ping_receiver`, which +/// records the payload into its own PDA. +#[test] +fn inbox_dispatch_delivers_payload_to_ping_receiver() { + let inbox_id = programs::cross_zone_inbox().id(); + let receiver_id = programs::ping_receiver().id(); + + let self_zone = [1_u8; 32]; + let src_zone = [2_u8; 32]; + let src_block_id = 5; + + let mut state = base_state(); + seed_inbox_config(&mut state, self_zone, src_zone, receiver_id); + + // The payload is the ping_receiver instruction, serialized as risc0 words in + // little-endian bytes (the contract the inbox reverses when forwarding). + let inner = b"hello-cross-zone".to_vec(); + let words = risc0_zkvm::serde::to_vec(&ReceiverInstruction::Record { + payload: inner.clone(), + }) + .expect("serialize ping instruction"); + let payload: Vec = words.iter().flat_map(|word| word.to_le_bytes()).collect(); + + let msg = CrossZoneMessage { + src_zone, + src_block_id, + src_tx_index: 0, + src_program_id: [9_u32; 8], + target_program_id: receiver_id, + payload, + l1_inclusion_witness: None, + }; + + let seen_id = inbox_seen_shard_account_id(inbox_id, &src_zone, src_block_id); + let record_id = ping_record_pda(receiver_id); + + let message = Message::try_new( + inbox_id, + vec![inbox_config_account_id(inbox_id), seen_id, record_id], + vec![], + InboxInstruction::Dispatch(msg), + ) + .expect("build dispatch message"); + let tx = PublicTransaction::new(message, WitnessSet::from_raw_parts(vec![])); + + let diff = ValidatedStateDiff::from_public_transaction(&tx, &state, 1, 0) + .expect("dispatch must validate and execute"); + let record = diff + .public_diff() + .get(&record_id) + .expect("ping record account must change") + .clone(); + assert_eq!( + record.data.into_inner(), + inner, + "ping_receiver must record the delivered payload" + ); +} + +/// Drives `bridge_lock::Lock` and asserts it debits the holder, credits the +/// escrow, and records the forwarded mint in the outbox PDA. +#[test] +fn lock_escrows_balance_and_emits_to_outbox() { + let bridge_lock_id = programs::bridge_lock().id(); + let wrapped_token_id = programs::wrapped_token().id(); + let outbox_id = programs::cross_zone_outbox().id(); + let zone_b = [2_u8; 32]; + let ordinal = 0; + + let mut state = base_state(); + + let holder_key = PrivateKey::try_new([7; 32]).expect("valid key"); + let holder_id = AccountId::from(&PublicKey::new_from_private_key(&holder_key)); + state.insert_genesis_account( + holder_id, + Account { + program_owner: bridge_lock_id, + balance: 0, + data: bridge_lock_core::balance_bytes(INITIAL_BALANCE) + .to_vec() + .try_into() + .expect("balance fits in account data"), + nonce: 0_u128.into(), + }, + ); + + let payload = mint_payload(); + let target_accounts = vec![ + wrapped_token_core::config_account_id(wrapped_token_id).into_value(), + wrapped_token_core::holding_account_id(wrapped_token_id, &RECIPIENT).into_value(), + ]; + let lock = bridge_lock_core::Instruction::Lock { + amount: LOCK_AMOUNT, + target_zone: zone_b, + target_program_id: wrapped_token_id, + target_accounts, + payload: payload.clone(), + outbox_program_id: outbox_id, + ordinal, + }; + + let escrow_id = bridge_lock_core::escrow_account_id(bridge_lock_id); + let outbox_record_id = outbox_pda(outbox_id, &zone_b, ordinal); + let message = Message::try_new( + bridge_lock_id, + vec![holder_id, escrow_id, outbox_record_id], + vec![0_u128.into()], + lock, + ) + .expect("build lock message"); + let witness = WitnessSet::for_message(&message, &[&holder_key]); + let tx = PublicTransaction::new(message, witness); + + let diff = ValidatedStateDiff::from_public_transaction(&tx, &state, 1, 0) + .expect("lock must validate and execute"); + let public_diff = diff.public_diff(); + + let holder_after = + bridge_lock_core::read_balance(&public_diff[&holder_id].data.clone().into_inner()); + assert_eq!( + holder_after, + INITIAL_BALANCE - LOCK_AMOUNT, + "holder debited" + ); + + let escrow_after = + bridge_lock_core::read_balance(&public_diff[&escrow_id].data.clone().into_inner()); + assert_eq!(escrow_after, LOCK_AMOUNT, "escrow credited"); + + let record = + OutboxRecord::from_bytes(&public_diff[&outbox_record_id].data.clone().into_inner()) + .expect("outbox PDA holds an OutboxRecord"); + assert_eq!(record.target_zone, zone_b); + assert_eq!(record.target_program_id, wrapped_token_id); + assert_eq!( + record.payload, payload, + "emitted payload is the wrapped mint" + ); +} + +/// Drives a hand-built `cross_zone_inbox::Dispatch` (as the watcher would inject) +/// and asserts it chains into `wrapped_token::Mint`, crediting the recipient. +#[test] +fn inbox_dispatch_mints_wrapped_token() { + let inbox_id = programs::cross_zone_inbox().id(); + let wrapped_token_id = programs::wrapped_token().id(); + + let self_zone = [1_u8; 32]; + let src_zone = [2_u8; 32]; + let src_block_id = 5; + + let mut state = base_state(); + seed_inbox_config(&mut state, self_zone, src_zone, wrapped_token_id); + seed_wrapped_config(&mut state); + + let msg = CrossZoneMessage { + src_zone, + src_block_id, + src_tx_index: 0, + src_program_id: [9_u32; 8], + target_program_id: wrapped_token_id, + payload: mint_payload(), + l1_inclusion_witness: None, + }; + + let seen_id = inbox_seen_shard_account_id(inbox_id, &src_zone, src_block_id); + let wrapped_config_id = wrapped_token_core::config_account_id(wrapped_token_id); + let holding_id = wrapped_token_core::holding_account_id(wrapped_token_id, &RECIPIENT); + + let message = Message::try_new( + inbox_id, + vec![ + inbox_config_account_id(inbox_id), + seen_id, + wrapped_config_id, + holding_id, + ], + vec![], + InboxInstruction::Dispatch(msg), + ) + .expect("build dispatch message"); + let tx = PublicTransaction::new(message, WitnessSet::from_raw_parts(vec![])); + + let diff = ValidatedStateDiff::from_public_transaction(&tx, &state, 1, 0) + .expect("dispatch must validate and execute"); + let minted = wrapped_token_core::read_balance( + &diff.public_diff()[&holding_id].data.clone().into_inner(), + ); + assert_eq!( + minted, LOCK_AMOUNT, + "recipient holding minted the locked amount" + ); +} + +/// A dispatch whose message key is already in the seen-shard is an idempotent +/// no-op: the inbox makes no chained call, so the wrapped token is not minted a +/// second time. This is the bridge's replay defense. +#[test] +fn mint_replay_rejected() { + let inbox_id = programs::cross_zone_inbox().id(); + let wrapped_token_id = programs::wrapped_token().id(); + + let self_zone = [1_u8; 32]; + let src_zone = [2_u8; 32]; + let src_block_id = 5; + let src_tx_index = 0; + + let mut state = base_state(); + seed_inbox_config(&mut state, self_zone, src_zone, wrapped_token_id); + seed_wrapped_config(&mut state); + + // Seed the seen-shard as already containing this message's key, so the inbox + // takes the replay no-op branch. The shard is inbox-owned (claimed on a prior + // delivery), so the guest leaves it untouched. + let seen_id = inbox_seen_shard_account_id(inbox_id, &src_zone, src_block_id); + let mut shard = SeenShard::default(); + shard.insert(message_key(&src_zone, src_block_id, src_tx_index)); + state.insert_genesis_account( + seen_id, + Account { + program_owner: inbox_id, + balance: 0, + data: shard + .to_bytes() + .try_into() + .expect("shard fits in account data"), + nonce: 0_u128.into(), + }, + ); + + let msg = CrossZoneMessage { + src_zone, + src_block_id, + src_tx_index, + src_program_id: [9_u32; 8], + target_program_id: wrapped_token_id, + payload: mint_payload(), + l1_inclusion_witness: None, + }; + + let wrapped_config_id = wrapped_token_core::config_account_id(wrapped_token_id); + let holding_id = wrapped_token_core::holding_account_id(wrapped_token_id, &RECIPIENT); + + let message = Message::try_new( + inbox_id, + vec![ + inbox_config_account_id(inbox_id), + seen_id, + wrapped_config_id, + holding_id, + ], + vec![], + InboxInstruction::Dispatch(msg), + ) + .expect("build dispatch message"); + let tx = PublicTransaction::new(message, WitnessSet::from_raw_parts(vec![])); + + let diff = ValidatedStateDiff::from_public_transaction(&tx, &state, 1, 0) + .expect("a replayed dispatch is a valid no-op, not an error"); + let public_diff = diff.public_diff(); + + // No mint: the holding is never credited on replay. + let minted = public_diff.get(&holding_id).map_or(0, |account| { + wrapped_token_core::read_balance(&account.data.clone().into_inner()) + }); + assert_eq!(minted, 0, "a replayed message must not mint again"); + + // The seen-shard is untouched by the no-op. + if let Some(seen) = public_diff.get(&seen_id) { + let shard_after = + SeenShard::from_bytes(&seen.data.clone().into_inner()).expect("seen shard decodes"); + assert_eq!(shard_after, shard, "replay must not modify the seen-shard"); + } +} diff --git a/integration_tests/tests/cross_zone_verified.rs b/integration_tests/tests/cross_zone_verified.rs new file mode 100644 index 00000000..e2f016c0 --- /dev/null +++ b/integration_tests/tests/cross_zone_verified.rs @@ -0,0 +1,162 @@ +#![expect( + clippy::tests_outside_test_module, + reason = "top-level test functions are conventional for integration tests" +)] + +//! Cross-zone round trip with the indexer in the loop (Option B). A ping on zone +//! A is delivered to zone B, and zone B's indexer independently re-derives the +//! injected dispatch from zone A's finalized blocks before applying it. The +//! payload landing in the indexer's state proves verification passed; a forgery +//! would have halted the indexer instead. + +use std::{net::SocketAddr, time::Duration}; + +use anyhow::{Context as _, Result}; +use common::transaction::LeeTransaction; +use cross_zone_outbox_core::outbox_pda; +use integration_tests::{ + config::{self, SequencerPartialConfig}, + indexer_client::IndexerClient, + setup::{setup_bedrock_node, setup_indexer, setup_sequencer}, +}; +use lee::{AccountId, PublicTransaction, public_transaction::Message}; +use lee_core::program::ProgramId; +use ping_core::{ReceiverInstruction, SenderInstruction, ping_record_pda}; +use sequencer_core::config::{CrossZoneConfig, CrossZonePeer}; +use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder}; +use tokio::test; + +const DELIVERY_TIMEOUT: Duration = Duration::from_secs(600); +const PING_PAYLOAD: &[u8] = b"hello-verified-zone"; + +#[test] +async fn indexer_verifies_and_delivers_cross_zone_ping() -> Result<()> { + // Declared first so it outlives both zones (drops run in reverse order). + let (_bedrock, bedrock_addr) = setup_bedrock_node() + .await + .context("Failed to set up shared Bedrock node")?; + + let partial = SequencerPartialConfig::default(); + let channel_a = config::bedrock_channel_id(); + let channel_b = config::bedrock_channel_id_b(); + let zone_a: [u8; 32] = *channel_a.as_ref(); + let zone_b: [u8; 32] = *channel_b.as_ref(); + + let receiver_id = programs::ping_receiver().id(); + let cross_zone = CrossZoneConfig { + peers: vec![CrossZonePeer { + channel_id: zone_a, + allowed_targets: vec![receiver_id], + expected_block_signing_pubkey: None, + }], + }; + + // Zone A: source. Zone B: destination, with the watcher on its sequencer and + // the verifier on its indexer. + let (seq_a, _seq_a_home) = setup_sequencer(partial, bedrock_addr, vec![], channel_a, None) + .await + .context("Failed to set up zone A sequencer")?; + let (_idx_a, _idx_a_home) = setup_indexer(bedrock_addr, channel_a, None) + .await + .context("Failed to set up zone A indexer")?; + let (_seq_b, _seq_b_home) = setup_sequencer( + partial, + bedrock_addr, + vec![], + channel_b, + Some(cross_zone.clone()), + ) + .await + .context("Failed to set up zone B sequencer")?; + let (idx_b, _idx_b_home) = setup_indexer(bedrock_addr, channel_b, Some(cross_zone)) + .await + .context("Failed to set up zone B indexer")?; + + // Submit the ping on zone A, addressed to ping_receiver on zone B. + let ping = build_ping_tx(zone_b, receiver_id); + sequencer_client(seq_a.addr())? + .send_transaction(ping) + .await + .context("Failed to submit ping on zone A")?; + + // Wait until zone B's indexer records the delivered payload. The indexer only + // applies the dispatch after re-deriving and verifying it. + let record_id = ping_record_pda(receiver_id); + let indexer_url = config::addr_to_url(config::UrlProtocol::Ws, idx_b.addr()) + .context("Failed to build indexer URL")?; + let indexer = IndexerClient::new(&indexer_url) + .await + .context("Failed to build indexer client")?; + + let delivered = wait_for_indexer_delivery(&indexer, record_id).await?; + assert_eq!( + delivered, PING_PAYLOAD, + "Zone B's indexer must record the verified cross-zone payload" + ); + Ok(()) +} + +fn build_ping_tx(target_zone: [u8; 32], receiver_id: ProgramId) -> LeeTransaction { + let outbox_id = programs::cross_zone_outbox().id(); + let ordinal = 0; + + let words = risc0_zkvm::serde::to_vec(&ReceiverInstruction::Record { + payload: PING_PAYLOAD.to_vec(), + }) + .expect("serialize ping instruction"); + let payload: Vec = words.iter().flat_map(|word| word.to_le_bytes()).collect(); + + let send = SenderInstruction::Send { + outbox_program_id: outbox_id, + target_zone, + target_program_id: receiver_id, + target_accounts: vec![ping_record_pda(receiver_id).into_value()], + payload, + ordinal, + }; + + let outbox_account = outbox_pda(outbox_id, &target_zone, ordinal); + let message = Message::try_new( + programs::ping_sender().id(), + vec![outbox_account], + vec![], + send, + ) + .expect("build ping message"); + LeeTransaction::Public(PublicTransaction::new( + message, + lee::public_transaction::WitnessSet::from_raw_parts(vec![]), + )) +} + +fn sequencer_client(addr: SocketAddr) -> Result { + let url = config::addr_to_url(config::UrlProtocol::Http, addr) + .context("Failed to build sequencer URL")?; + SequencerClientBuilder::default() + .build(url) + .context("Failed to build sequencer client") +} + +/// Polls zone B's indexer until the ping record PDA holds a payload. +async fn wait_for_indexer_delivery( + indexer: &IndexerClient, + record_id: AccountId, +) -> Result> { + let account_id = indexer_service_protocol::AccountId { + value: record_id.into_value(), + }; + let wait = async { + loop { + let account = + indexer_service_rpc::RpcClient::get_account(&**indexer, account_id).await?; + let data = account.data.0; + if !data.is_empty() { + return Ok::, anyhow::Error>(data); + } + tokio::time::sleep(Duration::from_secs(3)).await; + } + }; + tokio::time::timeout(DELIVERY_TIMEOUT, wait) + .await + .context("Zone B's indexer did not record the payload in time")? +} diff --git a/integration_tests/tests/indexer_ffi_helpers/mod.rs b/integration_tests/tests/indexer_ffi_helpers/mod.rs index 170102fd..09e0a927 100644 --- a/integration_tests/tests/indexer_ffi_helpers/mod.rs +++ b/integration_tests/tests/indexer_ffi_helpers/mod.rs @@ -50,8 +50,12 @@ pub fn setup_indexer_ffi(bedrock_addr: SocketAddr) -> Result<(IndexerServiceFFI, temp_indexer_dir.path().display() ); - let indexer_config = integration_tests::config::indexer_config(bedrock_addr) - .context("Failed to create Indexer config")?; + let indexer_config = integration_tests::config::indexer_config( + bedrock_addr, + integration_tests::config::bedrock_channel_id(), + None, + ) + .context("Failed to create Indexer config")?; let config_json = serde_json::to_vec(&indexer_config)?; let config_path = temp_indexer_dir.path().join("indexer_config.json"); diff --git a/integration_tests/tests/indexer_stall.rs b/integration_tests/tests/indexer_stall.rs new file mode 100644 index 00000000..ae1b8b6a --- /dev/null +++ b/integration_tests/tests/indexer_stall.rs @@ -0,0 +1,54 @@ +#![expect( + clippy::tests_outside_test_module, + reason = "We don't care about these in tests" +)] + +use std::time::Duration; + +use anyhow::{Context as _, Result}; +use indexer_service_protocol::IndexerSyncState; +use indexer_service_rpc::RpcClient as _; +use integration_tests::{TestContext, wait_for_indexer_to_catch_up}; +use log::info; + +const CAUGHT_UP_STATUS_TIMEOUT: Duration = Duration::from_secs(60); + +/// Test that the indexer status RPC reports caught-up with no stall after a clean run. +/// +/// The sequencer keeps producing blocks while we assert, so the status is polled until a +/// `CaughtUp` snapshot is observed and the indexed tip is checked as a lower bound. +/// +/// TODO: Integration-level park testing (publishing a bad block to force a stall) is a follow-up +/// needing fault injection support in the test harness. +#[tokio::test] +async fn indexer_status_rpc_reports_caught_up_with_no_stall() -> Result<()> { + let ctx = TestContext::new().await?; + + let indexer_tip = wait_for_indexer_to_catch_up(&ctx).await?; + + let status = tokio::time::timeout(CAUGHT_UP_STATUS_TIMEOUT, async { + loop { + let status = ctx.indexer_client().get_status().await?; + if status.state == IndexerSyncState::CaughtUp { + return anyhow::Ok(status); + } + info!("Waiting for caught-up indexer status, got {status:?}"); + tokio::time::sleep(Duration::from_millis(500)).await; + } + }) + .await + .context("Timed out waiting for indexer status to report caught-up")??; + + assert!( + status.stall_reason.is_none(), + "indexer should have no stall reason after a clean run, got {status:?}" + ); + // test for >= here because the sequencer keeps producing blocks while we assert, + // so the indexed tip may be ahead of the tip we observed when we waited for caught-up. + assert!( + status.indexed_block_id >= Some(indexer_tip), + "status indexed_block_id should be at least the caught-up tip {indexer_tip}, got {status:?}" + ); + + Ok(()) +} diff --git a/integration_tests/tests/keys.rs b/integration_tests/tests/keys.rs index 7fc73c62..10b5710e 100644 --- a/integration_tests/tests/keys.rs +++ b/integration_tests/tests/keys.rs @@ -57,8 +57,8 @@ async fn sync_private_account_with_non_zero_chain_index() -> Result<()> { }); let sub_ret = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - let SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash } = sub_ret else { - anyhow::bail!("Expected PrivacyPreservingTransfer return value"); + let SubcommandReturnValue::TransactionExecuted { tx_hash } = sub_ret else { + anyhow::bail!("Expected TransactionExecuted return value"); }; let tx = fetch_privacy_preserving_tx(ctx.sequencer_client(), tx_hash).await; diff --git a/integration_tests/tests/pinata.rs b/integration_tests/tests/pinata.rs index 167d9b66..f2c634a2 100644 --- a/integration_tests/tests/pinata.rs +++ b/integration_tests/tests/pinata.rs @@ -162,8 +162,8 @@ async fn claim_pinata_to_existing_private_account() -> Result<()> { let pinata_balance_pre = account_balance(&ctx, system_accounts::pinata_account_id()).await?; let result = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - let SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash: _ } = result else { - anyhow::bail!("Expected PrivacyPreservingTransfer return value"); + let SubcommandReturnValue::TransactionExecuted { tx_hash: _ } = result else { + anyhow::bail!("Expected TransactionExecuted return value"); }; info!("Waiting for next block creation"); diff --git a/integration_tests/tests/program_deployment.rs b/integration_tests/tests/program_deployment.rs index 2b242549..c92daeb3 100644 --- a/integration_tests/tests/program_deployment.rs +++ b/integration_tests/tests/program_deployment.rs @@ -11,7 +11,7 @@ use integration_tests::{TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, get_account use log::info; use sequencer_service_rpc::RpcClient as _; use tokio::test; -use wallet::cli::Command; +use wallet::{cli::Command, config::WalletConfigOverrides}; #[test] async fn deploy_and_execute_program() -> Result<()> { @@ -29,9 +29,6 @@ async fn deploy_and_execute_program() -> Result<()> { wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - info!("Waiting for next block creation"); - tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - let account_id = new_account(&mut ctx, false, None).await?; let nonces = ctx.wallet().get_accounts_nonces(vec![account_id]).await?; @@ -65,3 +62,37 @@ async fn deploy_and_execute_program() -> Result<()> { Ok(()) } + +#[test] +async fn deploy_invalid_program_fails() -> Result<()> { + // An invalid program bytecode is rejected by the sequencer during block production, so the + // deployment transaction is never included in a block. Shrink the wallet's polling window so + // the command gives up quickly instead of waiting for the full default timeout. + let mut ctx = TestContext::builder() + .with_wallet_config_overrides(WalletConfigOverrides { + seq_poll_timeout: Some(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)), + seq_tx_poll_max_blocks: Some(5), + seq_poll_max_retries: Some(2), + ..WalletConfigOverrides::default() + }) + .build() + .await?; + + let mut tempfile = tempfile::NamedTempFile::new()?; + tempfile.write_all(b"this is not a valid program binary")?; + + let command = Command::DeployProgram { + binary_filepath: tempfile.path().to_owned(), + }; + + let result = wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await; + + assert!( + result.is_err(), + "Deploying an invalid program should fail, but got: {result:?}" + ); + + info!("Deploying an invalid program failed as expected"); + + Ok(()) +} diff --git a/integration_tests/tests/two_zone.rs b/integration_tests/tests/two_zone.rs new file mode 100644 index 00000000..c895acd1 --- /dev/null +++ b/integration_tests/tests/two_zone.rs @@ -0,0 +1,110 @@ +#![expect( + clippy::tests_outside_test_module, + reason = "top-level test functions are conventional for integration tests" +)] + +//! Two zones (sequencer + indexer each, on separate channels) sharing one +//! Bedrock node, each producing and finalizing blocks independently. + +use std::{net::SocketAddr, time::Duration}; + +use anyhow::{Context as _, Result}; +use indexer_service_rpc::RpcClient as _; +use integration_tests::{ + config::{self, SequencerPartialConfig}, + indexer_client::IndexerClient, + setup::{setup_bedrock_node, setup_indexer, setup_sequencer}, +}; +use sequencer_service_rpc::{RpcClient as _, SequencerClientBuilder}; +use tokio::test; + +const ZONE_LIVE_TIMEOUT: Duration = Duration::from_secs(360); + +// Genesis is block 1, so reaching 2 means a block was produced past it. +const MIN_BLOCK_ID: u64 = 2; + +#[test] +async fn two_zones_share_one_bedrock_and_both_advance() -> Result<()> { + // Declared first so it outlives both zones (drops run in reverse order). + let (_bedrock, bedrock_addr) = setup_bedrock_node() + .await + .context("Failed to set up shared Bedrock node")?; + + let partial = SequencerPartialConfig::default(); + let channel_a = config::bedrock_channel_id(); + let channel_b = config::bedrock_channel_id_b(); + + // Empty genesis is enough: the clock transaction drives block production. + let (seq_a, _seq_a_home) = setup_sequencer(partial, bedrock_addr, vec![], channel_a, None) + .await + .context("Failed to set up zone A sequencer")?; + let (idx_a, _idx_a_home) = setup_indexer(bedrock_addr, channel_a, None) + .await + .context("Failed to set up zone A indexer")?; + let (seq_b, _seq_b_home) = setup_sequencer(partial, bedrock_addr, vec![], channel_b, None) + .await + .context("Failed to set up zone B sequencer")?; + let (idx_b, _idx_b_home) = setup_indexer(bedrock_addr, channel_b, None) + .await + .context("Failed to set up zone B indexer")?; + + let (height_a, height_b) = tokio::try_join!( + wait_until_zone_live("A", seq_a.addr(), idx_a.addr()), + wait_until_zone_live("B", seq_b.addr(), idx_b.addr()), + )?; + + assert!( + height_a >= MIN_BLOCK_ID, + "Zone A indexer only reached block {height_a}, expected >= {MIN_BLOCK_ID}" + ); + assert!( + height_b >= MIN_BLOCK_ID, + "Zone B indexer only reached block {height_b}, expected >= {MIN_BLOCK_ID}" + ); + + Ok(()) +} + +/// Wait for the sequencer to produce past genesis and the indexer to finalize up +/// to it. Returns the indexer's finalized block id. +async fn wait_until_zone_live( + label: &str, + sequencer_addr: SocketAddr, + indexer_addr: SocketAddr, +) -> Result { + let sequencer_url = config::addr_to_url(config::UrlProtocol::Http, sequencer_addr) + .context("Failed to build sequencer URL")?; + let sequencer = SequencerClientBuilder::default() + .build(sequencer_url) + .context("Failed to build sequencer client")?; + + let indexer_url = config::addr_to_url(config::UrlProtocol::Ws, indexer_addr) + .context("Failed to build indexer URL")?; + let indexer = IndexerClient::new(&indexer_url) + .await + .context("Failed to build indexer client")?; + + let wait = async { + loop { + if sequencer.get_last_block_id().await? >= MIN_BLOCK_ID { + break; + } + tokio::time::sleep(Duration::from_secs(2)).await; + } + let target = sequencer.get_last_block_id().await?; + loop { + let finalized = indexer.get_last_finalized_block_id().await?.unwrap_or(0); + if finalized >= target { + log::info!( + "Zone {label} live: sequencer at {target}, indexer finalized {finalized}" + ); + return Ok::(finalized); + } + tokio::time::sleep(Duration::from_secs(2)).await; + } + }; + + tokio::time::timeout(ZONE_LIVE_TIMEOUT, wait) + .await + .with_context(|| format!("Zone {label} did not become live within {ZONE_LIVE_TIMEOUT:?}"))? +} diff --git a/integration_tests/tests/vault.rs b/integration_tests/tests/vault.rs index e9ea2075..70be480e 100644 --- a/integration_tests/tests/vault.rs +++ b/integration_tests/tests/vault.rs @@ -30,7 +30,7 @@ async fn public_transfer_and_public_claim() -> Result<()> { .get_account_balance(recipient_vault_id) .await?; - let transfer_result = wallet::cli::execute_subcommand( + wallet::cli::execute_subcommand( ctx.wallet_mut(), Command::Vault(VaultSubcommand::Transfer { from: public_mention(sender), @@ -39,10 +39,6 @@ async fn public_transfer_and_public_claim() -> Result<()> { }), ) .await?; - assert!( - matches!(transfer_result, SubcommandReturnValue::Empty), - "Expected Empty return value for public vault transfer" - ); let sender_balance_after_transfer = ctx.sequencer_client().get_account_balance(sender).await?; let recipient_balance_after_transfer = ctx @@ -64,7 +60,7 @@ async fn public_transfer_and_public_claim() -> Result<()> { recipient_vault_balance_before + amount ); - let claim_result = wallet::cli::execute_subcommand( + wallet::cli::execute_subcommand( ctx.wallet_mut(), Command::Vault(VaultSubcommand::Claim { account_id: public_mention(recipient), @@ -72,10 +68,6 @@ async fn public_transfer_and_public_claim() -> Result<()> { }), ) .await?; - assert!( - matches!(claim_result, SubcommandReturnValue::Empty), - "Expected Empty return value for public vault claim" - ); let sender_balance_after_claim = ctx.sequencer_client().get_account_balance(sender).await?; let recipient_balance_after_claim = ctx @@ -138,9 +130,9 @@ async fn private_transfer_and_private_claim() -> Result<()> { assert!( matches!( transfer_result, - SubcommandReturnValue::PrivacyPreservingTransfer { .. } + SubcommandReturnValue::TransactionExecuted { .. } ), - "Expected PrivacyPreservingTransfer return value for private vault transfer" + "Expected TransactionExecuted return value for private vault transfer" ); let sender_balance_after_transfer = ctx @@ -179,9 +171,9 @@ async fn private_transfer_and_private_claim() -> Result<()> { assert!( matches!( claim_result, - SubcommandReturnValue::PrivacyPreservingTransfer { .. } + SubcommandReturnValue::TransactionExecuted { .. } ), - "Expected PrivacyPreservingTransfer return value for private vault claim" + "Expected TransactionExecuted return value for private vault claim" ); let sender_balance_after_claim = ctx diff --git a/lee/state_machine/src/program_deployment_transaction/message.rs b/lee/state_machine/src/program_deployment_transaction/message.rs index 866399e8..34c6a806 100644 --- a/lee/state_machine/src/program_deployment_transaction/message.rs +++ b/lee/state_machine/src/program_deployment_transaction/message.rs @@ -1,10 +1,18 @@ use borsh::{BorshDeserialize, BorshSerialize}; -#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)] +#[derive(Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)] pub struct Message { pub(crate) bytecode: Vec, } +impl std::fmt::Debug for Message { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Message") + .field("bytecode", &format_args!("<{} bytes>", self.bytecode.len())) + .finish() + } +} + impl Message { #[must_use] pub const fn new(bytecode: Vec) -> Self { diff --git a/lee/state_machine/src/state/mod.rs b/lee/state_machine/src/state/mod.rs index fcf71a21..5f5a4639 100644 --- a/lee/state_machine/src/state/mod.rs +++ b/lee/state_machine/src/state/mod.rs @@ -194,6 +194,14 @@ impl V03State { self.programs.insert(program.id(), program); } + /// Seeds a single genesis account that is not produced by any transaction + /// (e.g. the cross-zone inbox config or a bridge-lock holding). Lets the + /// sequencer and indexer seed identical zone-specific state after building + /// the shared initial state. + pub fn insert_genesis_account(&mut self, account_id: AccountId, account: Account) { + self.public_state.insert(account_id, account); + } + pub fn apply_state_diff(&mut self, diff: ValidatedStateDiff) { let StateDiff { signer_account_ids, diff --git a/lez/common/src/block.rs b/lez/common/src/block.rs index 6e956f9f..b22eec9f 100644 --- a/lez/common/src/block.rs +++ b/lez/common/src/block.rs @@ -55,6 +55,31 @@ pub struct Block { pub bedrock_status: BedrockStatus, } +impl Block { + /// Recomputes the hash from this block's contents, for integrity verification + /// against the value stored in `header.hash`. + #[must_use] + pub fn recompute_hash(&self) -> BlockHash { + HashableBlockData { + block_id: self.header.block_id, + prev_block_hash: self.header.prev_block_hash, + timestamp: self.header.timestamp, + transactions: self.body.transactions.clone(), + } + .compute_hash() + } + + /// Recomputes the signed hash from the block contents and checks the header + /// signature against `expected_pubkey`. Used to pin a peer zone's + /// block-signing key, so a block inscribed by anyone other than that zone's + /// sequencer is rejected even if it reached the channel. + #[must_use] + pub fn is_signed_by(&self, expected_pubkey: &lee::PublicKey) -> bool { + let hash = HashableBlockData::from(self.clone()).compute_hash(); + self.header.signature.is_valid_for(&hash.0, expected_pubkey) + } +} + impl Serialize for Block { fn serialize(&self, serializer: S) -> Result { crate::borsh_base64::serialize(self, serializer) @@ -76,11 +101,13 @@ pub struct HashableBlockData { } impl HashableBlockData { + /// Domain-separated hash of the block contents: `SHA256(PREFIX || borsh(self))`. + /// The single source of truth for both producing and verifying a block hash. #[must_use] - pub fn into_pending_block(self, signing_key: &lee::PrivateKey) -> Block { + pub fn compute_hash(&self) -> BlockHash { const PREFIX: &[u8; 32] = b"/LEE/v0.3/Message/Block/\x00\x00\x00\x00\x00\x00\x00\x00"; - let data_bytes = borsh::to_vec(&self).unwrap(); + let data_bytes = borsh::to_vec(self).unwrap(); let mut bytes = Vec::with_capacity( PREFIX .len() @@ -89,8 +116,12 @@ impl HashableBlockData { ); bytes.extend_from_slice(PREFIX); bytes.extend_from_slice(&data_bytes); + OwnHasher::hash(&bytes) + } - let hash = OwnHasher::hash(&bytes); + #[must_use] + pub fn into_pending_block(self, signing_key: &lee::PrivateKey) -> Block { + let hash = self.compute_hash(); let signature = lee::Signature::new(signing_key, &hash.0); Block { header: BlockHeader { @@ -132,4 +163,33 @@ mod tests { let block_from_bytes = borsh::from_slice::(&bytes).unwrap(); assert_eq!(hashable, block_from_bytes); } + + #[test] + fn recompute_hash_matches_header_for_well_formed_block() { + let key = lee::PrivateKey::try_new([7_u8; 32]).expect("valid key"); + let block = HashableBlockData { + block_id: 5, + prev_block_hash: HashType([9_u8; 32]), + timestamp: 42, + transactions: vec![test_utils::produce_dummy_empty_transaction()], + } + .into_pending_block(&key); + assert_eq!(block.recompute_hash(), block.header.hash); + } + + #[test] + fn recompute_hash_detects_tampering() { + let key = lee::PrivateKey::try_new([7_u8; 32]).expect("valid key"); + let block = HashableBlockData { + block_id: 5, + prev_block_hash: HashType([9_u8; 32]), + timestamp: 42, + transactions: vec![test_utils::produce_dummy_empty_transaction()], + } + .into_pending_block(&key); + + let mut tampered = block; + tampered.header.timestamp = 99; // header changed; stale hash no longer matches + assert_ne!(tampered.recompute_hash(), tampered.header.hash); + } } diff --git a/lez/common/src/transaction.rs b/lez/common/src/transaction.rs index b5aee648..b5f0bc87 100644 --- a/lez/common/src/transaction.rs +++ b/lez/common/src/transaction.rs @@ -92,9 +92,8 @@ impl LeeTransaction { Ok(diff) } - /// Computes the validated state diff without enforcing the system-account - /// restriction. Shared by [`Self::validate_on_state`] and - /// [`Self::execute_without_system_accounts_check_on_state`]. + /// Computes the validated state diff. Shared by [`Self::validate_on_state`] + /// (which adds the system-account guards) and [`Self::execute_on_state`]. fn compute_state_diff( &self, state: &V03State, @@ -129,16 +128,12 @@ impl LeeTransaction { Ok(self) } - /// Similar to [`Self::execute_check_on_state`], but skips the system-account guard. + /// Executes the transaction against the current state and applies the resulting diff, + /// without the system-account guards enforced by [`Self::execute_check_on_state`]. /// - /// FIXME: HOT FIX (testnet v0.2): the indexer replays blocks the sequencer already - /// accepted, including sequencer-generated deposit transactions that - /// legitimately modify the bridge account. The `TransactionOrigin::Sequencer` - /// tag that lets the sequencer bypass the guard is not carried in the block, - /// so the indexer cannot yet distinguish deposit txs from user txs. - /// - /// REMOVE ME when the indexer can authenticate deposit transactions. - pub fn execute_without_system_accounts_check_on_state( + /// The indexer replays blocks the sequencer already validated and inscribed on Bedrock, + /// so it trusts those inscriptions and re-derives state without re-validating them. + pub fn execute_on_state( self, state: &mut V03State, block_id: BlockId, diff --git a/lez/configs/docker-all-in-one/indexer_config.json b/lez/configs/docker-all-in-one/indexer_config.json index c1ff65b0..5791f64a 100644 --- a/lez/configs/docker-all-in-one/indexer_config.json +++ b/lez/configs/docker-all-in-one/indexer_config.json @@ -3,5 +3,6 @@ "bedrock_config": { "addr": "http://logos-blockchain-node-0:18080" }, - "channel_id": "0101010101010101010101010101010101010101010101010101010101010101" + "channel_id": "0101010101010101010101010101010101010101010101010101010101010101", + "allow_chain_reset": true } diff --git a/lez/cross_zone/Cargo.toml b/lez/cross_zone/Cargo.toml new file mode 100644 index 00000000..465436f1 --- /dev/null +++ b/lez/cross_zone/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "cross_zone" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee.workspace = true +lee_core.workspace = true +programs.workspace = true +cross_zone_inbox_core.workspace = true +bridge_lock_core.workspace = true +ping_core.workspace = true +risc0-zkvm.workspace = true diff --git a/lez/cross_zone/src/lib.rs b/lez/cross_zone/src/lib.rs new file mode 100644 index 00000000..544d4286 --- /dev/null +++ b/lez/cross_zone/src/lib.rs @@ -0,0 +1,189 @@ +//! Host-side cross-zone helpers that need program ids (`programs`) or the state +//! machine (`lee`), kept out of the guest-pure cores. Mirrors `system_accounts`: +//! it resolves builtin program ids and bakes them into transactions and genesis +//! accounts for the watcher (sequencer) and verifier (indexer). + +use std::collections::BTreeMap; + +pub use cross_zone_inbox_core::{CrossZoneConfig, CrossZonePeer}; +use cross_zone_inbox_core::{ + CrossZoneMessage, InboxConfig, Instruction, ZoneId, inbox_config_account_id, + inbox_seen_shard_account_id, +}; +use lee_core::{ + account::{Account, AccountId}, + program::ProgramId, +}; + +/// The cross-zone emission fields a watcher or verifier reads off a source +/// transaction, common to every emitter program. +pub struct Emission { + pub target_zone: ZoneId, + pub target_program_id: ProgramId, + pub target_accounts: Vec<[u8; 32]>, + pub payload: Vec, +} + +/// Whether a program may only be invoked by sequencer-origin transactions. +/// +/// The cross-zone inbox is injected solely by the watcher; a user-submitted call +/// must be rejected at ingress, since `TransactionOrigin` is not carried in the +/// block. +#[must_use] +pub fn is_sequencer_only_program(program_id: ProgramId) -> bool { + program_id == programs::cross_zone_inbox().id() +} + +/// Extracts the cross-zone emission from a source transaction. +/// +/// Recognizes the known emitter programs (`ping_sender`, `bridge_lock`). The +/// watcher and verifier both use this so they agree on what a given source tx +/// emits. +#[must_use] +pub fn extract_emission(program_id: ProgramId, instruction_data: &[u32]) -> Option { + if program_id == programs::ping_sender().id() { + let ping_core::SenderInstruction::Send { + target_zone, + target_program_id, + target_accounts, + payload, + .. + } = risc0_zkvm::serde::from_slice(instruction_data).ok()?; + Some(Emission { + target_zone, + target_program_id, + target_accounts, + payload, + }) + } else if program_id == programs::bridge_lock().id() { + let bridge_lock_core::Instruction::Lock { + target_zone, + target_program_id, + target_accounts, + payload, + .. + } = risc0_zkvm::serde::from_slice(instruction_data).ok()?; + Some(Emission { + target_zone, + target_program_id, + target_accounts, + payload, + }) + } else { + None + } +} + +/// Builds the sequencer-origin dispatch transaction. Pure for fixed inputs, so +/// the watcher's injected tx and the indexer's re-derived tx are byte-identical. +fn build_inbox_dispatch_tx( + inbox_id: ProgramId, + msg: &CrossZoneMessage, + target_account_ids: Vec, +) -> lee::PublicTransaction { + let mut account_ids = Vec::with_capacity(target_account_ids.len().saturating_add(2)); + account_ids.push(inbox_config_account_id(inbox_id)); + account_ids.push(inbox_seen_shard_account_id( + inbox_id, + &msg.src_zone, + msg.src_block_id, + )); + account_ids.extend(target_account_ids); + + let message = lee::public_transaction::Message::try_new( + inbox_id, + account_ids, + vec![], + Instruction::Dispatch(msg.clone()), + ) + .expect("inbox dispatch instruction must serialize"); + + lee::PublicTransaction::new( + message, + lee::public_transaction::WitnessSet::from_raw_parts(vec![]), + ) +} + +/// Builds the dispatch transaction for one peer emission. +/// +/// Both the sequencer's watcher and the indexer's verifier go through this so +/// their transactions are byte-identical for the same emission (the basis of the +/// Option B check). +#[must_use] +pub fn build_dispatch_from_emission( + src_zone: ZoneId, + src_block_id: u64, + src_tx_index: u32, + src_program_id: ProgramId, + target_program_id: ProgramId, + target_accounts: &[[u8; 32]], + payload: Vec, +) -> lee::PublicTransaction { + let msg = CrossZoneMessage { + src_zone, + src_block_id, + src_tx_index, + src_program_id, + target_program_id, + payload, + l1_inclusion_witness: None, + }; + let target_ids = target_accounts + .iter() + .copied() + .map(AccountId::new) + .collect(); + build_inbox_dispatch_tx(programs::cross_zone_inbox().id(), &msg, target_ids) +} + +/// Builds the inbox config account a zone seeds into genesis state. +/// +/// Lets the inbox guest authorize inbound peer messages. The sequencer and +/// indexer seed the same account from the same config, keeping their replayed +/// state consistent. +#[must_use] +pub fn build_inbox_config_account( + self_zone: ZoneId, + cross_zone: &CrossZoneConfig, +) -> (AccountId, Account) { + let inbox_id = programs::cross_zone_inbox().id(); + + let mut allowed_targets = BTreeMap::new(); + for peer in &cross_zone.peers { + allowed_targets.insert(peer.channel_id, peer.allowed_targets.clone()); + } + let config = InboxConfig { + self_zone, + allowed_peers: BTreeMap::new(), + allowed_targets, + }; + + let account = Account { + program_owner: inbox_id, + balance: 0, + data: config + .to_bytes() + .try_into() + .expect("inbox config fits in account data"), + nonce: 0_u128.into(), + }; + (inbox_config_account_id(inbox_id), account) +} + +/// Builds the genesis holding account funding a holder's bridgeable balance. +/// +/// Owned by `bridge_lock`, data is the LE balance. Not produced by any +/// transaction, so the sequencer and indexer both seed it through this one +/// builder. +#[must_use] +pub fn build_holding_account(holder: AccountId, amount: u128) -> (AccountId, Account) { + let account = Account { + program_owner: programs::bridge_lock().id(), + data: bridge_lock_core::balance_bytes(amount) + .to_vec() + .try_into() + .expect("balance fits in account data"), + ..Default::default() + }; + (holder, account) +} diff --git a/lez/indexer/core/Cargo.toml b/lez/indexer/core/Cargo.toml index b49f6fee..14941773 100644 --- a/lez/indexer/core/Cargo.toml +++ b/lez/indexer/core/Cargo.toml @@ -16,6 +16,9 @@ common.workspace = true logos-blockchain-zone-sdk.workspace = true lee.workspace = true lee_core.workspace = true +cross_zone.workspace = true +cross_zone_inbox_core.workspace = true +programs.workspace = true storage.workspace = true testnet_initial_state.workspace = true @@ -29,8 +32,12 @@ futures.workspace = true url.workspace = true logos-blockchain-core.workspace = true serde_json.workspace = true +thiserror.workspace = true async-stream.workspace = true tokio.workspace = true +risc0-zkvm.workspace = true +hex.workspace = true [dev-dependencies] tempfile.workspace = true +ping_core.workspace = true diff --git a/lez/indexer/core/src/block_store.rs b/lez/indexer/core/src/block_store.rs index 4d583686..8c974399 100644 --- a/lez/indexer/core/src/block_store.rs +++ b/lez/indexer/core/src/block_store.rs @@ -2,17 +2,40 @@ use std::{path::Path, sync::Arc}; use anyhow::{Context as _, Result}; use common::{ - block::{BedrockStatus, Block}, + HashType, + block::{BedrockStatus, Block, BlockHeader}, transaction::{LeeTransaction, clock_invocation}, }; -use lee::{Account, AccountId, V03State}; +use lee::{Account, AccountId, GENESIS_BLOCK_ID, V03State}; use lee_core::BlockId; -use log::info; +use log::warn; use logos_blockchain_core::header::HeaderId; use logos_blockchain_zone_sdk::Slot; use storage::indexer::RocksDBIO; use tokio::sync::RwLock; +use crate::{ingest_error::BlockIngestError, stall_reason::StallReason}; + +struct Tip { + block_id: u64, + hash: HashType, +} + +/// Outcome of feeding a parsed L2 block to the validated tip. +pub enum AcceptOutcome { + /// Chained and applied; tip and L1 read cursor both advance. + Applied, + /// A duplicate re-delivery of the current tip. Just L2 advances. + AlreadyApplied, + /// Did not chain or failed to apply; tip stays frozen, stall recorded. + Parked(BlockIngestError), + /// Chained but failed to apply, possibly transiently + /// ([`BlockIngestError::is_retryable`]); nothing recorded, tip and state + /// untouched. The caller retries and parks via + /// [`IndexerStore::record_stall`] once it gives up. + RetryableFailure(BlockIngestError), +} + #[derive(Clone)] pub struct IndexerStore { dbio: Arc, @@ -22,13 +45,19 @@ pub struct IndexerStore { impl IndexerStore { /// Starting database at the start of new chain. /// Creates files if necessary. - pub fn open_db(location: &Path) -> Result { + pub fn open_db(location: &Path, genesis_seed: Vec<(AccountId, Account)>) -> Result { #[cfg(not(feature = "testnet"))] - let initial_state = testnet_initial_state::initial_state(); + let mut initial_state = testnet_initial_state::initial_state(); #[cfg(feature = "testnet")] - let initial_state = testnet_initial_state::initial_state_testnet(); + let mut initial_state = testnet_initial_state::initial_state_testnet(); + // Seed any zone-specific genesis accounts (the cross-zone inbox config and + // bridge-lock holdings) so the indexer's replayed state matches the + // sequencer's; none are produced by a transaction. + for (account_id, account) in genesis_seed { + initial_state.insert_genesis_account(account_id, account); + } let dbio = RocksDBIO::open_or_create(location, &initial_state)?; let current_state = dbio.final_state()?; @@ -118,6 +147,36 @@ impl IndexerStore { Ok(()) } + /// The L1 inscription slot of the validated tip, written atomically with it + /// by [`Self::accept_block`]. `None` on a cold store or one written before + /// the slot was recorded. + pub fn get_tip_slot(&self) -> Result> { + Ok(self.dbio.get_meta_tip_slot_in_db()?.map(Slot::from)) + } + + pub fn get_stall_reason(&self) -> Result> { + let Some(bytes) = self.dbio.get_stall_reason_bytes()? else { + return Ok(None); + }; + let stall: Option = + serde_json::from_slice(&bytes).context("Failed to deserialize stored stall reason")?; + Ok(stall) + } + + pub fn set_stall_reason(&self, stall: &Option) -> Result<()> { + let bytes = serde_json::to_vec(stall).context("Failed to serialize stall reason")?; + self.dbio.put_stall_reason_bytes(&bytes)?; + Ok(()) + } + + /// Clears a recorded stall marker if one is present, skipping the write otherwise. + fn clear_stall_if_present(&self) -> Result<()> { + if self.get_stall_reason()?.is_some() { + self.set_stall_reason(&None)?; + } + Ok(()) + } + /// Recalculation of final state directly from DB. /// /// Used for indexer healthcheck. @@ -139,142 +198,256 @@ impl IndexerStore { .get_account_by_id(*account_id)) } - pub async fn put_block(&self, mut block: Block, l1_header: HeaderId) -> Result<()> { - info!("Applying block {}", block.header.block_id); - { - let mut state_guard = self.current_state.write().await; + /// The last successfully applied block, or `None` on a cold store. + /// Read fresh from the store each call. + fn validated_tip(&self) -> Result> { + let Some(block_id) = self.dbio.get_meta_last_block_id_in_db()? else { + return Ok(None); + }; + let Some(block) = self.dbio.get_block(block_id)? else { + return Ok(None); + }; + Ok(Some(Tip { + block_id, + hash: block.header.hash, + })) + } - let (clock_tx, user_txs) = block - .body - .transactions - .split_last() - .ok_or_else(|| anyhow::anyhow!("Block has no transactions"))?; - - anyhow::ensure!( - *clock_tx == LeeTransaction::Public(clock_invocation(block.header.timestamp)), - "Last transaction in block must be the clock invocation for the block timestamp" - ); - - let is_genesis = block.header.block_id == 1; - for transaction in user_txs { - if is_genesis { - let genesis_tx = match transaction { - LeeTransaction::Public(public_tx) => public_tx, - LeeTransaction::PrivacyPreserving(_) - | LeeTransaction::ProgramDeployment(_) => { - anyhow::bail!("Genesis block should contain only public transactions") - } - }; - state_guard - .transition_from_public_transaction( - genesis_tx, - block.header.block_id, - block.header.timestamp, - ) - .context("Failed to execute genesis public transaction")?; - } else { - transaction - .clone() - .transaction_stateless_check()? - // FIXME: HOT FIX (testnet v0.2): does not check for system account updates due to - // sequencer-generated deposit tx'es; - // CHANGE ME back to `execute_check_on_state` when the indexer can authenticate deposit transactions - .execute_without_system_accounts_check_on_state( - &mut state_guard, - block.header.block_id, - block.header.timestamp, - )?; - } + /// Record the stall reason. + /// + /// - First stall is stored verbatim + /// - Subsequent stalls only bump `orphans_since`, preserving the original cause. + pub fn record_stall( + &self, + header: Option<&BlockHeader>, + l1_slot: Slot, + error: BlockIngestError, + ) -> Result<()> { + let stall = match self.get_stall_reason()? { + Some(mut existing) => { + existing.orphans_since = existing.orphans_since.saturating_add(1); + existing } + None => StallReason { + // need to map out of `header` because they are not ser/de + block_id: header.map(|h| h.block_id), + block_hash: header.map(|h| h.hash), + prev_block_hash: header.map(|h| h.prev_block_hash), + first_seen: header.map(|h| h.timestamp), + l1_slot, + error, + orphans_since: 0, + }, + }; + self.set_stall_reason(&Some(stall)) + } - // Apply the clock invocation directly (it is expected to modify clock accounts). - let LeeTransaction::Public(clock_public_tx) = clock_tx else { - anyhow::bail!("Clock invocation must be a public transaction"); - }; - state_guard.transition_from_public_transaction( - clock_public_tx, - block.header.block_id, - block.header.timestamp, - )?; + /// Validates `block` against the tip and, if it chains, applies it atomically + /// (scratch clone, commit only on full success) and advances the tip. + /// Retryable apply failures return `RetryableFailure` without recording a stall + /// or touching state; other failures record the stall and return `Parked`. + pub async fn accept_block(&self, block: &Block, l1_slot: Slot) -> Result { + let tip = self.validated_tip()?; + + // Re-delivery of an already-applied block is idempotent, not a divergence + if let Some(tip) = &tip + && block.header.block_id <= tip.block_id + && let Some(stored) = self.get_block_at_id(block.header.block_id)? + && stored.header.hash == block.header.hash + { + return Ok(AcceptOutcome::AlreadyApplied); } - // ToDo: Currently we are fetching only finalized blocks - // if it changes, the following lines need to be updated - // to represent correct block finality - block.bedrock_status = BedrockStatus::Finalized; + if let Err(err) = validate_against_tip(tip.as_ref(), block) { + self.record_stall(Some(&block.header), l1_slot, err.clone())?; + return Ok(AcceptOutcome::Parked(err)); + } - info!("Putting block {} into DB", block.header.block_id); - Ok(self.dbio.put_block(&block, l1_header.into())?) + // TODO: we use scratch state to be atomic, but need to revisit how expensive a clone is + let mut scratch = self.current_state.read().await.clone(); + if let Err(err) = apply_block_to_scratch(block, &mut scratch) { + if err.is_retryable() { + return Ok(AcceptOutcome::RetryableFailure(err)); + } + self.record_stall(Some(&block.header), l1_slot, err.clone())?; + return Ok(AcceptOutcome::Parked(err)); + } + + let mut stored = block.clone(); + stored.bedrock_status = BedrockStatus::Finalized; + self.dbio + .put_block(&stored, [0_u8; 32], l1_slot.into_inner(), &scratch) + .context("Failed to persist accepted block")?; + + // Commit in-memory state (infallible) only after the DB write succeeded. + *self.current_state.write().await = scratch; + // Best-effort: the block is durably applied, so a failed stall clear must not + // fail the apply. It self-heals on the next clear. + if let Err(err) = self.clear_stall_if_present() { + warn!("Failed to clear stall marker after applying block: {err:#}"); + } + Ok(AcceptOutcome::Applied) + } +} + +/// Checks that `block` is the valid continuation of `tip`: hash integrity, +/// then block-id continuity, then `prev_block_hash` linkage. A `None` tip +/// (cold store) expects the genesis block. +fn validate_against_tip(tip: Option<&Tip>, block: &Block) -> Result<(), BlockIngestError> { + let computed = block.recompute_hash(); + if computed != block.header.hash { + return Err(BlockIngestError::HashMismatch { + computed, + header: block.header.hash, + }); + } + + match tip { + None => { + if block.header.block_id != GENESIS_BLOCK_ID { + return Err(BlockIngestError::UnexpectedBlockId { + expected: GENESIS_BLOCK_ID, + got: block.header.block_id, + }); + } + } + Some(tip) => { + let expected = tip + .block_id + .checked_add(1) + .expect("block id should not overflow"); + if block.header.block_id != expected { + return Err(BlockIngestError::UnexpectedBlockId { + expected, + got: block.header.block_id, + }); + } + if block.header.prev_block_hash != tip.hash { + return Err(BlockIngestError::BrokenChainLink { + expected_prev: tip.hash, + got_prev: block.header.prev_block_hash, + }); + } + } + } + Ok(()) +} + +/// Applies a block's transactions to `state`, mapping every failure to a +/// [`BlockIngestError`] so the caller can park rather than crash. Operates on a +/// scratch state; the caller commits only on `Ok`. +fn apply_block_to_scratch(block: &Block, state: &mut V03State) -> Result<(), BlockIngestError> { + let (clock_tx, user_txs) = block + .body + .transactions + .split_last() + .ok_or(BlockIngestError::EmptyBlock)?; + + let expected_clock = LeeTransaction::Public(clock_invocation(block.header.timestamp)); + if *clock_tx != expected_clock { + return Err(BlockIngestError::InvalidClockTransaction); + } + + let is_genesis = block.header.block_id == GENESIS_BLOCK_ID; + for (tx_index, transaction) in user_txs.iter().enumerate() { + let state_transition = |err: anyhow::Error| BlockIngestError::StateTransition { + tx_index: tx_index.try_into().expect("tx index fits in u64"), + reason: format!("{err:#}"), + }; + if is_genesis { + let LeeTransaction::Public(public_tx) = transaction else { + return Err(BlockIngestError::NonPublicGenesisTransaction); + }; + state + .transition_from_public_transaction( + public_tx, + block.header.block_id, + block.header.timestamp, + ) + .map_err(|err| state_transition(err.into()))?; + } else { + transaction + .clone() + .execute_on_state(state, block.header.block_id, block.header.timestamp) + .map_err(|err| state_transition(err.into()))?; + } + } + + let LeeTransaction::Public(clock_public_tx) = clock_tx else { + return Err(BlockIngestError::InvalidClockTransaction); + }; + state + .transition_from_public_transaction( + clock_public_tx, + block.header.block_id, + block.header.timestamp, + ) + .map_err(|err| BlockIngestError::StateTransition { + tx_index: user_txs.len().try_into().expect("tx index fits in u64"), + reason: format!("{:#}", anyhow::Error::from(err)), + })?; + + Ok(()) +} + +#[cfg(test)] +mod stall_reason_tests { + use common::HashType; + + use super::*; + use crate::{ingest_error::BlockIngestError, stall_reason::StallReason}; + + #[tokio::test] + async fn stall_reason_roundtrips_and_clears() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + assert!(store.get_stall_reason().expect("get").is_none()); + + let stall = StallReason { + block_id: Some(7), + block_hash: Some(HashType([1_u8; 32])), + prev_block_hash: Some(HashType([2_u8; 32])), + l1_slot: Slot::from(42), + error: BlockIngestError::StateTransition { + tx_index: 0, + reason: "boom".to_owned(), + }, + first_seen: Some(99), + orphans_since: 3, + }; + store.set_stall_reason(&Some(stall)).expect("set stall"); + + let got = store.get_stall_reason().expect("get").expect("present"); + assert_eq!(got.block_id, Some(7)); + assert_eq!(got.orphans_since, 3); + assert!(matches!( + got.error, + BlockIngestError::StateTransition { .. } + )); + assert_eq!(got.block_hash, Some(HashType([1_u8; 32]))); + assert_eq!(got.prev_block_hash, Some(HashType([2_u8; 32]))); + assert_eq!(got.l1_slot, Slot::from(42)); + assert_eq!(got.first_seen, Some(99)); + + store.set_stall_reason(&None).expect("clear"); + assert!(store.get_stall_reason().expect("get").is_none()); } } #[cfg(test)] mod tests { - use common::{HashType, block::HashableBlockData}; + use common::test_utils::{create_transaction_native_token_transfer, produce_dummy_block}; use tempfile::tempdir; use testnet_initial_state::initial_pub_accounts_private_keys; use super::*; - struct TestFixture { - storage: IndexerStore, - from: AccountId, - to: AccountId, - _home: tempfile::TempDir, - } - - #[expect( - clippy::arithmetic_side_effects, - reason = "test helper with bounded inputs" - )] - async fn store_with_transfer_blocks( - block_count: u64, - prev_hash: Option, - ) -> TestFixture { - let home = tempdir().unwrap(); - let storage = IndexerStore::open_db(home.path()).unwrap(); - - let initial_accounts = initial_pub_accounts_private_keys(); - let from = initial_accounts[0].account_id; - let to = initial_accounts[1].account_id; - let sign_key = initial_accounts[0].pub_sign_key.clone(); - - let mut prev_hash = prev_hash; - for i in 0..block_count { - let tx = common::test_utils::create_transaction_native_token_transfer( - from, - u128::from(i), - to, - 10, - &sign_key, - ); - let block_id = i + 1; - - let next_block = common::test_utils::produce_dummy_block(block_id, prev_hash, vec![tx]); - prev_hash = Some(next_block.header.hash); - - storage - .put_block( - next_block, - HeaderId::from([u8::try_from(i + 1).unwrap(); 32]), - ) - .await - .unwrap(); - } - - TestFixture { - storage, - from, - to, - _home: home, - } - } - #[test] fn correct_startup() { let home = tempdir().unwrap(); - let storage = IndexerStore::open_db(home.as_ref()).unwrap(); + let storage = IndexerStore::open_db(home.as_ref(), Vec::new()).unwrap(); let final_id = storage.get_last_block_id().unwrap(); @@ -282,75 +455,474 @@ mod tests { } #[tokio::test] - async fn state_transition() { + async fn accept_block_applies_transfers_and_advances_tip() { let home = tempdir().unwrap(); - let storage = IndexerStore::open_db(home.as_ref()).unwrap(); + let store = IndexerStore::open_db(home.as_ref(), Vec::new()).unwrap(); let initial_accounts = initial_pub_accounts_private_keys(); let from = initial_accounts[0].account_id; let to = initial_accounts[1].account_id; let sign_key = initial_accounts[0].pub_sign_key.clone(); - let clock_tx = LeeTransaction::Public(clock_invocation(0)); - let genesis_block_data = HashableBlockData { - block_id: 1, - prev_block_hash: HashType::default(), - timestamp: 0, - transactions: vec![clock_tx], - }; - let genesis_block = genesis_block_data - .into_pending_block(&common::test_utils::sequencer_sign_key_for_testing()); - let mut prev_hash = Some(genesis_block.header.hash); - storage - .put_block(genesis_block, HeaderId::from([0_u8; 32])) - .await - .unwrap(); + // Genesis (block 1): clock-only. + let genesis = produce_dummy_block(1, None, vec![]); + let mut prev_hash = genesis.header.hash; + assert!(matches!( + store.accept_block(&genesis, Slot::from(0)).await.unwrap(), + AcceptOutcome::Applied + )); - for i in 0..10_u128 { - let tx = common::test_utils::create_transaction_native_token_transfer( - from, i, to, 10, &sign_key, - ); - let block_id = u64::try_from(i + 1).unwrap(); - let next_block = common::test_utils::produce_dummy_block(block_id, prev_hash, vec![tx]); - prev_hash = Some(next_block.header.hash); - storage - .put_block( - next_block, - HeaderId::from([u8::try_from(i + 1).unwrap(); 32]), - ) - .await - .unwrap(); + // Blocks 2..=11: one native transfer of 10 each (nonces 0..=9). + for i in 0..10_u64 { + let tx = create_transaction_native_token_transfer(from, i.into(), to, 10, &sign_key); + let block = produce_dummy_block(i + 2, Some(prev_hash), vec![tx]); + prev_hash = block.header.hash; + assert!(matches!( + store.accept_block(&block, Slot::from(0)).await.unwrap(), + AcceptOutcome::Applied + )); } - let acc1_val = storage.account_current_state(&from).await.unwrap(); - let acc2_val = storage.account_current_state(&to).await.unwrap(); - - assert_eq!(acc1_val.balance, 9900); - assert_eq!(acc2_val.balance, 20100); + assert_eq!( + store.account_current_state(&from).await.unwrap().balance, + 9900 + ); + assert_eq!( + store.account_current_state(&to).await.unwrap().balance, + 20100 + ); + // Tip advanced to the last applied block; a clean run leaves no stall. + assert_eq!(store.get_last_block_id().unwrap(), Some(11)); + assert!(store.get_stall_reason().unwrap().is_none()); } #[tokio::test] - async fn account_state_at_block() { - let TestFixture { - storage, - from, - to, - _home, - } = store_with_transfer_blocks(10, None).await; + async fn account_state_at_block_reflects_history() { + let home = tempdir().unwrap(); + let store = IndexerStore::open_db(home.as_ref(), Vec::new()).unwrap(); - let acc1_at_1 = storage.account_state_at_block(&from, 1).unwrap(); - let acc2_at_1 = storage.account_state_at_block(&to, 1).unwrap(); - assert_eq!(acc1_at_1.balance, 9990); - assert_eq!(acc2_at_1.balance, 20010); + let initial_accounts = initial_pub_accounts_private_keys(); + let from = initial_accounts[0].account_id; + let to = initial_accounts[1].account_id; + let sign_key = initial_accounts[0].pub_sign_key.clone(); - let acc1_at_5 = storage.account_state_at_block(&from, 5).unwrap(); - let acc2_at_5 = storage.account_state_at_block(&to, 5).unwrap(); - assert_eq!(acc1_at_5.balance, 9950); - assert_eq!(acc2_at_5.balance, 20050); + let genesis = produce_dummy_block(1, None, vec![]); + let mut prev_hash = genesis.header.hash; + store.accept_block(&genesis, Slot::from(0)).await.unwrap(); - let acc1_at_9 = storage.account_state_at_block(&from, 9).unwrap(); - let acc2_at_9 = storage.account_state_at_block(&to, 9).unwrap(); - assert_eq!(acc1_at_9.balance, 9910); - assert_eq!(acc2_at_9.balance, 20090); + for i in 0..10_u64 { + let tx = create_transaction_native_token_transfer(from, i.into(), to, 10, &sign_key); + let block = produce_dummy_block(i + 2, Some(prev_hash), vec![tx]); + prev_hash = block.header.hash; + store.accept_block(&block, Slot::from(0)).await.unwrap(); + } + + // State at block N is inclusive of block N. + // Block 1 (genesis, clock-only): no transfers yet. + assert_eq!( + store.account_state_at_block(&from, 1).unwrap().balance, + 10000 + ); + assert_eq!(store.account_state_at_block(&to, 1).unwrap().balance, 20000); + // Through block 5: 4 transfers applied (blocks 2..=5). + assert_eq!( + store.account_state_at_block(&from, 5).unwrap().balance, + 9960 + ); + assert_eq!(store.account_state_at_block(&to, 5).unwrap().balance, 20040); + // Through block 9: 8 transfers applied (blocks 2..=9). + assert_eq!( + store.account_state_at_block(&from, 9).unwrap().balance, + 9920 + ); + assert_eq!(store.account_state_at_block(&to, 9).unwrap().balance, 20080); + } +} + +#[cfg(test)] +mod accept_tests { + use common::{HashType, block::HashableBlockData, test_utils::produce_dummy_block}; + + use super::*; + use crate::ingest_error::BlockIngestError; + + fn signing_key() -> lee::PrivateKey { + lee::PrivateKey::try_new([7_u8; 32]).expect("valid key") + } + + // A block with a correct hash but empty body β€” enough to exercise the + // acceptance checks (id/link/hash), which run before any state application. + fn valid_hash_block(block_id: u64, prev: HashType) -> common::block::Block { + HashableBlockData { + block_id, + prev_block_hash: prev, + timestamp: 0, + transactions: vec![], + } + .into_pending_block(&signing_key()) + } + + #[tokio::test] + async fn non_genesis_first_block_parks_with_unexpected_id() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + let block = valid_hash_block(2, HashType([0_u8; 32])); + let outcome = store + .accept_block(&block, Slot::from(0)) + .await + .expect("accept"); + + assert!(matches!( + outcome, + AcceptOutcome::Parked(BlockIngestError::UnexpectedBlockId { + expected: 1, + got: 2 + }) + )); + let stall = store.get_stall_reason().expect("get").expect("present"); + assert_eq!(stall.block_id, Some(2)); + assert_eq!(stall.orphans_since, 0); + } + + #[tokio::test] + async fn hash_mismatch_parks() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + let mut block = valid_hash_block(1, HashType([0_u8; 32])); + block.header.timestamp = 999; // invalidates the stored hash + + let outcome = store + .accept_block(&block, Slot::from(0)) + .await + .expect("accept"); + assert!(matches!( + outcome, + AcceptOutcome::Parked(BlockIngestError::HashMismatch { .. }) + )); + } + + #[tokio::test] + async fn second_break_bumps_orphan_count_and_keeps_first() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + let first = valid_hash_block(2, HashType([0_u8; 32])); + store + .accept_block(&first, Slot::from(0)) + .await + .expect("accept"); + let second = valid_hash_block(3, HashType([0_u8; 32])); + store + .accept_block(&second, Slot::from(0)) + .await + .expect("accept"); + + let stall = store.get_stall_reason().expect("get").expect("present"); + assert_eq!(stall.block_id, Some(2), "first stall preserved"); + assert_eq!(stall.orphans_since, 1, "second break counted as orphan"); + } + + #[tokio::test] + async fn deserialize_break_records_stall_without_header() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + store + .record_stall( + None, + Slot::from(0), + BlockIngestError::Deserialize("bad bytes".to_owned()), + ) + .expect("record"); + + let stall = store.get_stall_reason().expect("get").expect("present"); + assert_eq!(stall.block_id, None); + assert!(matches!(stall.error, BlockIngestError::Deserialize(_))); + } + + #[tokio::test] + async fn parks_then_recovers_on_valid_continuation() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + // Genesis (block 1, clock-only) applies and advances the tip. + let genesis = produce_dummy_block(1, None, vec![]); + assert!(matches!( + store.accept_block(&genesis, Slot::from(0)).await.unwrap(), + AcceptOutcome::Applied + )); + + // A block that skips ahead (id 3 while the tip is 1) parks the indexer. + let bad = produce_dummy_block(3, Some(genesis.header.hash), vec![]); + assert!(matches!( + store.accept_block(&bad, Slot::from(0)).await.unwrap(), + AcceptOutcome::Parked(BlockIngestError::UnexpectedBlockId { + expected: 2, + got: 3 + }) + )); + assert!( + store.get_stall_reason().unwrap().is_some(), + "indexer should be parked after the bad block" + ); + assert_eq!( + store.get_last_block_id().unwrap(), + Some(1), + "validated tip must stay frozen at genesis while parked" + ); + + // The valid continuation (block 2 chaining on genesis) recovers the chain. + let next = produce_dummy_block(2, Some(genesis.header.hash), vec![]); + assert!(matches!( + store.accept_block(&next, Slot::from(0)).await.unwrap(), + AcceptOutcome::Applied + )); + assert!( + store.get_stall_reason().unwrap().is_none(), + "stall reason must clear on recovery" + ); + assert_eq!( + store.get_last_block_id().unwrap(), + Some(2), + "tip must advance to the recovered block" + ); + } + + #[tokio::test] + async fn accept_block_records_tip_inscription_slot() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + assert_eq!(store.get_tip_slot().expect("get"), None); + + let genesis = produce_dummy_block(1, None, vec![]); + store + .accept_block(&genesis, Slot::from(1_000)) + .await + .expect("accept"); + assert_eq!(store.get_tip_slot().expect("get"), Some(Slot::from(1_000))); + + let block2 = produce_dummy_block(2, Some(genesis.header.hash), vec![]); + store + .accept_block(&block2, Slot::from(1_005)) + .await + .expect("accept"); + assert_eq!(store.get_tip_slot().expect("get"), Some(Slot::from(1_005))); + + // A parked block freezes the tip, so its slot must not advance either. + let bad = produce_dummy_block(4, Some(block2.header.hash), vec![]); + assert!(matches!( + store.accept_block(&bad, Slot::from(1_010)).await.unwrap(), + AcceptOutcome::Parked(_) + )); + assert_eq!(store.get_tip_slot().expect("get"), Some(Slot::from(1_005))); + + // Neither must a re-delivered old block move it. + assert!(matches!( + store + .accept_block(&genesis, Slot::from(1_015)) + .await + .unwrap(), + AcceptOutcome::AlreadyApplied + )); + assert_eq!(store.get_tip_slot().expect("get"), Some(Slot::from(1_005))); + } + + #[tokio::test] + async fn redelivered_tip_block_is_idempotent_not_parked() { + use testnet_initial_state::initial_pub_accounts_private_keys; + + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + let accounts = initial_pub_accounts_private_keys(); + let from = accounts[0].account_id; + let to = accounts[1].account_id; + let sign_key = accounts[0].pub_sign_key.clone(); + + let genesis = produce_dummy_block(1, None, vec![]); + store + .accept_block(&genesis, Slot::from(0)) + .await + .expect("accept genesis"); + + // Block 2: a single transfer of 10. + let tx = common::test_utils::create_transaction_native_token_transfer( + from, 0, to, 10, &sign_key, + ); + let block = produce_dummy_block(2, Some(genesis.header.hash), vec![tx]); + assert!(matches!( + store.accept_block(&block, Slot::from(0)).await.unwrap(), + AcceptOutcome::Applied + )); + let balance_after = store.account_current_state(&from).await.unwrap().balance; + + // Re-deliver the exact same block: idempotent skip, no state change, no park. + assert!(matches!( + store.accept_block(&block, Slot::from(0)).await.unwrap(), + AcceptOutcome::AlreadyApplied + )); + assert_eq!( + store.account_current_state(&from).await.unwrap().balance, + balance_after, + "re-delivered block must not be applied twice" + ); + assert_eq!( + store.get_last_block_id().unwrap(), + Some(2), + "tip must stay at the already-applied block" + ); + assert!( + store.get_stall_reason().unwrap().is_none(), + "a benign duplicate must not park the indexer" + ); + } + + #[tokio::test] + async fn redelivered_block_below_tip_is_idempotent_not_parked() { + use testnet_initial_state::initial_pub_accounts_private_keys; + + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + let accounts = initial_pub_accounts_private_keys(); + let from = accounts[0].account_id; + let to = accounts[1].account_id; + let sign_key = accounts[0].pub_sign_key.clone(); + + // Build a short chain: genesis (1) -> block 2 -> block 3, so the tip is 3. + let genesis = produce_dummy_block(1, None, vec![]); + store + .accept_block(&genesis, Slot::from(0)) + .await + .expect("accept genesis"); + + let tx2 = common::test_utils::create_transaction_native_token_transfer( + from, 0, to, 10, &sign_key, + ); + let block2 = produce_dummy_block(2, Some(genesis.header.hash), vec![tx2]); + assert!(matches!( + store.accept_block(&block2, Slot::from(0)).await.unwrap(), + AcceptOutcome::Applied + )); + + let tx3 = common::test_utils::create_transaction_native_token_transfer( + from, 1, to, 10, &sign_key, + ); + let block3 = produce_dummy_block(3, Some(block2.header.hash), vec![tx3]); + assert!(matches!( + store.accept_block(&block3, Slot::from(0)).await.unwrap(), + AcceptOutcome::Applied + )); + + let balance_after = store.account_current_state(&from).await.unwrap().balance; + + // Re-deliver block 2 (id below the tip): a re-delivery, not a divergence. + assert!(matches!( + store.accept_block(&block2, Slot::from(0)).await.unwrap(), + AcceptOutcome::AlreadyApplied + )); + assert_eq!( + store.account_current_state(&from).await.unwrap().balance, + balance_after, + "re-delivered block below the tip must not be applied again" + ); + assert_eq!( + store.get_last_block_id().unwrap(), + Some(3), + "tip must stay at the current head" + ); + assert!( + store.get_stall_reason().unwrap().is_none(), + "a benign re-delivery must not park the indexer" + ); + } + + #[tokio::test] + async fn accept_block_snapshots_state_at_breakpoint_interval() { + use testnet_initial_state::initial_pub_accounts_private_keys; + + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + let accounts = initial_pub_accounts_private_keys(); + let from = accounts[0].account_id; + let to = accounts[1].account_id; + let sign_key = accounts[0].pub_sign_key.clone(); + + let genesis = produce_dummy_block(1, None, vec![]); + assert!(matches!( + store.accept_block(&genesis, Slot::from(0)).await.unwrap(), + AcceptOutcome::Applied + )); + let mut prev_hash = genesis.header.hash; + + // Blocks 2..=101: one transfer of 1 each; block 100 crosses the interval. + for i in 0..100_u64 { + let tx = common::test_utils::create_transaction_native_token_transfer( + from, + i.into(), + to, + 1, + &sign_key, + ); + let block = produce_dummy_block(i + 2, Some(prev_hash), vec![tx]); + prev_hash = block.header.hash; + assert!(matches!( + store.accept_block(&block, Slot::from(0)).await.unwrap(), + AcceptOutcome::Applied + )); + } + + // Snapshot at block 100 = genesis + 99 transfers, written with the block. + let bp1 = store.dbio.get_breakpoint(1).expect("breakpoint 1 present"); + assert_eq!(bp1.get_account_by_id(from).balance, 10000 - 99); + + // The #605 restart: reopening past the boundary must work. + drop(store); + let reopened = IndexerStore::open_db(dir.path(), Vec::new()).expect("reopen"); + assert_eq!(reopened.last_block().unwrap(), Some(101)); + } + + #[tokio::test] + async fn transient_apply_failure_returns_retryable_failure_without_stall() { + use testnet_initial_state::initial_pub_accounts_private_keys; + + let dir = tempfile::tempdir().expect("tempdir"); + let store = IndexerStore::open_db(dir.path(), Vec::new()).expect("open store"); + + let accounts = initial_pub_accounts_private_keys(); + let from = accounts[0].account_id; + let to = accounts[1].account_id; + let sign_key = accounts[0].pub_sign_key.clone(); + + let genesis = produce_dummy_block(1, None, vec![]); + store + .accept_block(&genesis, Slot::from(0)) + .await + .expect("accept genesis"); + + // Overdraft: rejected during execution β†’ StateTransition β†’ retryable. + let tx = common::test_utils::create_transaction_native_token_transfer( + from, + 0, + to, + 1_000_000_000, + &sign_key, + ); + let block = produce_dummy_block(2, Some(genesis.header.hash), vec![tx]); + let outcome = store.accept_block(&block, Slot::from(0)).await.unwrap(); + + assert!(matches!( + outcome, + AcceptOutcome::RetryableFailure(BlockIngestError::StateTransition { .. }) + )); + assert!( + store.get_stall_reason().unwrap().is_none(), + "retryable failure must not persist a stall" + ); + assert_eq!(store.get_last_block_id().unwrap(), Some(1), "tip frozen"); } } diff --git a/lez/indexer/core/src/chain_consistency.rs b/lez/indexer/core/src/chain_consistency.rs new file mode 100644 index 00000000..df4b5ba6 --- /dev/null +++ b/lez/indexer/core/src/chain_consistency.rs @@ -0,0 +1,550 @@ +//! Startup check that the local store still belongs to the chain the +//! connected channel serves. + +use anyhow::Result; +use common::{HashType, block::Block}; +use futures::StreamExt as _; +use log::warn; +use logos_blockchain_zone_sdk::{Slot, ZoneMessage, adapter::Node as _}; + +use crate::IndexerCore; + +/// Upper bound on the channel reads of the startup consistency check. +const CHANNEL_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60); + +/// Result of comparing the indexer's stored chain against the channel. +pub enum ChainConsistency { + /// Channel still serves our anchor block (the stored tip position, or the + /// parked block while stalled). + Consistent, + /// We could not determine the outcome due to one of: + /// + /// - cold store (no anchor to compare at) + /// - the channel served only blocks newer than the anchor + /// - or the channel read was inconclusive (timeout / error / empty stream) + /// + /// NOTE: None of these prove a reset, so the caller proceeds. + /// A genuine divergence is still caught later when the ingest loop tries to apply and parks. + Inconclusive, + /// Positive evidence that the channel is a different chain than the store. + /// + /// Details in [`ChainMismatch`], and impl's Display trait. + Inconsistent(ChainMismatch), +} + +/// The evidence behind a [`ChainConsistency::Inconsistent`]. +pub enum ChainMismatch { + /// The channel serves a different block at the anchor's id. + Block { + ours: (u64, HashType), + channel: (u64, HashType), + }, + /// The channel serves a block at/below the anchor's id past the anchor + /// slot; on the same chain those ids live at earlier slots. + ReinscribedBlock { + channel: (u64, HashType), + slot: Slot, + anchor_slot: Slot, + }, + /// The channel has content past the anchor slot but no longer the + /// inscription we anchored on. + AnchorSlotChanged { anchor_slot: Slot }, + /// The channel does not exist on the connected chain. + ChannelMissing, + /// The channel's history ends before the anchor slot. + ChannelBehindAnchor { tip_slot: Slot, anchor_slot: Slot }, +} + +impl std::fmt::Display for ChainMismatch { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Block { ours, channel } => write!( + f, + "stored block {} {} != channel block {} {}", + ours.0, ours.1, channel.0, channel.1 + ), + Self::ReinscribedBlock { + channel, + slot, + anchor_slot, + } => write!( + f, + "channel re-serves block {} {} at slot {} past our anchor slot {}", + channel.0, + channel.1, + slot.into_inner(), + anchor_slot.into_inner() + ), + Self::AnchorSlotChanged { anchor_slot } => write!( + f, + "channel content at slot {} no longer includes the inscription we parked on", + anchor_slot.into_inner() + ), + Self::ChannelMissing => write!(f, "channel does not exist on the connected chain"), + Self::ChannelBehindAnchor { + tip_slot, + anchor_slot, + } => write!( + f, + "channel tip slot {} is behind our anchor slot {}", + tip_slot.into_inner(), + anchor_slot.into_inner() + ), + } + } +} + +/// A block that must still be inscribed at `slot` if the channel is the chain +/// the store was built from: the tip at the read cursor, or the recorded +/// parked block while stalled. +struct Anchor { + slot: Slot, + /// The anchor block's `(id, hash)`. + /// + /// `None` when parked on an undeserializable inscription (no header was recorded). + block: Option<(u64, HashType)>, +} + +impl Anchor { + /// Probes a channel message read at/after the anchor slot. + /// See [`IndexerCore::verify_chain_at_anchor`]. + pub fn probe_anchor_slot(&self, msg: &ZoneMessage, slot: Slot) -> AnchorProbe { + if slot < self.slot { + return AnchorProbe::KeepLooking; + } + let Some((anchor_id, anchor_hash)) = self.block else { + // Anchored on an undeserializable inscription: any message still + // present at that slot means the history is intact. + return if slot == self.slot { + AnchorProbe::SameChain + } else { + AnchorProbe::Mismatch(ChainMismatch::AnchorSlotChanged { + anchor_slot: self.slot, + }) + }; + }; + let ZoneMessage::Block(zone_block) = msg else { + return AnchorProbe::KeepLooking; + }; + let Ok(block) = borsh::from_slice::(&zone_block.data) else { + return AnchorProbe::KeepLooking; + }; + let (id, hash) = (block.header.block_id, block.header.hash); + if id == anchor_id { + return if hash == anchor_hash { + AnchorProbe::SameChain + } else { + AnchorProbe::Mismatch(ChainMismatch::Block { + ours: (anchor_id, anchor_hash), + channel: (id, hash), + }) + }; + } + if id > anchor_id { + return AnchorProbe::Bail; + } + if slot == self.slot { + // Older ids can share the anchor's slot on the same chain. + return AnchorProbe::KeepLooking; + } + // An id below the anchor served past the anchor slot is impossible on the + // same chain, even if the content is identical (deterministic genesis). + AnchorProbe::Mismatch(ChainMismatch::ReinscribedBlock { + channel: (id, hash), + slot, + anchor_slot: self.slot, + }) + } +} + +/// What a single channel message tells the anchored consistency check. +enum AnchorProbe { + /// The anchor is still in place: same chain. + SameChain, + Mismatch(ChainMismatch), + /// Only newer ids past the anchor: plausible on the same chain, so stop + /// scanning without a verdict. + Bail, + KeepLooking, +} + +#[expect( + clippy::multiple_inherent_impl, + reason = "split for clarity & isolation of relevant code" +)] +impl IndexerCore { + /// Verifies whether the channel still serves the same chain the store was built from. + /// This may change frequently during development where we reset the chain from time to + /// time in devnet/testnet, but we do not expect [`ChainConsistency::Inconsistent`] in + /// production. + /// + /// To compare the chains, we use an [`Anchor`] block that is either the parked L2 block + /// while stalled, or the tip L2 block at its own inscription L1 slot. + pub(crate) async fn verify_chain_consistency(&self) -> Result { + let Some(anchor) = self.get_startup_anchor()? else { + // empty or cold store: nothing to compare + return Ok(ChainConsistency::Inconclusive); + }; + + self.verify_chain_at_anchor(&anchor).await + } + + /// Builds the anchor for the startup check. + /// + /// - If stalled, returns the recorded _parked_ block + /// - If not stalled, returns the validated tip at its _own_ inscription slot. + /// - If the store is empty, returns `None`. + fn get_startup_anchor(&self) -> Result> { + if let Some(stall) = self.store.get_stall_reason()? { + return Ok(Some(Anchor { + slot: stall.l1_slot, + block: stall.block_id.zip(stall.block_hash), + })); + } + + // not stalled, so anchor on the tip at its own inscription slot + let Some(slot) = self.store.get_tip_slot()?.or(self.store.get_zone_cursor()?) else { + return Ok(None); + }; + let Some(tip_id) = self.store.get_last_block_id()? else { + return Ok(None); + }; + let Some(tip) = self.store.get_block_at_id(tip_id)? else { + return Ok(None); + }; + Ok(Some(Anchor { + slot, + block: Some((tip_id, tip.header.hash)), + })) + } + + /// Verifies the channel still carries the anchor block at its slot. + /// + /// The anchor was finalized at `anchor.slot`, so the same chain must still + /// serve it there, while a reset chain re-inscribes its content only at + /// later wall-clock slots. + /// + /// Only positive evidence of a different chain yields `Inconsistent`. + /// Absence of data stays `Inconclusive`. + async fn verify_chain_at_anchor(&self, anchor: &Anchor) -> Result { + match self.node.channel_state(self.config.channel_id).await { + Ok(state) => { + if let Some(mismatch) = frontier_verdict(anchor.slot, state.map(|s| s.tip_slot)) { + return Ok(ChainConsistency::Inconsistent(mismatch)); + } + } + Err(err) => { + warn!("Failed to read channel state for the consistency check: {err:#}"); + } + } + + // `next_messages` is exclusive, so `slot - 1` includes the anchor slot. + let Some(from_slot) = anchor.slot.into_inner().checked_sub(1) else { + return Ok(ChainConsistency::Inconclusive); + }; + + let scan = async { + let stream = self + .zone_indexer + .next_messages(Some(Slot::from(from_slot))) + .await?; + let mut stream = std::pin::pin!(stream); + + while let Some((msg, slot)) = stream.next().await { + match anchor.probe_anchor_slot(&msg, slot) { + AnchorProbe::SameChain => return Ok(Some(ChainConsistency::Consistent)), + AnchorProbe::Mismatch(mismatch) => { + return Ok(Some(ChainConsistency::Inconsistent(mismatch))); + } + AnchorProbe::Bail => return Ok(Some(ChainConsistency::Inconclusive)), + AnchorProbe::KeepLooking => { /* dont do anything */ } + } + } + Ok::<_, anyhow::Error>(None) + }; + + match tokio::time::timeout(CHANNEL_READ_TIMEOUT, scan).await { + Ok(Ok(Some(outcome))) => Ok(outcome), + Ok(Ok(None)) => Ok(ChainConsistency::Inconclusive), + Ok(Err(err)) => { + warn!( + "Failed to read the anchor slot for the consistency check; proceeding: {err:#}" + ); + Ok(ChainConsistency::Inconclusive) + } + Err(_elapsed) => { + warn!("Timed out reading the anchor slot for the consistency check; proceeding"); + Ok(ChainConsistency::Inconclusive) + } + } + } +} + +/// Checks the channel frontier against the anchor slot. +/// +/// The anchor block was finalized at `anchor_slot`, so on the same chain the +/// channel tip can never be behind it, and the channel must exist. +fn frontier_verdict(anchor_slot: Slot, channel_tip_slot: Option) -> Option { + match channel_tip_slot { + None => Some(ChainMismatch::ChannelMissing), + Some(tip_slot) if tip_slot < anchor_slot => Some(ChainMismatch::ChannelBehindAnchor { + tip_slot, + anchor_slot, + }), + Some(_) => None, + } +} + +#[cfg(test)] +mod tests { + use std::time::Duration; + + use common::block::HashableBlockData; + use logos_blockchain_core::mantle::ops::channel::{MsgId, inscribe::Inscription}; + use logos_blockchain_zone_sdk::ZoneBlock; + + use super::*; + use crate::{ + BlockIngestError, + block_store::AcceptOutcome, + config::{ChannelId, ClientConfig, IndexerConfig}, + }; + + fn unreachable_core(dir: &std::path::Path) -> IndexerCore { + let config = IndexerConfig { + consensus_info_polling_interval: Duration::from_secs(1), + bedrock_config: ClientConfig { + addr: "http://localhost:1".parse().expect("url"), + auth: None, + }, + channel_id: ChannelId::from([1; 32]), + allow_chain_reset: false, + cross_zone: None, + bridge_lock_holdings: Vec::new(), + }; + IndexerCore::open(config, dir).expect("open core") + } + + fn test_block(block_id: u64, timestamp: u64) -> Block { + HashableBlockData { + block_id, + prev_block_hash: HashType([0; 32]), + timestamp, + transactions: vec![], + } + .into_pending_block(&lee::PrivateKey::try_new([7; 32]).expect("valid key")) + } + + fn block_msg(block: &Block) -> ZoneMessage { + let bytes = borsh::to_vec(block).expect("serialize"); + ZoneMessage::Block(ZoneBlock { + id: MsgId::from([0_u8; 32]), + data: Inscription::try_from(bytes.as_slice()).expect("inscription"), + }) + } + + fn anchor_for(block: &Block, slot: Slot) -> Anchor { + Anchor { + slot, + block: Some((block.header.block_id, block.header.hash)), + } + } + + #[tokio::test] + async fn cold_store_is_inconclusive() { + // An empty store has no cursor, so there is nothing to compare: the check + // must be Inconclusive (not Consistent), and it returns before any L1 read. + let dir = tempfile::tempdir().expect("tempdir"); + let core = unreachable_core(dir.path()); + assert!(matches!( + core.verify_chain_consistency().await.expect("verify"), + ChainConsistency::Inconclusive + )); + } + + #[tokio::test] + async fn parked_store_with_unreachable_node_is_inconclusive() { + // Network failure is not evidence of a reset: a parked store must stay + // parked (Inconclusive), not error out or trip the wipe path. + let dir = tempfile::tempdir().expect("tempdir"); + let core = unreachable_core(dir.path()); + let parked = test_block(5, 42); + core.store + .record_stall( + Some(&parked.header), + Slot::from(1_000), + BlockIngestError::EmptyBlock, + ) + .expect("record stall"); + assert!(matches!( + core.verify_chain_consistency().await.expect("verify"), + ChainConsistency::Inconclusive + )); + } + + #[tokio::test] + async fn caught_up_store_with_unreachable_node_is_inconclusive() { + let dir = tempfile::tempdir().expect("tempdir"); + let core = unreachable_core(dir.path()); + let genesis = common::test_utils::produce_dummy_block(1, None, vec![]); + assert!(matches!( + core.store + .accept_block(&genesis, Slot::from(1_000)) + .await + .expect("accept"), + AcceptOutcome::Applied + )); + core.store + .set_zone_cursor(&Slot::from(1_000)) + .expect("set cursor"); + assert!(matches!( + core.verify_chain_consistency().await.expect("verify"), + ChainConsistency::Inconclusive + )); + } + + #[tokio::test] + async fn startup_anchor_prefers_tip_slot_over_lagging_cursor() { + // Cursor persist failures are warn-only, so the read cursor can lag the + // tip by several blocks. The anchor must pair the tip with its own + // inscription slot; pairing it with the stale cursor would make the scan + // misread the chain's intermediate blocks as re-inscriptions. + let dir = tempfile::tempdir().expect("tempdir"); + let core = unreachable_core(dir.path()); + + let genesis = common::test_utils::produce_dummy_block(1, None, vec![]); + core.store + .accept_block(&genesis, Slot::from(1_000)) + .await + .expect("accept"); + let block2 = common::test_utils::produce_dummy_block(2, Some(genesis.header.hash), vec![]); + core.store + .accept_block(&block2, Slot::from(1_005)) + .await + .expect("accept"); + let block3 = common::test_utils::produce_dummy_block(3, Some(block2.header.hash), vec![]); + core.store + .accept_block(&block3, Slot::from(1_010)) + .await + .expect("accept"); + + // Cursor last persisted at the genesis slot: two blocks behind the tip. + core.store + .set_zone_cursor(&Slot::from(1_000)) + .expect("set cursor"); + + let anchor = core.get_startup_anchor().expect("anchor").expect("present"); + assert_eq!(anchor.slot, Slot::from(1_010)); + assert_eq!(anchor.block, Some((3, block3.header.hash))); + } + + #[test] + fn probe_finds_anchor_block_at_slot() { + let tip = test_block(5, 42); + let anchor = anchor_for(&tip, Slot::from(1_000)); + assert!(matches!( + anchor.probe_anchor_slot(&block_msg(&tip), Slot::from(1_000)), + AnchorProbe::SameChain + )); + } + + #[test] + fn probe_flags_different_block_at_anchor_id() { + let tip = test_block(5, 42); + let anchor = anchor_for(&tip, Slot::from(1_000)); + // Same id, different content (timestamp) => different hash. + let other = test_block(5, 43); + assert!(matches!( + anchor.probe_anchor_slot(&block_msg(&other), Slot::from(1_000)), + AnchorProbe::Mismatch(ChainMismatch::Block { .. }) + )); + } + + #[test] + fn probe_flags_old_id_reinscribed_past_the_anchor_slot() { + // A reset chain re-inscribes from genesis at later slots. Even if the + // content is byte-identical (deterministic genesis), an id at/below + // the anchor past the anchor slot is impossible on the same chain. + let tip = test_block(5, 42); + let anchor = anchor_for(&tip, Slot::from(1_000)); + let genesis = test_block(1, 0); + assert!(matches!( + anchor.probe_anchor_slot(&block_msg(&genesis), Slot::from(1_001)), + AnchorProbe::Mismatch(ChainMismatch::ReinscribedBlock { .. }) + )); + } + + #[test] + fn probe_skips_older_blocks_sharing_the_anchor_slot() { + let tip = test_block(5, 42); + let anchor = anchor_for(&tip, Slot::from(1_000)); + let earlier = test_block(4, 41); + assert!(matches!( + anchor.probe_anchor_slot(&block_msg(&earlier), Slot::from(1_000)), + AnchorProbe::KeepLooking + )); + } + + #[test] + fn probe_bails_on_newer_ids_past_the_anchor() { + // Blocks newer than the anchor are plausible on the same chain (e.g. + // published while we were down), so they must never count as evidence. + let tip = test_block(5, 42); + let anchor = anchor_for(&tip, Slot::from(1_000)); + let newer = test_block(6, 43); + assert!(matches!( + anchor.probe_anchor_slot(&block_msg(&newer), Slot::from(1_001)), + AnchorProbe::Bail + )); + } + + #[test] + fn probe_skips_undeserializable_inscriptions() { + let tip = test_block(5, 42); + let anchor = anchor_for(&tip, Slot::from(1_000)); + let garbage = ZoneMessage::Block(ZoneBlock { + id: MsgId::from([0_u8; 32]), + data: Inscription::try_from(&[1_u8, 2, 3][..]).expect("inscription"), + }); + assert!(matches!( + anchor.probe_anchor_slot(&garbage, Slot::from(1_000)), + AnchorProbe::KeepLooking + )); + } + + #[test] + fn probe_accepts_any_message_for_a_headerless_anchor() { + // A deserialize park records no header: any message still present at + // the anchor slot means the history is intact. + let anchor = Anchor { + slot: Slot::from(1_000), + block: None, + }; + let garbage = ZoneMessage::Block(ZoneBlock { + id: MsgId::from([0_u8; 32]), + data: Inscription::try_from(&[1_u8, 2, 3][..]).expect("inscription"), + }); + assert!(matches!( + anchor.probe_anchor_slot(&garbage, Slot::from(1_000)), + AnchorProbe::SameChain + )); + assert!(matches!( + anchor.probe_anchor_slot(&garbage, Slot::from(1_001)), + AnchorProbe::Mismatch(ChainMismatch::AnchorSlotChanged { .. }) + )); + } + + #[test] + fn frontier_flags_missing_channel_and_short_history() { + assert!(matches!( + frontier_verdict(Slot::from(1_000), None), + Some(ChainMismatch::ChannelMissing) + )); + assert!(matches!( + frontier_verdict(Slot::from(1_000), Some(Slot::from(999))), + Some(ChainMismatch::ChannelBehindAnchor { .. }) + )); + assert!(frontier_verdict(Slot::from(1_000), Some(Slot::from(1_000))).is_none()); + assert!(frontier_verdict(Slot::from(1_000), Some(Slot::from(2_000))).is_none()); + } +} diff --git a/lez/indexer/core/src/config.rs b/lez/indexer/core/src/config.rs index cb7f3dfe..159da23c 100644 --- a/lez/indexer/core/src/config.rs +++ b/lez/indexer/core/src/config.rs @@ -2,7 +2,9 @@ use std::{fs::File, io::BufReader, path::Path, time::Duration}; use anyhow::{Context as _, Result}; use common::config::BasicAuth; +use cross_zone_inbox_core::CrossZoneConfig; use humantime_serde; +use lee::AccountId; pub use logos_blockchain_core::mantle::ops::channel::ChannelId; use serde::{Deserialize, Serialize}; use url::Url; @@ -20,6 +22,29 @@ pub struct IndexerConfig { pub consensus_info_polling_interval: Duration, pub bedrock_config: ClientConfig, pub channel_id: ChannelId, + /// Cross-zone configuration. `None` disables the indexer's cross-zone handling. + #[serde(default)] + pub cross_zone: Option, + /// Bridge-lock holdings to seed into genesis, mirroring the sequencer's + /// `SupplyBridgeLockHolding` actions. They are not produced by any + /// transaction, so the indexer must seed them to match the sequencer's state. + #[serde(default)] + pub bridge_lock_holdings: Vec, + /// Whether to wipe the indexer store and re-index from scratch when the startup + /// chain-identity check finds the channel serving a different block than the one + /// stored at the same id. + /// + /// Defaults to `false`: on mismatch the indexer refuses to start. + #[serde(default)] + pub allow_chain_reset: bool, +} + +/// A genesis-funded bridge-lock holder balance, configured identically on the +/// sequencer (via `SupplyBridgeLockHolding`) and the indexer. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct BridgeLockHolding { + pub holder: AccountId, + pub amount: u128, } impl IndexerConfig { diff --git a/lez/indexer/core/src/cross_zone_verifier.rs b/lez/indexer/core/src/cross_zone_verifier.rs new file mode 100644 index 00000000..6e2a818e --- /dev/null +++ b/lez/indexer/core/src/cross_zone_verifier.rs @@ -0,0 +1,500 @@ +use std::{ + collections::{HashMap, HashSet}, + sync::Arc, + time::Duration, +}; + +use anyhow::{Result, bail}; +use common::{block::Block, transaction::LeeTransaction}; +use cross_zone::{build_dispatch_from_emission, extract_emission}; +use cross_zone_inbox_core::{ + CrossZoneMessage, Instruction as InboxInstruction, MessageKey, ZoneId, message_key, +}; +use futures::StreamExt as _; +use lee::PublicKey; +use log::{error, info}; +use logos_blockchain_core::mantle::ops::channel::ChannelId; +use logos_blockchain_zone_sdk::{ + CommonHttpClient, ZoneMessage, adapter::NodeHttpClient, indexer::ZoneIndexer, +}; +use tokio::sync::RwLock; + +use crate::config::IndexerConfig; + +/// How often the verifier logs that it is still waiting on a lagging peer reader, +/// so a stuck wait is observable without rejecting a legitimate message. +const LAG_LOG_INTERVAL: Duration = Duration::from_secs(30); + +/// Cache of finalized peer-zone blocks, filled by per-peer reader tasks and read +/// by the verifier to re-derive cross-zone dispatch transactions. +#[derive(Clone, Default)] +struct PeerBlocks { + chains: Arc>>>, +} + +impl PeerBlocks { + async fn insert(&self, zone: ZoneId, block: Block) { + self.chains + .write() + .await + .entry(zone) + .or_default() + .insert(block.header.block_id, block); + } + + async fn get(&self, zone: ZoneId, block_id: u64) -> Option { + self.chains + .read() + .await + .get(&zone) + .and_then(|chain| chain.get(&block_id).cloned()) + } + + /// The highest block id this reader has finalized for `zone`, or `None` if it + /// has read nothing yet. Used to tell forgery (we have read past the + /// referenced block and it is absent) from lag (we simply have not caught up). + async fn highest_seen(&self, zone: ZoneId) -> Option { + self.chains + .read() + .await + .get(&zone) + .and_then(|chain| chain.keys().copied().max()) + } +} + +/// The indexer-side Option B verifier. +/// +/// For every cross-zone dispatch in a block it re-derives the transaction from +/// the peer's finalized block and rejects it if the bytes differ (a forgery) or +/// the message was already delivered (a replay), so delivery no longer relies on +/// trusting the sequencer. +#[derive(Clone)] +pub struct CrossZoneVerifier { + self_zone: ZoneId, + /// Pinned block-signing key per peer zone, enforced during re-derivation. + peer_pubkeys: HashMap, + peers: PeerBlocks, + seen: Arc>>, +} + +impl CrossZoneVerifier { + /// Builds the verifier and spawns one peer reader per configured peer. + /// Returns `None` when cross-zone messaging is disabled. + pub fn start(config: &IndexerConfig) -> Option { + let cross_zone = config.cross_zone.as_ref()?; + let self_zone: ZoneId = *config.channel_id.as_ref(); + let peers = PeerBlocks::default(); + let mut peer_pubkeys = HashMap::new(); + + for peer in &cross_zone.peers { + let node = NodeHttpClient::new( + CommonHttpClient::new(config.bedrock_config.auth.clone().map(Into::into)), + config.bedrock_config.addr.clone(), + ); + if let Some(bytes) = peer.expected_block_signing_pubkey { + let pubkey = PublicKey::try_new(bytes) + .expect("configured peer block-signing pubkey is a valid key"); + peer_pubkeys.insert(peer.channel_id, pubkey); + } + tokio::spawn(read_peer( + ZoneIndexer::new(ChannelId::from(peer.channel_id), node), + peer.channel_id, + peers.clone(), + config.consensus_info_polling_interval, + )); + } + + Some(Self { + self_zone, + peer_pubkeys, + peers, + seen: Arc::new(RwLock::new(HashSet::new())), + }) + } + + /// Verifies every cross-zone dispatch in a block, returning `Err` on the + /// first forged or replayed dispatch. The caller halts ingestion on error. + pub async fn verify_block(&self, block: &Block) -> Result<()> { + for tx in &block.body.transactions { + let Some(msg) = Self::decode_dispatch(tx) else { + continue; + }; + + let key = message_key(&msg.src_zone, msg.src_block_id, msg.src_tx_index); + if self.seen.read().await.contains(&key) { + bail!( + "cross-zone replay: message {} re-delivered", + hex::encode(key) + ); + } + + let expected = self.rederive(&msg).await?; + if LeeTransaction::Public(expected) != *tx { + bail!( + "forged cross-zone dispatch from zone {} block {} tx {}: re-derivation mismatch", + hex::encode(msg.src_zone), + msg.src_block_id, + msg.src_tx_index + ); + } + + self.seen.write().await.insert(key); + info!( + "Verified cross-zone dispatch from zone {} block {} tx {}", + hex::encode(msg.src_zone), + msg.src_block_id, + msg.src_tx_index + ); + } + Ok(()) + } + + /// Decodes a transaction into the cross-zone message it dispatches, or `None` + /// if it is not an inbox dispatch. + fn decode_dispatch(tx: &LeeTransaction) -> Option { + let LeeTransaction::Public(public_tx) = tx else { + return None; + }; + if public_tx.message().program_id != programs::cross_zone_inbox().id() { + return None; + } + match risc0_zkvm::serde::from_slice::( + &public_tx.message().instruction_data, + ) { + Ok(InboxInstruction::Dispatch(msg)) => Some(msg), + Err(_) => None, + } + } + + /// Re-derives the dispatch transaction the watcher should have injected for + /// `msg`, reading the source emission from the peer's finalized block. + async fn rederive(&self, msg: &CrossZoneMessage) -> Result { + let peer_block = self + .wait_for_peer_block(msg.src_zone, msg.src_block_id) + .await?; + + // Equivocation defense: the source block must be signed by the peer's + // pinned block-signing key, not merely inscribed on the channel. + if let Some(expected) = self.peer_pubkeys.get(&msg.src_zone) + && !peer_block.is_signed_by(expected) + { + bail!( + "forged cross-zone dispatch: peer zone {} block {} is not signed by the pinned block-signing key", + hex::encode(msg.src_zone), + msg.src_block_id + ); + } + + let emission_tx = peer_block + .body + .transactions + .get(usize::try_from(msg.src_tx_index).expect("u32 index fits in usize")) + .ok_or_else(|| { + anyhow::anyhow!( + "src_tx_index {} out of range in peer block", + msg.src_tx_index + ) + })?; + + let LeeTransaction::Public(emission_tx) = emission_tx else { + bail!("peer emission transaction is not public"); + }; + let message = emission_tx.message(); + let emission = + extract_emission(message.program_id, &message.instruction_data).ok_or_else(|| { + anyhow::anyhow!("peer transaction at src_tx_index is not a recognized emitter") + })?; + + if emission.target_zone != self.self_zone { + bail!("peer emission targets a different zone"); + } + + Ok(build_dispatch_from_emission( + msg.src_zone, + msg.src_block_id, + msg.src_tx_index, + message.program_id, + emission.target_program_id, + &emission.target_accounts, + emission.payload, + )) + } + + /// Resolves the referenced peer block, distinguishing forgery from lag. + /// + /// If the block is cached, return it. If our peer reader has already + /// finalized past `block_id` and we still do not have it, the reference is to + /// a block that does not exist on the peer chain, a forgery, so reject now. + /// Otherwise the reader simply has not caught up yet: keep waiting, since a + /// legitimate dispatch is only injected after its peer block finalized and + /// our reader of the same finalized chain will see it too. Rejecting on a + /// timeout here would turn a lagging reader into a permanent halt of an + /// honest message. + async fn wait_for_peer_block(&self, zone: ZoneId, block_id: u64) -> Result { + let mut waited = Duration::ZERO; + loop { + if let Some(block) = self.peers.get(zone, block_id).await { + return Ok(block); + } + if self + .peers + .highest_seen(zone) + .await + .is_some_and(|h| h >= block_id) + { + bail!( + "forged cross-zone reference: peer zone {} finalized past block {} but it is absent", + hex::encode(zone), + block_id + ); + } + if !waited.is_zero() && waited.as_secs().is_multiple_of(LAG_LOG_INTERVAL.as_secs()) { + info!( + "Waiting for peer zone {} to finalize block {} ({}s); reader is behind", + hex::encode(zone), + block_id, + waited.as_secs() + ); + } + tokio::time::sleep(Duration::from_secs(1)).await; + waited = waited.saturating_add(Duration::from_secs(1)); + } + } +} + +/// Reads a peer zone's finalized blocks from Bedrock into the shared cache. +#[expect( + clippy::infinite_loop, + reason = "the peer reader runs for the lifetime of the indexer process" +)] +async fn read_peer( + zone_indexer: ZoneIndexer, + peer_zone: ZoneId, + peers: PeerBlocks, + poll_interval: Duration, +) { + info!( + "Cross-zone peer reader started for {}", + hex::encode(peer_zone) + ); + + let mut cursor = None; + loop { + let stream = match zone_indexer.next_messages(cursor).await { + Ok(stream) => stream, + Err(err) => { + error!( + "Peer reader next_messages failed for {}: {err}", + hex::encode(peer_zone) + ); + tokio::time::sleep(poll_interval).await; + continue; + } + }; + let mut stream = std::pin::pin!(stream); + + while let Some((msg, slot)) = stream.next().await { + if let ZoneMessage::Block(zone_block) = msg { + match borsh::from_slice::(&zone_block.data) { + Ok(block) => peers.insert(peer_zone, block).await, + Err(err) => error!("Peer reader failed to deserialize block: {err}"), + } + } + cursor = Some(slot); + } + + tokio::time::sleep(poll_interval).await; + } +} + +#[cfg(test)] +mod tests { + use common::test_utils::produce_dummy_block; + use lee::{ + PrivateKey, PublicKey, PublicTransaction, + public_transaction::{Message, WitnessSet}, + }; + use ping_core::{SenderInstruction, ping_record_pda}; + + use super::*; + + const SELF_ZONE: ZoneId = [1; 32]; + const PEER_ZONE: ZoneId = [2; 32]; + const PEER_BLOCK_ID: u64 = 5; + + fn verifier() -> CrossZoneVerifier { + verifier_with_pinned_keys(HashMap::new()) + } + + fn verifier_with_pinned_keys(peer_pubkeys: HashMap) -> CrossZoneVerifier { + CrossZoneVerifier { + self_zone: SELF_ZONE, + peer_pubkeys, + peers: PeerBlocks::default(), + seen: Arc::new(RwLock::new(HashSet::new())), + } + } + + /// A `ping_sender` emission addressed to `SELF_ZONE` carrying `payload`. + fn emission(payload: &[u8]) -> LeeTransaction { + let receiver_id = programs::ping_receiver().id(); + let send = SenderInstruction::Send { + outbox_program_id: programs::cross_zone_outbox().id(), + target_zone: SELF_ZONE, + target_program_id: receiver_id, + target_accounts: vec![ping_record_pda(receiver_id).into_value()], + payload: payload.to_vec(), + ordinal: 0, + }; + let message = Message::try_new(programs::ping_sender().id(), vec![], vec![], send) + .expect("emission serializes"); + LeeTransaction::Public(PublicTransaction::new( + message, + WitnessSet::from_raw_parts(vec![]), + )) + } + + /// The dispatch a watcher would inject for a `PEER_BLOCK_ID` emission of `payload`. + fn dispatch(payload: &[u8]) -> LeeTransaction { + let receiver_id = programs::ping_receiver().id(); + LeeTransaction::Public(build_dispatch_from_emission( + PEER_ZONE, + PEER_BLOCK_ID, + 0, + programs::ping_sender().id(), + receiver_id, + &[ping_record_pda(receiver_id).into_value()], + payload.to_vec(), + )) + } + + #[tokio::test] + async fn verifies_dispatch_matching_a_peer_emission() { + let verifier = verifier(); + verifier + .peers + .insert( + PEER_ZONE, + produce_dummy_block(PEER_BLOCK_ID, None, vec![emission(b"hi")]), + ) + .await; + + let block = produce_dummy_block(9, None, vec![dispatch(b"hi")]); + verifier + .verify_block(&block) + .await + .expect("dispatch matching the peer emission verifies"); + } + + #[tokio::test] + async fn rejects_dispatch_with_no_matching_emission() { + let verifier = verifier(); + // The peer block carries the real emission, but the block claims a + // different payload, so re-derivation does not reproduce it. + verifier + .peers + .insert( + PEER_ZONE, + produce_dummy_block(PEER_BLOCK_ID, None, vec![emission(b"real")]), + ) + .await; + + let block = produce_dummy_block(9, None, vec![dispatch(b"forged")]); + let err = verifier.verify_block(&block).await.unwrap_err(); + assert!( + err.to_string().contains("forged"), + "unexpected error: {err}" + ); + } + + #[tokio::test] + async fn verifies_dispatch_signed_by_the_pinned_peer_key() { + // produce_dummy_block signs with PrivateKey([37; 32]); pin its pubkey. + let signer = PublicKey::new_from_private_key(&PrivateKey::try_new([37; 32]).unwrap()); + let mut keys = HashMap::new(); + keys.insert(PEER_ZONE, signer); + let verifier = verifier_with_pinned_keys(keys); + verifier + .peers + .insert( + PEER_ZONE, + produce_dummy_block(PEER_BLOCK_ID, None, vec![emission(b"hi")]), + ) + .await; + + let block = produce_dummy_block(9, None, vec![dispatch(b"hi")]); + verifier + .verify_block(&block) + .await + .expect("a dispatch from the pinned signer verifies"); + } + + #[tokio::test] + async fn rejects_dispatch_from_a_block_not_signed_by_the_pinned_key() { + // Pin a different key than the one that signed the peer block. + let mut keys = HashMap::new(); + keys.insert(PEER_ZONE, PublicKey::try_new([42; 32]).unwrap()); + let verifier = verifier_with_pinned_keys(keys); + verifier + .peers + .insert( + PEER_ZONE, + produce_dummy_block(PEER_BLOCK_ID, None, vec![emission(b"hi")]), + ) + .await; + + let block = produce_dummy_block(9, None, vec![dispatch(b"hi")]); + let err = verifier.verify_block(&block).await.unwrap_err(); + assert!( + err.to_string().contains("pinned"), + "unexpected error: {err}" + ); + } + + #[tokio::test] + async fn rejects_reference_to_a_block_the_peer_never_finalized() { + let verifier = verifier(); + // The reader has finalized past PEER_BLOCK_ID (it holds a later block) but + // never saw PEER_BLOCK_ID itself, so a dispatch referencing it is a forgery + // and must be rejected rather than waited on forever. + verifier + .peers + .insert( + PEER_ZONE, + produce_dummy_block(PEER_BLOCK_ID + 1, None, vec![emission(b"hi")]), + ) + .await; + + let block = produce_dummy_block(9, None, vec![dispatch(b"hi")]); + let err = verifier.verify_block(&block).await.unwrap_err(); + assert!( + err.to_string().contains("forged"), + "unexpected error: {err}" + ); + } + + #[tokio::test] + async fn rejects_replayed_dispatch() { + let verifier = verifier(); + verifier + .peers + .insert( + PEER_ZONE, + produce_dummy_block(PEER_BLOCK_ID, None, vec![emission(b"hi")]), + ) + .await; + + let first = produce_dummy_block(9, None, vec![dispatch(b"hi")]); + verifier + .verify_block(&first) + .await + .expect("first delivery verifies"); + + let replay = produce_dummy_block(10, None, vec![dispatch(b"hi")]); + let err = verifier.verify_block(&replay).await.unwrap_err(); + assert!( + err.to_string().contains("replay"), + "unexpected error: {err}" + ); + } +} diff --git a/lez/indexer/core/src/ingest_error.rs b/lez/indexer/core/src/ingest_error.rs new file mode 100644 index 00000000..299013d7 --- /dev/null +++ b/lez/indexer/core/src/ingest_error.rs @@ -0,0 +1,80 @@ +use common::HashType; +use serde::{Deserialize, Serialize}; + +/// Why the indexer could not apply an L2 block from the channel. +/// +/// Persisted in `RocksDB`, so every variant must have the following +/// traits: `Clone + Serialize + Deserialize`. +#[derive(Debug, Clone, Serialize, Deserialize, thiserror::Error)] +pub enum BlockIngestError { + #[error("Failed to deserialize L2 block: {0}")] + /// Here we store the error string that is derived from [`borsh::from_slice`]'s [`Err`]. + Deserialize(String), + #[error("Unexpected block id: expected {expected}, got {got}")] + UnexpectedBlockId { expected: u64, got: u64 }, + #[error("Broken chain link: expected prev {expected_prev}, got {got_prev}")] + BrokenChainLink { + expected_prev: HashType, + got_prev: HashType, + }, + #[error("Block hash mismatch: computed {computed}, header {header}")] + HashMismatch { + computed: HashType, + header: HashType, + }, + #[error("Block has no transactions")] + EmptyBlock, + #[error("Last transaction must be the public clock invocation for the block timestamp")] + InvalidClockTransaction, + #[error("Genesis block must contain only public transactions")] + NonPublicGenesisTransaction, + #[error("State transition failed at transaction {tx_index}: {reason}")] + StateTransition { + /// Index of the failing transaction within the block body. + tx_index: u64, + /// Reason string from `lee::Error` to `anyhow::Error` to `{:#}`. + /// + /// This is required because `lee::Error` is not `Clone + Serialize + Deserialize`, so we + /// cannot store it directly. + reason: String, + }, +} + +impl BlockIngestError { + /// Whether the failure may be transient rather than a property of the block. + /// + /// FIXME: `StateTransition` is too coarse β€” its `reason` string mixes genuine + /// state-transition rejections with infra failures (risc0 executor teardown, + /// storage errors). Once it carries a structured cause, narrow this so only + /// infra failures retry. + #[must_use] + pub const fn is_retryable(&self) -> bool { + matches!(self, Self::StateTransition { .. }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn serializes_and_round_trips_externally_tagged() { + let err = BlockIngestError::UnexpectedBlockId { + expected: 5, + got: 7, + }; + let value = serde_json::to_value(&err).expect("serialize"); + assert_eq!( + value, + serde_json::json!({ "UnexpectedBlockId": { "expected": 5, "got": 7 } }) + ); + let back: BlockIngestError = serde_json::from_value(value).expect("deserialize"); + assert!(matches!( + back, + BlockIngestError::UnexpectedBlockId { + expected: 5, + got: 7 + } + )); + } +} diff --git a/lez/indexer/core/src/lib.rs b/lez/indexer/core/src/lib.rs index 0d595fc0..7bee6387 100644 --- a/lez/indexer/core/src/lib.rs +++ b/lez/indexer/core/src/lib.rs @@ -3,35 +3,85 @@ use std::{path::Path, sync::Arc}; use anyhow::Result; use arc_swap::ArcSwap; use common::block::Block; -// ToDo: Remove after testnet +// TODO: Remove after testnet use futures::StreamExt as _; +pub use ingest_error::BlockIngestError; use log::{error, info, warn}; -use logos_blockchain_core::header::HeaderId; use logos_blockchain_zone_sdk::{ - CommonHttpClient, ZoneMessage, adapter::NodeHttpClient, indexer::ZoneIndexer, + CommonHttpClient, Slot, ZoneMessage, adapter::NodeHttpClient, indexer::ZoneIndexer, }; +use retry::ApplyRetryGate; +pub use stall_reason::StallReason; use crate::{ - block_store::IndexerStore, + block_store::{AcceptOutcome, IndexerStore}, + chain_consistency::ChainConsistency, config::IndexerConfig, + cross_zone_verifier::CrossZoneVerifier, status::{IndexerStatus, IndexerSyncStatus}, }; - pub mod block_store; +pub mod chain_consistency; pub mod config; +pub mod cross_zone_verifier; +pub mod ingest_error; +mod retry; +pub mod stall_reason; pub mod status; +/// Consecutive failed apply attempts of the same block before parking. +const APPLY_RETRY_LIMIT: u32 = 3; + #[derive(Clone)] pub struct IndexerCore { pub zone_indexer: Arc>, + /// Direct node handle for queries outside `ZoneIndexer`'s streaming API. + pub node: NodeHttpClient, pub config: IndexerConfig, pub store: IndexerStore, /// Live ingestion status; updated by the ingest stream, read by `status`. pub status: Arc>, + /// Option B cross-zone verifier; `None` when cross-zone messaging is disabled. + pub verifier: Option, } impl IndexerCore { - pub fn new(config: IndexerConfig, storage_dir: &Path) -> Result { + /// Builds the core, then verifies the stored chain matches the channel's by + /// re-reading the channel at the stored tip's position. + /// + /// On mismatch: refuse (error) unless `config.allow_chain_reset` is set, in which case wipe the + /// store and re-index from scratch. + pub async fn new(config: IndexerConfig, storage_dir: &Path) -> Result { + let home = storage_dir.join(format!("rocksdb-{}", config.channel_id)); + let core = Self::open(config.clone(), storage_dir)?; + match core.verify_chain_consistency().await? { + // `Inconclusive` is deliberately treated the same as `Consistent`. + // + // We could not prove a reset, so proceed from the cursor without wiping + // a possibly-valid store. A genuinely divergent chain is still caught + // later when the ingest loop tries to apply and parks. + ChainConsistency::Consistent | ChainConsistency::Inconclusive => Ok(core), + ChainConsistency::Inconsistent(mismatch) if config.allow_chain_reset => { + warn!( + "Chain reset detected ({mismatch}). Wiping indexer store at {} and \ + re-indexing.", + home.display() + ); + drop(core); // sole owner before the ingest task is spawned β†’ closes the DB + storage::indexer::RocksDBIO::destroy(&home)?; + Self::open(config, storage_dir) + } + ChainConsistency::Inconsistent(mismatch) => Err(anyhow::anyhow!( + "Indexer store at {} holds a different chain than the channel now serves \ + ({mismatch}). Delete the indexer storage directory, point at a fresh one, or \ + set `allow_chain_reset` in the indexer config.", + home.display() + )), + } + } + + /// Opens the store and builds the core without the chain-identity check. + fn open(config: IndexerConfig, storage_dir: &Path) -> Result { // Namespace the DB by channel so indexers on different channels can // share a storage dir without their RocksDB state colliding. let home = storage_dir.join(format!("rocksdb-{}", config.channel_id)); @@ -41,13 +91,37 @@ impl IndexerCore { CommonHttpClient::new(basic_auth), config.bedrock_config.addr.clone(), ); - let zone_indexer = ZoneIndexer::new(config.channel_id, node); + let zone_indexer = ZoneIndexer::new(config.channel_id, node.clone()); + + // Genesis accounts the indexer must seed to match the sequencer's state, + // since none are produced by a transaction: the cross-zone inbox config + // and any bridge-lock holdings. Both go through the same builders the + // sequencer uses, so the states are byte-identical. + let mut genesis_seed = Vec::new(); + if let Some(cross_zone) = config.cross_zone.as_ref() { + let self_zone: [u8; 32] = *config.channel_id.as_ref(); + genesis_seed.push(cross_zone::build_inbox_config_account( + self_zone, cross_zone, + )); + } + for holding in &config.bridge_lock_holdings { + genesis_seed.push(cross_zone::build_holding_account( + holding.holder, + holding.amount, + )); + } + + // Option B verifier: re-derives each cross-zone dispatch from the peer's + // finalized blocks. `None` when cross-zone messaging is disabled. + let verifier = CrossZoneVerifier::start(&config); Ok(Self { zone_indexer: Arc::new(zone_indexer), + store: IndexerStore::open_db(&home, genesis_seed)?, + node, config, - store: IndexerStore::open_db(&home)?, status: Arc::new(ArcSwap::from_pointee(IndexerSyncStatus::starting())), + verifier, }) } @@ -58,10 +132,27 @@ impl IndexerCore { #[must_use] pub fn status(&self) -> IndexerStatus { let sync = IndexerSyncStatus::clone(&self.status.load()); - let indexed_block_id = self.store.get_last_block_id().ok().flatten(); + // Log-and-fall-back rather than collapsing a store error into the same + // `None` as "legitimately absent": a DB read failure must not silently + // masquerade as "no tip yet" / "no stall recorded" in the snapshot. + let indexed_block_id = match self.store.get_last_block_id() { + Ok(id) => id, + Err(err) => { + warn!("Failed to read last indexed block id for status: {err:#}"); + None + } + }; + let stall_reason = match self.store.get_stall_reason() { + Ok(reason) => reason, + Err(err) => { + warn!("Failed to read stall reason for status: {err:#}"); + None + } + }; IndexerStatus { sync, indexed_block_id, + stall_reason, } } @@ -70,6 +161,41 @@ impl IndexerCore { self.status.store(Arc::new(status)); } + /// Advances the in-memory L1 read cursor past `slot` and persists it. + /// A persist failure is only logged: the worst case is re-reading a batch + /// after a restart, which ingestion handles idempotently. + fn advance_cursor(&self, cursor: &mut Option, slot: Slot) { + *cursor = Some(slot); + if let Err(err) = self.store.set_zone_cursor(&slot) { + warn!("Failed to persist indexer cursor: {err:#}"); + } + } + + /// Parks on an inscription that could not be parsed as an L2 block: + /// records the stall and flips the status. The validated tip stays frozen. + /// + /// Returns `false` if the stall could not be recorded durably; the caller + /// must then hold the cursor and retry instead of advancing past the slot. + fn park_undeserializable(&self, slot: Slot, error: std::io::Error) -> bool { + let error = anyhow::Error::new(error); + + // use `:#` to get the entire error chain + let reason = format!("{error:#}"); + error!("Failed to deserialize L2 block from zone-sdk: {reason}"); + if let Err(err) = + self.store + .record_stall(None, slot, BlockIngestError::Deserialize(reason.clone())) + { + error!("Failed to record stall reason: {err:#}"); + self.set_status(IndexerSyncStatus::error(format!("store error: {err:#}"))); + return false; + } + self.set_status(IndexerSyncStatus::stalled(format!( + "failed to deserialize L2 block: {reason}" + ))); + true + } + pub fn subscribe_parse_block_stream(&self) -> impl futures::Stream> + '_ { let poll_interval = self.config.consensus_info_polling_interval; let initial_cursor = self @@ -79,6 +205,7 @@ impl IndexerCore { async_stream::stream! { let mut cursor = initial_cursor; + let mut retry_gate = ApplyRetryGate::new(); if cursor.is_some() { info!("Resuming indexer from cursor {cursor:?}"); @@ -90,8 +217,6 @@ impl IndexerCore { let stream = match self.zone_indexer.next_messages(cursor).await { Ok(s) => s, Err(err) => { - // `next_messages` reads L1 consensus info internally, so - // this also covers an unreachable/misconfigured L1 node. error!("Failed to start zone-sdk next_messages stream: {err}"); self.set_status(IndexerSyncStatus::error(format!( "cannot reach L1 / read channel: {err}" @@ -102,11 +227,8 @@ impl IndexerCore { }; let mut stream = std::pin::pin!(stream); - // Flip to Syncing on the first message of this cycle (not merely on - // a successful poll) so the steady-state CaughtUp status doesn't - // flicker. Until then the state stays Starting (cold-start scan of - // empty L1 history) or CaughtUp (idle). let mut announced_syncing = false; + let mut had_cycle_error = false; while let Some((msg, slot)) = stream.next().await { if !announced_syncing { @@ -116,46 +238,133 @@ impl IndexerCore { let zone_block = match msg { ZoneMessage::Block(b) => b, - // Non-block messages don't carry a cursor position; the - // next ZoneBlock advances past them implicitly. + // FIXME: will be handled in prep of decentralized sequencers ZoneMessage::Deposit(_) | ZoneMessage::Withdraw(_) => continue, }; let block: Block = match borsh::from_slice(&zone_block.data) { Ok(b) => b, - Err(e) => { - error!("Failed to deserialize L2 block from zone-sdk: {e}"); - // Advance past the broken inscription so we don't - // re-process it on restart. - cursor = Some(slot); - if let Err(err) = self.store.set_zone_cursor(&slot) { - warn!("Failed to persist indexer cursor: {err:#}"); + Err(error) => { + // The stall must be durable before the cursor moves. + if !self.park_undeserializable(slot, error) { + had_cycle_error = true; + break; } + // L1 proceeds regardless + self.advance_cursor(&mut cursor, slot); continue; } }; - info!("Indexed L2 block {}", block.header.block_id); - - // TODO: Remove l1_header placeholder once storage layer - // no longer requires it. Zone-sdk handles L1 tracking internally. - let placeholder_l1_header = HeaderId::from([0_u8; 32]); - if let Err(err) = self.store.put_block(block.clone(), placeholder_l1_header).await { - error!("Failed to store block {}: {err:#}", block.header.block_id); + // Option B: re-derive and verify every cross-zone dispatch + // before applying the block. A forged or replayed dispatch + // halts ingestion rather than persisting an invalid state. + if let Some(verifier) = &self.verifier + && let Err(err) = verifier.verify_block(&block).await + { + error!( + "Cross-zone verification failed for block {}: {err:#}. Halting indexer ingestion.", + block.header.block_id + ); + self.set_status(IndexerSyncStatus::error(format!( + "cross-zone verification failed: {err:#}" + ))); + return; } - cursor = Some(slot); - if let Err(err) = self.store.set_zone_cursor(&slot) { - warn!("Failed to persist indexer cursor: {err:#}"); + match self.store.accept_block(&block, slot).await { + Ok(AcceptOutcome::Applied) => { + retry_gate.reset(); + info!("Indexed L2 block {}", block.header.block_id); + self.set_status(IndexerSyncStatus::syncing()); + self.advance_cursor(&mut cursor, slot); + yield Ok(block); + } + Ok(AcceptOutcome::AlreadyApplied) => { + info!( + "Skipping already-applied block {}", + block.header.block_id + ); + self.advance_cursor(&mut cursor, slot); + } + Ok(AcceptOutcome::Parked(ingest_err)) => { + error!( + "Parked at block {}: {ingest_err}", + block.header.block_id + ); + self.set_status(IndexerSyncStatus::stalled(ingest_err.to_string())); + // L1 proceeds regardless + self.advance_cursor(&mut cursor, slot); + } + Ok(AcceptOutcome::RetryableFailure(ingest_err)) => { + let attempts = retry_gate.register_failure(block.header.block_id); + if attempts >= APPLY_RETRY_LIMIT { + error!( + "Parked at block {} after {attempts} failed apply attempts: {ingest_err}", + block.header.block_id + ); + // The stall must be durable before the cursor moves. + if let Err(err) = self.store.record_stall( + Some(&block.header), + slot, + ingest_err.clone(), + ) { + error!( + "Failed to record stall reason for block {}: {err:#}", + block.header.block_id + ); + self.set_status(IndexerSyncStatus::error(format!( + "store error: {err:#}" + ))); + had_cycle_error = true; + break; + } + self.set_status(IndexerSyncStatus::stalled(ingest_err.to_string())); + self.advance_cursor(&mut cursor, slot); + retry_gate.reset(); + } else { + error!( + "Failed to apply block {} (attempt {attempts}/{APPLY_RETRY_LIMIT}), will retry: {ingest_err}", + block.header.block_id + ); + self.set_status(IndexerSyncStatus::error(format!( + "apply failed, retrying: {ingest_err}" + ))); + had_cycle_error = true; + break; + } + } + Err(err) => { + // Infrastructure error (DB read/write), not a bad block. + // will re-poll from the same cursor next cycle. + error!( + "Store error applying block {}: {err:#}", + block.header.block_id + ); + self.set_status(IndexerSyncStatus::error(format!( + "store error: {err:#}" + ))); + had_cycle_error = true; + break; + } } - yield Ok(block); } - // Stream drained: caught up to LIB as of this cycle. Clears any - // prior error (e.g. a transient L1 disconnect that left no - // backlog, so the `Syncing` branch above never ran). Sleep then - // poll again. - self.set_status(IndexerSyncStatus::caught_up()); + if had_cycle_error { + tokio::time::sleep(poll_interval).await; + continue; + } + + // Stream drained. Stay Stalled if parked; otherwise we are caught up. + // A store error here must not be collapsed to "no stall recorded": + // that would wrongly flip us to caught-up, so we log and hold state. + match self.store.get_stall_reason() { + Ok(None) => self.set_status(IndexerSyncStatus::caught_up()), + Ok(Some(_)) => {} + Err(err) => { + warn!("Failed to read stall reason after draining stream; not marking caught up: {err:#}"); + } + } tokio::time::sleep(poll_interval).await; } } diff --git a/lez/indexer/core/src/retry.rs b/lez/indexer/core/src/retry.rs new file mode 100644 index 00000000..d7c5f5a2 --- /dev/null +++ b/lez/indexer/core/src/retry.rs @@ -0,0 +1,64 @@ +//! Retry gate for possibly-transient block-apply failures. + +/// Counts consecutive apply failures per block id so the ingest loop can +/// retry before parking. +/// +/// A failure streak is keyed to one block id: a failure of a different block +/// starts a fresh streak. Reset only on a genuinely applied block β€” the +/// `AlreadyApplied` replay of the prefix after a retry cycle must not clear +/// the failing block's streak. +pub struct ApplyRetryGate { + failing: Option<(u64, u32)>, +} + +impl ApplyRetryGate { + #[must_use] + pub const fn new() -> Self { + Self { failing: None } + } + + /// Registers a failed apply of `block_id`; returns its consecutive + /// attempt count. + pub const fn register_failure(&mut self, block_id: u64) -> u32 { + let attempts = match self.failing { + Some((id, attempts)) if id == block_id => attempts.saturating_add(1), + _ => 1, + }; + self.failing = Some((block_id, attempts)); + attempts + } + + /// Clears the streak; call when a block actually applies. + pub const fn reset(&mut self) { + self.failing = None; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn counts_consecutive_failures_of_same_block() { + let mut gate = ApplyRetryGate::new(); + assert_eq!(gate.register_failure(7), 1); + assert_eq!(gate.register_failure(7), 2); + assert_eq!(gate.register_failure(7), 3); + } + + #[test] + fn different_block_starts_fresh_streak() { + let mut gate = ApplyRetryGate::new(); + gate.register_failure(7); + gate.register_failure(7); + assert_eq!(gate.register_failure(8), 1); + } + + #[test] + fn reset_clears_streak() { + let mut gate = ApplyRetryGate::new(); + gate.register_failure(7); + gate.reset(); + assert_eq!(gate.register_failure(7), 1); + } +} diff --git a/lez/indexer/core/src/stall_reason.rs b/lez/indexer/core/src/stall_reason.rs new file mode 100644 index 00000000..c5118fcc --- /dev/null +++ b/lez/indexer/core/src/stall_reason.rs @@ -0,0 +1,25 @@ +use common::HashType; +use logos_blockchain_zone_sdk::Slot; +use serde::{Deserialize, Serialize}; + +use crate::ingest_error::BlockIngestError; + +/// Diagnostic record of the first block that broke the L2 chain. +/// +/// The block-derived fields are `None` for a deserialize break (no header was +/// ever parsed). `l1_slot` is the L1 slot the breaking inscription was read at. +/// `first_seen` is the breaking block's L2 timestamp (`None` for a deserialize break). +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct StallReason { + pub block_id: Option, + pub block_hash: Option, + pub prev_block_hash: Option, + pub l1_slot: Slot, + pub error: BlockIngestError, + pub first_seen: Option, + /// Number of later non-chaining blocks (orphans, since the tip is frozen). + /// + /// TODO: We could store a different "branch" of blocks following this break, but for now we + /// just count them. + pub orphans_since: u64, +} diff --git a/lez/indexer/core/src/status.rs b/lez/indexer/core/src/status.rs index 1193e124..a483fde6 100644 --- a/lez/indexer/core/src/status.rs +++ b/lez/indexer/core/src/status.rs @@ -1,9 +1,10 @@ use serde::Serialize; +use crate::stall_reason::StallReason; + /// Coarse lifecycle state of the indexer's ingestion loop, so a client can tell /// "still catching up" apart from "something went wrong". #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] -#[serde(rename_all = "snake_case")] pub enum IndexerSyncState { /// Booted; no ingestion cycle has run yet. Starting, @@ -13,12 +14,14 @@ pub enum IndexerSyncState { CaughtUp, /// The last cycle failed (e.g. the L1 node is unreachable). See `last_error`. Error, + /// Parked on a stall reason: the validated tip is frozen awaiting a valid + /// continuation. See `last_error` and the snapshot's `stall_reason`. + Stalled, } /// Live ingestion status owned by the ingest loop: the coarse `state` plus the /// reason when it is `Error`. #[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] pub struct IndexerSyncStatus { pub state: IndexerSyncState, pub last_error: Option, @@ -56,6 +59,15 @@ impl IndexerSyncStatus { last_error: Some(reason), } } + + /// Parked on a stall reason; `reason` mirrors the stall's error message. + /// The full stall is attached to the [`IndexerStatus`] snapshot. + pub(crate) const fn stalled(reason: String) -> Self { + Self { + state: IndexerSyncState::Stalled, + last_error: Some(reason), + } + } } /// Full status snapshot returned to callers (FFI/RPC): the live [`IndexerSyncStatus`] @@ -64,11 +76,11 @@ impl IndexerSyncStatus { /// The tip is tracked by the store, not the ingest loop, so it lives here on the /// returned snapshot rather than inside the shared [`IndexerSyncStatus`]. #[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] pub struct IndexerStatus { #[serde(flatten)] pub sync: IndexerSyncStatus, pub indexed_block_id: Option, + pub stall_reason: Option, } #[cfg(test)] @@ -80,14 +92,16 @@ mod tests { let status = IndexerStatus { sync: IndexerSyncStatus::error("boom".to_owned()), indexed_block_id: Some(7), + stall_reason: None, }; let value = serde_json::to_value(&status).expect("serialize"); assert_eq!( value, serde_json::json!({ - "state": "error", - "lastError": "boom", - "indexedBlockId": 7, + "state": "Error", + "last_error": "boom", + "indexed_block_id": 7, + "stall_reason": null, }) ); } @@ -97,7 +111,36 @@ mod tests { let value = serde_json::to_value(IndexerSyncStatus::caught_up()).expect("serialize"); assert_eq!( value, - serde_json::json!({ "state": "caught_up", "lastError": null }) + serde_json::json!({ "state": "CaughtUp", "last_error": null }) ); } + + #[test] + fn stalled_status_serializes_with_stall_reason() { + use logos_blockchain_zone_sdk::Slot; + + use crate::{ingest_error::BlockIngestError, stall_reason::StallReason}; + + let status = IndexerStatus { + sync: IndexerSyncStatus::stalled("broken chain link".to_owned()), + indexed_block_id: Some(41), + stall_reason: Some(StallReason { + block_id: Some(42), + block_hash: None, + prev_block_hash: None, + l1_slot: Slot::from(0), + error: BlockIngestError::StateTransition { + tx_index: 0, + reason: String::default(), + }, + first_seen: None, + orphans_since: 2, + }), + }; + let value = serde_json::to_value(&status).expect("serialize"); + assert_eq!(value["state"], serde_json::json!("Stalled")); + assert_eq!(value["last_error"], serde_json::json!("broken chain link")); + assert_eq!(value["indexed_block_id"], serde_json::json!(41)); + assert_eq!(value["stall_reason"]["orphans_since"], serde_json::json!(2)); + } } diff --git a/lez/indexer/ffi/indexer_ffi.h b/lez/indexer/ffi/indexer_ffi.h index 8347ad3c..26857af7 100644 --- a/lez/indexer/ffi/indexer_ffi.h +++ b/lez/indexer/ffi/indexer_ffi.h @@ -503,9 +503,9 @@ struct LastBlockIdResult query_last_block(const struct IndexerServiceFFI *indexe * Query the indexer's current sync status as a JSON C-string. * * The JSON schema is owned by `indexer_core` (`IndexerStatus`): an object with - * `state` (`starting`/`syncing`/`caught_up`/`error`), `indexedBlockId`, and - * `lastError`. Lets a client distinguish "still catching up" from "something - * went wrong". + * `state` (`Starting`/`Syncing`/`CaughtUp`/`Error`/`Stalled`), + * `indexed_block_id`, `last_error`, and `stall_reason`. Lets a client + * distinguish "still catching up" from "something went wrong". * * # Arguments * diff --git a/lez/indexer/ffi/src/api/lifecycle.rs b/lez/indexer/ffi/src/api/lifecycle.rs index f668f3ee..8a1eafaf 100644 --- a/lez/indexer/ffi/src/api/lifecycle.rs +++ b/lez/indexer/ffi/src/api/lifecycle.rs @@ -112,10 +112,12 @@ unsafe fn setup_indexer( unsafe { Runtime::from_borrowed(caller.as_ref()) } }; - let core = IndexerCore::new(config, &storage_dir).map_err(|e| { - log::error!("Could not initialize indexer core: {e}"); - OperationStatus::InitializationError - })?; + let core = runtime + .block_on(IndexerCore::new(config, &storage_dir)) + .map_err(|e| { + log::error!("Could not initialize indexer core: {e}"); + OperationStatus::InitializationError + })?; // The block stream writes each parsed block into the store as a side effect // of being polled, so we spawn a task that simply drains it. There are no diff --git a/lez/indexer/ffi/src/api/query.rs b/lez/indexer/ffi/src/api/query.rs index 1943f6d4..dff027fa 100644 --- a/lez/indexer/ffi/src/api/query.rs +++ b/lez/indexer/ffi/src/api/query.rs @@ -91,9 +91,9 @@ pub unsafe extern "C" fn query_last_block(indexer: *const IndexerServiceFFI) -> /// Query the indexer's current sync status as a JSON C-string. /// /// The JSON schema is owned by `indexer_core` (`IndexerStatus`): an object with -/// `state` (`starting`/`syncing`/`caught_up`/`error`), `indexedBlockId`, and -/// `lastError`. Lets a client distinguish "still catching up" from "something -/// went wrong". +/// `state` (`Starting`/`Syncing`/`CaughtUp`/`Error`/`Stalled`), +/// `indexed_block_id`, `last_error`, and `stall_reason`. Lets a client +/// distinguish "still catching up" from "something went wrong". /// /// # Arguments /// diff --git a/lez/indexer/service/configs/debug/indexer_config.json b/lez/indexer/service/configs/debug/indexer_config.json index 85227700..be5cf353 100644 --- a/lez/indexer/service/configs/debug/indexer_config.json +++ b/lez/indexer/service/configs/debug/indexer_config.json @@ -1,7 +1,8 @@ { - "consensus_info_polling_interval": "1s", - "bedrock_config": { - "addr": "http://localhost:18080" - }, - "channel_id": "0101010101010101010101010101010101010101010101010101010101010101" + "consensus_info_polling_interval": "1s", + "bedrock_config": { + "addr": "http://localhost:18080" + }, + "channel_id": "0101010101010101010101010101010101010101010101010101010101010101", + "allow_chain_reset": true } diff --git a/lez/indexer/service/configs/docker/indexer_config.json b/lez/indexer/service/configs/docker/indexer_config.json index f083ca27..ce28af0b 100644 --- a/lez/indexer/service/configs/docker/indexer_config.json +++ b/lez/indexer/service/configs/docker/indexer_config.json @@ -3,5 +3,6 @@ "bedrock_config": { "addr": "http://host.docker.internal:18080" }, - "channel_id": "0101010101010101010101010101010101010101010101010101010101010101" + "channel_id": "0101010101010101010101010101010101010101010101010101010101010101", + "allow_chain_reset": true } diff --git a/lez/indexer/service/protocol/Cargo.toml b/lez/indexer/service/protocol/Cargo.toml index 5a4176f5..b3e8f65c 100644 --- a/lez/indexer/service/protocol/Cargo.toml +++ b/lez/indexer/service/protocol/Cargo.toml @@ -11,6 +11,7 @@ workspace = true lee_core = { workspace = true, optional = true, features = ["host"] } lee = { workspace = true, optional = true } common = { workspace = true, optional = true } +indexer_core = { workspace = true, optional = true } serde = { workspace = true, features = ["derive"] } serde_with.workspace = true @@ -22,4 +23,4 @@ anyhow.workspace = true [features] # Enable conversion to/from LEE core types -convert = ["dep:lee_core", "dep:lee", "dep:common"] +convert = ["dep:lee_core", "dep:lee", "dep:common", "dep:indexer_core"] diff --git a/lez/indexer/service/protocol/src/convert.rs b/lez/indexer/service/protocol/src/convert.rs index cd0bff7e..55c4dc6c 100644 --- a/lez/indexer/service/protocol/src/convert.rs +++ b/lez/indexer/service/protocol/src/convert.rs @@ -3,11 +3,12 @@ use lee_core::account::Nonce; use crate::{ - Account, AccountId, BedrockStatus, Block, BlockBody, BlockHeader, Ciphertext, Commitment, - CommitmentSetDigest, Data, EncryptedAccountData, EphemeralPublicKey, HashType, Nullifier, - PrivacyPreservingMessage, PrivacyPreservingTransaction, ProgramDeploymentMessage, - ProgramDeploymentTransaction, ProgramId, Proof, PublicKey, PublicMessage, PublicTransaction, - Signature, Transaction, ValidityWindow, WitnessSet, + Account, AccountId, BedrockStatus, Block, BlockBody, BlockHeader, BlockIngestError, Ciphertext, + Commitment, CommitmentSetDigest, Data, EncryptedAccountData, EphemeralPublicKey, HashType, + IndexerStatus, IndexerSyncState, Nullifier, PrivacyPreservingMessage, + PrivacyPreservingTransaction, ProgramDeploymentMessage, ProgramDeploymentTransaction, + ProgramId, Proof, PublicKey, PublicMessage, PublicTransaction, Signature, StallReason, + Transaction, ValidityWindow, WitnessSet, }; // ============================================================================ @@ -707,3 +708,94 @@ impl TryFrom for lee_core::program::ValidityWindow { value.0.try_into() } } + +// ============================================================================ +// Indexer status conversions +// ============================================================================ + +impl From for IndexerSyncState { + fn from(value: indexer_core::status::IndexerSyncState) -> Self { + match value { + indexer_core::status::IndexerSyncState::Starting => Self::Starting, + indexer_core::status::IndexerSyncState::Syncing => Self::Syncing, + indexer_core::status::IndexerSyncState::CaughtUp => Self::CaughtUp, + indexer_core::status::IndexerSyncState::Error => Self::Error, + indexer_core::status::IndexerSyncState::Stalled => Self::Stalled, + } + } +} + +impl From for BlockIngestError { + fn from(value: indexer_core::BlockIngestError) -> Self { + match value { + indexer_core::BlockIngestError::Deserialize(msg) => Self::Deserialize(msg), + indexer_core::BlockIngestError::UnexpectedBlockId { expected, got } => { + Self::UnexpectedBlockId { expected, got } + } + indexer_core::BlockIngestError::BrokenChainLink { + expected_prev, + got_prev, + } => Self::BrokenChainLink { + expected_prev: expected_prev.into(), + got_prev: got_prev.into(), + }, + indexer_core::BlockIngestError::HashMismatch { computed, header } => { + Self::HashMismatch { + computed: computed.into(), + header: header.into(), + } + } + indexer_core::BlockIngestError::EmptyBlock => Self::EmptyBlock, + indexer_core::BlockIngestError::InvalidClockTransaction => { + Self::InvalidClockTransaction + } + indexer_core::BlockIngestError::NonPublicGenesisTransaction => { + Self::NonPublicGenesisTransaction + } + indexer_core::BlockIngestError::StateTransition { tx_index, reason } => { + Self::StateTransition { tx_index, reason } + } + } + } +} + +impl From for StallReason { + fn from(value: indexer_core::StallReason) -> Self { + let indexer_core::StallReason { + block_id, + block_hash, + prev_block_hash, + l1_slot, + error, + first_seen, + orphans_since, + } = value; + + Self { + block_id, + block_hash: block_hash.map(Into::into), + prev_block_hash: prev_block_hash.map(Into::into), + l1_slot: l1_slot.into_inner(), + error: error.into(), + first_seen, + orphans_since, + } + } +} + +impl From for IndexerStatus { + fn from(value: indexer_core::status::IndexerStatus) -> Self { + let indexer_core::status::IndexerStatus { + sync, + indexed_block_id, + stall_reason, + } = value; + + Self { + state: sync.state.into(), + last_error: sync.last_error, + indexed_block_id, + stall_reason: stall_reason.map(Into::into), + } + } +} diff --git a/lez/indexer/service/protocol/src/lib.rs b/lez/indexer/service/protocol/src/lib.rs index a670dee6..e17d539b 100644 --- a/lez/indexer/service/protocol/src/lib.rs +++ b/lez/indexer/service/protocol/src/lib.rs @@ -363,3 +363,73 @@ pub enum BedrockStatus { Safe, Finalized, } + +/// Coarse lifecycle state of the indexer's ingestion loop, so a client can tell +/// "still catching up" apart from "something went wrong". +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] +pub enum IndexerSyncState { + /// Booted; no ingestion cycle has run yet. + Starting, + /// Streaming finalized messages toward the L1 frontier. + Syncing, + /// Drained the stream up to the last finalized block; idle until new blocks finalize. + CaughtUp, + /// The last cycle failed (e.g. the L1 node is unreachable). See `last_error`. + Error, + /// Parked on a stall reason: the validated tip is frozen awaiting a valid + /// continuation. See `last_error` and `stall_reason`. + Stalled, +} + +/// Why the indexer could not apply an L2 block from the channel. +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] +pub enum BlockIngestError { + Deserialize(String), + UnexpectedBlockId { + expected: u64, + got: u64, + }, + BrokenChainLink { + expected_prev: HashType, + got_prev: HashType, + }, + HashMismatch { + computed: HashType, + header: HashType, + }, + EmptyBlock, + InvalidClockTransaction, + NonPublicGenesisTransaction, + StateTransition { + /// Index of the failing transaction within the block body. + tx_index: u64, + reason: String, + }, +} + +/// Diagnostic record of the first block that broke the L2 chain. +/// +/// The block-derived fields are `None` for a deserialize break (no header was +/// ever parsed). `l1_slot` is the L1 slot the breaking inscription was read at. +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] +pub struct StallReason { + pub block_id: Option, + pub block_hash: Option, + pub prev_block_hash: Option, + pub l1_slot: u64, + pub error: BlockIngestError, + /// The breaking block's L2 timestamp (`None` for a deserialize break). + pub first_seen: Option, + /// Number of later non-chaining blocks seen while the tip is frozen. + pub orphans_since: u64, +} + +/// Status snapshot returned by `getStatus`: the ingestion state plus the +/// indexed L2 tip and, when stalled, the stall diagnostics. +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] +pub struct IndexerStatus { + pub state: IndexerSyncState, + pub last_error: Option, + pub indexed_block_id: Option, + pub stall_reason: Option, +} diff --git a/lez/indexer/service/rpc/src/lib.rs b/lez/indexer/service/rpc/src/lib.rs index 5763fe82..8ea807eb 100644 --- a/lez/indexer/service/rpc/src/lib.rs +++ b/lez/indexer/service/rpc/src/lib.rs @@ -1,4 +1,6 @@ -use indexer_service_protocol::{Account, AccountId, Block, BlockId, HashType, Transaction}; +use indexer_service_protocol::{ + Account, AccountId, Block, BlockId, HashType, IndexerStatus, Transaction, +}; use jsonrpsee::proc_macros::rpc; #[cfg(feature = "server")] use jsonrpsee::{core::SubscriptionResult, types::ErrorObjectOwned}; @@ -69,6 +71,9 @@ pub trait Rpc { limit: u64, ) -> Result, ErrorObjectOwned>; + #[method(name = "getStatus")] + async fn get_status(&self) -> Result; + // ToDo: expand healthcheck response into some kind of report #[method(name = "checkHealth")] async fn healthcheck(&self) -> Result<(), ErrorObjectOwned>; diff --git a/lez/indexer/service/src/lib.rs b/lez/indexer/service/src/lib.rs index b1c57163..aa142b38 100644 --- a/lez/indexer/service/src/lib.rs +++ b/lez/indexer/service/src/lib.rs @@ -5,6 +5,7 @@ pub use indexer_core::config::*; use indexer_service_rpc::RpcServer as _; use jsonrpsee::server::{Server, ServerHandle}; use log::{error, info}; +use tokio_util::sync::CancellationToken; pub mod service; @@ -69,9 +70,10 @@ pub async fn run_server( config: IndexerConfig, storage_dir: &Path, port: u16, + shutdown: CancellationToken, ) -> Result { #[cfg(feature = "mock-responses")] - let _ = (config, storage_dir); + let _ = (config, storage_dir, shutdown); let server = Server::builder() .build(SocketAddr::from(([0, 0, 0, 0], port))) @@ -86,7 +88,8 @@ pub async fn run_server( #[cfg(not(feature = "mock-responses"))] let handle = { - let service = service::IndexerService::new(config, storage_dir) + let service = service::IndexerService::new(config, storage_dir, shutdown.child_token()) + .await .context("Failed to initialize indexer service")?; server.start(service.into_rpc()) }; diff --git a/lez/indexer/service/src/main.rs b/lez/indexer/service/src/main.rs index 3e36967d..52f195e9 100644 --- a/lez/indexer/service/src/main.rs +++ b/lez/indexer/service/src/main.rs @@ -34,7 +34,9 @@ async fn main() -> Result<()> { let cancellation_token = listen_for_shutdown_signal(); let config = indexer_service::IndexerConfig::from_path(&config_path)?; - let indexer_handle = indexer_service::run_server(config, data_dir.as_path(), port).await?; + let indexer_handle = + indexer_service::run_server(config, data_dir.as_path(), port, cancellation_token.clone()) + .await?; tokio::select! { () = cancellation_token.cancelled() => { diff --git a/lez/indexer/service/src/mock_service.rs b/lez/indexer/service/src/mock_service.rs index 7bf6c528..70af6239 100644 --- a/lez/indexer/service/src/mock_service.rs +++ b/lez/indexer/service/src/mock_service.rs @@ -10,10 +10,10 @@ use std::{collections::HashMap, sync::Arc, time::Duration}; use indexer_service_protocol::{ Account, AccountId, BedrockStatus, Block, BlockBody, BlockHeader, BlockId, Commitment, - CommitmentSetDigest, Data, EncryptedAccountData, HashType, PrivacyPreservingMessage, - PrivacyPreservingTransaction, ProgramDeploymentMessage, ProgramDeploymentTransaction, - ProgramId, PublicMessage, PublicTransaction, Signature, Transaction, ValidityWindow, - WitnessSet, + CommitmentSetDigest, Data, EncryptedAccountData, HashType, IndexerStatus, IndexerSyncState, + PrivacyPreservingMessage, PrivacyPreservingTransaction, ProgramDeploymentMessage, + ProgramDeploymentTransaction, ProgramId, PublicMessage, PublicTransaction, Signature, + Transaction, ValidityWindow, WitnessSet, }; use jsonrpsee::{ core::{SubscriptionResult, async_trait}, @@ -325,6 +325,24 @@ impl indexer_service_rpc::RpcServer for MockIndexerService { .collect()) } + async fn get_status(&self) -> Result { + let indexed_block_id = self + .state + .read() + .await + .blocks + .iter() + .rev() + .find(|block| block.bedrock_status == BedrockStatus::Finalized) + .map(|block| block.header.block_id); + Ok(IndexerStatus { + state: IndexerSyncState::CaughtUp, + last_error: None, + indexed_block_id, + stall_reason: None, + }) + } + async fn healthcheck(&self) -> Result<(), ErrorObjectOwned> { Ok(()) } diff --git a/lez/indexer/service/src/service.rs b/lez/indexer/service/src/service.rs index 7a8ed90f..09759362 100644 --- a/lez/indexer/service/src/service.rs +++ b/lez/indexer/service/src/service.rs @@ -2,9 +2,11 @@ use std::{path::Path, pin::pin, sync::Arc}; use anyhow::{Context as _, Result, bail}; use arc_swap::ArcSwap; -use futures::{StreamExt as _, never::Never}; +use futures::StreamExt as _; use indexer_core::{IndexerCore, config::IndexerConfig}; -use indexer_service_protocol::{Account, AccountId, Block, BlockId, HashType, Transaction}; +use indexer_service_protocol::{ + Account, AccountId, Block, BlockId, HashType, IndexerStatus, Transaction, +}; use jsonrpsee::{ SubscriptionSink, core::{Serialize, SubscriptionResult, async_trait}, @@ -12,6 +14,7 @@ use jsonrpsee::{ }; use log::{debug, error, info, warn}; use tokio::sync::mpsc::UnboundedSender; +use tokio_util::sync::CancellationToken; pub struct IndexerService { subscription_service: SubscriptionService, @@ -19,9 +22,13 @@ pub struct IndexerService { } impl IndexerService { - pub fn new(config: IndexerConfig, storage_dir: &Path) -> Result { - let indexer = IndexerCore::new(config, storage_dir)?; - let subscription_service = SubscriptionService::spawn_new(indexer.clone()); + pub async fn new( + config: IndexerConfig, + storage_dir: &Path, + shutdown: CancellationToken, + ) -> Result { + let indexer = IndexerCore::new(config, storage_dir).await?; + let subscription_service = SubscriptionService::spawn_new(indexer.clone(), shutdown); Ok(Self { subscription_service, @@ -149,6 +156,10 @@ impl indexer_service_rpc::RpcServer for IndexerService { Ok(tx_res) } + async fn get_status(&self) -> Result { + Ok(self.indexer.status().into()) + } + async fn healthcheck(&self) -> Result<(), ErrorObjectOwned> { // Checking, that indexer can calculate last state let _ = self @@ -164,15 +175,21 @@ impl indexer_service_rpc::RpcServer for IndexerService { struct SubscriptionService { parts: ArcSwap, indexer: IndexerCore, + /// Cancellation token that is used to signal the subscription service to shut down. + /// + /// NOTE: This will auto-cancel on `Drop`, so if your token is shared with other parts + /// use [`CancellationToken::child_token()`] instead. + shutdown: CancellationToken, } impl SubscriptionService { - pub fn spawn_new(indexer: IndexerCore) -> Self { - let parts = Self::spawn_respond_subscribers_loop(indexer.clone()); + pub fn spawn_new(indexer: IndexerCore, shutdown: CancellationToken) -> Self { + let parts = Self::spawn_respond_subscribers_loop(indexer.clone(), shutdown.clone()); Self { parts: ArcSwap::new(Arc::new(parts)), indexer, + shutdown, } } @@ -184,14 +201,18 @@ impl SubscriptionService { ); // Respawn the subscription service loop if it has finished (either with error or panic) - if guard.handle.is_finished() { + if guard.handle.is_finished() && !self.shutdown.is_cancelled() { drop(guard); - let new_parts = Self::spawn_respond_subscribers_loop(self.indexer.clone()); + let new_parts = Self::spawn_respond_subscribers_loop( + self.indexer.clone(), + self.shutdown.clone(), + ); let old_handle_and_sender = self.parts.swap(Arc::new(new_parts)); let old_parts = Arc::into_inner(old_handle_and_sender) .expect("There should be no other references to the old handle and sender"); match old_parts.handle.await { + Ok(Ok(())) => {} Ok(Err(err)) => { error!( "Subscription service loop has unexpectedly finished with error: {err:#}" @@ -209,7 +230,10 @@ impl SubscriptionService { Ok(()) } - fn spawn_respond_subscribers_loop(indexer: IndexerCore) -> SubscriptionLoopParts { + fn spawn_respond_subscribers_loop( + indexer: IndexerCore, + shutdown: CancellationToken, + ) -> SubscriptionLoopParts { let (new_subscription_sender, mut sub_receiver) = tokio::sync::mpsc::unbounded_channel::>(); @@ -225,6 +249,10 @@ impl SubscriptionService { )] loop { tokio::select! { + () = shutdown.cancelled() => { + info!("Shutdown requested; stopping block ingestion"); + return Ok(()); + } sub = sub_receiver.recv() => { let Some(subscription) = sub else { bail!("Subscription receiver closed unexpectedly"); @@ -253,10 +281,11 @@ impl SubscriptionService { } } }; - let res: anyhow::Result = run_loop.await; - let Err(err) = res; - error!("Subscription service loop has unexpectedly finished with error: {err:#?}"); - Err(err) + let res: anyhow::Result<()> = run_loop.await; + if let Err(err) = &res { + error!("Subscription service loop has unexpectedly finished with error: {err:#?}"); + } + res }); SubscriptionLoopParts { handle, @@ -267,12 +296,13 @@ impl SubscriptionService { impl Drop for SubscriptionService { fn drop(&mut self) { + self.shutdown.cancel(); self.parts.load().handle.abort(); } } struct SubscriptionLoopParts { - handle: tokio::task::JoinHandle>, + handle: tokio::task::JoinHandle>, new_subscription_sender: UnboundedSender>, } diff --git a/lez/programs/Cargo.toml b/lez/programs/Cargo.toml index 06038d7f..707c3d6b 100644 --- a/lez/programs/Cargo.toml +++ b/lez/programs/Cargo.toml @@ -54,6 +54,36 @@ name = "vault" path = "vault/src/main.rs" required-features = ["programs"] +[[bin]] +name = "cross_zone_outbox" +path = "cross_zone_outbox/src/main.rs" +required-features = ["programs"] + +[[bin]] +name = "cross_zone_inbox" +path = "cross_zone_inbox/src/main.rs" +required-features = ["programs"] + +[[bin]] +name = "ping_sender" +path = "ping_sender/src/main.rs" +required-features = ["programs"] + +[[bin]] +name = "ping_receiver" +path = "ping_receiver/src/main.rs" +required-features = ["programs"] + +[[bin]] +name = "bridge_lock" +path = "bridge_lock/src/main.rs" +required-features = ["programs"] + +[[bin]] +name = "wrapped_token" +path = "wrapped_token/src/main.rs" +required-features = ["programs"] + [features] # TODO: Uncomment once https://github.com/risc0/risc0/issues/3772 is resolved. # default = ["artifacts"] @@ -78,6 +108,11 @@ programs = [ "dep:faucet_core", "dep:token_core", "dep:vault_core", + "dep:cross_zone_inbox_core", + "dep:cross_zone_outbox_core", + "dep:bridge_lock_core", + "dep:wrapped_token_core", + "dep:ping_core", ] [dependencies] @@ -92,6 +127,11 @@ clock_core = { workspace = true, optional = true } faucet_core = { workspace = true, optional = true } token_core = { workspace = true, optional = true } vault_core = { workspace = true, optional = true } +cross_zone_inbox_core = { workspace = true, optional = true } +cross_zone_outbox_core = { workspace = true, optional = true } +bridge_lock_core = { workspace = true, optional = true } +wrapped_token_core = { workspace = true, optional = true } +ping_core = { workspace = true, optional = true } amm_program = { path = "amm", optional = true } associated_token_account_program = { path = "associated_token_account", optional = true } diff --git a/lez/programs/bridge_lock/Cargo.toml b/lez/programs/bridge_lock/Cargo.toml new file mode 100644 index 00000000..8a4d6c20 --- /dev/null +++ b/lez/programs/bridge_lock/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "bridge_lock_program" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +bridge_lock_core.workspace = true +cross_zone_outbox_core.workspace = true +wrapped_token_core.workspace = true +risc0-zkvm.workspace = true diff --git a/lez/programs/bridge_lock/core/Cargo.toml b/lez/programs/bridge_lock/core/Cargo.toml new file mode 100644 index 00000000..190fc4f2 --- /dev/null +++ b/lez/programs/bridge_lock/core/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "bridge_lock_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +serde = { workspace = true, features = ["alloc"] } diff --git a/lez/programs/bridge_lock/core/src/lib.rs b/lez/programs/bridge_lock/core/src/lib.rs new file mode 100644 index 00000000..e5a36113 --- /dev/null +++ b/lez/programs/bridge_lock/core/src/lib.rs @@ -0,0 +1,71 @@ +//! Core types for the bridge-lock program, the source side of the cross-zone +//! token bridge. A holder locks part of their balance into an escrow and emits a +//! cross-zone message minting the wrapped token on the target zone. + +use lee_core::{ + account::AccountId, + program::{PdaSeed, ProgramId}, +}; +use serde::{Deserialize, Serialize}; + +const ESCROW_SEED_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/BridgeLockEscrow/0000/"; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum Instruction { + /// Lock `amount` of the holder's balance and emit a cross-zone message + /// minting the wrapped token on `target_zone`. The emission fields mirror + /// `cross_zone_outbox::Instruction::Emit` so the watcher reads them directly. + /// + /// Required accounts (3): holder holding (authorized), escrow PDA, outbox PDA. + Lock { + amount: u128, + target_zone: [u8; 32], + target_program_id: ProgramId, + target_accounts: Vec<[u8; 32]>, + payload: Vec, + outbox_program_id: ProgramId, + ordinal: u32, + }, +} + +/// PDA accumulating all locked balance on this zone. +#[must_use] +pub fn escrow_account_id(bridge_lock_id: ProgramId) -> AccountId { + AccountId::for_public_pda(&bridge_lock_id, &escrow_seed()) +} + +#[must_use] +pub const fn escrow_seed() -> PdaSeed { + PdaSeed::new(ESCROW_SEED_DOMAIN) +} + +/// Reads a bridgeable balance from account data; empty data is a zero balance. +#[must_use] +pub fn read_balance(data: &[u8]) -> u128 { + if data.len() < 16 { + return 0; + } + u128::from_le_bytes(data[..16].try_into().unwrap_or_else(|_| unreachable!())) +} + +#[must_use] +pub const fn balance_bytes(amount: u128) -> [u8; 16] { + amount.to_le_bytes() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn balance_round_trips() { + assert_eq!(read_balance(&balance_bytes(7)), 7); + assert_eq!(read_balance(&[]), 0); + } + + #[test] + fn escrow_is_stable() { + let id: ProgramId = [4; 8]; + assert_eq!(escrow_account_id(id), escrow_account_id(id)); + } +} diff --git a/lez/programs/bridge_lock/src/main.rs b/lez/programs/bridge_lock/src/main.rs new file mode 100644 index 00000000..aa8b5f93 --- /dev/null +++ b/lez/programs/bridge_lock/src/main.rs @@ -0,0 +1,121 @@ +use bridge_lock_core::{Instruction, balance_bytes, escrow_account_id, escrow_seed, read_balance}; +use cross_zone_outbox_core::Instruction as OutboxInstruction; +use lee_core::{ + account::AccountWithMetadata, + program::{AccountPostState, ChainedCall, Claim, ProgramInput, ProgramOutput, read_lee_inputs}, +}; +use wrapped_token_core::Instruction as WrappedInstruction; + +fn main() { + let ( + ProgramInput { + self_program_id, + caller_program_id, + pre_states, + instruction, + }, + instruction_words, + ) = read_lee_inputs::(); + + assert!( + caller_program_id.is_none(), + "bridge_lock is only invoked as a top-level user transaction" + ); + + let Instruction::Lock { + amount, + target_zone, + target_program_id, + target_accounts, + payload, + outbox_program_id, + ordinal, + } = instruction; + + // Value conservation: the forwarded payload must mint exactly what is locked. + let WrappedInstruction::Mint { + amount: mint_amount, + .. + } = decode_mint(&payload); + assert_eq!( + mint_amount, amount, + "locked amount must equal the wrapped mint amount" + ); + + // pre_states: [holder holding (authorized), escrow PDA, outbox PDA]. + let [holder, escrow, outbox] = <[AccountWithMetadata; 3]>::try_from(pre_states) + .expect("Lock requires holder, escrow, and outbox accounts"); + + assert!(holder.is_authorized, "holder must authorize the lock"); + assert_eq!( + holder.account.program_owner, self_program_id, + "holder account must be a bridge_lock holding" + ); + assert_eq!( + escrow.account_id, + escrow_account_id(self_program_id), + "second account must be the escrow PDA" + ); + + let holder_new = read_balance(&holder.account.data.clone().into_inner()) + .checked_sub(amount) + .expect("insufficient balance to lock"); + let escrow_new = read_balance(&escrow.account.data.clone().into_inner()) + .checked_add(amount) + .expect("escrow balance overflow"); + + let mut holder_account = holder.account.clone(); + holder_account.data = balance_bytes(holder_new) + .to_vec() + .try_into() + .expect("balance fits in account data"); + let holder_post = AccountPostState::new(holder_account); + + let mut escrow_account = escrow.account.clone(); + escrow_account.data = balance_bytes(escrow_new) + .to_vec() + .try_into() + .expect("balance fits in account data"); + let escrow_post = + AccountPostState::new_claimed_if_default(escrow_account, Claim::Pda(escrow_seed())); + + let call = ChainedCall::new( + outbox_program_id, + vec![outbox.clone()], + &OutboxInstruction::Emit { + target_zone, + target_program_id, + target_accounts, + payload, + ordinal, + }, + ); + + ProgramOutput::new( + self_program_id, + caller_program_id, + instruction_words, + vec![holder, escrow, outbox.clone()], + vec![ + holder_post, + escrow_post, + AccountPostState::new(outbox.account), + ], + ) + .with_chained_calls(vec![call]) + .write(); +} + +/// Decodes the cross-zone payload (risc0 words, little-endian bytes) into the +/// wrapped-token instruction it carries. +fn decode_mint(payload: &[u8]) -> WrappedInstruction { + assert!( + payload.len().is_multiple_of(4), + "payload must be u32-aligned instruction words" + ); + let words: Vec = payload + .chunks_exact(4) + .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap_or_else(|_| unreachable!()))) + .collect(); + risc0_zkvm::serde::from_slice(&words).expect("payload decodes to a wrapped-token instruction") +} diff --git a/lez/programs/cross_zone_inbox/Cargo.toml b/lez/programs/cross_zone_inbox/Cargo.toml new file mode 100644 index 00000000..6f4c651a --- /dev/null +++ b/lez/programs/cross_zone_inbox/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "cross_zone_inbox_program" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +cross_zone_inbox_core.workspace = true diff --git a/lez/programs/cross_zone_inbox/core/Cargo.toml b/lez/programs/cross_zone_inbox/core/Cargo.toml new file mode 100644 index 00000000..6bea109f --- /dev/null +++ b/lez/programs/cross_zone_inbox/core/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "cross_zone_inbox_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +serde = { workspace = true, features = ["alloc"] } +risc0-zkvm.workspace = true +borsh.workspace = true diff --git a/lez/programs/cross_zone_inbox/core/src/lib.rs b/lez/programs/cross_zone_inbox/core/src/lib.rs new file mode 100644 index 00000000..761a8a1c --- /dev/null +++ b/lez/programs/cross_zone_inbox/core/src/lib.rs @@ -0,0 +1,203 @@ +use std::collections::{BTreeMap, BTreeSet}; + +use borsh::{BorshDeserialize, BorshSerialize}; +use lee_core::{ + account::AccountId, + program::{PdaSeed, ProgramId}, +}; +use serde::{Deserialize, Serialize}; + +/// Source blocks per seen-set shard, so no single seen account grows without bound. +pub const EPOCH_BLOCKS: u64 = 10_000; + +const MESSAGE_KEY_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/CrossZoneMsgKey/00000/"; +const INBOX_CONFIG_SEED: [u8; 32] = *b"/LEZ/v0.3/CrossZoneInboxCfg/000/"; +const INBOX_SEEN_SEED_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/CrossZoneInboxSeen/00/"; + +/// Raw 32-byte zone (channel) id; the host maps it to the zone-sdk `ChannelId`. +pub type ZoneId = [u8; 32]; + +/// Block-signing public key pinned per peer zone. +pub type ExpectedPubkey = [u8; 32]; + +/// Content-addressed replay key for a delivered message. +pub type MessageKey = [u8; 32]; + +/// A peer zone whose outbox a zone watches for inbound cross-zone messages. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct CrossZonePeer { + /// The peer's Bedrock channel; its 32 bytes double as the peer's zone id. + pub channel_id: ZoneId, + /// Programs on the local zone a message from this peer is allowed to target. + pub allowed_targets: Vec, + /// The peer's block-signing public key, pinned to reject blocks inscribed by + /// anyone other than that zone's sequencer. `None` skips the check (the + /// channel signer is still authenticated by the zone-sdk). + #[serde(default)] + pub expected_block_signing_pubkey: Option<[u8; 32]>, +} + +/// Cross-zone configuration shared by a zone's sequencer (watcher) and indexer +/// (verifier): the peers it reads from Bedrock and, per peer, the local programs +/// they may deliver to. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct CrossZoneConfig { + pub peers: Vec, +} + +/// A finalized outbound message observed on a peer zone, addressed to a program +/// on this zone. The watcher fills it from the peer's block; it is never +/// self-reported by a user. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct CrossZoneMessage { + pub src_zone: ZoneId, + pub src_block_id: u64, + pub src_tx_index: u32, + pub src_program_id: ProgramId, + pub target_program_id: ProgramId, + pub payload: Vec, + /// Reserved for a future source-state proof; MUST be `None` in v1. + pub l1_inclusion_witness: Option>, +} + +/// Peer and per-peer target allowlists, plus this inbox's own zone id. +#[derive( + Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshSerialize, BorshDeserialize, +)] +pub struct InboxConfig { + pub self_zone: ZoneId, + pub allowed_peers: BTreeMap, + pub allowed_targets: BTreeMap>, +} + +impl InboxConfig { + /// Borsh-encoded form stored in the inbox config account. + #[must_use] + pub fn to_bytes(&self) -> Vec { + borsh::to_vec(self).expect("InboxConfig serializes") + } + + /// Decodes an [`InboxConfig`] from account data. + pub fn from_bytes(bytes: &[u8]) -> borsh::io::Result { + borsh::from_slice(bytes) + } +} + +/// The replay keys seen for one `(src_zone, epoch)` shard. +#[derive(Clone, Debug, Default, PartialEq, Eq, BorshSerialize, BorshDeserialize)] +pub struct SeenShard(pub BTreeSet); + +impl SeenShard { + /// Decodes a shard from account data; empty data is an empty shard. + pub fn from_bytes(bytes: &[u8]) -> borsh::io::Result { + if bytes.is_empty() { + return Ok(Self::default()); + } + borsh::from_slice(bytes) + } + + #[must_use] + pub fn to_bytes(&self) -> Vec { + borsh::to_vec(self).expect("SeenShard serializes") + } + + #[must_use] + pub fn contains(&self, key: &MessageKey) -> bool { + self.0.contains(key) + } + + /// Inserts a key; returns true if it was newly inserted. + pub fn insert(&mut self, key: MessageKey) -> bool { + self.0.insert(key) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum Instruction { + /// Delivers a finalized peer message to its target program. + Dispatch(CrossZoneMessage), +} + +/// Content-addressed replay key for a delivered message. +/// +/// Hashes `(src_zone, src_block_id, src_tx_index)` under a domain separator. +/// Watcher-independent and immune to proof malleability, since it keys on block +/// id plus index rather than a tx hash. +#[must_use] +pub fn message_key(src_zone: &ZoneId, src_block_id: u64, src_tx_index: u32) -> MessageKey { + use risc0_zkvm::sha::{Impl, Sha256 as _}; + + let mut bytes = [0_u8; 76]; + bytes[..32].copy_from_slice(&MESSAGE_KEY_DOMAIN); + bytes[32..64].copy_from_slice(src_zone); + bytes[64..72].copy_from_slice(&src_block_id.to_le_bytes()); + bytes[72..].copy_from_slice(&src_tx_index.to_le_bytes()); + + Impl::hash_bytes(&bytes) + .as_bytes() + .try_into() + .unwrap_or_else(|_| unreachable!()) +} + +/// The config account holding the allowlists. +#[must_use] +pub fn inbox_config_account_id(inbox_id: ProgramId) -> AccountId { + AccountId::for_public_pda(&inbox_id, &PdaSeed::new(INBOX_CONFIG_SEED)) +} + +/// The seen-set shard for the `(src_zone, epoch)` the message falls in. +#[must_use] +pub fn inbox_seen_shard_account_id( + inbox_id: ProgramId, + src_zone: &ZoneId, + src_block_id: u64, +) -> AccountId { + AccountId::for_public_pda(&inbox_id, &inbox_seen_shard_seed(src_zone, src_block_id)) +} + +/// Seed of the seen-shard PDA, exposed so the guest can claim the account. +#[must_use] +pub fn inbox_seen_shard_seed(src_zone: &ZoneId, src_block_id: u64) -> PdaSeed { + use risc0_zkvm::sha::{Impl, Sha256 as _}; + + let src_epoch = src_block_id.wrapping_div(EPOCH_BLOCKS); + let mut bytes = [0_u8; 72]; + bytes[..32].copy_from_slice(&INBOX_SEEN_SEED_DOMAIN); + bytes[32..64].copy_from_slice(src_zone); + bytes[64..].copy_from_slice(&src_epoch.to_le_bytes()); + + let seed: [u8; 32] = Impl::hash_bytes(&bytes) + .as_bytes() + .try_into() + .unwrap_or_else(|_| unreachable!()); + PdaSeed::new(seed) +} +#[cfg(test)] +mod tests { + use super::*; + + fn zone(b: u8) -> ZoneId { + [b; 32] + } + + #[test] + fn message_key_is_stable_and_content_addressed() { + assert_eq!(message_key(&zone(1), 7, 3), message_key(&zone(1), 7, 3)); + assert_ne!(message_key(&zone(1), 7, 3), message_key(&zone(2), 7, 3)); + assert_ne!(message_key(&zone(1), 7, 3), message_key(&zone(1), 8, 3)); + assert_ne!(message_key(&zone(1), 7, 3), message_key(&zone(1), 7, 4)); + } + + #[test] + fn seen_shards_split_on_epoch_boundary() { + let id: ProgramId = [9; 8]; + assert_eq!( + inbox_seen_shard_account_id(id, &zone(1), 0), + inbox_seen_shard_account_id(id, &zone(1), EPOCH_BLOCKS - 1), + ); + assert_ne!( + inbox_seen_shard_account_id(id, &zone(1), EPOCH_BLOCKS - 1), + inbox_seen_shard_account_id(id, &zone(1), EPOCH_BLOCKS), + ); + } +} diff --git a/lez/programs/cross_zone_inbox/src/main.rs b/lez/programs/cross_zone_inbox/src/main.rs new file mode 100644 index 00000000..8bb06ed8 --- /dev/null +++ b/lez/programs/cross_zone_inbox/src/main.rs @@ -0,0 +1,125 @@ +use cross_zone_inbox_core::{ + InboxConfig, Instruction, SeenShard, inbox_config_account_id, inbox_seen_shard_account_id, + inbox_seen_shard_seed, message_key, +}; +use lee_core::{ + account::AccountWithMetadata, + program::{AccountPostState, ChainedCall, Claim, ProgramInput, ProgramOutput, read_lee_inputs}, +}; + +fn unchanged(pre: &AccountWithMetadata) -> AccountPostState { + AccountPostState::new(pre.account.clone()) +} + +fn main() { + let ( + ProgramInput { + self_program_id, + caller_program_id, + pre_states, + instruction, + }, + instruction_words, + ) = read_lee_inputs::(); + + assert!( + caller_program_id.is_none(), + "Inbox is only invoked as a top-level sequencer-origin transaction" + ); + + let Instruction::Dispatch(msg) = instruction; + + assert!( + msg.l1_inclusion_witness.is_none(), + "l1_inclusion_witness must be None in v1" + ); + + // pre_states layout: [config, seen_shard, then the target accounts]. + let mut accounts = pre_states.into_iter(); + let config = accounts.next().expect("config account required"); + let seen = accounts.next().expect("seen shard account required"); + let target_accounts: Vec = accounts.collect(); + + assert_eq!( + config.account_id, + inbox_config_account_id(self_program_id), + "First account must be the inbox config PDA" + ); + assert_eq!( + seen.account_id, + inbox_seen_shard_account_id(self_program_id, &msg.src_zone, msg.src_block_id), + "Second account must be the seen-shard PDA" + ); + + let cfg = InboxConfig::from_bytes(&config.account.data.clone().into_inner()) + .expect("inbox config decodes"); + + assert!( + msg.src_zone != cfg.self_zone, + "Source zone must not be this zone" + ); + let allowed_targets = cfg + .allowed_targets + .get(&msg.src_zone) + .expect("Source zone is not an allowed peer"); + assert!( + allowed_targets.contains(&msg.target_program_id), + "Target program is not allowed for this peer" + ); + + let key = message_key(&msg.src_zone, msg.src_block_id, msg.src_tx_index); + let mut shard = + SeenShard::from_bytes(&seen.account.data.clone().into_inner()).expect("seen shard decodes"); + let already_seen = shard.contains(&key); + + // On replay this is a no-op: the seen shard is untouched and no call is made. + let (seen_post, chained_calls) = if already_seen { + (unchanged(&seen), vec![]) + } else { + shard.insert(key); + let mut seen_account = seen.account.clone(); + seen_account.data = shard + .to_bytes() + .try_into() + .expect("seen shard fits in account data"); + let seen_post = AccountPostState::new_claimed_if_default( + seen_account, + Claim::Pda(inbox_seen_shard_seed(&msg.src_zone, msg.src_block_id)), + ); + + // The payload carries the target instruction as risc0 words, little-endian. + assert!( + msg.payload.len().is_multiple_of(4), + "payload must be u32-aligned instruction words" + ); + let instruction_data = msg + .payload + .chunks_exact(4) + .map(|c| u32::from_le_bytes(c.try_into().unwrap_or_else(|_| unreachable!()))) + .collect(); + + let call = ChainedCall { + program_id: msg.target_program_id, + pre_states: target_accounts.clone(), + instruction_data, + pda_seeds: vec![], + }; + (seen_post, vec![call]) + }; + + let mut post_states = vec![unchanged(&config), seen_post]; + post_states.extend(target_accounts.iter().map(unchanged)); + + let mut output_pre_states = vec![config, seen]; + output_pre_states.extend(target_accounts); + + ProgramOutput::new( + self_program_id, + caller_program_id, + instruction_words, + output_pre_states, + post_states, + ) + .with_chained_calls(chained_calls) + .write(); +} diff --git a/lez/programs/cross_zone_outbox/Cargo.toml b/lez/programs/cross_zone_outbox/Cargo.toml new file mode 100644 index 00000000..2f236cbf --- /dev/null +++ b/lez/programs/cross_zone_outbox/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "cross_zone_outbox_program" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +cross_zone_outbox_core.workspace = true diff --git a/lez/programs/cross_zone_outbox/core/Cargo.toml b/lez/programs/cross_zone_outbox/core/Cargo.toml new file mode 100644 index 00000000..c7876286 --- /dev/null +++ b/lez/programs/cross_zone_outbox/core/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "cross_zone_outbox_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +serde = { workspace = true, features = ["alloc"] } +risc0-zkvm.workspace = true +borsh.workspace = true diff --git a/lez/programs/cross_zone_outbox/core/src/lib.rs b/lez/programs/cross_zone_outbox/core/src/lib.rs new file mode 100644 index 00000000..736b5fa2 --- /dev/null +++ b/lez/programs/cross_zone_outbox/core/src/lib.rs @@ -0,0 +1,93 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use lee_core::{ + account::AccountId, + program::{PdaSeed, ProgramId}, +}; +use serde::{Deserialize, Serialize}; + +const OUTBOX_SEED_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/CrossZoneOutbox/00000/"; + +/// Raw 32-byte zone (channel) id; the host maps it to the zone-sdk `ChannelId`. +pub type ZoneId = [u8; 32]; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum Instruction { + /// Records an outbound cross-zone message as a write to a self-owned PDA. + /// + /// Required accounts (1): + /// - Outbox PDA account + Emit { + target_zone: ZoneId, + target_program_id: ProgramId, + /// Accounts the destination inbox must hand to the target program's + /// chained call. The emitter specifies them; the watcher forwards them + /// verbatim so the inbox stays target-agnostic. + target_accounts: Vec<[u8; 32]>, + payload: Vec, + ordinal: u32, + }, +} + +/// The message as stored in an outbox PDA. The destination zone's watcher reads +/// this from the inscribed block; the source coordinates are filled by the +/// watcher, not stored here. +#[derive(Clone, Debug, PartialEq, Eq, BorshSerialize, BorshDeserialize)] +pub struct OutboxRecord { + pub target_zone: ZoneId, + pub target_program_id: ProgramId, + pub target_accounts: Vec<[u8; 32]>, + pub payload: Vec, +} + +impl OutboxRecord { + /// Borsh-encoded form stored in the outbox PDA's account data. + #[must_use] + pub fn to_bytes(&self) -> Vec { + borsh::to_vec(self).expect("OutboxRecord serializes") + } + + /// Decodes an [`OutboxRecord`] from account data. + pub fn from_bytes(bytes: &[u8]) -> borsh::io::Result { + borsh::from_slice(bytes) + } +} + +/// PDA holding one emitted message, keyed by destination zone and a per-zone +/// ordinal. +#[must_use] +pub fn outbox_pda(outbox_id: ProgramId, target_zone: &ZoneId, ordinal: u32) -> AccountId { + AccountId::for_public_pda(&outbox_id, &outbox_pda_seed(target_zone, ordinal)) +} + +/// Seed of an outbox message PDA, exposed so the guest can claim the account. +#[must_use] +pub fn outbox_pda_seed(target_zone: &ZoneId, ordinal: u32) -> PdaSeed { + use risc0_zkvm::sha::{Impl, Sha256 as _}; + + let mut bytes = [0_u8; 68]; + bytes[..32].copy_from_slice(&OUTBOX_SEED_DOMAIN); + bytes[32..64].copy_from_slice(target_zone); + bytes[64..].copy_from_slice(&ordinal.to_le_bytes()); + + let seed: [u8; 32] = Impl::hash_bytes(&bytes) + .as_bytes() + .try_into() + .unwrap_or_else(|_| unreachable!()); + PdaSeed::new(seed) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn outbox_pda_is_unique_per_zone_and_ordinal() { + let id: ProgramId = [3; 8]; + let zone_a = [1; 32]; + let zone_b = [2; 32]; + + assert_eq!(outbox_pda(id, &zone_a, 0), outbox_pda(id, &zone_a, 0)); + assert_ne!(outbox_pda(id, &zone_a, 0), outbox_pda(id, &zone_a, 1)); + assert_ne!(outbox_pda(id, &zone_a, 0), outbox_pda(id, &zone_b, 0)); + } +} diff --git a/lez/programs/cross_zone_outbox/src/main.rs b/lez/programs/cross_zone_outbox/src/main.rs new file mode 100644 index 00000000..432e8d9c --- /dev/null +++ b/lez/programs/cross_zone_outbox/src/main.rs @@ -0,0 +1,72 @@ +use cross_zone_outbox_core::{Instruction, OutboxRecord, outbox_pda, outbox_pda_seed}; +use lee_core::{ + account::AccountWithMetadata, + program::{AccountPostState, Claim, ProgramInput, ProgramOutput, read_lee_inputs}, +}; + +fn main() { + let ( + ProgramInput { + self_program_id, + caller_program_id, + pre_states, + instruction, + }, + instruction_words, + ) = read_lee_inputs::(); + + assert!( + caller_program_id.is_some(), + "Outbox is only callable through a chain call from a user program" + ); + + let (target_zone, target_program_id, target_accounts, payload, ordinal) = match instruction { + Instruction::Emit { + target_zone, + target_program_id, + target_accounts, + payload, + ordinal, + } => ( + target_zone, + target_program_id, + target_accounts, + payload, + ordinal, + ), + }; + + let [outbox] = + <[AccountWithMetadata; 1]>::try_from(pre_states).expect("Emit requires exactly 1 account"); + + assert_eq!( + outbox.account_id, + outbox_pda(self_program_id, &target_zone, ordinal), + "Account must be the outbox PDA for (target_zone, ordinal)" + ); + + let mut post_account = outbox.account.clone(); + post_account.data = OutboxRecord { + target_zone, + target_program_id, + target_accounts, + payload, + } + .to_bytes() + .try_into() + .expect("OutboxRecord fits in account data"); + + let post = AccountPostState::new_claimed_if_default( + post_account, + Claim::Pda(outbox_pda_seed(&target_zone, ordinal)), + ); + + ProgramOutput::new( + self_program_id, + caller_program_id, + instruction_words, + vec![outbox], + vec![post], + ) + .write(); +} diff --git a/lez/programs/ping_core/Cargo.toml b/lez/programs/ping_core/Cargo.toml new file mode 100644 index 00000000..29870630 --- /dev/null +++ b/lez/programs/ping_core/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "ping_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +serde = { workspace = true, features = ["alloc"] } diff --git a/lez/programs/ping_core/src/lib.rs b/lez/programs/ping_core/src/lib.rs new file mode 100644 index 00000000..80b27247 --- /dev/null +++ b/lez/programs/ping_core/src/lib.rs @@ -0,0 +1,38 @@ +use lee_core::{ + account::AccountId, + program::{PdaSeed, ProgramId}, +}; +use serde::{Deserialize, Serialize}; + +const PING_RECORD_SEED: [u8; 32] = *b"/LEZ/v0.3/PingRecord/0000000000/"; + +/// Instruction delivered to `ping_receiver` by the inbox: record the payload. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum ReceiverInstruction { + Record { payload: Vec }, +} + +/// Instruction to `ping_sender`: forwarded verbatim into `cross_zone_outbox::Instruction::Emit`. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum SenderInstruction { + Send { + outbox_program_id: ProgramId, + target_zone: [u8; 32], + target_program_id: ProgramId, + target_accounts: Vec<[u8; 32]>, + payload: Vec, + ordinal: u32, + }, +} + +/// The account a `ping_receiver` records the latest delivered payload into. +#[must_use] +pub fn ping_record_pda(receiver_id: ProgramId) -> AccountId { + AccountId::for_public_pda(&receiver_id, &ping_record_seed()) +} + +/// Seed of the record PDA, exposed so the guest can claim the account. +#[must_use] +pub const fn ping_record_seed() -> PdaSeed { + PdaSeed::new(PING_RECORD_SEED) +} diff --git a/lez/programs/ping_receiver/Cargo.toml b/lez/programs/ping_receiver/Cargo.toml new file mode 100644 index 00000000..a1d88f39 --- /dev/null +++ b/lez/programs/ping_receiver/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "ping_receiver_program" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +ping_core.workspace = true diff --git a/lez/programs/ping_receiver/src/main.rs b/lez/programs/ping_receiver/src/main.rs new file mode 100644 index 00000000..4fd9679f --- /dev/null +++ b/lez/programs/ping_receiver/src/main.rs @@ -0,0 +1,48 @@ +use lee_core::{ + account::AccountWithMetadata, + program::{AccountPostState, Claim, ProgramInput, ProgramOutput, read_lee_inputs}, +}; +use ping_core::{ReceiverInstruction, ping_record_pda, ping_record_seed}; + +fn main() { + let ( + ProgramInput { + self_program_id, + caller_program_id, + pre_states, + instruction, + }, + instruction_words, + ) = read_lee_inputs::(); + + assert!( + caller_program_id.is_some(), + "ping_receiver is only callable through a chained call" + ); + + let payload = match instruction { + ReceiverInstruction::Record { payload } => payload, + }; + + let [record] = <[AccountWithMetadata; 1]>::try_from(pre_states) + .expect("Record requires exactly 1 account"); + assert_eq!( + record.account_id, + ping_record_pda(self_program_id), + "Account must be the ping record PDA" + ); + + let mut post_account = record.account.clone(); + post_account.data = payload.try_into().expect("payload fits in account data"); + let post = + AccountPostState::new_claimed_if_default(post_account, Claim::Pda(ping_record_seed())); + + ProgramOutput::new( + self_program_id, + caller_program_id, + instruction_words, + vec![record], + vec![post], + ) + .write(); +} diff --git a/lez/programs/ping_sender/Cargo.toml b/lez/programs/ping_sender/Cargo.toml new file mode 100644 index 00000000..8567ae55 --- /dev/null +++ b/lez/programs/ping_sender/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "ping_sender_program" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +ping_core.workspace = true +cross_zone_outbox_core.workspace = true diff --git a/lez/programs/ping_sender/src/main.rs b/lez/programs/ping_sender/src/main.rs new file mode 100644 index 00000000..d0ad04f5 --- /dev/null +++ b/lez/programs/ping_sender/src/main.rs @@ -0,0 +1,59 @@ +use cross_zone_outbox_core::Instruction as OutboxInstruction; +use lee_core::{ + account::AccountWithMetadata, + program::{AccountPostState, ChainedCall, ProgramInput, ProgramOutput, read_lee_inputs}, +}; +use ping_core::SenderInstruction; + +fn main() { + let ( + ProgramInput { + self_program_id, + caller_program_id, + pre_states, + instruction, + }, + instruction_words, + ) = read_lee_inputs::(); + + assert!( + caller_program_id.is_none(), + "ping_sender is only invoked as a top-level user transaction" + ); + + let SenderInstruction::Send { + outbox_program_id, + target_zone, + target_program_id, + target_accounts, + payload, + ordinal, + } = instruction; + + // The single account is the outbox PDA the chained call writes into; the + // outbox claims it, so ping_sender forwards it unchanged. + let [outbox] = + <[AccountWithMetadata; 1]>::try_from(pre_states).expect("Send requires exactly 1 account"); + + let call = ChainedCall::new( + outbox_program_id, + vec![outbox.clone()], + &OutboxInstruction::Emit { + target_zone, + target_program_id, + target_accounts, + payload, + ordinal, + }, + ); + + ProgramOutput::new( + self_program_id, + caller_program_id, + instruction_words, + vec![outbox.clone()], + vec![AccountPostState::new(outbox.account)], + ) + .with_chained_calls(vec![call]) + .write(); +} diff --git a/lez/programs/src/lib.rs b/lez/programs/src/lib.rs index 4acade0f..fb448038 100644 --- a/lez/programs/src/lib.rs +++ b/lez/programs/src/lib.rs @@ -10,9 +10,12 @@ mod inner { use guests::{ AMM_ELF, AMM_ID, ASSOCIATED_TOKEN_ACCOUNT_ELF, ASSOCIATED_TOKEN_ACCOUNT_ID, - AUTHENTICATED_TRANSFER_ELF, AUTHENTICATED_TRANSFER_ID, BRIDGE_ELF, BRIDGE_ID, CLOCK_ELF, - CLOCK_ID, FAUCET_ELF, FAUCET_ID, PINATA_ELF, PINATA_ID, PINATA_TOKEN_ELF, PINATA_TOKEN_ID, - TOKEN_ELF, TOKEN_ID, VAULT_ELF, VAULT_ID, + AUTHENTICATED_TRANSFER_ELF, AUTHENTICATED_TRANSFER_ID, BRIDGE_ELF, BRIDGE_ID, + BRIDGE_LOCK_ELF, BRIDGE_LOCK_ID, CLOCK_ELF, CLOCK_ID, CROSS_ZONE_INBOX_ELF, + CROSS_ZONE_INBOX_ID, CROSS_ZONE_OUTBOX_ELF, CROSS_ZONE_OUTBOX_ID, FAUCET_ELF, FAUCET_ID, + PINATA_ELF, PINATA_ID, PINATA_TOKEN_ELF, PINATA_TOKEN_ID, PING_RECEIVER_ELF, + PING_RECEIVER_ID, PING_SENDER_ELF, PING_SENDER_ID, TOKEN_ELF, TOKEN_ID, VAULT_ELF, + VAULT_ID, WRAPPED_TOKEN_ELF, WRAPPED_TOKEN_ID, }; use lee::program::Program; @@ -87,6 +90,42 @@ mod inner { Program::new_unchecked(BRIDGE_ID, Cow::Borrowed(BRIDGE_ELF)) } + #[must_use] + #[inline] + pub const fn cross_zone_outbox() -> Program { + Program::new_unchecked(CROSS_ZONE_OUTBOX_ID, Cow::Borrowed(CROSS_ZONE_OUTBOX_ELF)) + } + + #[must_use] + #[inline] + pub const fn cross_zone_inbox() -> Program { + Program::new_unchecked(CROSS_ZONE_INBOX_ID, Cow::Borrowed(CROSS_ZONE_INBOX_ELF)) + } + + #[must_use] + #[inline] + pub const fn ping_sender() -> Program { + Program::new_unchecked(PING_SENDER_ID, Cow::Borrowed(PING_SENDER_ELF)) + } + + #[must_use] + #[inline] + pub const fn ping_receiver() -> Program { + Program::new_unchecked(PING_RECEIVER_ID, Cow::Borrowed(PING_RECEIVER_ELF)) + } + + #[must_use] + #[inline] + pub const fn bridge_lock() -> Program { + Program::new_unchecked(BRIDGE_LOCK_ID, Cow::Borrowed(BRIDGE_LOCK_ELF)) + } + + #[must_use] + #[inline] + pub const fn wrapped_token() -> Program { + Program::new_unchecked(WRAPPED_TOKEN_ID, Cow::Borrowed(WRAPPED_TOKEN_ELF)) + } + #[cfg(test)] mod tests { use super::*; @@ -127,6 +166,12 @@ mod inner { (PINATA_TOKEN_ELF, PINATA_TOKEN_ID), (TOKEN_ELF, TOKEN_ID), (VAULT_ELF, VAULT_ID), + (CROSS_ZONE_OUTBOX_ELF, CROSS_ZONE_OUTBOX_ID), + (CROSS_ZONE_INBOX_ELF, CROSS_ZONE_INBOX_ID), + (PING_SENDER_ELF, PING_SENDER_ID), + (PING_RECEIVER_ELF, PING_RECEIVER_ID), + (BRIDGE_LOCK_ELF, BRIDGE_LOCK_ID), + (WRAPPED_TOKEN_ELF, WRAPPED_TOKEN_ID), ]; for (elf, expected_id) in cases { let program = Program::new((*elf).into()).unwrap(); diff --git a/lez/programs/wrapped_token/Cargo.toml b/lez/programs/wrapped_token/Cargo.toml new file mode 100644 index 00000000..a80f60ff --- /dev/null +++ b/lez/programs/wrapped_token/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "wrapped_token_program" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +wrapped_token_core.workspace = true diff --git a/lez/programs/wrapped_token/core/Cargo.toml b/lez/programs/wrapped_token/core/Cargo.toml new file mode 100644 index 00000000..ef0aabbc --- /dev/null +++ b/lez/programs/wrapped_token/core/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "wrapped_token_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +serde = { workspace = true, features = ["alloc"] } +risc0-zkvm.workspace = true diff --git a/lez/programs/wrapped_token/core/src/lib.rs b/lez/programs/wrapped_token/core/src/lib.rs new file mode 100644 index 00000000..8e13567a --- /dev/null +++ b/lez/programs/wrapped_token/core/src/lib.rs @@ -0,0 +1,121 @@ +//! Core types for the wrapped-token program, the destination side of the +//! cross-zone bridge. Only the cross-zone inbox may mint; the guest enforces +//! this by reading the authorized minter from a genesis-seeded config account. + +use lee_core::{ + account::AccountId, + program::{PdaSeed, ProgramId}, +}; +use serde::{Deserialize, Serialize}; + +const CONFIG_SEED_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/WrappedTokenConfig/00/"; +const HOLDING_SEED_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/WrappedTokenHold/00000"; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum Instruction { + /// Credit `amount` wrapped tokens to `recipient`'s holding. Delivered only by + /// the cross-zone inbox. + /// + /// Required accounts (2): the wrapped-token config PDA, then the recipient's + /// holding PDA. + Mint { recipient: [u8; 32], amount: u128 }, +} + +/// PDA holding the authorized minter program id (the cross-zone inbox), seeded at +/// genesis so the guest can pin its caller without importing the inbox image id. +#[must_use] +pub fn config_account_id(wrapped_token_id: ProgramId) -> AccountId { + AccountId::for_public_pda(&wrapped_token_id, &config_seed()) +} + +#[must_use] +pub const fn config_seed() -> PdaSeed { + PdaSeed::new(CONFIG_SEED_DOMAIN) +} + +/// PDA holding one recipient's wrapped-token balance. +#[must_use] +pub fn holding_account_id(wrapped_token_id: ProgramId, recipient: &[u8; 32]) -> AccountId { + AccountId::for_public_pda(&wrapped_token_id, &holding_seed(recipient)) +} + +#[must_use] +pub fn holding_seed(recipient: &[u8; 32]) -> PdaSeed { + use risc0_zkvm::sha::{Impl, Sha256 as _}; + + let mut bytes = [0_u8; 64]; + bytes[..32].copy_from_slice(&HOLDING_SEED_DOMAIN); + bytes[32..].copy_from_slice(recipient); + let seed: [u8; 32] = Impl::hash_bytes(&bytes) + .as_bytes() + .try_into() + .unwrap_or_else(|_| unreachable!()); + PdaSeed::new(seed) +} + +/// Encodes the authorized minter program id for the config account's data. +#[must_use] +pub fn minter_bytes(minter: ProgramId) -> [u8; 32] { + let mut bytes = [0_u8; 32]; + for (word, chunk) in minter.iter().zip(bytes.chunks_exact_mut(4)) { + chunk.copy_from_slice(&word.to_le_bytes()); + } + bytes +} + +/// Decodes the authorized minter program id from the config account's data. +#[must_use] +pub fn read_minter(data: &[u8]) -> Option { + if data.len() < 32 { + return None; + } + let mut minter = [0_u32; 8]; + for (word, chunk) in minter.iter_mut().zip(data[..32].chunks_exact(4)) { + *word = u32::from_le_bytes(chunk.try_into().unwrap_or_else(|_| unreachable!())); + } + Some(minter) +} + +/// Reads a wrapped-token balance from account data; empty data is a zero balance. +#[must_use] +pub fn read_balance(data: &[u8]) -> u128 { + if data.len() < 16 { + return 0; + } + u128::from_le_bytes(data[..16].try_into().unwrap_or_else(|_| unreachable!())) +} + +#[must_use] +pub const fn balance_bytes(amount: u128) -> [u8; 16] { + amount.to_le_bytes() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn minter_round_trips() { + let minter: ProgramId = [1, 2, 3, 4, 5, 6, 7, 8]; + assert_eq!(read_minter(&minter_bytes(minter)), Some(minter)); + } + + #[test] + fn balance_round_trips() { + assert_eq!(read_balance(&balance_bytes(42)), 42); + assert_eq!(read_balance(&[]), 0); + } + + #[test] + fn holding_is_unique_per_recipient() { + let id: ProgramId = [9; 8]; + assert_ne!( + holding_account_id(id, &[1; 32]), + holding_account_id(id, &[2; 32]) + ); + assert_eq!( + holding_account_id(id, &[1; 32]), + holding_account_id(id, &[1; 32]) + ); + } +} diff --git a/lez/programs/wrapped_token/src/main.rs b/lez/programs/wrapped_token/src/main.rs new file mode 100644 index 00000000..d0e7595f --- /dev/null +++ b/lez/programs/wrapped_token/src/main.rs @@ -0,0 +1,70 @@ +use lee_core::{ + account::AccountWithMetadata, + program::{AccountPostState, Claim, ProgramInput, ProgramOutput, read_lee_inputs}, +}; +use wrapped_token_core::{ + Instruction, balance_bytes, config_account_id, holding_account_id, holding_seed, read_balance, + read_minter, +}; + +fn main() { + let ( + ProgramInput { + self_program_id, + caller_program_id, + pre_states, + instruction, + }, + instruction_words, + ) = read_lee_inputs::(); + + let Instruction::Mint { recipient, amount } = instruction; + + // pre_states: [config PDA, recipient holding PDA]. + let [config, holding] = <[AccountWithMetadata; 2]>::try_from(pre_states) + .expect("Mint requires the config and recipient holding accounts"); + + // The config PDA is genesis-seeded with the authorized minter (the cross-zone + // inbox). Pin the caller to it, since the guest cannot import the inbox id. + assert_eq!( + config.account_id, + config_account_id(self_program_id), + "first account must be the wrapped-token config PDA" + ); + let minter = read_minter(&config.account.data.clone().into_inner()) + .expect("config account holds an authorized minter id"); + assert_eq!( + caller_program_id, + Some(minter), + "Mint is only callable by the authorized minter (the cross-zone inbox)" + ); + + assert_eq!( + holding.account_id, + holding_account_id(self_program_id, &recipient), + "second account must be the recipient holding PDA" + ); + + let new_balance = read_balance(&holding.account.data.clone().into_inner()) + .checked_add(amount) + .expect("wrapped-token balance overflow"); + let mut holding_account = holding.account.clone(); + holding_account.data = balance_bytes(new_balance) + .to_vec() + .try_into() + .expect("balance fits in account data"); + let holding_post = AccountPostState::new_claimed_if_default( + holding_account, + Claim::Pda(holding_seed(&recipient)), + ); + let config_post = AccountPostState::new(config.account.clone()); + + ProgramOutput::new( + self_program_id, + caller_program_id, + instruction_words, + vec![config, holding], + vec![config_post, holding_post], + ) + .write(); +} diff --git a/lez/sequencer/core/Cargo.toml b/lez/sequencer/core/Cargo.toml index 2931c833..cd89f223 100644 --- a/lez/sequencer/core/Cargo.toml +++ b/lez/sequencer/core/Cargo.toml @@ -20,6 +20,8 @@ bridge_core.workspace = true vault_core.workspace = true programs.workspace = true system_accounts.workspace = true +cross_zone.workspace = true +cross_zone_inbox_core.workspace = true logos-blockchain-key-management-system-service.workspace = true logos-blockchain-core.workspace = true @@ -39,6 +41,7 @@ url.workspace = true num-bigint.workspace = true risc0-zkvm.workspace = true futures.workspace = true +itertools.workspace = true [features] default = [] diff --git a/lez/sequencer/core/src/block_store.rs b/lez/sequencer/core/src/block_store.rs index 0db05d74..77b57661 100644 --- a/lez/sequencer/core/src/block_store.rs +++ b/lez/sequencer/core/src/block_store.rs @@ -7,18 +7,19 @@ use common::{ transaction::LeeTransaction, }; use lee::V03State; +use lee_core::BlockId; use log::info; use logos_blockchain_zone_sdk::sequencer::SequencerCheckpoint; -pub use storage::DbResult; use storage::sequencer::{ RocksDBIO, sequencer_cells::{PendingDepositEventRecord, WithdrawalReconciliationKey}, }; +pub use storage::{DbResult, sequencer::DbDump}; pub struct SequencerStore { dbio: Arc, // TODO: Consider adding the hashmap to the database for faster recovery. - tx_hash_to_block_map: HashMap, + tx_hash_to_block_map: HashMap, genesis_id: u64, signing_key: lee::PrivateKey, } @@ -96,7 +97,7 @@ impl SequencerStore { /// Returns the transaction corresponding to the given hash, if it exists in the blockchain. #[must_use] - pub fn get_transaction_by_hash(&self, hash: HashType) -> Option { + pub fn get_transaction_by_hash(&self, hash: HashType) -> Option<(LeeTransaction, BlockId)> { let block_id = *self.tx_hash_to_block_map.get(&hash)?; let block = self .get_block_at_id(block_id) @@ -105,7 +106,7 @@ impl SequencerStore { .expect("Block should be present since the hash is in the map"); for transaction in block.body.transactions { if transaction.hash() == hash { - return Some(transaction); + return Some((transaction, block_id)); } } panic!( @@ -149,6 +150,28 @@ impl SequencerStore { self.dbio.get_lee_state() } + /// Remove the persisted zone-sdk checkpoint so the next startup is treated as a fresh start. + pub fn delete_zone_checkpoint(&self) -> DbResult<()> { + self.dbio.delete_zone_sdk_checkpoint_bytes() + } + + /// Reset every stored block to `Pending` so the next fresh start republishes the whole chain. + pub fn reset_all_blocks_to_pending(&self) -> DbResult<()> { + self.dbio.reset_all_blocks_to_pending() + } + + /// Single-blob [`DbDump`] of the whole store; restore with [`Self::restore_db_from_dump`]. + pub fn dump(&self) -> DbResult { + self.dbio.dump_all() + } + + /// Create a fresh rocksdb at `location` from `dump`, closing it before returning so a sequencer + /// can open it normally afterwards. + pub fn restore_db_from_dump(location: &Path, dump: &DbDump) -> DbResult<()> { + RocksDBIO::restore_from_dump(location, dump)?; + Ok(()) + } + pub fn get_zone_checkpoint(&self) -> Result> { let Some(bytes) = self.dbio.get_zone_sdk_checkpoint_bytes()? else { return Ok(None); @@ -181,8 +204,6 @@ pub(crate) fn block_to_transactions_map(block: &Block) -> HashMap #[cfg(test)] mod tests { - #![expect(clippy::shadow_unrelated, reason = "We don't care about it in tests")] - use common::{block::HashableBlockData, test_utils::sequencer_sign_key_for_testing}; use tempfile::tempdir; @@ -224,8 +245,8 @@ mod tests { .update(&block, &[], vec![], &dummy_state) .unwrap(); // Try again - let retrieved_tx = node_store.get_transaction_by_hash(tx.hash()); - assert_eq!(Some(tx), retrieved_tx); + let output = node_store.get_transaction_by_hash(tx.hash()); + assert_eq!(Some((tx, 1)), output); } #[test] @@ -383,7 +404,7 @@ mod tests { // Re-open the store and verify that the transaction is still retrievable (which means it // was cached correctly) let node_store = SequencerStore::open_db(path, signing_key).unwrap(); - let retrieved_tx = node_store.get_transaction_by_hash(tx.hash()); - assert_eq!(Some(tx), retrieved_tx); + let output = node_store.get_transaction_by_hash(tx.hash()); + assert_eq!(Some((tx, 1)), output); } } diff --git a/lez/sequencer/core/src/config.rs b/lez/sequencer/core/src/config.rs index b445bcd5..a11a9988 100644 --- a/lez/sequencer/core/src/config.rs +++ b/lez/sequencer/core/src/config.rs @@ -8,6 +8,7 @@ use std::{ use anyhow::Result; use bytesize::ByteSize; use common::config::BasicAuth; +pub use cross_zone_inbox_core::{CrossZoneConfig, CrossZonePeer}; use humantime_serde; use lee::AccountId; use logos_blockchain_core::mantle::ops::channel::ChannelId; @@ -25,6 +26,11 @@ pub enum GenesisAction { SupplyBridgeAccount { balance: u128, }, + /// Seeds a bridge-lock holder's initial bridgeable balance into genesis state. + SupplyBridgeLockHolding { + holder: AccountId, + amount: u128, + }, } // TODO: Provide default values @@ -53,6 +59,9 @@ pub struct SequencerConfig { /// Genesis configuration. #[serde(default)] pub genesis: Vec, + /// Cross-zone messaging configuration. `None` disables the watcher. + #[serde(default)] + pub cross_zone: Option, } #[derive(Clone, Serialize, Deserialize)] diff --git a/lez/sequencer/core/src/cross_zone_watcher.rs b/lez/sequencer/core/src/cross_zone_watcher.rs new file mode 100644 index 00000000..ffa238c4 --- /dev/null +++ b/lez/sequencer/core/src/cross_zone_watcher.rs @@ -0,0 +1,185 @@ +use std::time::Duration; + +use common::{block::Block, transaction::LeeTransaction}; +use cross_zone::{build_dispatch_from_emission, extract_emission}; +use futures::StreamExt as _; +use lee::PublicKey; +use lee_core::program::ProgramId; +use log::{debug, error, info, warn}; +use logos_blockchain_core::mantle::ops::channel::ChannelId; +use logos_blockchain_zone_sdk::{ + CommonHttpClient, ZoneMessage, adapter::NodeHttpClient, indexer::ZoneIndexer, +}; +use mempool::MemPoolHandle; + +use crate::{ + TransactionOrigin, + config::{BedrockConfig, CrossZoneConfig}, +}; + +/// Spawns one watcher task per configured peer. +/// +/// Each task reads the peer's finalized blocks from Bedrock, recognizes outbound +/// messages addressed to this zone, and injects the matching inbox dispatch as a +/// sequencer-origin transaction into the local mempool. +pub fn spawn_watchers( + bedrock_config: &BedrockConfig, + cross_zone: &CrossZoneConfig, + poll_interval: Duration, + mempool_handle: &MemPoolHandle<(TransactionOrigin, LeeTransaction)>, +) { + let self_zone: [u8; 32] = *bedrock_config.channel_id.as_ref(); + + for peer in cross_zone.peers.clone() { + let node = NodeHttpClient::new( + CommonHttpClient::new(bedrock_config.auth.clone().map(Into::into)), + bedrock_config.node_url.clone(), + ); + let expected_pubkey = peer.expected_block_signing_pubkey.map(|bytes| { + PublicKey::try_new(bytes).expect("configured peer block-signing pubkey is a valid key") + }); + tokio::spawn(watch_peer( + ZoneIndexer::new(ChannelId::from(peer.channel_id), node), + peer.channel_id, + peer.allowed_targets, + expected_pubkey, + self_zone, + poll_interval, + mempool_handle.clone(), + )); + } +} + +#[expect( + clippy::infinite_loop, + reason = "the peer watcher runs for the lifetime of the sequencer process" +)] +async fn watch_peer( + zone_indexer: ZoneIndexer, + peer_zone: [u8; 32], + allowed_targets: Vec, + expected_pubkey: Option, + self_zone: [u8; 32], + poll_interval: Duration, + mempool_handle: MemPoolHandle<(TransactionOrigin, LeeTransaction)>, +) { + info!( + "Cross-zone watcher started for peer {}", + hex::encode(peer_zone) + ); + + let mut cursor = None; + loop { + let stream = match zone_indexer.next_messages(cursor).await { + Ok(stream) => stream, + Err(err) => { + error!( + "Watcher next_messages failed for peer {}: {err}", + hex::encode(peer_zone) + ); + tokio::time::sleep(poll_interval).await; + continue; + } + }; + let mut stream = std::pin::pin!(stream); + + while let Some((msg, slot)) = stream.next().await { + let zone_block = match msg { + ZoneMessage::Block(block) => block, + ZoneMessage::Deposit(_) | ZoneMessage::Withdraw(_) => continue, + }; + match borsh::from_slice::(&zone_block.data) { + Ok(block) => { + debug!( + "Watcher observed finalized peer {} block {}", + hex::encode(peer_zone), + block.header.block_id + ); + // Reject blocks not signed by the pinned peer key (equivocation): + // the channel signer is authenticated by the zone-sdk, but that + // does not prove the peer's honest sequencer produced the block. + if expected_pubkey + .as_ref() + .is_some_and(|pk| !block.is_signed_by(pk)) + { + warn!( + "Watcher dropping peer {} block {}: block-signing key does not match the pinned key", + hex::encode(peer_zone), + block.header.block_id + ); + } else { + deliver_block( + &block, + peer_zone, + self_zone, + &allowed_targets, + &mempool_handle, + ) + .await; + } + } + Err(err) => error!("Watcher failed to deserialize peer block: {err}"), + } + cursor = Some(slot); + } + + // Stream ended (caught up to the peer's last finalized block); poll again. + tokio::time::sleep(poll_interval).await; + } +} + +/// Scans one peer block for outbound messages and injects a dispatch per match. +async fn deliver_block( + block: &Block, + peer_zone: [u8; 32], + self_zone: [u8; 32], + allowed_targets: &[ProgramId], + mempool_handle: &MemPoolHandle<(TransactionOrigin, LeeTransaction)>, +) { + for (index, tx) in block.body.transactions.iter().enumerate() { + let LeeTransaction::Public(public_tx) = tx else { + continue; + }; + let message = public_tx.message(); + let Some(emission) = extract_emission(message.program_id, &message.instruction_data) else { + continue; + }; + + if emission.target_zone != self_zone { + continue; + } + if !allowed_targets.contains(&emission.target_program_id) { + warn!( + "Watcher dropping message to disallowed target from peer {}", + hex::encode(peer_zone) + ); + continue; + } + + let dispatch = build_dispatch_from_emission( + peer_zone, + block.header.block_id, + u32::try_from(index).unwrap_or(u32::MAX), + message.program_id, + emission.target_program_id, + &emission.target_accounts, + emission.payload, + ); + + match mempool_handle + .push(( + TransactionOrigin::Sequencer, + LeeTransaction::Public(dispatch), + )) + .await + { + Ok(()) => info!( + "Watcher injected cross-zone dispatch from peer {} block {} tx {}", + hex::encode(peer_zone), + block.header.block_id, + index + ), + Err(err) => error!("Watcher failed to enqueue inbox dispatch: {err}"), + } + } +} diff --git a/lez/sequencer/core/src/lib.rs b/lez/sequencer/core/src/lib.rs index 75dcc1a9..3a96e6d5 100644 --- a/lez/sequencer/core/src/lib.rs +++ b/lez/sequencer/core/src/lib.rs @@ -8,6 +8,7 @@ use common::{ transaction::{LeeTransaction, clock_invocation}, }; use config::{GenesisAction, SequencerConfig}; +use itertools::Itertools as _; use lee::{AccountId, PublicTransaction, public_transaction::Message}; use lee_core::GENESIS_BLOCK_ID; use log::{error, info, warn}; @@ -31,6 +32,7 @@ use crate::{ pub mod block_publisher; pub mod block_store; pub mod config; +pub mod cross_zone_watcher; #[cfg(feature = "mock")] pub mod mock; @@ -124,7 +126,7 @@ impl SequencerCore { load_or_create_signing_key(&config.home.join("bedrock_signing_key")) .expect("Failed to load or create bedrock signing key"); - let (store, state, genesis_block) = Self::open_or_create_store(&config); + let (store, state, _genesis_block) = Self::open_or_create_store(&config); let latest_block_meta = store .latest_block_meta() @@ -151,15 +153,44 @@ impl SequencerCore { .await .expect("Failed to initialize Block Publisher"); - // On a truly fresh start (no checkpoint persisted yet), publish the - // genesis block so the indexer can find the channel start. After the - // first publish, zone-sdk's checkpoint persistence covers further - // restarts. + // Fresh start (no checkpoint): republish all pending blocks if is_fresh_start { - block_publisher - .publish_block(&genesis_block, vec![]) - .await - .expect("Failed to publish genesis block"); + let mut pending_blocks = store + .get_all_blocks() + .filter_ok(|block| matches!(block.bedrock_status, BedrockStatus::Pending)) + .collect::, _>>() + .expect("Failed to read blocks from store while republishing on fresh start"); + pending_blocks.sort_unstable_by_key(|block| block.header.block_id); + + assert!( + pending_blocks + .first() + .is_none_or(|block| block.header.block_id == GENESIS_BLOCK_ID), + "First pending block on fresh start should be the genesis block" + ); + + for block in &pending_blocks { + block_publisher + .publish_block(block, vec![]) + .await + .unwrap_or_else(|err| { + panic!( + "Failed to publish block {} on fresh start: {err:#}", + block.header.block_id + ) + }); + } + } + + // Cross-zone messaging: start a watcher per configured peer. The inbox + // config account is seeded into genesis state in `build_genesis_state`. + if let Some(cross_zone) = &config.cross_zone { + cross_zone_watcher::spawn_watchers( + &config.bedrock_config, + cross_zone, + config.block_create_timeout, + &mempool_handle, + ); } let sequencer_core = Self { @@ -609,14 +640,18 @@ fn build_genesis_state(config: &SequencerConfig) -> (lee::V03State, Vec build_supply_account_genesis_transaction(account_id, *balance), + } => Some(build_supply_account_genesis_transaction( + account_id, *balance, + )), GenesisAction::SupplyBridgeAccount { balance } => { - build_supply_bridge_account_genesis_transaction(*balance) + Some(build_supply_bridge_account_genesis_transaction(*balance)) } + // Force-inserted below: bridge_lock has no mint transaction. + GenesisAction::SupplyBridgeLockHolding { .. } => None, }) .chain(std::iter::once(clock_invocation(0))) .inspect(|tx| { @@ -627,9 +662,36 @@ fn build_genesis_state(config: &SequencerConfig) -> (lee::V03State, Vec bool { + cross_zone::is_sequencer_only_program(program_id) +} + fn build_supply_account_genesis_transaction( account_id: &AccountId, balance: u128, diff --git a/lez/sequencer/core/src/tests.rs b/lez/sequencer/core/src/tests.rs index 4b5ebfbc..05e2ec4c 100644 --- a/lez/sequencer/core/src/tests.rs +++ b/lez/sequencer/core/src/tests.rs @@ -33,6 +33,7 @@ use crate::{ block_store::SequencerStore, build_genesis_state, config::{BedrockConfig, SequencerConfig}, + is_sequencer_only_program, mock::SequencerCoreWithMockClients, }; @@ -59,9 +60,21 @@ fn setup_sequencer_config() -> SequencerConfig { }, retry_pending_blocks_timeout: Duration::from_mins(4), genesis: vec![], + cross_zone: None, } } +#[test] +fn only_the_cross_zone_inbox_is_sequencer_only() { + assert!(is_sequencer_only_program(programs::cross_zone_inbox().id())); + assert!(!is_sequencer_only_program( + programs::cross_zone_outbox().id() + )); + assert!(!is_sequencer_only_program(programs::wrapped_token().id())); + assert!(!is_sequencer_only_program(programs::ping_sender().id())); + assert!(!is_sequencer_only_program(programs::clock().id())); +} + fn create_signing_key_for_account1() -> lee::PrivateKey { initial_pub_accounts_private_keys()[0].pub_sign_key.clone() } diff --git a/lez/sequencer/service/rpc/src/lib.rs b/lez/sequencer/service/rpc/src/lib.rs index 914232c0..f7aa8b56 100644 --- a/lez/sequencer/service/rpc/src/lib.rs +++ b/lez/sequencer/service/rpc/src/lib.rs @@ -68,7 +68,7 @@ pub trait Rpc { async fn get_transaction( &self, tx_hash: HashType, - ) -> Result, ErrorObjectOwned>; + ) -> Result, ErrorObjectOwned>; #[method(name = "getAccountsNonces")] async fn get_accounts_nonces( diff --git a/lez/sequencer/service/src/main.rs b/lez/sequencer/service/src/main.rs index e78ad502..8b577bb8 100644 --- a/lez/sequencer/service/src/main.rs +++ b/lez/sequencer/service/src/main.rs @@ -24,6 +24,8 @@ async fn main() -> Result<()> { let Args { config_path, port } = Args::parse(); + // TODO: handle this cancellation token more gracefully within Sequencer service + // similar to how we do in Indexer let cancellation_token = listen_for_shutdown_signal(); let config = sequencer_service::SequencerConfig::from_path(&config_path)?; diff --git a/lez/sequencer/service/src/service.rs b/lez/sequencer/service/src/service.rs index 3a48e7cc..60be5fdd 100644 --- a/lez/sequencer/service/src/service.rs +++ b/lez/sequencer/service/src/service.rs @@ -74,6 +74,20 @@ impl sequencer_service_rpc::RpcServer ) })?; + // Sequencer-only programs (the cross-zone inbox) are injected by the + // watcher; a user must not invoke them top-level, or anyone could forge + // an inbound cross-zone delivery. Chained user calls are already rejected + // by the inbox guest's caller-is-none assertion. + if let LeeTransaction::Public(public_tx) = &authenticated_tx + && sequencer_core::is_sequencer_only_program(public_tx.message().program_id) + { + return Err(ErrorObjectOwned::owned( + ErrorCode::InvalidParams.code(), + "Program is sequencer-only and cannot be invoked by a user transaction".to_owned(), + None::<()>, + )); + } + self.mempool_handle .push((TransactionOrigin::User, authenticated_tx)) .await @@ -131,7 +145,7 @@ impl sequencer_service_rpc::RpcServer async fn get_transaction( &self, tx_hash: HashType, - ) -> Result, ErrorObjectOwned> { + ) -> Result, ErrorObjectOwned> { let sequencer = self.sequencer.lock().await; Ok(sequencer.block_store().get_transaction_by_hash(tx_hash)) } diff --git a/lez/storage/Cargo.toml b/lez/storage/Cargo.toml index 8767b525..18c58e11 100644 --- a/lez/storage/Cargo.toml +++ b/lez/storage/Cargo.toml @@ -13,8 +13,10 @@ lee.workspace = true thiserror.workspace = true borsh.workspace = true +log.workspace = true rocksdb.workspace = true tempfile.workspace = true +zstd.workspace = true [dev-dependencies] programs.workspace = true diff --git a/lez/storage/src/error.rs b/lez/storage/src/error.rs index 3056e09b..069a95d8 100644 --- a/lez/storage/src/error.rs +++ b/lez/storage/src/error.rs @@ -12,6 +12,12 @@ pub enum DbError { error: borsh::io::Error, additional_info: Option, }, + #[error("Compression error: {}", additional_info.as_deref().unwrap_or("No additional info"))] + CompressionError { + #[source] + error: std::io::Error, + additional_info: Option, + }, #[error("Logic Error: {additional_info}")] DbInteractionError { additional_info: String }, } @@ -33,6 +39,14 @@ impl DbError { } } + #[must_use] + pub const fn compression_error(err: std::io::Error, message: Option) -> Self { + Self::CompressionError { + error: err, + additional_info: message, + } + } + #[must_use] pub const fn db_interaction_error(message: String) -> Self { Self::DbInteractionError { diff --git a/lez/storage/src/indexer/indexer_cells.rs b/lez/storage/src/indexer/indexer_cells.rs index b19a5510..ef104f1a 100644 --- a/lez/storage/src/indexer/indexer_cells.rs +++ b/lez/storage/src/indexer/indexer_cells.rs @@ -7,9 +7,9 @@ use crate::{ error::DbError, indexer::{ ACC_NUM_CELL_NAME, BLOCK_HASH_CELL_NAME, BREAKPOINT_CELL_NAME, CF_ACC_META, - CF_BREAKPOINT_NAME, CF_HASH_TO_ID, CF_TX_TO_ID, DB_META_LAST_BREAKPOINT_ID, - DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY, DB_META_ZONE_SDK_INDEXER_CURSOR_KEY, - TX_HASH_CELL_NAME, + CF_BREAKPOINT_NAME, CF_HASH_TO_ID, CF_TX_TO_ID, + DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY, DB_META_STALL_REASON_KEY, + DB_META_TIP_SLOT_KEY, DB_META_ZONE_SDK_INDEXER_CURSOR_KEY, TX_HASH_CELL_NAME, }, }; @@ -36,29 +36,6 @@ impl SimpleWritableCell for LastObservedL1LibHeaderCell { } } -#[derive(Debug, BorshSerialize, BorshDeserialize)] -pub struct LastBreakpointIdCell(pub u64); - -impl SimpleStorableCell for LastBreakpointIdCell { - type KeyParams = (); - - const CELL_NAME: &'static str = DB_META_LAST_BREAKPOINT_ID; - const CF_NAME: &'static str = CF_META_NAME; -} - -impl SimpleReadableCell for LastBreakpointIdCell {} - -impl SimpleWritableCell for LastBreakpointIdCell { - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize last breakpoint id".to_owned()), - ) - }) - } -} - #[derive(BorshDeserialize)] pub struct BreakpointCellOwned(pub V03State); @@ -212,6 +189,27 @@ impl SimpleWritableCell for AccNumTxCell { } } +/// The L1 inscription slot of the tip block, written atomically with the tip. +#[derive(Debug, BorshSerialize, BorshDeserialize)] +pub struct TipSlotCell(pub u64); + +impl SimpleStorableCell for TipSlotCell { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_META_TIP_SLOT_KEY; + const CF_NAME: &'static str = CF_META_NAME; +} + +impl SimpleReadableCell for TipSlotCell {} + +impl SimpleWritableCell for TipSlotCell { + fn value_constructor(&self) -> DbResult> { + borsh::to_vec(&self).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize tip slot".to_owned())) + }) + } +} + /// Opaque bytes for the zone-sdk indexer cursor `Option<(MsgId, Slot)>`. /// The caller serializes via `serde_json` (neither type derives borsh). #[derive(BorshDeserialize)] @@ -247,6 +245,40 @@ impl SimpleWritableCell for ZoneSdkIndexerCursorCellRef<'_> { } } +/// Opaque JSON bytes for the indexer's persisted `Option`. +#[derive(BorshDeserialize)] +pub struct StallReasonCellOwned(pub Vec); + +impl SimpleStorableCell for StallReasonCellOwned { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_META_STALL_REASON_KEY; + const CF_NAME: &'static str = CF_META_NAME; +} + +impl SimpleReadableCell for StallReasonCellOwned {} + +#[derive(BorshSerialize)] +pub struct StallReasonCellRef<'bytes>(pub &'bytes [u8]); + +impl SimpleStorableCell for StallReasonCellRef<'_> { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_META_STALL_REASON_KEY; + const CF_NAME: &'static str = CF_META_NAME; +} + +impl SimpleWritableCell for StallReasonCellRef<'_> { + fn value_constructor(&self) -> DbResult> { + borsh::to_vec(&self).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize stall reason cell".to_owned()), + ) + }) + } +} + #[cfg(test)] mod uniform_tests { use crate::{ diff --git a/lez/storage/src/indexer/mod.rs b/lez/storage/src/indexer/mod.rs index 87d586fb..0955ef26 100644 --- a/lez/storage/src/indexer/mod.rs +++ b/lez/storage/src/indexer/mod.rs @@ -5,6 +5,7 @@ use common::{ transaction::{LeeTransaction, clock_invocation}, }; use lee::{GENESIS_BLOCK_ID, V03State}; +use log::warn; use rocksdb::{ BoundColumnFamily, ColumnFamilyDescriptor, DBWithThreadMode, MultiThreaded, Options, }; @@ -20,10 +21,12 @@ pub mod write_non_atomic; /// Key base for storing metainformation about id of last observed L1 lib header in db. pub const DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY: &str = "last_observed_l1_lib_header_in_db"; -/// Key base for storing metainformation about the last breakpoint. -pub const DB_META_LAST_BREAKPOINT_ID: &str = "last_breakpoint_id"; /// Key base for storing the zone-sdk indexer cursor (opaque bytes). pub const DB_META_ZONE_SDK_INDEXER_CURSOR_KEY: &str = "zone_sdk_indexer_cursor"; +/// Key base for storing the persisted `Option` diagnostic record (opaque JSON bytes). +pub const DB_META_STALL_REASON_KEY: &str = "stall_reason"; +/// Key base for storing the L1 inscription slot of the tip block. +pub const DB_META_TIP_SLOT_KEY: &str = "tip_slot"; /// Cell name for a breakpoint. pub const BREAKPOINT_CELL_NAME: &str = "breakpoint"; @@ -84,9 +87,10 @@ impl RocksDBIO { let dbio = Self { db }; - // First breakpoint setup - dbio.put_breakpoint(0, initial_state)?; - dbio.put_meta_last_breakpoint_id(0)?; + // Seed the genesis snapshot once; reopening must not clobber it. + if dbio.get_breakpoint_opt(0)?.is_none() { + dbio.put_breakpoint(0, initial_state)?; + } Ok(dbio) } @@ -152,8 +156,29 @@ impl RocksDBIO { )); } - let br_id = closest_breakpoint_id(block_id); - let mut state = self.get_breakpoint(br_id)?; + // walk down to the nearest snapshot that exists + let target = closest_breakpoint_id(block_id); + let mut br_id = target; + let mut state = loop { + match self.get_breakpoint_opt(br_id)? { + Some(state) => break state, + None if br_id == 0 => { + return Err(DbError::db_interaction_error( + "Breakpoint 0 is missing".to_owned(), + )); + } + None => { + br_id = br_id + .checked_sub(1) + .expect("breakpoint_id > 0 checked above"); + } + } + }; + if br_id < target { + warn!( + "Breakpoint {target} missing; replaying from breakpoint {br_id} for block {block_id}" + ); + } let start = u64::from(BREAKPOINT_INTERVAL) .checked_mul(br_id) @@ -211,20 +236,7 @@ fn apply_block_transactions(mut block: Block, state: &mut V03State) -> DbResult< })?; } else { transaction - .transaction_stateless_check() - .map_err(|err| { - DbError::db_interaction_error(format!( - "transaction pre check failed with err {err:?}" - )) - })? - // FIXME: HOT FIX (testnet v0.2): does not check for system account updates due to - // sequencer-generated deposit tx'es; - // CHANGE ME back to `execute_check_on_state` when the indexer can authenticate deposit transactions - .execute_without_system_accounts_check_on_state( - state, - block.header.block_id, - block.header.timestamp, - ) + .execute_on_state(state, block.header.block_id, block.header.timestamp) .map_err(|err| { DbError::db_interaction_error(format!( "transaction execution failed with err {err:?}" diff --git a/lez/storage/src/indexer/read_once.rs b/lez/storage/src/indexer/read_once.rs index 6e79adc4..3fbf6026 100644 --- a/lez/storage/src/indexer/read_once.rs +++ b/lez/storage/src/indexer/read_once.rs @@ -3,8 +3,8 @@ use crate::{ DBIO as _, cells::shared_cells::{BlockCell, FirstBlockCell, FirstBlockSetCell, LastBlockCell}, indexer::indexer_cells::{ - AccNumTxCell, BlockHashToBlockIdMapCell, BreakpointCellOwned, LastBreakpointIdCell, - LastObservedL1LibHeaderCell, TxHashToBlockIdMapCell, ZoneSdkIndexerCursorCellOwned, + AccNumTxCell, BlockHashToBlockIdMapCell, BreakpointCellOwned, LastObservedL1LibHeaderCell, + StallReasonCellOwned, TipSlotCell, TxHashToBlockIdMapCell, ZoneSdkIndexerCursorCellOwned, }, }; @@ -31,8 +31,8 @@ impl RocksDBIO { Ok(self.get_opt::(())?.is_some()) } - pub fn get_meta_last_breakpoint_id(&self) -> DbResult> { - self.get_opt::(()) + pub fn get_meta_tip_slot_in_db(&self) -> DbResult> { + self.get_opt::(()) .map(|opt| opt.map(|cell| cell.0)) } @@ -49,6 +49,11 @@ impl RocksDBIO { self.get::(br_id).map(|cell| cell.0) } + pub fn get_breakpoint_opt(&self, br_id: u64) -> DbResult> { + self.get_opt::(br_id) + .map(|opt| opt.map(|cell| cell.0)) + } + // Mappings pub fn get_block_id_by_hash(&self, hash: [u8; 32]) -> DbResult> { @@ -73,4 +78,8 @@ impl RocksDBIO { .get_opt::(())? .map(|cell| cell.0)) } + + pub fn get_stall_reason_bytes(&self) -> DbResult>> { + Ok(self.get_opt::(())?.map(|cell| cell.0)) + } } diff --git a/lez/storage/src/indexer/tests.rs b/lez/storage/src/indexer/tests.rs index 44df23d5..d87aaf1c 100644 --- a/lez/storage/src/indexer/tests.rs +++ b/lez/storage/src/indexer/tests.rs @@ -49,16 +49,16 @@ fn initial_state() -> lee::V03State { #[test] fn start_db() { + let initial_state = initial_state(); let temp_dir = tempdir().unwrap(); let temdir_path = temp_dir.path(); - let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state()).unwrap(); + let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap(); let first_id = dbio.get_meta_first_block_id_in_db().unwrap(); let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); let last_observed_l1_header = dbio.get_meta_last_observed_l1_lib_header_in_db().unwrap(); - let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); let last_block = dbio.get_block(1).unwrap(); let breakpoint = dbio.get_breakpoint(0).unwrap(); let final_state = dbio.final_state().unwrap(); @@ -67,7 +67,6 @@ fn start_db() { assert_eq!(first_id, None); assert_eq!(last_observed_l1_header, None); assert!(!is_first_set); - assert_eq!(last_br_id, Some(0)); // TODO: Will be None after we remove hardcoded testnet state assert!(last_block.is_none()); assert_eq!( breakpoint.get_account_by_id(acc1()), @@ -81,13 +80,15 @@ fn start_db() { #[test] fn one_block_insertion() { + let initial_state = initial_state(); let temp_dir = tempdir().unwrap(); let temdir_path = temp_dir.path(); - let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state()).unwrap(); + let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state).unwrap(); let genesis_block = genesis_block(); - dbio.put_block(&genesis_block, [0; 32]).unwrap(); + dbio.put_block(&genesis_block, [0; 32], 0, &initial_state) + .unwrap(); let prev_hash = genesis_block.header.hash; let from = acc1(); @@ -98,7 +99,7 @@ fn one_block_insertion() { common::test_utils::create_transaction_native_token_transfer(from, 0, to, 1, &sign_key); let block = produce_dummy_block(2, Some(prev_hash), vec![transfer_tx]); - dbio.put_block(&block, [1; 32]).unwrap(); + dbio.put_block(&block, [1; 32], 0, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); let first_id = dbio.get_meta_first_block_id_in_db().unwrap(); @@ -107,7 +108,6 @@ fn one_block_insertion() { .unwrap() .unwrap(); let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); - let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); let last_block = dbio.get_block(last_id).unwrap().unwrap(); let breakpoint = dbio.get_breakpoint(0).unwrap(); let final_state = dbio.final_state().unwrap(); @@ -116,7 +116,6 @@ fn one_block_insertion() { assert_eq!(first_id, Some(1)); assert_eq!(last_observed_l1_header, [1; 32]); assert!(is_first_set); - assert_eq!(last_br_id, Some(0)); assert_eq!(last_block.header.hash, block.header.hash); assert_eq!( breakpoint.get_account_by_id(acc1()).balance @@ -131,17 +130,44 @@ fn one_block_insertion() { } #[test] -fn new_breakpoint() { +fn put_block_records_tip_inscription_slot() { + let initial_state = initial_state(); let temp_dir = tempdir().unwrap(); - let temdir_path = temp_dir.path(); + let dbio = RocksDBIO::open_or_create(temp_dir.path(), &initial_state).unwrap(); - let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state()).unwrap(); + assert_eq!(dbio.get_meta_tip_slot_in_db().unwrap(), None); + + let genesis_block = genesis_block(); + dbio.put_block(&genesis_block, [0; 32], 1_000, &initial_state) + .unwrap(); + assert_eq!(dbio.get_meta_tip_slot_in_db().unwrap(), Some(1_000)); + + let block = produce_dummy_block(2, Some(genesis_block.header.hash), vec![]); + dbio.put_block(&block, [1; 32], 1_005, &initial_state) + .unwrap(); + assert_eq!(dbio.get_meta_tip_slot_in_db().unwrap(), Some(1_005)); + + // Re-inserting a block at/below the tip must not move the tip slot. + dbio.put_block(&genesis_block, [0; 32], 1_010, &initial_state) + .unwrap(); + assert_eq!(dbio.get_meta_tip_slot_in_db().unwrap(), Some(1_005)); +} + +#[test] +fn put_block_stores_breakpoint_in_same_batch() { + let initial_state = initial_state(); + let temp_dir = tempdir().unwrap(); + let dbio = RocksDBIO::open_or_create(temp_dir.path(), &initial_state).unwrap(); let from = acc1(); let to = acc2(); let sign_key = acc1_sign_key(); - for i in 1..=BREAKPOINT_INTERVAL + 1 { + // Chain blocks 1..=BREAKPOINT_INTERVAL. The snapshot is scheduled internally + // by put_block at the boundary block; every call passes the same recognizable + // marker state (the initial one), proving it's stored verbatim rather than + // recomputed. + for i in 1..=BREAKPOINT_INTERVAL { let prev_hash = dbio.get_meta_last_block_id_in_db().unwrap().map(|last_id| { let last_block = dbio.get_block(last_id).unwrap().unwrap(); last_block.header.hash @@ -155,51 +181,70 @@ fn new_breakpoint() { &sign_key, ); let block = produce_dummy_block(i.into(), prev_hash, vec![transfer_tx]); - dbio.put_block(&block, [i; 32]).unwrap(); + + dbio.put_block(&block, [i; 32], 0, &initial_state).unwrap(); } - let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); - let first_id = dbio.get_meta_first_block_id_in_db().unwrap(); - let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); - let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - let prev_breakpoint = dbio.get_breakpoint(0).unwrap(); - let breakpoint = dbio.get_breakpoint(1).unwrap(); - let final_state = dbio.final_state().unwrap(); + let bp1 = dbio.get_breakpoint(1).unwrap(); + assert_eq!(bp1.get_account_by_id(acc1()).balance, 10000); + assert_eq!(bp1.get_account_by_id(acc2()).balance, 20000); + // Only the boundary block schedules a write: breakpoint 0 must be the only other one. + assert_eq!( + dbio.get_breakpoint(0) + .unwrap() + .get_account_by_id(acc1()) + .balance, + 10000 + ); +} - assert_eq!(last_id, 101); - assert_eq!(first_id, Some(1)); - assert!(is_first_set); - assert_eq!(last_br_id, Some(1)); - assert_ne!(last_block.header.hash, genesis_block().header.hash); +#[test] +fn state_replay_falls_back_over_missing_breakpoints() { + let initial_state = initial_state(); + let temp_dir = tempdir().unwrap(); + let dbio = RocksDBIO::open_or_create(temp_dir.path(), &initial_state).unwrap(); + + let from = acc1(); + let to = acc2(); + let sign_key = acc1_sign_key(); + + for i in 1..=u64::from(BREAKPOINT_INTERVAL) + 1 { + let prev_hash = dbio.get_meta_last_block_id_in_db().unwrap().map(|last_id| { + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + last_block.header.hash + }); + let transfer_tx = common::test_utils::create_transaction_native_token_transfer( + from, + (i - 1).into(), + to, + 1, + &sign_key, + ); + let block = produce_dummy_block(i, prev_hash, vec![transfer_tx]); + dbio.put_block(&block, [0; 32], 0, &initial_state).unwrap(); + } + + // Simulate a store whose boundary snapshot was lost (#605). + dbio.delete_breakpoint(1).unwrap(); + assert!(dbio.get_breakpoint_opt(1).unwrap().is_none()); + let final_state = dbio.final_state().unwrap(); assert_eq!( - prev_breakpoint.get_account_by_id(acc1()).balance - - final_state.get_account_by_id(acc1()).balance, - 101 + 10000 - final_state.get_account_by_id(acc1()).balance, + u128::from(BREAKPOINT_INTERVAL) + 1 ); assert_eq!( - final_state.get_account_by_id(acc2()).balance - - prev_breakpoint.get_account_by_id(acc2()).balance, - 101 - ); - assert_eq!( - breakpoint.get_account_by_id(acc1()).balance - - final_state.get_account_by_id(acc1()).balance, - 1 - ); - assert_eq!( - final_state.get_account_by_id(acc2()).balance - - breakpoint.get_account_by_id(acc2()).balance, - 1 + final_state.get_account_by_id(acc2()).balance - 20000, + u128::from(BREAKPOINT_INTERVAL) + 1 ); } #[test] fn simple_maps() { + let initial_state = initial_state(); let temp_dir = tempdir().unwrap(); let temdir_path = temp_dir.path(); - let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state()).unwrap(); + let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state).unwrap(); let from = acc1(); let to = acc2(); @@ -211,7 +256,7 @@ fn simple_maps() { let control_hash1 = block.header.hash; - dbio.put_block(&block, [1; 32]).unwrap(); + dbio.put_block(&block, [1; 32], 0, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); let last_block = dbio.get_block(last_id).unwrap().unwrap(); @@ -223,7 +268,7 @@ fn simple_maps() { let control_hash2 = block.header.hash; - dbio.put_block(&block, [2; 32]).unwrap(); + dbio.put_block(&block, [2; 32], 0, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); let last_block = dbio.get_block(last_id).unwrap().unwrap(); @@ -235,7 +280,7 @@ fn simple_maps() { let control_tx_hash1 = transfer_tx.hash(); let block = produce_dummy_block(3, Some(prev_hash), vec![transfer_tx]); - dbio.put_block(&block, [3; 32]).unwrap(); + dbio.put_block(&block, [3; 32], 0, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); let last_block = dbio.get_block(last_id).unwrap().unwrap(); @@ -247,7 +292,7 @@ fn simple_maps() { let control_tx_hash2 = transfer_tx.hash(); let block = produce_dummy_block(4, Some(prev_hash), vec![transfer_tx]); - dbio.put_block(&block, [4; 32]).unwrap(); + dbio.put_block(&block, [4; 32], 0, &initial_state).unwrap(); let control_block_id1 = dbio.get_block_id_by_hash(control_hash1.0).unwrap().unwrap(); let control_block_id2 = dbio.get_block_id_by_hash(control_hash2.0).unwrap().unwrap(); @@ -268,12 +313,13 @@ fn simple_maps() { #[test] fn block_batch() { + let initial_state = initial_state(); let temp_dir = tempdir().unwrap(); let temdir_path = temp_dir.path(); let mut block_res = vec![]; - let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state()).unwrap(); + let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state).unwrap(); let from = acc1(); let to = acc2(); @@ -284,7 +330,7 @@ fn block_batch() { let block = produce_dummy_block(1, None, vec![transfer_tx]); block_res.push(block.clone()); - dbio.put_block(&block, [1; 32]).unwrap(); + dbio.put_block(&block, [1; 32], 0, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); let last_block = dbio.get_block(last_id).unwrap().unwrap(); @@ -295,7 +341,7 @@ fn block_batch() { let block = produce_dummy_block(2, Some(prev_hash), vec![transfer_tx]); block_res.push(block.clone()); - dbio.put_block(&block, [2; 32]).unwrap(); + dbio.put_block(&block, [2; 32], 0, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); let last_block = dbio.get_block(last_id).unwrap().unwrap(); @@ -306,7 +352,7 @@ fn block_batch() { let block = produce_dummy_block(3, Some(prev_hash), vec![transfer_tx]); block_res.push(block.clone()); - dbio.put_block(&block, [3; 32]).unwrap(); + dbio.put_block(&block, [3; 32], 0, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); let last_block = dbio.get_block(last_id).unwrap().unwrap(); @@ -317,7 +363,7 @@ fn block_batch() { let block = produce_dummy_block(4, Some(prev_hash), vec![transfer_tx]); block_res.push(block.clone()); - dbio.put_block(&block, [4; 32]).unwrap(); + dbio.put_block(&block, [4; 32], 0, &initial_state).unwrap(); let block_hashes_mem: Vec<[u8; 32]> = block_res.into_iter().map(|bl| bl.header.hash.0).collect(); @@ -356,10 +402,11 @@ fn block_batch() { #[test] fn account_map() { + let initial_state = initial_state(); let temp_dir = tempdir().unwrap(); let temdir_path = temp_dir.path(); - let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state()).unwrap(); + let dbio = RocksDBIO::open_or_create(temdir_path, &initial_state).unwrap(); let from = acc1(); let to = acc2(); @@ -376,7 +423,7 @@ fn account_map() { let block = produce_dummy_block(1, None, vec![transfer_tx1, transfer_tx2]); - dbio.put_block(&block, [1; 32]).unwrap(); + dbio.put_block(&block, [1; 32], 0, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); let last_block = dbio.get_block(last_id).unwrap().unwrap(); @@ -391,7 +438,7 @@ fn account_map() { let block = produce_dummy_block(2, Some(prev_hash), vec![transfer_tx1, transfer_tx2]); - dbio.put_block(&block, [2; 32]).unwrap(); + dbio.put_block(&block, [2; 32], 0, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); let last_block = dbio.get_block(last_id).unwrap().unwrap(); @@ -406,7 +453,7 @@ fn account_map() { let block = produce_dummy_block(3, Some(prev_hash), vec![transfer_tx1, transfer_tx2]); - dbio.put_block(&block, [3; 32]).unwrap(); + dbio.put_block(&block, [3; 32], 0, &initial_state).unwrap(); let last_id = dbio.get_meta_last_block_id_in_db().unwrap().unwrap(); let last_block = dbio.get_block(last_id).unwrap().unwrap(); @@ -418,7 +465,7 @@ fn account_map() { let block = produce_dummy_block(4, Some(prev_hash), vec![transfer_tx]); - dbio.put_block(&block, [4; 32]).unwrap(); + dbio.put_block(&block, [4; 32], 0, &initial_state).unwrap(); let acc1_tx = dbio.get_acc_transactions(*acc1().value(), 0, 7).unwrap(); let acc1_tx_hashes: Vec<[u8; 32]> = acc1_tx.into_iter().map(|tx| tx.hash().0).collect(); @@ -431,3 +478,15 @@ fn account_map() { assert_eq!(acc1_tx_limited_hashes.as_slice(), &tx_hash_res[1..5]); } + +#[test] +fn reopen_preserves_seeded_breakpoint() { + let initial_state = initial_state(); + let temp_dir = tempdir().unwrap(); + { + let dbio = RocksDBIO::open_or_create(temp_dir.path(), &initial_state).unwrap(); + assert!(dbio.get_breakpoint_opt(0).unwrap().is_some()); + } // drop releases the RocksDB lock + let dbio = RocksDBIO::open_or_create(temp_dir.path(), &initial_state).unwrap(); + assert!(dbio.get_breakpoint_opt(0).unwrap().is_some()); +} diff --git a/lez/storage/src/indexer/write_atomic.rs b/lez/storage/src/indexer/write_atomic.rs index ec28f8ec..729bcc02 100644 --- a/lez/storage/src/indexer/write_atomic.rs +++ b/lez/storage/src/indexer/write_atomic.rs @@ -2,13 +2,13 @@ use std::collections::HashMap; use rocksdb::WriteBatch; -use super::{BREAKPOINT_INTERVAL, Block, DbError, DbResult, RocksDBIO}; +use super::{BREAKPOINT_INTERVAL, Block, DbError, DbResult, RocksDBIO, V03State}; use crate::{ DBIO as _, cells::shared_cells::{FirstBlockCell, FirstBlockSetCell, LastBlockCell}, indexer::indexer_cells::{ - AccNumTxCell, BlockHashToBlockIdMapCell, LastBreakpointIdCell, LastObservedL1LibHeaderCell, - TxHashToBlockIdMapCell, + AccNumTxCell, BlockHashToBlockIdMapCell, BreakpointCellRef, LastObservedL1LibHeaderCell, + TipSlotCell, TxHashToBlockIdMapCell, }, }; @@ -131,21 +131,28 @@ impl RocksDBIO { self.put_batch(&LastObservedL1LibHeaderCell(l1_lib_header), (), write_batch) } - pub fn put_meta_last_breakpoint_id_batch( + pub fn put_meta_tip_slot_in_db_batch( &self, - br_id: u64, + l1_slot: u64, write_batch: &mut WriteBatch, ) -> DbResult<()> { - self.put_batch(&LastBreakpointIdCell(br_id), (), write_batch) + self.put_batch(&TipSlotCell(l1_slot), (), write_batch) } pub fn put_meta_is_first_block_set_batch(&self, write_batch: &mut WriteBatch) -> DbResult<()> { self.put_batch(&FirstBlockSetCell(true), (), write_batch) } - // Block - - pub fn put_block(&self, block: &Block, l1_lib_header: [u8; 32]) -> DbResult<()> { + /// Put a block atomically (via [`WriteBatch`]) along with its L1 header, `Slot`, + /// and (at interval-boundary blocks) a snapshot of `post_state`, the block's + /// post-application state. + pub fn put_block( + &self, + block: &Block, + l1_lib_header: [u8; 32], + l1_slot: u64, + post_state: &V03State, + ) -> DbResult<()> { let cf_block = self.block_column(); let last_curr_block = self.get_meta_last_block_id_in_db()?.unwrap_or(0); let mut write_batch = WriteBatch::default(); @@ -163,6 +170,7 @@ impl RocksDBIO { if block.header.block_id > last_curr_block { self.put_meta_last_block_in_db_batch(block.header.block_id, &mut write_batch)?; self.put_meta_last_observed_l1_lib_header_in_db_batch(l1_lib_header, &mut write_batch)?; + self.put_meta_tip_slot_in_db_batch(l1_slot, &mut write_batch)?; } if last_curr_block == 0 { self.put_meta_first_block_in_db_batch(block, &mut write_batch)?; @@ -208,18 +216,23 @@ impl RocksDBIO { self.put_account_transactions_dependant(acc_id, &tx_hashes, &mut write_batch)?; } - self.db.write(write_batch).map_err(|rerr| { - DbError::rocksdb_cast_message(rerr, Some("Failed to write batch".to_owned())) - })?; - if block .header .block_id .is_multiple_of(BREAKPOINT_INTERVAL.into()) { - self.put_next_breakpoint()?; + let br_id = block + .header + .block_id + .checked_div(BREAKPOINT_INTERVAL.into()) + .expect("Breakpoint interval is not zero"); + self.put_batch(&BreakpointCellRef(post_state), br_id, &mut write_batch)?; } + self.db.write(write_batch).map_err(|rerr| { + DbError::rocksdb_cast_message(rerr, Some("Failed to write batch".to_owned())) + })?; + Ok(()) } } diff --git a/lez/storage/src/indexer/write_non_atomic.rs b/lez/storage/src/indexer/write_non_atomic.rs index 7ddab1dd..a2c178ec 100644 --- a/lez/storage/src/indexer/write_non_atomic.rs +++ b/lez/storage/src/indexer/write_non_atomic.rs @@ -1,9 +1,11 @@ -use super::{BREAKPOINT_INTERVAL, DbError, DbResult, RocksDBIO, V03State}; +use super::{DbResult, RocksDBIO, V03State}; +#[cfg(test)] +use crate::error::DbError; use crate::{ DBIO as _, cells::shared_cells::{FirstBlockSetCell, LastBlockCell}, indexer::indexer_cells::{ - BreakpointCellRef, LastBreakpointIdCell, LastObservedL1LibHeaderCell, + BreakpointCellRef, LastObservedL1LibHeaderCell, StallReasonCellRef, ZoneSdkIndexerCursorCellRef, }, }; @@ -23,10 +25,6 @@ impl RocksDBIO { self.put(&LastObservedL1LibHeaderCell(l1_lib_header), ()) } - pub fn put_meta_last_breakpoint_id(&self, br_id: u64) -> DbResult<()> { - self.put(&LastBreakpointIdCell(br_id), ()) - } - pub fn put_meta_is_first_block_set(&self) -> DbResult<()> { self.put(&FirstBlockSetCell(true), ()) } @@ -35,32 +33,25 @@ impl RocksDBIO { self.put(&ZoneSdkIndexerCursorCellRef(bytes), ()) } + pub fn put_stall_reason_bytes(&self, bytes: &[u8]) -> DbResult<()> { + self.put(&StallReasonCellRef(bytes), ()) + } + // State pub fn put_breakpoint(&self, br_id: u64, breakpoint: &V03State) -> DbResult<()> { self.put(&BreakpointCellRef(breakpoint), br_id) } - pub fn put_next_breakpoint(&self) -> DbResult<()> { - let last_block = self.get_meta_last_block_id_in_db()?.unwrap_or(0); - let next_breakpoint_id = self - .get_meta_last_breakpoint_id()? - .unwrap_or(0) - .checked_add(1) - .expect("Breakpoint Id will be lesser than u64::MAX"); - let block_to_break_id = next_breakpoint_id - .checked_mul(u64::from(BREAKPOINT_INTERVAL)) - .expect("Reached maximum breakpoint id"); - - if block_to_break_id <= last_block { - let next_breakpoint = self.calculate_state_for_id(block_to_break_id)?; - - self.put_breakpoint(next_breakpoint_id, &next_breakpoint)?; - self.put_meta_last_breakpoint_id(next_breakpoint_id) - } else { - Err(DbError::db_interaction_error( - "Breakpoint not yet achieved".to_owned(), - )) - } + /// Deletes a breakpoint snapshot. Test-only fault injection for simulating + /// stores whose boundary snapshot was lost. + #[cfg(test)] + pub(crate) fn delete_breakpoint(&self, br_id: u64) -> DbResult<()> { + let key = borsh::to_vec(&br_id).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize breakpoint id".to_owned())) + })?; + self.db + .delete_cf(&self.breakpoint_column(), key) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None)) } } diff --git a/lez/storage/src/lib.rs b/lez/storage/src/lib.rs index 2edb0ee3..8fd485d3 100644 --- a/lez/storage/src/lib.rs +++ b/lez/storage/src/lib.rs @@ -1,7 +1,7 @@ use rocksdb::{DBWithThreadMode, MultiThreaded, WriteBatch}; use crate::{ - cells::{SimpleReadableCell, SimpleWritableCell}, + cells::{SimpleReadableCell, SimpleStorableCell, SimpleWritableCell}, error::DbError, }; @@ -66,4 +66,17 @@ pub trait DBIO { ) -> DbResult<()> { cell.put_batch(self.db(), params, write_batch) } + + /// Delete a cell. Deleting an absent key is a no-op (rocksdb semantics). + fn del(&self, params: T::KeyParams) -> DbResult<()> { + let cf_ref = T::column_ref(self.db()); + self.db() + .delete_cf(&cf_ref, T::key_constructor(params)?) + .map_err(|rerr| { + DbError::rocksdb_cast_message( + rerr, + Some(format!("Failed to delete {:?}", T::CELL_NAME)), + ) + }) + } } diff --git a/lez/storage/src/sequencer/mod.rs b/lez/storage/src/sequencer/mod.rs index 44068517..1a3a1886 100644 --- a/lez/storage/src/sequencer/mod.rs +++ b/lez/storage/src/sequencer/mod.rs @@ -1,12 +1,14 @@ use std::{path::Path, sync::Arc}; +use borsh::{BorshDeserialize, BorshSerialize}; use common::{ HashType, block::{BedrockStatus, Block, BlockMeta}, }; use lee::V03State; use rocksdb::{ - BoundColumnFamily, ColumnFamilyDescriptor, DBWithThreadMode, MultiThreaded, Options, WriteBatch, + BoundColumnFamily, ColumnFamilyDescriptor, DBWithThreadMode, IteratorMode, MultiThreaded, + Options, WriteBatch, }; use crate::{ @@ -44,6 +46,47 @@ pub const DB_LEE_STATE_KEY: &str = "lee_state"; /// Name of state column family. pub const CF_LEE_STATE_NAME: &str = "cf_lee_state"; +/// A single key/value entry from a column family, used inside [`DbDump`]. +#[derive(BorshSerialize, BorshDeserialize)] +pub struct DbDumpEntry { + pub cf_name: String, + pub key: Vec, + pub value: Vec, +} + +/// Schema-agnostic single-blob snapshot of a store: every key/value pair across all column +/// families. Lets a prebuilt store ship as one committed file instead of a rocksdb directory. +#[derive(BorshSerialize, BorshDeserialize)] +pub struct DbDump { + pub entries: Vec, +} + +impl DbDump { + /// Serialize the dump to a zstd-compressed borsh blob. + pub fn to_bytes(&self) -> DbResult> { + /// zstd compression level for [`DbDump::to_bytes`]. Level 19 keeps the committed fixture + /// small without a meaningful decompression cost. + const DUMP_ZSTD_LEVEL: i32 = 19; + + let borsh = borsh::to_vec(self).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize DbDump".to_owned())) + })?; + zstd::encode_all(borsh.as_slice(), DUMP_ZSTD_LEVEL).map_err(|err| { + DbError::compression_error(err, Some("Failed to compress DbDump".to_owned())) + }) + } + + /// Deserialize a dump produced by [`Self::to_bytes`]. + pub fn from_bytes(bytes: &[u8]) -> DbResult { + let borsh = zstd::decode_all(bytes).map_err(|err| { + DbError::db_interaction_error(format!("Failed to decompress DbDump: {err}")) + })?; + borsh::from_slice(&borsh).map_err(|err| { + DbError::compression_error(err, Some("Failed to deserialize DbDump".to_owned())) + }) + } +} + pub struct RocksDBIO { pub db: DBWithThreadMode, } @@ -84,6 +127,71 @@ impl RocksDBIO { Ok(dbio) } + /// Dump every key/value pair across all column families into a [`DbDump`]. Column families are + /// discovered from disk, so new ones are captured without a hardcoded list. + pub fn dump_all(&self) -> DbResult { + let cf_names = + DBWithThreadMode::::list_cf(&Options::default(), self.db.path()) + .map_err(|rerr| { + DbError::rocksdb_cast_message( + rerr, + Some("Failed to list column families for dump".to_owned()), + ) + })?; + + let mut entries = Vec::new(); + for cf_name in cf_names { + let cf = self.db.cf_handle(&cf_name).ok_or_else(|| { + DbError::db_interaction_error(format!( + "Column family {cf_name:?} listed on disk but not opened; add it to `open_inner`" + )) + })?; + for item in self.db.iterator_cf(&cf, IteratorMode::Start) { + let (key, value) = item.map_err(|rerr| { + DbError::rocksdb_cast_message( + rerr, + Some(format!( + "Failed to iterate column family {cf_name:?} for dump" + )), + ) + })?; + entries.push(DbDumpEntry { + cf_name: cf_name.clone(), + key: key.into_vec(), + value: value.into_vec(), + }); + } + } + Ok(DbDump { entries }) + } + + /// Create a fresh rocksdb at `path` populated from a [`DbDump`]. + pub fn restore_from_dump(path: &Path, dump: &DbDump) -> DbResult { + let mut db_opts = Options::default(); + db_opts.create_missing_column_families(true); + db_opts.create_if_missing(true); + let dbio = Self::open_inner(path, &db_opts)?; + + let mut batch = WriteBatch::default(); + for entry in &dump.entries { + let cf = dbio.db.cf_handle(&entry.cf_name).ok_or_else(|| { + DbError::db_interaction_error(format!( + "Unknown column family {:?} in dump", + entry.cf_name + )) + })?; + batch.put_cf(&cf, &entry.key, &entry.value); + } + dbio.db.write(batch).map_err(|rerr| { + DbError::rocksdb_cast_message( + rerr, + Some("Failed to write dump restore batch".to_owned()), + ) + })?; + + Ok(dbio) + } + fn open_inner(path: &Path, db_opts: &Options) -> DbResult { let mut cf_opts = Options::default(); cf_opts.set_max_write_buffer_number(16); @@ -246,6 +354,11 @@ impl RocksDBIO { self.put(&ZoneSdkCheckpointCellRef(bytes), ()) } + /// Remove the persisted zone-sdk checkpoint so the next startup is treated as a fresh start. + pub fn delete_zone_sdk_checkpoint_bytes(&self) -> DbResult<()> { + self.del::(()) + } + pub fn get_pending_deposit_events(&self) -> DbResult> { Ok(self .get_opt::(())? @@ -448,10 +561,29 @@ impl RocksDBIO { } pub fn mark_block_as_finalized(&self, block_id: u64) -> DbResult<()> { + self.set_block_bedrock_status(block_id, BedrockStatus::Finalized) + } + + /// Reset every stored block to [`BedrockStatus::Pending`], for snapshotting a store to replay + /// against a fresh Bedrock instance that knows none of the blocks yet. + pub fn reset_all_blocks_to_pending(&self) -> DbResult<()> { + let block_ids: Vec = self + .get_all_blocks() + .filter_map(Result::ok) + .filter(|block| !matches!(block.bedrock_status, BedrockStatus::Pending)) + .map(|block| block.header.block_id) + .collect(); + for id in block_ids { + self.set_block_bedrock_status(id, BedrockStatus::Pending)?; + } + Ok(()) + } + + fn set_block_bedrock_status(&self, block_id: u64, status: BedrockStatus) -> DbResult<()> { let mut block = self.get_block(block_id)?.ok_or_else(|| { DbError::db_interaction_error(format!("Block with id {block_id} not found")) })?; - block.bedrock_status = BedrockStatus::Finalized; + block.bedrock_status = status; let cf_block = self.block_column(); self.db @@ -473,7 +605,7 @@ impl RocksDBIO { .map_err(|rerr| { DbError::rocksdb_cast_message( rerr, - Some(format!("Failed to mark block {block_id} as finalized")), + Some(format!("Failed to set block {block_id} bedrock status")), ) })?; diff --git a/lez/testnet_initial_state/Cargo.toml b/lez/testnet_initial_state/Cargo.toml index f06d5f51..c0899ea9 100644 --- a/lez/testnet_initial_state/Cargo.toml +++ b/lez/testnet_initial_state/Cargo.toml @@ -10,6 +10,7 @@ lee.workspace = true lee_core.workspace = true system_accounts.workspace = true programs.workspace = true +wrapped_token_core.workspace = true serde.workspace = true diff --git a/lez/testnet_initial_state/src/lib.rs b/lez/testnet_initial_state/src/lib.rs index 3296b953..c92371cd 100644 --- a/lez/testnet_initial_state/src/lib.rs +++ b/lez/testnet_initial_state/src/lib.rs @@ -258,9 +258,30 @@ fn initial_public_accounts() -> HashMap { .into_iter() .map(|clock_id| (clock_id, system_accounts::clock_account())), ) + .chain(std::iter::once(wrapped_token_config_account())) .collect() } +/// The wrapped-token config account. +/// +/// Seeded so the `wrapped_token` guest can pin its authorized minter (the +/// cross-zone inbox) without importing the inbox id. Fixed for every zone, so it +/// lives in the shared initial state. +fn wrapped_token_config_account() -> (AccountId, Account) { + let wrapped_token_id = programs::wrapped_token().id(); + ( + wrapped_token_core::config_account_id(wrapped_token_id), + Account { + program_owner: wrapped_token_id, + data: wrapped_token_core::minter_bytes(programs::cross_zone_inbox().id()) + .to_vec() + .try_into() + .expect("minter id fits in account data"), + ..Default::default() + }, + ) +} + fn initial_programs() -> Vec { vec![ programs::authenticated_transfer(), @@ -271,6 +292,12 @@ fn initial_programs() -> Vec { programs::vault(), programs::faucet(), programs::bridge(), + programs::cross_zone_outbox(), + programs::cross_zone_inbox(), + programs::ping_sender(), + programs::ping_receiver(), + programs::bridge_lock(), + programs::wrapped_token(), ] } diff --git a/lez/wallet/src/cli/mod.rs b/lez/wallet/src/cli/mod.rs index 43b6dc7a..44b578a1 100644 --- a/lez/wallet/src/cli/mod.rs +++ b/lez/wallet/src/cli/mod.rs @@ -7,6 +7,7 @@ use common::{HashType, transaction::LeeTransaction}; use derive_more::Display; use futures::TryFutureExt as _; use lee::ProgramDeploymentTransaction; +use lee_core::BlockId; use sequencer_service_rpc::RpcClient as _; pub use crate::helperfunctions::{read_mnemonic, read_pin}; @@ -117,11 +118,11 @@ pub struct Args { #[derive(Debug, Clone)] pub enum SubcommandReturnValue { - PrivacyPreservingTransfer { tx_hash: HashType }, + TransactionExecuted { tx_hash: HashType }, RegisterAccount { account_id: lee::AccountId }, Account(lee::Account), Empty, - SyncedToBlock(u64), + SyncedToBlock(BlockId), } #[derive(Debug, Display, Clone, PartialEq, Eq, Hash)] @@ -286,13 +287,16 @@ pub async fn execute_subcommand( ))?; let message = lee::program_deployment_transaction::Message::new(bytecode); let transaction = ProgramDeploymentTransaction::new(message); - let _response = wallet_core + let tx_hash = wallet_core .sequencer_client .send_transaction(LeeTransaction::ProgramDeployment(transaction)) .await .context("Transaction submission error")?; - SubcommandReturnValue::Empty + wallet_core + .poll_and_finalize_public_transaction(tx_hash) + .await + .context("Transaction finalization error")? } }; diff --git a/lez/wallet/src/cli/programs/bridge.rs b/lez/wallet/src/cli/programs/bridge.rs index 8d8cf5ee..302ffbfb 100644 --- a/lez/wallet/src/cli/programs/bridge.rs +++ b/lez/wallet/src/cli/programs/bridge.rs @@ -47,9 +47,10 @@ impl WalletSubcommand for BridgeSubcommand { .send_withdraw(sender_account_id, amount, bedrock_account_pk) .await?; - println!("Transaction hash is {tx_hash}"); - - Ok(SubcommandReturnValue::Empty) + wallet_core + .poll_and_finalize_public_transaction(tx_hash) + .await + .context("Transaction finalization error") } } } diff --git a/lez/wallet/src/cli/programs/native_token_transfer.rs b/lez/wallet/src/cli/programs/native_token_transfer.rs index ccc2f14c..aaa3efe1 100644 --- a/lez/wallet/src/cli/programs/native_token_transfer.rs +++ b/lez/wallet/src/cli/programs/native_token_transfer.rs @@ -443,7 +443,7 @@ impl NativeTokenTransferProgramSubcommandShielded { wallet_core.store_persistent_data()?; - Ok(SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash }) + Ok(SubcommandReturnValue::TransactionExecuted { tx_hash }) } } diff --git a/lez/wallet/src/lib.rs b/lez/wallet/src/lib.rs index 4b4ba149..e0b89077 100644 --- a/lez/wallet/src/lib.rs +++ b/lez/wallet/src/lib.rs @@ -474,11 +474,6 @@ impl WalletCore { Some(Commitment::new(&account_id, account)) } - /// Poll transactions. - pub async fn poll_native_token_transfer(&self, hash: HashType) -> Result { - self.poller.poll_tx(hash).await - } - pub async fn check_private_account_initialized( &self, account_id: AccountId, @@ -539,7 +534,6 @@ impl WalletCore { } } - println!("Transaction data is {:?}", tx.message); Ok(()) } @@ -548,10 +542,11 @@ impl WalletCore { tx_hash: HashType, ) -> Result { println!("Transaction hash is {tx_hash}"); - let transfer_tx = self.poll_native_token_transfer(tx_hash).await?; - println!("Transaction data is {transfer_tx:?}"); + let (tx, block_id) = self.poller.poll_tx(tx_hash).await?; + println!("Transaction is included in block {block_id}"); + println!("Transaction data is {tx:?}"); self.store_persistent_data()?; - Ok(cli::SubcommandReturnValue::Empty) + Ok(cli::SubcommandReturnValue::TransactionExecuted { tx_hash }) } /// Pass an empty slice when the recipient is foreign and no accounts need decoding. @@ -561,12 +556,17 @@ impl WalletCore { acc_decode_data: &[AccDecodeData], ) -> Result { println!("Transaction hash is {tx_hash}"); - let transfer_tx = self.poll_native_token_transfer(tx_hash).await?; - if let common::transaction::LeeTransaction::PrivacyPreserving(tx) = transfer_tx { - self.decode_insert_privacy_preserving_transaction_results(&tx, acc_decode_data)?; + let (tx, block_id) = self.poller.poll_tx(tx_hash).await?; + println!("Transaction is included in block {block_id}"); + println!("Transaction data is {tx:?}"); + if let common::transaction::LeeTransaction::PrivacyPreserving(private_tx) = tx { + self.decode_insert_privacy_preserving_transaction_results( + &private_tx, + acc_decode_data, + )?; } self.store_persistent_data()?; - Ok(cli::SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash }) + Ok(cli::SubcommandReturnValue::TransactionExecuted { tx_hash }) } pub async fn send_privacy_preserving_tx( diff --git a/lez/wallet/src/poller.rs b/lez/wallet/src/poller.rs index 52802887..63ba2275 100644 --- a/lez/wallet/src/poller.rs +++ b/lez/wallet/src/poller.rs @@ -2,6 +2,7 @@ use std::time::Duration; use anyhow::Result; use common::{HashType, block::Block, transaction::LeeTransaction}; +use lee_core::BlockId; use log::{info, warn}; use sequencer_service_rpc::{RpcClient as _, SequencerClient}; @@ -30,7 +31,7 @@ impl TxPoller { } // TODO: this polling is not based on blocks, but on timeouts, need to fix this. - pub async fn poll_tx(&self, tx_hash: HashType) -> Result { + pub async fn poll_tx(&self, tx_hash: HashType) -> Result<(LeeTransaction, BlockId)> { let max_blocks_to_query = self.polling_max_blocks_to_query; info!("Starting poll for transaction {tx_hash}"); @@ -41,7 +42,7 @@ impl TxPoller { loop { match self.client.get_transaction(tx_hash).await { - Ok(Some(tx)) => return Ok(tx), + Ok(Some((tx, block_id))) => return Ok((tx, block_id)), Ok(None) => {} Err(err) => { warn!("Failed to get transaction by hash {tx_hash} with error: {err:#?}"); diff --git a/programs/bridge_lock/core/Cargo.toml b/programs/bridge_lock/core/Cargo.toml new file mode 100644 index 00000000..2c9e2f58 --- /dev/null +++ b/programs/bridge_lock/core/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "bridge_lock_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +serde = { workspace = true, features = ["alloc"] } +lee = { workspace = true, optional = true } + +[features] +# Host-only genesis helper; pulls `lee`, so the risc0 guest builds without it. +host = ["dep:lee"] diff --git a/programs/bridge_lock/core/src/lib.rs b/programs/bridge_lock/core/src/lib.rs new file mode 100644 index 00000000..eae1d681 --- /dev/null +++ b/programs/bridge_lock/core/src/lib.rs @@ -0,0 +1,92 @@ +//! Core types for the bridge-lock program, the source side of the cross-zone +//! token bridge. A holder locks part of their balance into an escrow and emits a +//! cross-zone message minting the wrapped token on the target zone. + +use lee_core::{ + account::AccountId, + program::{PdaSeed, ProgramId}, +}; +use serde::{Deserialize, Serialize}; + +const ESCROW_SEED_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/BridgeLockEscrow/0000/"; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum Instruction { + /// Lock `amount` of the holder's balance and emit a cross-zone message + /// minting the wrapped token on `target_zone`. The emission fields mirror + /// `cross_zone_outbox::Instruction::Emit` so the watcher reads them directly. + /// + /// Required accounts (3): holder holding (authorized), escrow PDA, outbox PDA. + Lock { + amount: u128, + target_zone: [u8; 32], + target_program_id: ProgramId, + target_accounts: Vec<[u8; 32]>, + payload: Vec, + outbox_program_id: ProgramId, + ordinal: u32, + }, +} + +/// PDA accumulating all locked balance on this zone. +#[must_use] +pub fn escrow_account_id(bridge_lock_id: ProgramId) -> AccountId { + AccountId::for_public_pda(&bridge_lock_id, &escrow_seed()) +} + +#[must_use] +pub fn escrow_seed() -> PdaSeed { + PdaSeed::new(ESCROW_SEED_DOMAIN) +} + +/// Reads a bridgeable balance from account data; empty data is a zero balance. +#[must_use] +pub fn read_balance(data: &[u8]) -> u128 { + if data.len() < 16 { + return 0; + } + u128::from_le_bytes(data[..16].try_into().unwrap_or_else(|_| unreachable!())) +} + +#[must_use] +pub fn balance_bytes(amount: u128) -> [u8; 16] { + amount.to_le_bytes() +} + +/// Builds the genesis holding account funding a holder's bridgeable balance: +/// owned by bridge_lock, data is the LE balance, at the holder's account id. It +/// is not produced by any transaction, so the sequencer and the indexer both +/// seed it through this one builder to keep their genesis states identical. +#[cfg(feature = "host")] +#[must_use] +pub fn build_holding_account( + holder: AccountId, + amount: u128, +) -> (AccountId, lee_core::account::Account) { + let account = lee_core::account::Account { + program_owner: lee::program::Program::bridge_lock().id(), + data: balance_bytes(amount) + .to_vec() + .try_into() + .expect("balance fits in account data"), + ..Default::default() + }; + (holder, account) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn balance_round_trips() { + assert_eq!(read_balance(&balance_bytes(7)), 7); + assert_eq!(read_balance(&[]), 0); + } + + #[test] + fn escrow_is_stable() { + let id: ProgramId = [4; 8]; + assert_eq!(escrow_account_id(id), escrow_account_id(id)); + } +} diff --git a/programs/cross_zone_inbox/core/Cargo.toml b/programs/cross_zone_inbox/core/Cargo.toml new file mode 100644 index 00000000..374a287d --- /dev/null +++ b/programs/cross_zone_inbox/core/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "cross_zone_inbox_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +serde = { workspace = true, features = ["alloc"] } +risc0-zkvm.workspace = true +borsh.workspace = true +lee = { workspace = true, optional = true } +ping_core = { workspace = true, optional = true } +bridge_lock_core = { workspace = true, optional = true } + +[features] +# Host-only transaction builder and emission extractor; pull `lee` and the +# emitter cores, so the risc0 guest builds without them. +host = ["dep:lee", "dep:ping_core", "dep:bridge_lock_core"] diff --git a/programs/cross_zone_inbox/core/src/lib.rs b/programs/cross_zone_inbox/core/src/lib.rs new file mode 100644 index 00000000..edb90424 --- /dev/null +++ b/programs/cross_zone_inbox/core/src/lib.rs @@ -0,0 +1,378 @@ +use std::collections::{BTreeMap, BTreeSet}; + +use borsh::{BorshDeserialize, BorshSerialize}; +use lee_core::{ + account::AccountId, + program::{PdaSeed, ProgramId}, +}; +use serde::{Deserialize, Serialize}; + +/// Raw 32-byte zone (channel) id; the host maps it to the zone-sdk `ChannelId`. +pub type ZoneId = [u8; 32]; + +/// Block-signing public key pinned per peer zone. +pub type ExpectedPubkey = [u8; 32]; + +/// Content-addressed replay key for a delivered message. +pub type MessageKey = [u8; 32]; + +/// Source blocks per seen-set shard, so no single seen account grows without bound. +pub const EPOCH_BLOCKS: u64 = 10_000; + +const MESSAGE_KEY_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/CrossZoneMsgKey/00000/"; +const INBOX_CONFIG_SEED: [u8; 32] = *b"/LEZ/v0.3/CrossZoneInboxCfg/000/"; +const INBOX_SEEN_SEED_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/CrossZoneInboxSeen/00/"; + +/// A peer zone whose outbox a zone watches for inbound cross-zone messages. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct CrossZonePeer { + /// The peer's Bedrock channel; its 32 bytes double as the peer's zone id. + pub channel_id: ZoneId, + /// Programs on the local zone a message from this peer is allowed to target. + pub allowed_targets: Vec, + /// The peer's block-signing public key, pinned to reject blocks inscribed by + /// anyone other than that zone's sequencer. `None` skips the check (the + /// channel signer is still authenticated by the zone-sdk). + #[serde(default)] + pub expected_block_signing_pubkey: Option<[u8; 32]>, +} + +/// Cross-zone configuration shared by a zone's sequencer (watcher) and indexer +/// (verifier): the peers it reads from Bedrock and, per peer, the local programs +/// they may deliver to. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct CrossZoneConfig { + pub peers: Vec, +} + +/// A finalized outbound message observed on a peer zone, addressed to a program +/// on this zone. The watcher fills it from the peer's block; it is never +/// self-reported by a user. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct CrossZoneMessage { + pub src_zone: ZoneId, + pub src_block_id: u64, + pub src_tx_index: u32, + pub src_program_id: ProgramId, + pub target_program_id: ProgramId, + pub payload: Vec, + /// Reserved for a future source-state proof; MUST be `None` in v1. + pub l1_inclusion_witness: Option>, +} + +/// Peer and per-peer target allowlists, plus this inbox's own zone id. +#[derive( + Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshSerialize, BorshDeserialize, +)] +pub struct InboxConfig { + pub self_zone: ZoneId, + pub allowed_peers: BTreeMap, + pub allowed_targets: BTreeMap>, +} + +impl InboxConfig { + /// Borsh-encoded form stored in the inbox config account. + #[must_use] + pub fn to_bytes(&self) -> Vec { + borsh::to_vec(self).expect("InboxConfig serializes") + } + + /// Decodes an [`InboxConfig`] from account data. + pub fn from_bytes(bytes: &[u8]) -> borsh::io::Result { + borsh::from_slice(bytes) + } +} + +/// The replay keys seen for one `(src_zone, epoch)` shard. +#[derive(Clone, Debug, Default, PartialEq, Eq, BorshSerialize, BorshDeserialize)] +pub struct SeenShard(pub BTreeSet); + +impl SeenShard { + /// Decodes a shard from account data; empty data is an empty shard. + pub fn from_bytes(bytes: &[u8]) -> borsh::io::Result { + if bytes.is_empty() { + return Ok(Self::default()); + } + borsh::from_slice(bytes) + } + + #[must_use] + pub fn to_bytes(&self) -> Vec { + borsh::to_vec(self).expect("SeenShard serializes") + } + + #[must_use] + pub fn contains(&self, key: &MessageKey) -> bool { + self.0.contains(key) + } + + /// Inserts a key; returns true if it was newly inserted. + pub fn insert(&mut self, key: MessageKey) -> bool { + self.0.insert(key) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum Instruction { + /// Delivers a finalized peer message to its target program. + Dispatch(CrossZoneMessage), +} + +/// Content-addressed replay key: `(src_zone, src_block_id, src_tx_index)` hashed +/// under a domain separator. Watcher-independent and immune to proof +/// malleability, since it keys on block id plus index rather than a tx hash. +#[must_use] +pub fn message_key(src_zone: &ZoneId, src_block_id: u64, src_tx_index: u32) -> MessageKey { + use risc0_zkvm::sha::{Impl, Sha256 as _}; + + let mut bytes = Vec::with_capacity(MESSAGE_KEY_DOMAIN.len() + 32 + 8 + 4); + bytes.extend_from_slice(&MESSAGE_KEY_DOMAIN); + bytes.extend_from_slice(src_zone); + bytes.extend_from_slice(&src_block_id.to_le_bytes()); + bytes.extend_from_slice(&src_tx_index.to_le_bytes()); + + Impl::hash_bytes(&bytes) + .as_bytes() + .try_into() + .unwrap_or_else(|_| unreachable!()) +} + +/// The config account holding the allowlists. +#[must_use] +pub fn inbox_config_account_id(inbox_id: ProgramId) -> AccountId { + AccountId::for_public_pda(&inbox_id, &PdaSeed::new(INBOX_CONFIG_SEED)) +} + +/// The seen-set shard for the `(src_zone, epoch)` the message falls in. +#[must_use] +pub fn inbox_seen_shard_account_id( + inbox_id: ProgramId, + src_zone: &ZoneId, + src_block_id: u64, +) -> AccountId { + AccountId::for_public_pda(&inbox_id, &inbox_seen_shard_seed(src_zone, src_block_id)) +} + +/// Seed of the seen-shard PDA, exposed so the guest can claim the account. +#[must_use] +pub fn inbox_seen_shard_seed(src_zone: &ZoneId, src_block_id: u64) -> PdaSeed { + use risc0_zkvm::sha::{Impl, Sha256 as _}; + + let src_epoch = src_block_id / EPOCH_BLOCKS; + let mut bytes = Vec::with_capacity(INBOX_SEEN_SEED_DOMAIN.len() + 32 + 8); + bytes.extend_from_slice(&INBOX_SEEN_SEED_DOMAIN); + bytes.extend_from_slice(src_zone); + bytes.extend_from_slice(&src_epoch.to_le_bytes()); + + let seed: [u8; 32] = Impl::hash_bytes(&bytes) + .as_bytes() + .try_into() + .unwrap_or_else(|_| unreachable!()); + PdaSeed::new(seed) +} + +/// Builds the sequencer-origin dispatch transaction. Pure, so the watcher's +/// injected tx and the indexer's re-derived tx are byte-identical for the same +/// inputs (the basis of the Option B check). `target_account_ids` are the +/// inbox's chained-call targets; deriving them is target-specific. +#[cfg(feature = "host")] +#[must_use] +pub fn build_inbox_dispatch_tx( + inbox_id: ProgramId, + msg: &CrossZoneMessage, + target_account_ids: Vec, +) -> lee::PublicTransaction { + let mut account_ids = Vec::with_capacity(2 + target_account_ids.len()); + account_ids.push(inbox_config_account_id(inbox_id)); + account_ids.push(inbox_seen_shard_account_id( + inbox_id, + &msg.src_zone, + msg.src_block_id, + )); + account_ids.extend(target_account_ids); + + let message = lee::public_transaction::Message::try_new( + inbox_id, + account_ids, + vec![], + Instruction::Dispatch(msg.clone()), + ) + .expect("inbox dispatch instruction must serialize"); + + lee::PublicTransaction::new( + message, + lee::public_transaction::WitnessSet::from_raw_parts(vec![]), + ) +} + +/// The cross-zone emission fields a watcher or verifier reads off a source +/// transaction, common to every emitter program. +#[cfg(feature = "host")] +pub struct Emission { + pub target_zone: ZoneId, + pub target_program_id: ProgramId, + pub target_accounts: Vec<[u8; 32]>, + pub payload: Vec, +} + +/// Extracts the cross-zone emission from a source transaction, recognizing the +/// known emitter programs. Returns `None` for any other program. The watcher and +/// verifier both use this so they agree on what a given source tx emits. +/// +/// Option A: each emitter is decoded explicitly. The principled alternative is to +/// read the outbox PDA write, which would need re-execution of the source tx. +#[cfg(feature = "host")] +#[must_use] +pub fn extract_emission( + program_id: ProgramId, + instruction_data: &[u32], + ping_sender_id: ProgramId, + bridge_lock_id: ProgramId, +) -> Option { + if program_id == ping_sender_id { + let ping_core::SenderInstruction::Send { + target_zone, + target_program_id, + target_accounts, + payload, + .. + } = risc0_zkvm::serde::from_slice(instruction_data).ok()?; + Some(Emission { + target_zone, + target_program_id, + target_accounts, + payload, + }) + } else if program_id == bridge_lock_id { + let bridge_lock_core::Instruction::Lock { + target_zone, + target_program_id, + target_accounts, + payload, + .. + } = risc0_zkvm::serde::from_slice(instruction_data).ok()?; + Some(Emission { + target_zone, + target_program_id, + target_accounts, + payload, + }) + } else { + None + } +} + +/// Builds the dispatch transaction for one peer emission. Both the sequencer's +/// watcher and the indexer's verifier go through this so their transactions are +/// byte-identical for the same emission (the basis of the Option B check). +#[cfg(feature = "host")] +#[must_use] +pub fn build_dispatch_from_emission( + inbox_id: ProgramId, + src_zone: ZoneId, + src_block_id: u64, + src_tx_index: u32, + src_program_id: ProgramId, + target_program_id: ProgramId, + target_accounts: &[[u8; 32]], + payload: Vec, +) -> lee::PublicTransaction { + let msg = CrossZoneMessage { + src_zone, + src_block_id, + src_tx_index, + src_program_id, + target_program_id, + payload, + l1_inclusion_witness: None, + }; + let target_ids = target_accounts + .iter() + .copied() + .map(AccountId::new) + .collect(); + build_inbox_dispatch_tx(inbox_id, &msg, target_ids) +} + +/// Builds the inbox config account a zone seeds into genesis state so the inbox +/// guest can authorize inbound peer messages. The sequencer and indexer seed the +/// same account from the same config, keeping their replayed state consistent. +#[cfg(feature = "host")] +#[must_use] +pub fn build_inbox_config_account( + self_zone: ZoneId, + cross_zone: &CrossZoneConfig, +) -> (AccountId, lee_core::account::Account) { + let inbox_id = lee::program::Program::cross_zone_inbox().id(); + + let mut allowed_targets = BTreeMap::new(); + for peer in &cross_zone.peers { + allowed_targets.insert(peer.channel_id, peer.allowed_targets.clone()); + } + let config = InboxConfig { + self_zone, + allowed_peers: BTreeMap::new(), + allowed_targets, + }; + + let account = lee_core::account::Account { + program_owner: inbox_id, + balance: 0, + data: config + .to_bytes() + .try_into() + .expect("inbox config fits in account data"), + nonce: 0_u128.into(), + }; + (inbox_config_account_id(inbox_id), account) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn zone(b: u8) -> ZoneId { + [b; 32] + } + + #[test] + fn message_key_is_stable_and_content_addressed() { + assert_eq!(message_key(&zone(1), 7, 3), message_key(&zone(1), 7, 3)); + assert_ne!(message_key(&zone(1), 7, 3), message_key(&zone(2), 7, 3)); + assert_ne!(message_key(&zone(1), 7, 3), message_key(&zone(1), 8, 3)); + assert_ne!(message_key(&zone(1), 7, 3), message_key(&zone(1), 7, 4)); + } + + #[test] + fn seen_shards_split_on_epoch_boundary() { + let id: ProgramId = [9; 8]; + assert_eq!( + inbox_seen_shard_account_id(id, &zone(1), 0), + inbox_seen_shard_account_id(id, &zone(1), EPOCH_BLOCKS - 1), + ); + assert_ne!( + inbox_seen_shard_account_id(id, &zone(1), EPOCH_BLOCKS - 1), + inbox_seen_shard_account_id(id, &zone(1), EPOCH_BLOCKS), + ); + } + + #[cfg(feature = "host")] + #[test] + fn build_inbox_dispatch_tx_is_deterministic() { + let inbox: ProgramId = [5; 8]; + let msg = CrossZoneMessage { + src_zone: zone(1), + src_block_id: 42, + src_tx_index: 2, + src_program_id: [6; 8], + target_program_id: [7; 8], + payload: vec![1, 2, 3, 4], + l1_inclusion_witness: None, + }; + let targets = vec![AccountId::new([8; 32]), AccountId::new([9; 32])]; + + let tx1 = build_inbox_dispatch_tx(inbox, &msg, targets.clone()); + let tx2 = build_inbox_dispatch_tx(inbox, &msg, targets); + assert_eq!(tx1, tx2); + } +} diff --git a/programs/cross_zone_outbox/core/Cargo.toml b/programs/cross_zone_outbox/core/Cargo.toml new file mode 100644 index 00000000..c7876286 --- /dev/null +++ b/programs/cross_zone_outbox/core/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "cross_zone_outbox_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +serde = { workspace = true, features = ["alloc"] } +risc0-zkvm.workspace = true +borsh.workspace = true diff --git a/programs/cross_zone_outbox/core/src/lib.rs b/programs/cross_zone_outbox/core/src/lib.rs new file mode 100644 index 00000000..b3449598 --- /dev/null +++ b/programs/cross_zone_outbox/core/src/lib.rs @@ -0,0 +1,93 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use lee_core::{ + account::AccountId, + program::{PdaSeed, ProgramId}, +}; +use serde::{Deserialize, Serialize}; + +/// Raw 32-byte zone (channel) id; the host maps it to the zone-sdk `ChannelId`. +pub type ZoneId = [u8; 32]; + +const OUTBOX_SEED_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/CrossZoneOutbox/00000/"; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum Instruction { + /// Records an outbound cross-zone message as a write to a self-owned PDA. + /// + /// Required accounts (1): + /// - Outbox PDA account + Emit { + target_zone: ZoneId, + target_program_id: ProgramId, + /// Accounts the destination inbox must hand to the target program's + /// chained call. The emitter specifies them; the watcher forwards them + /// verbatim so the inbox stays target-agnostic. + target_accounts: Vec<[u8; 32]>, + payload: Vec, + ordinal: u32, + }, +} + +/// The message as stored in an outbox PDA. The destination zone's watcher reads +/// this from the inscribed block; the source coordinates are filled by the +/// watcher, not stored here. +#[derive(Clone, Debug, PartialEq, Eq, BorshSerialize, BorshDeserialize)] +pub struct OutboxRecord { + pub target_zone: ZoneId, + pub target_program_id: ProgramId, + pub target_accounts: Vec<[u8; 32]>, + pub payload: Vec, +} + +impl OutboxRecord { + /// Borsh-encoded form stored in the outbox PDA's account data. + #[must_use] + pub fn to_bytes(&self) -> Vec { + borsh::to_vec(self).expect("OutboxRecord serializes") + } + + /// Decodes an [`OutboxRecord`] from account data. + pub fn from_bytes(bytes: &[u8]) -> borsh::io::Result { + borsh::from_slice(bytes) + } +} + +/// PDA holding one emitted message, keyed by destination zone and a per-zone +/// ordinal. +#[must_use] +pub fn outbox_pda(outbox_id: ProgramId, target_zone: &ZoneId, ordinal: u32) -> AccountId { + AccountId::for_public_pda(&outbox_id, &outbox_pda_seed(target_zone, ordinal)) +} + +/// Seed of an outbox message PDA, exposed so the guest can claim the account. +#[must_use] +pub fn outbox_pda_seed(target_zone: &ZoneId, ordinal: u32) -> PdaSeed { + use risc0_zkvm::sha::{Impl, Sha256 as _}; + + let mut bytes = Vec::with_capacity(OUTBOX_SEED_DOMAIN.len() + target_zone.len() + 4); + bytes.extend_from_slice(&OUTBOX_SEED_DOMAIN); + bytes.extend_from_slice(target_zone); + bytes.extend_from_slice(&ordinal.to_le_bytes()); + + let seed: [u8; 32] = Impl::hash_bytes(&bytes) + .as_bytes() + .try_into() + .unwrap_or_else(|_| unreachable!()); + PdaSeed::new(seed) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn outbox_pda_is_unique_per_zone_and_ordinal() { + let id: ProgramId = [3; 8]; + let zone_a = [1; 32]; + let zone_b = [2; 32]; + + assert_eq!(outbox_pda(id, &zone_a, 0), outbox_pda(id, &zone_a, 0)); + assert_ne!(outbox_pda(id, &zone_a, 0), outbox_pda(id, &zone_a, 1)); + assert_ne!(outbox_pda(id, &zone_a, 0), outbox_pda(id, &zone_b, 0)); + } +} diff --git a/programs/ping/core/Cargo.toml b/programs/ping/core/Cargo.toml new file mode 100644 index 00000000..29870630 --- /dev/null +++ b/programs/ping/core/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "ping_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +serde = { workspace = true, features = ["alloc"] } diff --git a/programs/ping/core/src/lib.rs b/programs/ping/core/src/lib.rs new file mode 100644 index 00000000..268e5b98 --- /dev/null +++ b/programs/ping/core/src/lib.rs @@ -0,0 +1,38 @@ +use lee_core::{ + account::AccountId, + program::{PdaSeed, ProgramId}, +}; +use serde::{Deserialize, Serialize}; + +const PING_RECORD_SEED: [u8; 32] = *b"/LEZ/v0.3/PingRecord/0000000000/"; + +/// Instruction delivered to `ping_receiver` by the inbox: record the payload. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum ReceiverInstruction { + Record { payload: Vec }, +} + +/// Instruction to `ping_sender`: forwarded verbatim into `cross_zone_outbox::Instruction::Emit`. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum SenderInstruction { + Send { + outbox_program_id: ProgramId, + target_zone: [u8; 32], + target_program_id: ProgramId, + target_accounts: Vec<[u8; 32]>, + payload: Vec, + ordinal: u32, + }, +} + +/// The account a `ping_receiver` records the latest delivered payload into. +#[must_use] +pub fn ping_record_pda(receiver_id: ProgramId) -> AccountId { + AccountId::for_public_pda(&receiver_id, &ping_record_seed()) +} + +/// Seed of the record PDA, exposed so the guest can claim the account. +#[must_use] +pub fn ping_record_seed() -> PdaSeed { + PdaSeed::new(PING_RECORD_SEED) +} diff --git a/programs/wrapped_token/core/Cargo.toml b/programs/wrapped_token/core/Cargo.toml new file mode 100644 index 00000000..ef0aabbc --- /dev/null +++ b/programs/wrapped_token/core/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "wrapped_token_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +lee_core.workspace = true +serde = { workspace = true, features = ["alloc"] } +risc0-zkvm.workspace = true diff --git a/programs/wrapped_token/core/src/lib.rs b/programs/wrapped_token/core/src/lib.rs new file mode 100644 index 00000000..ea4ffd97 --- /dev/null +++ b/programs/wrapped_token/core/src/lib.rs @@ -0,0 +1,121 @@ +//! Core types for the wrapped-token program, the destination side of the +//! cross-zone bridge. Only the cross-zone inbox may mint; the guest enforces +//! this by reading the authorized minter from a genesis-seeded config account. + +use lee_core::{ + account::AccountId, + program::{PdaSeed, ProgramId}, +}; +use serde::{Deserialize, Serialize}; + +const CONFIG_SEED_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/WrappedTokenConfig/00/"; +const HOLDING_SEED_DOMAIN: [u8; 32] = *b"/LEZ/v0.3/WrappedTokenHold/00000"; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum Instruction { + /// Credit `amount` wrapped tokens to `recipient`'s holding. Delivered only by + /// the cross-zone inbox. + /// + /// Required accounts (2): the wrapped-token config PDA, then the recipient's + /// holding PDA. + Mint { recipient: [u8; 32], amount: u128 }, +} + +/// PDA holding the authorized minter program id (the cross-zone inbox), seeded at +/// genesis so the guest can pin its caller without importing the inbox image id. +#[must_use] +pub fn config_account_id(wrapped_token_id: ProgramId) -> AccountId { + AccountId::for_public_pda(&wrapped_token_id, &config_seed()) +} + +#[must_use] +pub fn config_seed() -> PdaSeed { + PdaSeed::new(CONFIG_SEED_DOMAIN) +} + +/// PDA holding one recipient's wrapped-token balance. +#[must_use] +pub fn holding_account_id(wrapped_token_id: ProgramId, recipient: &[u8; 32]) -> AccountId { + AccountId::for_public_pda(&wrapped_token_id, &holding_seed(recipient)) +} + +#[must_use] +pub fn holding_seed(recipient: &[u8; 32]) -> PdaSeed { + use risc0_zkvm::sha::{Impl, Sha256 as _}; + + let mut bytes = Vec::with_capacity(HOLDING_SEED_DOMAIN.len() + recipient.len()); + bytes.extend_from_slice(&HOLDING_SEED_DOMAIN); + bytes.extend_from_slice(recipient); + let seed: [u8; 32] = Impl::hash_bytes(&bytes) + .as_bytes() + .try_into() + .unwrap_or_else(|_| unreachable!()); + PdaSeed::new(seed) +} + +/// Encodes the authorized minter program id for the config account's data. +#[must_use] +pub fn minter_bytes(minter: ProgramId) -> [u8; 32] { + let mut bytes = [0_u8; 32]; + for (word, chunk) in minter.iter().zip(bytes.chunks_exact_mut(4)) { + chunk.copy_from_slice(&word.to_le_bytes()); + } + bytes +} + +/// Decodes the authorized minter program id from the config account's data. +#[must_use] +pub fn read_minter(data: &[u8]) -> Option { + if data.len() < 32 { + return None; + } + let mut minter = [0_u32; 8]; + for (word, chunk) in minter.iter_mut().zip(data[..32].chunks_exact(4)) { + *word = u32::from_le_bytes(chunk.try_into().unwrap_or_else(|_| unreachable!())); + } + Some(minter) +} + +/// Reads a wrapped-token balance from account data; empty data is a zero balance. +#[must_use] +pub fn read_balance(data: &[u8]) -> u128 { + if data.len() < 16 { + return 0; + } + u128::from_le_bytes(data[..16].try_into().unwrap_or_else(|_| unreachable!())) +} + +#[must_use] +pub fn balance_bytes(amount: u128) -> [u8; 16] { + amount.to_le_bytes() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn minter_round_trips() { + let minter: ProgramId = [1, 2, 3, 4, 5, 6, 7, 8]; + assert_eq!(read_minter(&minter_bytes(minter)), Some(minter)); + } + + #[test] + fn balance_round_trips() { + assert_eq!(read_balance(&balance_bytes(42)), 42); + assert_eq!(read_balance(&[]), 0); + } + + #[test] + fn holding_is_unique_per_recipient() { + let id: ProgramId = [9; 8]; + assert_ne!( + holding_account_id(id, &[1; 32]), + holding_account_id(id, &[2; 32]) + ); + assert_eq!( + holding_account_id(id, &[1; 32]), + holding_account_id(id, &[1; 32]) + ); + } +} diff --git a/test_fixtures/Cargo.toml b/test_fixtures/Cargo.toml index 71f887b6..88526dea 100644 --- a/test_fixtures/Cargo.toml +++ b/test_fixtures/Cargo.toml @@ -21,6 +21,7 @@ wallet.workspace = true programs.workspace = true anyhow.workspace = true +bip39.workspace = true bytesize.workspace = true env_logger.workspace = true futures.workspace = true @@ -31,4 +32,5 @@ serde_json.workspace = true tempfile.workspace = true testcontainers = { version = "0.27.3", features = ["docker-compose"] } tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } +tokio-util.workspace = true url.workspace = true diff --git a/test_fixtures/README.md b/test_fixtures/README.md new file mode 100644 index 00000000..502ed8b5 --- /dev/null +++ b/test_fixtures/README.md @@ -0,0 +1,31 @@ +# test_fixtures + +Shared test/bench fixtures that spin up bedrock + sequencer + indexer + wallet end-to-end. + +## Library + +`TestContext` drives the full stack for integration tests and benches: + +```rust +let ctx = TestContext::new().await?; // fast: restores the prebuilt db dump +let ctx = TestContext::builder() + .from_scratch() // apply genesis + claim supply live + .with_genesis(actions) // custom genesis (implies from_scratch) + .disable_indexer() + .build().await?; +``` + +`TestContext::new()` restores `fixtures/prebuilt_sequencer_db.dump` (genesis + initial-supply claim +blocks) instead of re-applying genesis and re-proving the private claims, then syncs the wallet. Use +`from_scratch()` to build genesis live. + +## Binary + +`regenerate_test_fixture` regenerates that dump (needs Docker): + +```sh +just regenerate-test-fixture # RISC0_DEV_MODE=1 cargo run -p test_fixtures --bin regenerate_test_fixture +``` + +Rerun and commit `fixtures/prebuilt_sequencer_db.dump` after changing genesis, the default accounts, +block format, or initial state. diff --git a/test_fixtures/fixtures/prebuilt_sequencer_db.dump b/test_fixtures/fixtures/prebuilt_sequencer_db.dump new file mode 100644 index 00000000..fcaeac6f Binary files /dev/null and b/test_fixtures/fixtures/prebuilt_sequencer_db.dump differ diff --git a/test_fixtures/src/bin/regenerate_test_fixture.rs b/test_fixtures/src/bin/regenerate_test_fixture.rs new file mode 100644 index 00000000..80da8fdc --- /dev/null +++ b/test_fixtures/src/bin/regenerate_test_fixture.rs @@ -0,0 +1,126 @@ +//! Regenerate the prebuilt sequencer database dump for the fast `TestContext::new()` path. +//! Needs Docker. Run via `just regenerate-test-fixture`, then commit the dump. + +#![expect(clippy::print_stdout, reason = "It's normal in this small cli")] + +use std::{path::Path, time::Duration}; + +use anyhow::{Context as _, Result}; +use lee::PrivateKey; +use sequencer_core::block_store::SequencerStore; +use test_fixtures::{ + config, + setup::{ + prebuilt_sequencer_db_dump_path, setup_bedrock_node, + setup_private_accounts_with_initial_supply, setup_public_accounts_with_initial_supply, + setup_sequencer, setup_wallet, + }, +}; +use wallet::config::WalletConfigOverrides; + +#[tokio::main] +async fn main() -> Result<()> { + env_logger::init(); + + let dest = prebuilt_sequencer_db_dump_path(); + println!( + "πŸ—ƒοΈ Regenerating prebuilt sequencer db fixture at {}", + dest.display() + ); + + generate_prebuilt_fixture(&dest) + .await + .context("Failed to regenerate prebuilt sequencer database fixture")?; + + println!("βœ… Wrote fixture dump to {}", dest.display()); + Ok(()) +} + +/// Run a real sequencer with the default accounts, apply genesis + claim the initial supply +/// (genesis block + claim block), then strip the checkpoint and reset blocks to `Pending` so the +/// dump replays cleanly against a fresh Bedrock. Writes the dump to `dest`. +async fn generate_prebuilt_fixture(dest: &Path) -> Result<()> { + let (_bedrock_compose, bedrock_addr) = setup_bedrock_node() + .await + .context("Failed to setup Bedrock node")?; + + let initial_public_accounts = config::default_public_accounts_for_wallet(); + let initial_private_accounts = config::default_private_accounts_for_wallet(); + let genesis = + config::genesis_from_accounts(&initial_public_accounts, &initial_private_accounts); + + let (sequencer_handle, temp_sequencer_dir) = setup_sequencer( + config::SequencerPartialConfig::default(), + bedrock_addr, + genesis, + config::bedrock_channel_id(), + None, + ) + .await + .context("Failed to setup Sequencer for fixture generation")?; + + let (mut wallet, _temp_wallet_dir, _wallet_password) = setup_wallet( + sequencer_handle.addr(), + &initial_public_accounts, + &initial_private_accounts, + WalletConfigOverrides::default(), + ) + .context("Failed to setup wallet for fixture generation")?; + + setup_public_accounts_with_initial_supply(&mut wallet, &initial_public_accounts) + .await + .context("Failed to initialize public accounts for fixture generation")?; + setup_private_accounts_with_initial_supply(&mut wallet, &initial_private_accounts) + .await + .context("Failed to initialize private accounts for fixture generation")?; + + // Shut down gracefully to release the rocksdb lock before reopening the store. + drop(wallet); + drop(sequencer_handle); + + let db_path = temp_sequencer_dir.path().join("rocksdb"); + let store = open_store_with_retry(&db_path) + .await + .context("Failed to reopen sequencer store after shutdown")?; + store + .delete_zone_checkpoint() + .context("Failed to strip zone-sdk checkpoint from fixture database")?; + store + .reset_all_blocks_to_pending() + .context("Failed to reset fixture blocks to pending")?; + let dump = store.dump().context("Failed to dump fixture database")?; + drop(store); + + if let Some(parent) = dest.parent() { + std::fs::create_dir_all(parent) + .with_context(|| format!("Failed to create fixture directory {}", parent.display()))?; + } + let bytes = dump + .to_bytes() + .context("Failed to serialize fixture dump")?; + std::fs::write(dest, bytes) + .with_context(|| format!("Failed to write fixture dump to {}", dest.display()))?; + + Ok(()) +} + +/// Reopen the store, retrying while the shut-down sequencer's aborted tasks are still releasing +/// their db handles (the process holds the file lock until then). Must `await` between attempts. +async fn open_store_with_retry(db_path: &Path) -> Result { + let signing_key = PrivateKey::try_new(config::SEQUENCER_SIGNING_KEY) + .expect("Fixed sequencer signing key must be valid"); + + let mut last_err = None; + for _ in 0..100 { + // Let the runtime drop the aborted sequencer tasks before each attempt. + tokio::time::sleep(Duration::from_millis(100)).await; + match SequencerStore::open_db(db_path, signing_key.clone()) { + Ok(store) => return Ok(store), + Err(err) => last_err = Some(err), + } + } + Err(anyhow::anyhow!( + "Failed to open sequencer store at {} after retries: {last_err:?}", + db_path.display() + )) +} diff --git a/test_fixtures/src/config.rs b/test_fixtures/src/config.rs index 92ed6a18..960f349b 100644 --- a/test_fixtures/src/config.rs +++ b/test_fixtures/src/config.rs @@ -3,16 +3,25 @@ use std::{net::SocketAddr, path::PathBuf, time::Duration}; use anyhow::{Context as _, Result}; use bytesize::ByteSize; use indexer_service::{ChannelId, ClientConfig, IndexerConfig}; -use key_protocol::key_management::KeyChain; +use key_protocol::key_management::{KeyChain, secret_holders::SeedHolder}; use lee::{AccountId, PrivateKey, PublicKey}; use lee_core::Identifier; -use sequencer_core::config::{BedrockConfig, GenesisAction, SequencerConfig}; +use sequencer_core::config::{BedrockConfig, CrossZoneConfig, GenesisAction, SequencerConfig}; use url::Url; use wallet::config::WalletConfig; pub const INITIAL_PUBLIC_BALANCES_FOR_WALLET: [u128; 2] = [10_000, 20_000]; pub const INITIAL_PRIVATE_BALANCES_FOR_WALLET: [u128; 2] = [10_000, 20_000]; +/// Fixed sequencer signing key; exposed so the fixture generator can reopen the produced store. +pub const SEQUENCER_SIGNING_KEY: [u8; 32] = [37; 32]; + +// Fixed entropy seeds for the default accounts: deterministic so one prebuilt database is reusable, +// and distinct from the `testnet_initial_state` accounts to avoid depending on / double-funding +// them. +const DEFAULT_PUBLIC_ACCOUNT_SEEDS: [[u8; 32]; 2] = [[0x11; 32], [0x22; 32]]; +const DEFAULT_PRIVATE_ACCOUNT_SEEDS: [[u8; 32]; 2] = [[0x33; 32], [0x44; 32]]; + #[derive(Clone)] pub struct InitialPrivateAccountForWallet { pub key_chain: KeyChain, @@ -71,6 +80,8 @@ pub fn sequencer_config( home: PathBuf, bedrock_addr: SocketAddr, genesis_transactions: Vec, + channel_id: ChannelId, + cross_zone: Option, ) -> Result { let SequencerPartialConfig { max_num_tx_in_block, @@ -87,19 +98,23 @@ pub fn sequencer_config( block_create_timeout, retry_pending_blocks_timeout: Duration::from_secs(5), genesis: genesis_transactions, - signing_key: [37; 32], + signing_key: SEQUENCER_SIGNING_KEY, bedrock_config: BedrockConfig { - channel_id: bedrock_channel_id(), + channel_id, node_url: addr_to_url(UrlProtocol::Http, bedrock_addr) .context("Failed to convert bedrock addr to URL")?, auth: None, }, + cross_zone, }) } #[must_use] pub fn default_public_accounts_for_wallet() -> Vec<(PrivateKey, u128)> { - let mut private_keys = vec![PrivateKey::new_os_random(), PrivateKey::new_os_random()]; + let mut private_keys = DEFAULT_PUBLIC_ACCOUNT_SEEDS + .iter() + .map(|seed| PrivateKey::try_new(*seed).expect("Fixed public account seed must be valid")) + .collect::>(); private_keys.sort_unstable_by_key(|private_key| { AccountId::from(&PublicKey::new_from_private_key(private_key)) }); @@ -112,7 +127,10 @@ pub fn default_public_accounts_for_wallet() -> Vec<(PrivateKey, u128)> { #[must_use] pub fn default_private_accounts_for_wallet() -> Vec { - let mut key_chains = vec![KeyChain::new_os_random(), KeyChain::new_os_random()]; + let mut key_chains = DEFAULT_PRIVATE_ACCOUNT_SEEDS + .iter() + .map(|seed| deterministic_private_key_chain(*seed)) + .collect::>(); key_chains.sort_unstable(); key_chains @@ -126,6 +144,25 @@ pub fn default_private_accounts_for_wallet() -> Vec KeyChain { + let mnemonic = + bip39::Mnemonic::from_entropy(&entropy).expect("32 bytes of entropy is valid for bip39"); + let seed_holder = SeedHolder::from_mnemonic(&mnemonic, ""); + + let secret_spending_key = seed_holder.produce_top_secret_key_holder(); + let private_key_holder = secret_spending_key.produce_private_key_holder(None); + let nullifier_public_key = private_key_holder.generate_nullifier_public_key(); + let viewing_public_key = private_key_holder.generate_viewing_public_key(); + + KeyChain { + secret_spending_key, + private_key_holder, + nullifier_public_key, + viewing_public_key, + } +} + #[must_use] pub fn genesis_from_accounts( public_accounts: &[(PrivateKey, u128)], @@ -167,7 +204,11 @@ pub fn wallet_config(sequencer_addr: SocketAddr) -> Result { }) } -pub fn indexer_config(bedrock_addr: SocketAddr) -> Result { +pub fn indexer_config( + bedrock_addr: SocketAddr, + channel_id: ChannelId, + cross_zone: Option, +) -> Result { Ok(IndexerConfig { consensus_info_polling_interval: Duration::from_secs(1), bedrock_config: ClientConfig { @@ -175,7 +216,10 @@ pub fn indexer_config(bedrock_addr: SocketAddr) -> Result { .context("Failed to convert bedrock addr to URL")?, auth: None, }, - channel_id: bedrock_channel_id(), + channel_id, + cross_zone, + bridge_lock_holdings: Vec::new(), + allow_chain_reset: false, }) } @@ -200,3 +244,14 @@ pub fn bedrock_channel_id() -> ChannelId { .unwrap_or_else(|_| unreachable!()); ChannelId::from(channel_id) } + +/// A second zone's channel id, distinct from [`bedrock_channel_id`] so two zones +/// settle independently on one shared Bedrock node in the cross-zone tests. +#[must_use] +pub fn bedrock_channel_id_b() -> ChannelId { + let channel_id: [u8; 32] = [0_u8, 2] + .repeat(16) + .try_into() + .unwrap_or_else(|_| unreachable!()); + ChannelId::from(channel_id) +} diff --git a/test_fixtures/src/lib.rs b/test_fixtures/src/lib.rs index 43e2a9db..ff87c8e9 100644 --- a/test_fixtures/src/lib.rs +++ b/test_fixtures/src/lib.rs @@ -16,13 +16,17 @@ use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuil use serde::Serialize; use tempfile::TempDir; use testcontainers::compose::DockerCompose; -use wallet::{WalletCore, account::AccountIdWithPrivacy, cli::CliAccountMention}; +use wallet::{ + WalletCore, account::AccountIdWithPrivacy, cli::CliAccountMention, + config::WalletConfigOverrides, +}; use crate::{ indexer_client::IndexerClient, setup::{ setup_bedrock_node, setup_indexer, setup_private_accounts_with_initial_supply, - setup_public_accounts_with_initial_supply, setup_sequencer, setup_wallet, + setup_public_accounts_with_initial_supply, setup_sequencer, setup_sequencer_from_prebuilt, + setup_wallet, sync_wallet_from_prebuilt, }, }; @@ -92,7 +96,7 @@ impl TestContext { /// Get a builder for the test context to customize its configuration. #[must_use] - pub const fn builder() -> TestContextBuilder { + pub fn builder() -> TestContextBuilder { TestContextBuilder::new() } @@ -250,17 +254,31 @@ pub struct TestContextBuilder { genesis_transactions: Option>, sequencer_partial_config: Option, enable_indexer: bool, + wallet_config_overrides: WalletConfigOverrides, + from_scratch: bool, } impl TestContextBuilder { - const fn new() -> Self { + fn new() -> Self { Self { genesis_transactions: None, sequencer_partial_config: None, enable_indexer: true, + wallet_config_overrides: WalletConfigOverrides::default(), + from_scratch: false, } } + /// Override wallet config fields (e.g. polling timeouts) for the wallet built by this context. + #[must_use] + pub fn with_wallet_config_overrides( + mut self, + wallet_config_overrides: WalletConfigOverrides, + ) -> Self { + self.wallet_config_overrides = wallet_config_overrides; + self + } + #[must_use] pub fn with_genesis(mut self, genesis_transactions: Vec) -> Self { self.genesis_transactions = Some(genesis_transactions); @@ -276,6 +294,14 @@ impl TestContextBuilder { self } + /// Build from genesis live instead of loading the prebuilt fixture. Implied by + /// [`Self::with_genesis`]. + #[must_use] + pub const fn from_scratch(mut self) -> Self { + self.from_scratch = true; + self + } + /// Exclude Indexer from test context. /// Indexer is enabled by default. /// @@ -292,6 +318,8 @@ impl TestContextBuilder { genesis_transactions, sequencer_partial_config, enable_indexer, + wallet_config_overrides, + from_scratch, } = self; // Ensure logger is initialized only once @@ -299,14 +327,19 @@ impl TestContextBuilder { debug!("Test context setup"); + // The fixture bakes in the default accounts + genesis, so custom genesis / from_scratch + // must build live. Otherwise load the fixture (fails if it is missing). + let use_prebuilt = !from_scratch && genesis_transactions.is_none(); + let (bedrock_compose, bedrock_addr) = setup_bedrock_node() .await .context("Failed to setup Bedrock node")?; let indexer_components = if enable_indexer { - let (indexer_handle, temp_indexer_dir) = setup_indexer(bedrock_addr) - .await - .context("Failed to setup Indexer")?; + let (indexer_handle, temp_indexer_dir) = + setup_indexer(bedrock_addr, config::bedrock_channel_id(), None) + .await + .context("Failed to setup Indexer")?; let indexer_url = config::addr_to_url(config::UrlProtocol::Ws, indexer_handle.addr()) .context("Failed to convert indexer addr to URL")?; let indexer_client = IndexerClient::new(&indexer_url) @@ -323,40 +356,59 @@ impl TestContextBuilder { let initial_public_accounts = config::default_public_accounts_for_wallet(); let initial_private_accounts = config::default_private_accounts_for_wallet(); - // Wallet genesis must always be present so that - // setup_public/private_accounts_with_initial_supply can claim from the vault PDAs. - // When a test supplies custom genesis, merge rather than replace. - let wallet_genesis = - config::genesis_from_accounts(&initial_public_accounts, &initial_private_accounts); - let genesis = match genesis_transactions { - Some(mut custom) => { - custom.extend(wallet_genesis); - custom - } - None => wallet_genesis, + + let partial_config = sequencer_partial_config.unwrap_or_default(); + + let (sequencer_handle, temp_sequencer_dir) = if use_prebuilt { + setup_sequencer_from_prebuilt(partial_config, bedrock_addr) + .await + .context("Failed to setup Sequencer from prebuilt database")? + } else { + // Wallet genesis must always be present so that + // setup_public/private_accounts_with_initial_supply can claim from the vault PDAs. + // When a test supplies custom genesis, merge rather than replace. + let wallet_genesis = + config::genesis_from_accounts(&initial_public_accounts, &initial_private_accounts); + let genesis = match genesis_transactions { + Some(mut custom) => { + custom.extend(wallet_genesis); + custom + } + None => wallet_genesis, + }; + setup_sequencer( + partial_config, + bedrock_addr, + genesis, + config::bedrock_channel_id(), + None, + ) + .await + .context("Failed to setup Sequencer")? }; - let (sequencer_handle, temp_sequencer_dir) = setup_sequencer( - sequencer_partial_config.unwrap_or_default(), - bedrock_addr, - genesis, - ) - .await - .context("Failed to setup Sequencer")?; let (mut wallet, temp_wallet_dir, wallet_password) = setup_wallet( sequencer_handle.addr(), &initial_public_accounts, &initial_private_accounts, + wallet_config_overrides, ) .context("Failed to setup wallet")?; - setup_public_accounts_with_initial_supply(&mut wallet, &initial_public_accounts) - .await - .context("Failed to initialize public accounts in wallet")?; + if use_prebuilt { + // Funds already exist on-chain in the prebuilt blocks; sync instead of claiming live. + sync_wallet_from_prebuilt(&mut wallet) + .await + .context("Failed to sync wallet from prebuilt database")?; + } else { + setup_public_accounts_with_initial_supply(&mut wallet, &initial_public_accounts) + .await + .context("Failed to initialize public accounts in wallet")?; - setup_private_accounts_with_initial_supply(&mut wallet, &initial_private_accounts) - .await - .context("Failed to initialize private accounts in wallet")?; + setup_private_accounts_with_initial_supply(&mut wallet, &initial_private_accounts) + .await + .context("Failed to initialize private accounts in wallet")?; + } let sequencer_url = config::addr_to_url(config::UrlProtocol::Http, sequencer_handle.addr()) .context("Failed to convert sequencer addr to URL")?; @@ -388,6 +440,7 @@ impl TestContextBuilder { }) } } + /// A test context to be used in normal #[test] tests. pub struct BlockingTestContext { ctx: Option, @@ -459,7 +512,7 @@ pub async fn fetch_privacy_preserving_tx( seq_client: &SequencerClient, tx_hash: HashType, ) -> PrivacyPreservingTransaction { - let tx = seq_client.get_transaction(tx_hash).await.unwrap().unwrap(); + let (tx, _block_id) = seq_client.get_transaction(tx_hash).await.unwrap().unwrap(); match tx { LeeTransaction::PrivacyPreserving(privacy_preserving_transaction) => { diff --git a/test_fixtures/src/setup.rs b/test_fixtures/src/setup.rs index 6559192f..325e1628 100644 --- a/test_fixtures/src/setup.rs +++ b/test_fixtures/src/setup.rs @@ -1,9 +1,10 @@ use std::{net::SocketAddr, path::PathBuf}; use anyhow::{Context as _, Result, bail}; -use indexer_service::IndexerHandle; +use indexer_service::{ChannelId, IndexerHandle}; use lee::{AccountId, PrivateKey, PublicKey}; use log::{debug, warn}; +use sequencer_core::block_store::{DbDump, SequencerStore}; use sequencer_service::{GenesisAction, SequencerHandle}; use tempfile::TempDir; use testcontainers::compose::DockerCompose; @@ -19,6 +20,28 @@ use crate::{ private_mention, public_mention, }; +/// How to initialize the sequencer's database. +pub enum SequencerInit<'dump> { + /// Apply these genesis actions from scratch. + Genesis(Vec), + /// Restore an existing chain from a prebuilt dump (genesis actions are then irrelevant). + Prebuilt(&'dump DbDump), +} + +/// Committed single-file dump of the prebuilt sequencer database (`just regenerate-test-fixture`). +#[must_use] +pub fn prebuilt_sequencer_db_dump_path() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("fixtures/prebuilt_sequencer_db.dump") +} + +/// Load and deserialize the committed prebuilt-database dump. +fn load_prebuilt_dump() -> Result { + let path = prebuilt_sequencer_db_dump_path(); + let bytes = std::fs::read(&path) + .with_context(|| format!("Failed to read prebuilt db dump at {}", path.display()))?; + DbDump::from_bytes(&bytes).context("Failed to deserialize prebuilt db dump") +} + pub async fn setup_bedrock_node() -> Result<(DockerCompose, SocketAddr)> { let manifest_dir = env!("CARGO_MANIFEST_DIR"); let bedrock_compose_path = PathBuf::from(manifest_dir).join("../bedrock/docker-compose.yml"); @@ -89,7 +112,11 @@ pub async fn setup_bedrock_node() -> Result<(DockerCompose, SocketAddr)> { Ok((compose, addr)) } -pub async fn setup_indexer(bedrock_addr: SocketAddr) -> Result<(IndexerHandle, TempDir)> { +pub async fn setup_indexer( + bedrock_addr: SocketAddr, + channel_id: ChannelId, + cross_zone: Option, +) -> Result<(IndexerHandle, TempDir)> { let temp_indexer_dir = tempfile::tempdir().context("Failed to create temp dir for indexer home")?; @@ -98,19 +125,60 @@ pub async fn setup_indexer(bedrock_addr: SocketAddr) -> Result<(IndexerHandle, T temp_indexer_dir.path().display() ); - let indexer_config = - config::indexer_config(bedrock_addr).context("Failed to create Indexer config")?; + let indexer_config = config::indexer_config(bedrock_addr, channel_id, cross_zone) + .context("Failed to create Indexer config")?; - indexer_service::run_server(indexer_config, temp_indexer_dir.path(), 0) - .await - .context("Failed to run Indexer Service") - .map(|handle| (handle, temp_indexer_dir)) + indexer_service::run_server( + indexer_config, + temp_indexer_dir.path(), + 0, + tokio_util::sync::CancellationToken::new(), + ) + .await + .context("Failed to run Indexer Service") + .map(|handle| (handle, temp_indexer_dir)) } pub async fn setup_sequencer( partial: config::SequencerPartialConfig, bedrock_addr: SocketAddr, genesis_transactions: Vec, + channel_id: ChannelId, + cross_zone: Option, +) -> Result<(SequencerHandle, TempDir)> { + setup_sequencer_inner( + partial, + bedrock_addr, + SequencerInit::Genesis(genesis_transactions), + channel_id, + cross_zone, + ) + .await +} + +/// Like [`setup_sequencer`], but rebuilds the sequencer database from the committed prebuilt dump +/// so it starts from an existing chain instead of applying genesis from scratch. +pub async fn setup_sequencer_from_prebuilt( + partial: config::SequencerPartialConfig, + bedrock_addr: SocketAddr, +) -> Result<(SequencerHandle, TempDir)> { + let dump = load_prebuilt_dump()?; + setup_sequencer_inner( + partial, + bedrock_addr, + SequencerInit::Prebuilt(&dump), + config::bedrock_channel_id(), + None, + ) + .await +} + +async fn setup_sequencer_inner( + partial: config::SequencerPartialConfig, + bedrock_addr: SocketAddr, + init: SequencerInit<'_>, + channel_id: ChannelId, + cross_zone: Option, ) -> Result<(SequencerHandle, TempDir)> { let temp_sequencer_dir = tempfile::tempdir().context("Failed to create temp dir for sequencer home")?; @@ -120,11 +188,24 @@ pub async fn setup_sequencer( temp_sequencer_dir.path().display() ); + let genesis_transactions = match init { + SequencerInit::Genesis(genesis) => genesis, + SequencerInit::Prebuilt(dump) => { + // `SequencerCore::open_or_create_store` looks for `/rocksdb`. + let dst = temp_sequencer_dir.path().join("rocksdb"); + SequencerStore::restore_db_from_dump(&dst, dump) + .context("Failed to restore prebuilt sequencer database from dump")?; + Vec::new() + } + }; + let config = config::sequencer_config( partial, temp_sequencer_dir.path().to_owned(), bedrock_addr, genesis_transactions, + channel_id, + cross_zone, ) .context("Failed to create Sequencer config")?; @@ -137,6 +218,7 @@ pub fn setup_wallet( sequencer_addr: SocketAddr, initial_public_accounts: &[(PrivateKey, u128)], initial_private_accounts: &[InitialPrivateAccountForWallet], + config_overrides: WalletConfigOverrides, ) -> Result<(WalletCore, TempDir, String)> { let config = config::wallet_config(sequencer_addr).context("Failed to create Wallet config")?; let config_serialized = @@ -150,7 +232,6 @@ pub fn setup_wallet( .context("Failed to write wallet config in temp dir")?; let storage_path = temp_wallet_dir.path().join("storage.json"); - let config_overrides = WalletConfigOverrides::default(); let wallet_password = "test_pass".to_owned(); let (mut wallet, _mnemonic) = WalletCore::new_init_storage( @@ -192,10 +273,13 @@ pub async fn setup_public_accounts_with_initial_supply( initial_public_accounts: &[(PrivateKey, u128)], ) -> Result<()> { for (private_key, amount) in initial_public_accounts { - claim_funds_from_vault( + let account_id = AccountId::from(&PublicKey::new_from_private_key(private_key)); + wallet::cli::execute_subcommand( wallet, - AccountId::from(&PublicKey::new_from_private_key(private_key)), - *amount, + Command::Vault(VaultSubcommand::Claim { + account_id: public_mention(account_id), + amount: *amount, + }), ) .await .context("Failed to claim funds from vault into public account")?; @@ -221,24 +305,11 @@ pub async fn setup_private_accounts_with_initial_supply( Ok(()) } -async fn claim_funds_from_vault( - wallet: &mut WalletCore, - owner_id: AccountId, - amount: u128, -) -> Result<()> { - let result = wallet::cli::execute_subcommand( - wallet, - Command::Vault(VaultSubcommand::Claim { - account_id: public_mention(owner_id), - amount, - }), - ) - .await - .context("Failed to execute public vault claim command")?; - - let SubcommandReturnValue::Empty = result else { - bail!("Expected Empty return value for public vault claim"); - }; +pub async fn sync_wallet_from_prebuilt(wallet: &mut WalletCore) -> Result<()> { + wallet + .sync_to_latest_block() + .await + .context("Failed to sync wallet from prebuilt chain")?; Ok(()) } @@ -262,8 +333,8 @@ async fn claim_funds_from_vault_to_private( .await .context("Failed to execute private vault claim command")?; - let SubcommandReturnValue::PrivacyPreservingTransfer { .. } = result else { - bail!("Expected PrivacyPreservingTransfer return value for private vault claim"); + let SubcommandReturnValue::TransactionExecuted { .. } = result else { + bail!("Expected TransactionExecuted return value for private vault claim"); }; Ok(()) diff --git a/test_fixtures/tests/prebuilt_fixture.rs b/test_fixtures/tests/prebuilt_fixture.rs new file mode 100644 index 00000000..4960af81 --- /dev/null +++ b/test_fixtures/tests/prebuilt_fixture.rs @@ -0,0 +1,58 @@ +//! "Genesis follows config" checks for both the from-scratch and prebuilt `TestContext` paths. +//! Need Docker/Bedrock, like the integration tests. +#![expect(clippy::tests_outside_test_module, reason = "It's a tests module")] + +use anyhow::{Context as _, Result}; +use lee::{AccountId, PublicKey}; +use sequencer_service_rpc::RpcClient as _; +use test_fixtures::{ + TestContext, + config::{default_private_accounts_for_wallet, default_public_accounts_for_wallet}, + verify_commitment_is_in_state, +}; + +/// Builds from genesis (no prebuilt database) and checks the on-chain state follows the config. +#[tokio::test] +async fn genesis_from_scratch_follows_config() -> Result<()> { + let ctx = TestContext::builder().from_scratch().build().await?; + assert_context_follows_config(&ctx).await +} + +/// Loads the prebuilt database via [`TestContext::new`] and checks the restored state follows the +/// config β€” i.e. the dump round-trips and all expected data persists. +#[tokio::test] +async fn prebuilt_context_follows_config() -> Result<()> { + let ctx = TestContext::new().await?; + assert_context_follows_config(&ctx).await +} + +/// Assert every configured default account is funded with its configured balance, and every +/// configured private account's commitment is present in sequencer state. (The wallet also has an +/// unfunded default root account, so we check the configured accounts specifically.) +async fn assert_context_follows_config(ctx: &TestContext) -> Result<()> { + for (private_key, expected_balance) in default_public_accounts_for_wallet() { + let account_id = AccountId::from(&PublicKey::new_from_private_key(&private_key)); + let balance = ctx + .sequencer_client() + .get_account_balance(account_id) + .await?; + assert_eq!( + balance, expected_balance, + "public account {account_id} balance" + ); + } + + for account in default_private_accounts_for_wallet() { + let account_id = account.account_id(); + let commitment = ctx + .wallet() + .get_private_account_commitment(account_id) + .with_context(|| format!("commitment for private account {account_id}"))?; + assert!( + verify_commitment_is_in_state(commitment, ctx.sequencer_client()).await, + "private commitment for {account_id} should be in sequencer state" + ); + } + + Ok(()) +} diff --git a/tools/cross_zone_chat/Cargo.toml b/tools/cross_zone_chat/Cargo.toml new file mode 100644 index 00000000..2c74e6df --- /dev/null +++ b/tools/cross_zone_chat/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "cross_zone_chat" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[lints] +workspace = true + +[dependencies] +test_fixtures.workspace = true +sequencer_service_rpc = { workspace = true, features = ["client"] } +programs.workspace = true +ping_core.workspace = true +cross_zone_outbox_core.workspace = true +cross_zone_inbox_core.workspace = true +lee.workspace = true +common.workspace = true +risc0-zkvm.workspace = true +tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } +anyhow.workspace = true +serde = { workspace = true, features = ["derive"] } +axum.workspace = true +log.workspace = true +env_logger.workspace = true diff --git a/tools/cross_zone_chat/src/index.html b/tools/cross_zone_chat/src/index.html new file mode 100644 index 00000000..8dc5e82a --- /dev/null +++ b/tools/cross_zone_chat/src/index.html @@ -0,0 +1,234 @@ + + + + + +Cross-zone chat + + + +
+ Cross-zone chat β€” type in a column; each message shows its journey: sent into a source block, that block reaching Bedrock finality, then delivery into the other zone. The finality wait is the bulk of the latency. +
+
+
+

Zone A

+
+
+ + +
+
+
+

Zone B

+
+
+ + +
+
+
+ + + diff --git a/tools/cross_zone_chat/src/main.rs b/tools/cross_zone_chat/src/main.rs new file mode 100644 index 00000000..5a066e9e --- /dev/null +++ b/tools/cross_zone_chat/src/main.rs @@ -0,0 +1,533 @@ +//! Interactive cross-zone chat demo. +//! +//! Boots one Bedrock node hosting two zones (A and B), each running a sequencer +//! whose cross-zone watcher points at the other zone, then serves a local +//! two-column web UI. Type a message in one column and watch it cross Bedrock +//! and appear in the other; two people can chat across the zones. +//! +//! This is the `cross_zone_ping` integration-test topology made interactive: +//! a send builds a `ping_sender::Send` on the sender zone targeting the other +//! zone's `ping_receiver`, the other zone's watcher reads the finalized source +//! block and injects the inbox dispatch, and a per-zone block tailer decodes the +//! delivered `cross_zone_inbox::Dispatch` payloads back into chat text. +//! +//! To make the cross-zone machinery visible, each message is tracked through its +//! pipeline stages and surfaced to the page: submitted into a source block on the +//! sender zone, that block's Bedrock finality (pending -> finalized), and final +//! delivery into a block on the receiver zone. The Bedrock-finality wait is what +//! dominates the latency, so it is shown live. +//! +//! Prerequisite: a working local Docker daemon (Bedrock comes up via +//! `bedrock/docker-compose.yml`). Run with `RISC0_DEV_MODE=1` for fast, +//! proving-free latency; on macOS run under the `just cross-zone-chat` recipe so +//! the DYLD framework shim is set. + +#![allow( + clippy::arithmetic_side_effects, + clippy::print_stdout, + clippy::print_stderr, + clippy::unused_async, + clippy::needless_pass_by_value, + clippy::infinite_loop, + reason = "Demo binary: stdout banner is the deliverable; ordinal/elapsed arithmetic is \ + bounded at chat scale; the per-zone scanners and finality poller are daemon \ + loops that run for the process lifetime; axum handlers must be `async` and take \ + their extractors (State/Json/Query) by value to satisfy the framework's bounds." +)] + +use std::{ + collections::BTreeSet, + net::SocketAddr, + sync::{ + Arc, Mutex, + atomic::{AtomicU32, AtomicU64, Ordering}, + }, + time::{Duration, Instant}, +}; + +use anyhow::{Context as _, Result}; +use axum::{ + Json, Router, + extract::State, + http::StatusCode, + response::Html, + routing::{get, post}, +}; +use common::{block::BedrockStatus, transaction::LeeTransaction}; +use cross_zone_inbox_core::{CrossZoneConfig, CrossZonePeer, Instruction, ZoneId}; +use cross_zone_outbox_core::outbox_pda; +use lee::{ + ProgramId, PublicTransaction, + public_transaction::{Message, WitnessSet}, +}; +use log::{info, warn}; +use ping_core::{ReceiverInstruction, SenderInstruction, ping_record_pda}; +use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder}; +use serde::{Deserialize, Serialize}; +use test_fixtures::{ + config::{self, SequencerPartialConfig, UrlProtocol, bedrock_channel_id, bedrock_channel_id_b}, + setup::{setup_bedrock_node, setup_sequencer}, +}; + +const HTTP_PORT: u16 = 8088; +const POLL_INTERVAL: Duration = Duration::from_secs(1); + +/// One chat message tracked through its cross-zone pipeline. Displayed in the +/// receiving zone's column; the stage fields drive the on-page timeline. +struct TrackedMessage { + id: u64, + /// Label ("A" / "B") of the sender zone. + source_label: &'static str, + /// Label of the receiver zone; the column this message shows in. + dest_label: &'static str, + text: String, + /// Outbox ordinal of this send, used to match the source-zone block. + ordinal: u32, + /// Block on the sender zone that included the `ping_sender` tx. + source_block: Option, + /// Whether `source_block` has reached Bedrock finality. + finalized: bool, + /// Block on the receiver zone that delivered the inbox dispatch. + delivered_block: Option, + created: Instant, + /// Wall-clock seconds from submit to delivery, set once delivered. + delivered_secs: Option, +} + +/// A single send to perform. +#[derive(Deserialize)] +struct SendRequest { + zone: String, + text: String, +} + +/// The id assigned to a freshly submitted message. +#[derive(Serialize)] +struct SendResponse { + id: u64, +} + +/// One message's current pipeline state, as served to the page. +#[derive(Serialize)] +struct TimelineEntry { + id: u64, + /// Receiver column the message belongs to. + zone: &'static str, + source_zone: &'static str, + text: String, + source_block: Option, + finalized: bool, + delivered_block: Option, + /// Seconds elapsed (live until delivered, then frozen at delivery time). + elapsed: u64, +} + +/// Everything one zone needs to send to its peer. +struct ZoneRuntime { + /// The peer zone's channel id; the target of sends from this zone. + other_zone: ZoneId, + client: SequencerClient, + /// Monotonic outbox ordinal per (this zone -> peer); each send must use a + /// fresh value because the outbox PDA is claimed only when default. + ordinal: AtomicU32, +} + +struct AppState { + zone_a: ZoneRuntime, + zone_b: ZoneRuntime, + /// Monotonic id assigned to every message. + next_id: AtomicU64, + messages: Mutex>, +} + +impl AppState { + fn zone(&self, label: &str) -> Option<&ZoneRuntime> { + match label { + "A" => Some(&self.zone_a), + "B" => Some(&self.zone_b), + _ => None, + } + } + + /// Records a freshly submitted outbound message and returns its id. + fn record_outbound( + &self, + source_label: &'static str, + dest_label: &'static str, + ordinal: u32, + text: String, + ) -> u64 { + let id = self.next_id.fetch_add(1, Ordering::SeqCst); + self.messages + .lock() + .expect("messages mutex poisoned") + .push(TrackedMessage { + id, + source_label, + dest_label, + text, + ordinal, + source_block: None, + finalized: false, + delivered_block: None, + created: Instant::now(), + delivered_secs: None, + }); + info!("[stage] msg {id} submitted {source_label}->{dest_label}"); + id + } + + /// Marks the source-zone block that included a send, matched by ordinal. + fn mark_source_block(&self, source_label: &'static str, ordinal: u32, block_id: u64) { + let mut messages = self.messages.lock().expect("messages mutex poisoned"); + if let Some(message) = messages.iter_mut().find(|m| { + m.source_label == source_label && m.ordinal == ordinal && m.source_block.is_none() + }) { + message.source_block = Some(block_id); + info!( + "[stage] msg {} in source block {source_label}#{block_id} (+{}s)", + message.id, + message.created.elapsed().as_secs() + ); + } + } + + /// Marks delivery on the receiver zone. Prefers the oldest undelivered + /// message whose text matches; falls back to the oldest undelivered for the + /// zone (delivery order is preserved end to end). + fn mark_delivered(&self, dest_label: &'static str, text: &str, block_id: u64) { + let mut messages = self.messages.lock().expect("messages mutex poisoned"); + let undelivered = + |m: &TrackedMessage| m.dest_label == dest_label && m.delivered_block.is_none(); + let index = messages + .iter() + .position(|m| undelivered(m) && m.text == text) + .or_else(|| messages.iter().position(undelivered)); + if let Some(index) = index { + let message = &mut messages[index]; + message.delivered_block = Some(block_id); + let secs = message.created.elapsed().as_secs(); + message.delivered_secs = Some(secs); + info!( + "[stage] msg {} delivered {dest_label}#{block_id} (+{secs}s total)", + message.id + ); + } + } + + /// Snapshot of (source zone, block) pairs whose finality is still unknown. + fn pending_finality(&self) -> BTreeSet<(&'static str, u64)> { + self.messages + .lock() + .expect("messages mutex poisoned") + .iter() + .filter(|m| !m.finalized) + .filter_map(|m| m.source_block.map(|block| (m.source_label, block))) + .collect() + } + + /// Marks every message whose source block has reached Bedrock finality. + fn mark_finalized(&self, source_label: &'static str, block_id: u64) { + for message in self + .messages + .lock() + .expect("messages mutex poisoned") + .iter_mut() + { + if message.source_label == source_label + && message.source_block == Some(block_id) + && !message.finalized + { + message.finalized = true; + info!( + "[stage] msg {} source block {source_label}#{block_id} finalized on Bedrock (+{}s)", + message.id, + message.created.elapsed().as_secs() + ); + } + } + } + + fn timeline(&self) -> Vec { + self.messages + .lock() + .expect("messages mutex poisoned") + .iter() + .map(|m| TimelineEntry { + id: m.id, + zone: m.dest_label, + source_zone: m.source_label, + text: m.text.clone(), + source_block: m.source_block, + finalized: m.finalized, + delivered_block: m.delivered_block, + elapsed: m + .delivered_secs + .unwrap_or_else(|| m.created.elapsed().as_secs()), + }) + .collect() + } +} + +#[tokio::main] +async fn main() -> Result<()> { + env_logger::init(); + + // Declared first so Bedrock outlives both zones (drops run in reverse order). + let (_bedrock, bedrock_addr) = setup_bedrock_node() + .await + .context("Failed to set up shared Bedrock node")?; + + let channel_a = bedrock_channel_id(); + let channel_b = bedrock_channel_id_b(); + let zone_a: ZoneId = *channel_a.as_ref(); + let zone_b: ZoneId = *channel_b.as_ref(); + let receiver_id = programs::ping_receiver().id(); + + // Each zone watches the other and may deliver only to ping_receiver. + let cross_zone_a = watch_peer(zone_b, receiver_id); + let cross_zone_b = watch_peer(zone_a, receiver_id); + + let partial = SequencerPartialConfig::default(); + let (seq_a, _home_a) = + setup_sequencer(partial, bedrock_addr, vec![], channel_a, Some(cross_zone_a)) + .await + .context("Failed to set up zone A sequencer")?; + let (seq_b, _home_b) = + setup_sequencer(partial, bedrock_addr, vec![], channel_b, Some(cross_zone_b)) + .await + .context("Failed to set up zone B sequencer")?; + + let state = Arc::new(AppState { + zone_a: ZoneRuntime { + other_zone: zone_b, + client: sequencer_client(seq_a.addr())?, + ordinal: AtomicU32::new(0), + }, + zone_b: ZoneRuntime { + other_zone: zone_a, + client: sequencer_client(seq_b.addr())?, + ordinal: AtomicU32::new(0), + }, + next_id: AtomicU64::new(1), + messages: Mutex::new(Vec::new()), + }); + + tokio::spawn(scan_zone(Arc::clone(&state), "A")); + tokio::spawn(scan_zone(Arc::clone(&state), "B")); + tokio::spawn(poll_finality(Arc::clone(&state))); + + let app = Router::new() + .route("/", get(index)) + .route("/send", post(send_handler)) + .route("/timeline", get(timeline_handler)) + .with_state(state); + + let addr = SocketAddr::from(([127, 0, 0, 1], HTTP_PORT)); + let listener = tokio::net::TcpListener::bind(addr) + .await + .with_context(|| format!("Failed to bind {addr}"))?; + + println!("\n πŸ’¬ Cross-zone chat ready β€” open http://127.0.0.1:{HTTP_PORT} in your browser\n"); + + axum::serve(listener, app) + .await + .context("HTTP server error")?; + Ok(()) +} + +/// A cross-zone config whose single peer is `peer`, allowed to deliver only to +/// `receiver_id`. +fn watch_peer(peer: ZoneId, receiver_id: ProgramId) -> CrossZoneConfig { + CrossZoneConfig { + peers: vec![CrossZonePeer { + channel_id: peer, + allowed_targets: vec![receiver_id], + expected_block_signing_pubkey: None, + }], + } +} + +fn sequencer_client(addr: SocketAddr) -> Result { + let url = + config::addr_to_url(UrlProtocol::Http, addr).context("Failed to build sequencer URL")?; + SequencerClientBuilder::default() + .build(url) + .context("Failed to build sequencer client") +} + +/// Scans one zone's new blocks. A `ping_sender` tx marks the message's source +/// block (its outbound leg); an inbox dispatch marks delivery on this zone. +/// Runs forever; transient RPC errors are logged and retried. +async fn scan_zone(state: Arc, label: &'static str) { + let inbox_id = programs::cross_zone_inbox().id(); + let sender_id = programs::ping_sender().id(); + let client = &state.zone(label).expect("zone runtime exists").client; + + // Start from the current tip so genesis/boot blocks are skipped. + let mut cursor = client.get_last_block_id().await.unwrap_or(0); + + loop { + tokio::time::sleep(POLL_INTERVAL).await; + + let last = match client.get_last_block_id().await { + Ok(last) => last, + Err(err) => { + warn!("[{label}] get_last_block_id failed: {err:#}"); + continue; + } + }; + + while cursor < last { + let next = cursor.saturating_add(1); + match client.get_block(next).await { + Ok(Some(block)) => { + for tx in &block.body.transactions { + let LeeTransaction::Public(public) = tx else { + continue; + }; + let program_id = public.message.program_id; + let data = &public.message.instruction_data; + // A tx targets at most one of these programs; check both + // independently rather than chaining (avoids an empty else). + if program_id == inbox_id + && let Some(text) = decode_inbox_text(data) + { + state.mark_delivered(label, &text, next); + } + if program_id == sender_id + && let Some(ordinal) = decode_send_ordinal(data) + { + state.mark_source_block(label, ordinal, next); + } + } + } + Ok(None) => {} + Err(err) => { + warn!("[{label}] get_block({next}) failed: {err:#}"); + break; + } + } + cursor = next; + } + } +} + +/// Polls each tracked source block's Bedrock finality until it is finalized. +async fn poll_finality(state: Arc) { + loop { + tokio::time::sleep(POLL_INTERVAL).await; + for (label, block_id) in state.pending_finality() { + let client = &state.zone(label).expect("zone runtime exists").client; + match client.get_block(block_id).await { + Ok(Some(block)) => { + if matches!(block.bedrock_status, BedrockStatus::Finalized) { + state.mark_finalized(label, block_id); + } + } + Ok(None) => {} + Err(err) => warn!("[{label}] finality get_block({block_id}) failed: {err:#}"), + } + } + } +} + +/// Recovers the chat text from an inbox dispatch tx's instruction data. +fn decode_inbox_text(instruction_data: &[u32]) -> Option { + let instruction: Instruction = + risc0_zkvm::serde::from_slice::(instruction_data).ok()?; + let Instruction::Dispatch(message) = instruction; + decode_payload(&message.payload) +} + +/// Recovers the outbox ordinal from a `ping_sender::Send` tx's instruction data. +fn decode_send_ordinal(instruction_data: &[u32]) -> Option { + let instruction: SenderInstruction = + risc0_zkvm::serde::from_slice::(instruction_data).ok()?; + let SenderInstruction::Send { ordinal, .. } = instruction; + Some(ordinal) +} + +/// Decodes a `ping_receiver::Record` payload (risc0 words in LE bytes) to text. +fn decode_payload(payload: &[u8]) -> Option { + let chunks = payload.chunks_exact(4); + if !chunks.remainder().is_empty() { + return None; + } + let words: Vec = chunks + .map(|chunk| u32::from_le_bytes(chunk.try_into().expect("chunks_exact(4) yields 4 bytes"))) + .collect(); + let instruction: ReceiverInstruction = + risc0_zkvm::serde::from_slice::(&words).ok()?; + let ReceiverInstruction::Record { payload: bytes } = instruction; + Some(String::from_utf8_lossy(&bytes).into_owned()) +} + +/// Builds the unsigned `ping_sender::Send` that carries `text` to `other_zone`, +/// mirroring `integration_tests/tests/cross_zone_ping.rs`. +fn build_send_tx(other_zone: ZoneId, ordinal: u32, text: &str) -> LeeTransaction { + let receiver_id = programs::ping_receiver().id(); + let outbox_id = programs::cross_zone_outbox().id(); + + let words = risc0_zkvm::serde::to_vec(&ReceiverInstruction::Record { + payload: text.as_bytes().to_vec(), + }) + .expect("serialize record instruction"); + let payload: Vec = words.iter().flat_map(|word| word.to_le_bytes()).collect(); + + let send = SenderInstruction::Send { + outbox_program_id: outbox_id, + target_zone: other_zone, + target_program_id: receiver_id, + target_accounts: vec![ping_record_pda(receiver_id).into_value()], + payload, + ordinal, + }; + + let outbox_account = outbox_pda(outbox_id, &other_zone, ordinal); + let message = Message::try_new( + programs::ping_sender().id(), + vec![outbox_account], + vec![], + send, + ) + .expect("build ping_sender message"); + + LeeTransaction::Public(PublicTransaction::new( + message, + WitnessSet::from_raw_parts(vec![]), + )) +} + +async fn index() -> Html<&'static str> { + Html(include_str!("index.html")) +} + +async fn send_handler( + State(state): State>, + Json(request): Json, +) -> Result, (StatusCode, String)> { + let text = request.text.trim(); + if text.is_empty() { + return Err((StatusCode::BAD_REQUEST, "empty message".to_owned())); + } + let (source_label, dest_label) = match request.zone.as_str() { + "A" => ("A", "B"), + "B" => ("B", "A"), + other => return Err((StatusCode::BAD_REQUEST, format!("unknown zone {other}"))), + }; + let zone = state.zone(source_label).expect("zone runtime exists"); + + let ordinal = zone.ordinal.fetch_add(1, Ordering::SeqCst); + let tx = build_send_tx(zone.other_zone, ordinal, text); + let id = state.record_outbound(source_label, dest_label, ordinal, text.to_owned()); + zone.client + .send_transaction(tx) + .await + .map_err(|err| (StatusCode::BAD_GATEWAY, format!("submit failed: {err:#}")))?; + Ok(Json(SendResponse { id })) +} + +async fn timeline_handler(State(state): State>) -> Json> { + Json(state.timeline()) +} diff --git a/tools/integration_bench/src/harness.rs b/tools/integration_bench/src/harness.rs index 369387e3..37015003 100644 --- a/tools/integration_bench/src/harness.rs +++ b/tools/integration_bench/src/harness.rs @@ -108,7 +108,7 @@ async fn begin_step(ctx: &TestContext) -> Result { /// Finish a timed wallet step. Records submit (the time between `started` /// being captured and `ret` being received) and, if `ret` is a -/// [`SubcommandReturnValue::PrivacyPreservingTransfer`], polls the sequencer +/// [`SubcommandReturnValue::TransactionExecuted`], polls the sequencer /// for inclusion and records the inclusion latency. Returns a [`StepResult`]. async fn finalize_step( label: impl Into, @@ -132,7 +132,7 @@ async fn finalize_step( // recorded" signal. let should_wait_for_chain = !matches!(ret, SubcommandReturnValue::RegisterAccount { .. }); if should_wait_for_chain { - if let SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash } = ret { + if let SubcommandReturnValue::TransactionExecuted { tx_hash } = ret { tx_hash_str = Some(format!("{tx_hash}")); } let started_inclusion = Instant::now();